|
| 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