Skip to content

Commit

Permalink
Add --version cli option (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Aug 6, 2024
1 parent f61a643 commit a65295b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions bin/composer-dependency-analyser
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<?php declare(strict_types=1);

use ShipMonk\ComposerDependencyAnalyser\Analyser;
use ShipMonk\ComposerDependencyAnalyser\Exception\AbortException;
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidPathException;
Expand Down Expand Up @@ -52,6 +53,9 @@ try {
) {
$stdErrPrinter->printLine("\n<red>{$e->getMessage()}</red>" . PHP_EOL);
exit(255);

} catch (AbortException $e) {
exit(0);
}

exit($exitCode);
Expand Down
5 changes: 5 additions & 0 deletions src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Cli
{

private const OPTIONS = [
'version' => false,
'help' => false,
'verbose' => false,
'ignore-shadow-deps' => false,
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/CliOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
class CliOptions
{

/**
* @var true|null
*/
public $version = null;

/**
* @var true|null
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/AbortException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace ShipMonk\ComposerDependencyAnalyser\Exception;

class AbortException extends RuntimeException
{

public function __construct()
{
parent::__construct('');
}

}
28 changes: 27 additions & 1 deletion src/Initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
namespace ShipMonk\ComposerDependencyAnalyser;

use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use OutOfBoundsException;
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
use ShipMonk\ComposerDependencyAnalyser\Exception\AbortException;
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidPathException;
use ShipMonk\ComposerDependencyAnalyser\Result\ConsoleFormatter;
use ShipMonk\ComposerDependencyAnalyser\Result\JunitFormatter;
use ShipMonk\ComposerDependencyAnalyser\Result\ResultFormatter;
use Throwable;
use function class_exists;
use function count;
use function get_class;
use function is_file;
Expand All @@ -28,6 +32,7 @@ class Initializer
vendor/bin/composer-dependency-analyser
Options:
--version Print analyser version.
--help Print this help text and exit.
--verbose Print more usage examples
--show-all-usages Removes the limit of showing only few usages
Expand Down Expand Up @@ -220,6 +225,7 @@ public function initComposerClassLoaders(): array

/**
* @param list<string> $argv
* @throws AbortException
* @throws InvalidCliException
*/
public function initCliOptions(string $cwd, array $argv): CliOptions
Expand All @@ -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;
Expand Down Expand Up @@ -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";
}

}
6 changes: 4 additions & 2 deletions tests/BinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/InitializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
}

Expand Down

0 comments on commit a65295b

Please sign in to comment.