From 615485032e67d9500cbfa2e53acdb0449e26f191 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Fri, 14 Jun 2024 09:01:09 -0400 Subject: [PATCH] Show version in CLI (#2291) --- box.json | 5 ++-- composer.json | 7 +++-- src/Phinx/Console/PhinxApplication.php | 30 +++++++++++++++++++- tests/Phinx/Console/PhinxApplicationTest.php | 5 +++- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/box.json b/box.json index 165994372..3cd7bfc86 100644 --- a/box.json +++ b/box.json @@ -5,6 +5,7 @@ "KevinGH\\Box\\Compactor\\Php" ], "directories": ["src", "app", "data", "vendor/symfony/console/Resources"], + "exclude-composer-files": false, "files": [ "LICENSE" ], @@ -24,6 +25,6 @@ "in": "vendor" } ], - "output": "phinx.phar", - "exclude-composer-files": false + "git-tag": "git_tag", + "output": "phinx.phar" } diff --git a/composer.json b/composer.json index 7f8ee0be4..8b6ff06ec 100644 --- a/composer.json +++ b/composer.json @@ -37,17 +37,18 @@ ], "require": { "php-64bit": ">=8.1", + "composer-runtime-api": "^2.0", "cakephp/database": "^5.0.2", "psr/container": "^1.1|^2.0", - "symfony/console": "^6.0|^7.0", - "symfony/config": "^3.4|^4.0|^5.0|^6.0|^7.0" + "symfony/config": "^3.4|^4.0|^5.0|^6.0|^7.0", + "symfony/console": "^6.0|^7.0" }, "require-dev": { "ext-json": "*", "ext-pdo": "*", - "phpunit/phpunit": "^9.5.19", "cakephp/cakephp": "^5.0.2", "cakephp/cakephp-codesniffer": "^5.0", + "phpunit/phpunit": "^9.5.19", "symfony/yaml": "^3.4|^4.0|^5.0|^6.0|^7.0" }, "autoload": { diff --git a/src/Phinx/Console/PhinxApplication.php b/src/Phinx/Console/PhinxApplication.php index b25be2b1e..666eb54ea 100644 --- a/src/Phinx/Console/PhinxApplication.php +++ b/src/Phinx/Console/PhinxApplication.php @@ -8,6 +8,7 @@ namespace Phinx\Console; +use Composer\InstalledVersions; use Phinx\Console\Command\Breakpoint; use Phinx\Console\Command\Create; use Phinx\Console\Command\Init; @@ -27,12 +28,17 @@ */ class PhinxApplication extends Application { + /** + * @var string The current application version as determined by the getVersion() function. + */ + private string $version; + /** * Initialize the Phinx console application. */ public function __construct() { - parent::__construct('Phinx by CakePHP - https://phinx.org.'); + parent::__construct('Phinx by CakePHP - https://phinx.org.', $this->getVersion()); $this->addCommands([ new Init(), @@ -68,4 +74,26 @@ public function doRun(InputInterface $input, OutputInterface $output): int return parent::doRun($input, $output); } + + /** + * Get the current application version. + * + * @return string The application version if it could be found, otherwise 'UNKNOWN' + */ + public function getVersion(): string + { + if (isset($this->version)) { + return $this->version; + } + + // humbug/box will replace this with actual version when building + // so use that if available + $gitTag = '@git_tag@'; + if (!str_starts_with($gitTag, '@')) { + return $this->version = $gitTag; + } + + // Otherwise fallback to the version as reported by composer + return $this->version = InstalledVersions::getPrettyVersion('robmorgan/phinx') ?? 'UNKNOWN'; + } } diff --git a/tests/Phinx/Console/PhinxApplicationTest.php b/tests/Phinx/Console/PhinxApplicationTest.php index aa6e36aa6..5adb1f9d1 100644 --- a/tests/Phinx/Console/PhinxApplicationTest.php +++ b/tests/Phinx/Console/PhinxApplicationTest.php @@ -25,7 +25,10 @@ public function testRun($command, $result) $stream = $appTester->getOutput()->getStream(); rewind($stream); - $this->assertStringContainsString($result, stream_get_contents($stream)); + $contents = stream_get_contents($stream); + + $this->assertMatchesRegularExpression('/^Phinx by CakePHP - https:\/\/phinx.org\. .+\n/', $contents); + $this->assertStringContainsString($result, $contents); } public function provider()