Skip to content

Commit c682779

Browse files
authored
Merge pull request #2 from KentarouTakeda/open-modified-files
✨ 変更を行ったファイルのパスのみをブラウザで開く機能
2 parents 9785665 + 83d5585 commit c682779

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
*
33

44
!.gitignore
5+
!bin/
6+
!bin/**
57
!LICENSE
68
!Makefile
79
!README.md

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,12 @@ open:
4242
else \
4343
echo "Output file not found: output/php-chunked-xhtml/index.html"; \
4444
fi
45+
46+
open-modified:
47+
PATHS=`php bin/getModifiedFilePath.php`; \
48+
\
49+
if [ -n "$$PATHS" ]; then \
50+
open $$PATHS; \
51+
else \
52+
echo "Modified file not found"; \
53+
fi

bin/getModifiedFilePath.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
const LANG = 'ja';
4+
5+
main();
6+
7+
/**
8+
* LANG配下のgit-statusより更新されたファイルを取得し対応するHTMLファイルのパスを出力
9+
*
10+
* @return string[]
11+
*/
12+
function main(): void
13+
{
14+
$gitStatusOutput = getGitStatusOutput(LANG);
15+
16+
$modifiedFiles = array_filter(
17+
getModifiedFilesFromGitStatusOutput($gitStatusOutput),
18+
fn (string $file) => str_ends_with($file, '.xml'),
19+
);
20+
21+
$pageIds = array_filter(array_map(
22+
fn (string $file) => getPageIdFromFile(LANG . '/' . $file),
23+
$modifiedFiles,
24+
));
25+
26+
$pathes = array_map(
27+
fn (string $id) => "output/php-chunked-xhtml/{$id}.html",
28+
$pageIds,
29+
);
30+
31+
echo implode("\n", $pathes) . PHP_EOL;
32+
}
33+
34+
/**
35+
* 指定されたパスの git-status を取得
36+
*/
37+
function getGitStatusOutput(string $path): string
38+
{
39+
return `git -C {$path} status --porcelain=1`;
40+
}
41+
42+
/**
43+
* git-status の出力より更新されたファイルの一覧を取得
44+
*
45+
* @return string[]
46+
*/
47+
function getModifiedFilesFromGitStatusOutput(string $output): array
48+
{
49+
$files = [];
50+
51+
foreach (explode("\n", $output) as $line) {
52+
$status = substr($line, 0, 2);
53+
$file = substr($line, 3);
54+
55+
if (!in_array($status, [
56+
'??', // 新規の未追跡ファイル(Untracked)
57+
'A ', // インデックスに追加された新規ファイル(Added to index)
58+
'M ', // インデックスが修正されたファイル(Modified in index)
59+
'AM', // インデックスに追加され、ワーキングディレクトリでも変更されたファイル
60+
'MM', // インデックスが修正され、ワーキングディレクトリでも修正されたファイル
61+
'RM', // インデックスでリネームされ、ワーキングディレクトリで修正されたファイル
62+
'CM', // インデックスでコピーされ、ワーキングディレクトリで修正されたファイル
63+
' M', // ワーキングディレクトリで変更されたファイル(Modified in working tree)
64+
])) {
65+
continue;
66+
}
67+
68+
$files[] = preg_match('/^\S+ -> (\S+)$/', $file, $matches)
69+
? $matches[1]
70+
: $file;
71+
}
72+
73+
return $files;
74+
}
75+
76+
function getPageIdFromFile(string $file): ?string
77+
{
78+
$xml = new XMLReader();
79+
$xml->open($file);
80+
81+
while ($xml->nodeType !== XMLReader::ELEMENT) {
82+
if (!@$xml->read()) {
83+
return null;
84+
}
85+
}
86+
87+
$id = $xml->getAttribute('xml:id');
88+
89+
return $id;
90+
}

0 commit comments

Comments
 (0)