diff --git a/README.md b/README.md index 734ba74..4088329 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ cs2pr /path/to/checkstyle-report.xml - `--graceful-warnings`: Don't exit with error codes if there are only warnings - `--colorize`: Colorize the output. Useful if the same lint script should be used locally on the command line and remote on GitHub Actions. With this option, errors and warnings are better distinguishable on the command line and the output is still compatible with GitHub Annotations - `--notices-as-warnings` Converts notices to warnings. This can be useful because GitHub does not annotate notices. +- `--prepend-filename` Prepend the filename to the output message - `--prepend-source` When the checkstyle generating tool provides a `source` attribute, prepend the source to the output message. diff --git a/cs2pr b/cs2pr index 033c968..b4b5f3d 100755 --- a/cs2pr +++ b/cs2pr @@ -22,6 +22,7 @@ $version = '1.7.2-dev'; $colorize = false; $gracefulWarnings = false; $noticeAsWarning = false; +$prependFilename = false; $prependSource = false; // parameters @@ -36,6 +37,9 @@ foreach ($argv as $arg) { case 'colorize': $colorize = true; break; + case 'prepend-filename': + $prependFilename = true; + break; case 'prepend-source': $prependSource = true; break; @@ -64,6 +68,7 @@ if (count($params) === 1) { echo " --graceful-warnings Don't exit with error codes if there are only warnings.\n"; echo " --colorize Colorize the output (still compatible with Github Annotations)\n"; echo " --notices-as-warnings Convert notices to warnings (Github does not annotate notices otherwise).\n"; + echo " --prepend-filename Prepend error 'filename' attribute to the message.\n"; echo " --prepend-source Prepend error 'source' attribute to the message.\n"; exit(9); } @@ -102,6 +107,10 @@ foreach ($root as $file) { $message = $source.': '.$message; } + if ($prependFilename && $filename) { + $message = filenameOnly($filename).': '.$message; + } + $annotateType = annotateType($type, $noticeAsWarning); annotateCheck($annotateType, relativePath($filename), $line, $message, $colorize); @@ -180,3 +189,11 @@ function escapeProperty($property) return $property; } + +/** + * Get the filename only from a filepath. Built to work across windows & linux. + */ +function filenameOnly($filepath) +{ + return basename(str_replace("\\", "/", $filepath)); +} diff --git a/tests/errors/prepend-filename.expect b/tests/errors/prepend-filename.expect new file mode 100644 index 0000000..df6fa84 --- /dev/null +++ b/tests/errors/prepend-filename.expect @@ -0,0 +1,4 @@ +::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::boot.php: Call to static method getInstance() on an unknown class rex_one_time_password. +::error file=redaxo\src\addons\2factor_auth\boot.php,line=9::boot.php: Call to static method getInstance() on an unknown class rex_minibar. +::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::one_time_password.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly. +::error file=redaxo/src/addons/2factor_auth/lib/linux_filepath.php,line=0::linux_filepath.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly. \ No newline at end of file diff --git a/tests/errors/prepend-filename.xml b/tests/errors/prepend-filename.xml new file mode 100644 index 0000000..ab22fbe --- /dev/null +++ b/tests/errors/prepend-filename.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/tests/tests.php b/tests/tests.php index 2158b6b..e0f9151 100644 --- a/tests/tests.php +++ b/tests/tests.php @@ -52,6 +52,7 @@ function testXml($xmlPath, $expectedExit, $expectedOutput = null, $options = '') testXml(__DIR__.'/errors/notices.xml', 1, file_get_contents(__DIR__.'/errors/notices.expect')); testXml(__DIR__.'/errors/notices.xml', 1, file_get_contents(__DIR__.'/errors/notices-as-warnings.expect'), '--notices-as-warnings'); +testXml(__DIR__.'/errors/prepend-filename.xml', 1, file_get_contents(__DIR__.'/errors/prepend-filename.expect'), '--prepend-filename'); testXml(__DIR__.'/errors/mixed-source-attributes.xml', 1, file_get_contents(__DIR__.'/errors/mixed-source-attributes.expect'), '--prepend-source'); testXml(__DIR__.'/errors/mixed.xml', 1, file_get_contents(__DIR__.'/errors/mixed-colors.expect'), '--colorize');