diff --git a/.gitignore b/.gitignore index 4303942..9f80d96 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ * !.gitignore +!bin/ +!bin/** !LICENSE !Makefile !README.md diff --git a/Makefile b/Makefile index bbe03cc..e5d0bd5 100644 --- a/Makefile +++ b/Makefile @@ -42,3 +42,12 @@ open: else \ echo "Output file not found: output/php-chunked-xhtml/index.html"; \ fi + +open-modified: + PATHS=`php bin/getModifiedFilePath.php`; \ + \ + if [ -n "$$PATHS" ]; then \ + open $$PATHS; \ + else \ + echo "Modified file not found"; \ + fi diff --git a/bin/getModifiedFilePath.php b/bin/getModifiedFilePath.php new file mode 100644 index 0000000..11b516a --- /dev/null +++ b/bin/getModifiedFilePath.php @@ -0,0 +1,90 @@ + str_ends_with($file, '.xml'), + ); + + $pageIds = array_filter(array_map( + fn (string $file) => getPageIdFromFile(LANG . '/' . $file), + $modifiedFiles, + )); + + $pathes = array_map( + fn (string $id) => "output/php-chunked-xhtml/{$id}.html", + $pageIds, + ); + + echo implode("\n", $pathes) . PHP_EOL; +} + +/** + * 指定されたパスの git-status を取得 + */ +function getGitStatusOutput(string $path): string +{ + return `git -C {$path} status --porcelain=1`; +} + +/** + * git-status の出力より更新されたファイルの一覧を取得 + * + * @return string[] + */ +function getModifiedFilesFromGitStatusOutput(string $output): array +{ + $files = []; + + foreach (explode("\n", $output) as $line) { + $status = substr($line, 0, 2); + $file = substr($line, 3); + + if (!in_array($status, [ + '??', // 新規の未追跡ファイル(Untracked) + 'A ', // インデックスに追加された新規ファイル(Added to index) + 'M ', // インデックスが修正されたファイル(Modified in index) + 'AM', // インデックスに追加され、ワーキングディレクトリでも変更されたファイル + 'MM', // インデックスが修正され、ワーキングディレクトリでも修正されたファイル + 'RM', // インデックスでリネームされ、ワーキングディレクトリで修正されたファイル + 'CM', // インデックスでコピーされ、ワーキングディレクトリで修正されたファイル + ' M', // ワーキングディレクトリで変更されたファイル(Modified in working tree) + ])) { + continue; + } + + $files[] = preg_match('/^\S+ -> (\S+)$/', $file, $matches) + ? $matches[1] + : $file; + } + + return $files; +} + +function getPageIdFromFile(string $file): ?string +{ + $xml = new XMLReader(); + $xml->open($file); + + while ($xml->nodeType !== XMLReader::ELEMENT) { + if (!@$xml->read()) { + return null; + } + } + + $id = $xml->getAttribute('xml:id'); + + return $id; +}