diff --git a/README.md b/README.md index 1b54c4d..33c79ae 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ This tool reads your `composer.json` and scans all paths listed in `autoload` & - `--composer-json path/to/composer.json` for custom path to composer.json - `--dump-usages symfony/console` to show usages of certain package(s), `*` placeholder is supported - `--config path/to/config.php` for custom path to config file +- `--version` display version - `--help` display usage & cli options - `--verbose` to see more example classes & usages - `--show-all-usages` to see all usages diff --git a/bin/composer-dependency-analyser b/bin/composer-dependency-analyser index 1093343..2e39d81 100755 --- a/bin/composer-dependency-analyser +++ b/bin/composer-dependency-analyser @@ -2,6 +2,7 @@ printLine("\n{$e->getMessage()}" . PHP_EOL); exit(255); + +} catch (AbortException $e) { + exit(0); } exit($exitCode); diff --git a/src/Cli.php b/src/Cli.php index a10115b..f4d90ed 100644 --- a/src/Cli.php +++ b/src/Cli.php @@ -13,6 +13,7 @@ class Cli { private const OPTIONS = [ + 'version' => false, 'help' => false, 'verbose' => false, 'ignore-shadow-deps' => false, @@ -126,6 +127,10 @@ public function getProvidedOptions(): CliOptions { $options = new CliOptions(); + if (isset($this->providedOptions['version'])) { + $options->version = true; + } + if (isset($this->providedOptions['help'])) { $options->help = true; } diff --git a/src/CliOptions.php b/src/CliOptions.php index a0c06b7..3656225 100644 --- a/src/CliOptions.php +++ b/src/CliOptions.php @@ -5,6 +5,11 @@ class CliOptions { + /** + * @var true|null + */ + public $version = null; + /** * @var true|null */ diff --git a/src/Exception/AbortException.php b/src/Exception/AbortException.php new file mode 100644 index 0000000..00835b6 --- /dev/null +++ b/src/Exception/AbortException.php @@ -0,0 +1,13 @@ + $argv + * @throws AbortException * @throws InvalidCliException */ public function initCliOptions(string $cwd, array $argv): CliOptions @@ -228,7 +234,12 @@ public function initCliOptions(string $cwd, array $argv): CliOptions if ($cliOptions->help !== null) { $this->stdOutPrinter->printLine(self::$help); - throw new InvalidCliException(''); // just exit + throw new AbortException(); + } + + if ($cliOptions->version !== null) { + $this->stdOutPrinter->printLine($this->deduceVersion()); + throw new AbortException(); } return $cliOptions; @@ -256,4 +267,19 @@ public function initFormatter(CliOptions $options): ResultFormatter throw new InvalidConfigException("Invalid format option provided, allowed are 'console' or 'junit'."); } + private function deduceVersion(): string + { + try { + /** @throws OutOfBoundsException */ + $version = class_exists(InstalledVersions::class) + ? InstalledVersions::getPrettyVersion('shipmonk/composer-dependency-analyser') + : 'unknown'; + + } catch (OutOfBoundsException $e) { + $version = 'not found'; + } + + return "Version: $version"; + } + } diff --git a/tests/BinTest.php b/tests/BinTest.php index 271999b..6c8369b 100644 --- a/tests/BinTest.php +++ b/tests/BinTest.php @@ -24,6 +24,7 @@ public function test(): void $okOutput = 'No composer issues found'; $dumpingOutput = 'Dumping sample usages of'; $helpOutput = 'Usage:'; + $versionOutput = 'Version:'; $usingConfig = 'Using config'; @@ -32,8 +33,9 @@ public function test(): void $this->runCommand('php bin/composer-dependency-analyser', $rootDir, 0, $okOutput, $usingConfig); $this->runCommand('php bin/composer-dependency-analyser --verbose', $rootDir, 0, $okOutput, $usingConfig); $this->runCommand('php ../bin/composer-dependency-analyser', $testsDir, 255, null, $noComposerJsonError); - $this->runCommand('php bin/composer-dependency-analyser --help', $rootDir, 255, $helpOutput); - $this->runCommand('php ../bin/composer-dependency-analyser --help', $testsDir, 255, $helpOutput); + $this->runCommand('php bin/composer-dependency-analyser --help', $rootDir, 0, $helpOutput); + $this->runCommand('php ../bin/composer-dependency-analyser --help', $testsDir, 0, $helpOutput); + $this->runCommand('php ../bin/composer-dependency-analyser --version', $testsDir, 0, $versionOutput); $this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.json', $rootDir, 0, $okOutput, $usingConfig); $this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.lock', $rootDir, 255, null, $noPackagesError); $this->runCommand('php bin/composer-dependency-analyser --composer-json=README.md', $rootDir, 255, null, $parseError); diff --git a/tests/InitializerTest.php b/tests/InitializerTest.php index 9729f41..5bd4f7f 100644 --- a/tests/InitializerTest.php +++ b/tests/InitializerTest.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\TestCase; use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType; use ShipMonk\ComposerDependencyAnalyser\Config\PathToScan; -use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException; +use ShipMonk\ComposerDependencyAnalyser\Exception\AbortException; use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException; use ShipMonk\ComposerDependencyAnalyser\Result\ConsoleFormatter; use ShipMonk\ComposerDependencyAnalyser\Result\JunitFormatter; @@ -114,7 +114,7 @@ public function testInitCliOptionsHelp(): void $initializer = new Initializer(__DIR__, $printer, $printer); - $this->expectException(InvalidCliException::class); + $this->expectException(AbortException::class); $initializer->initCliOptions(__DIR__, ['script.php', '--help']); }