Skip to content

✨ 変更を行ったファイルのパスのみをブラウザで開く機能 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
*

!.gitignore
!bin/
!bin/**
!LICENSE
!Makefile
!README.md
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
90 changes: 90 additions & 0 deletions bin/getModifiedFilePath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

const LANG = 'ja';

main();

/**
* LANG配下のgit-statusより更新されたファイルを取得し対応するHTMLファイルのパスを出力
*
* @return string[]
*/
function main(): void
{
$gitStatusOutput = getGitStatusOutput(LANG);

$modifiedFiles = array_filter(
getModifiedFilesFromGitStatusOutput($gitStatusOutput),
fn (string $file) => 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;
}