From 4ed676613bbccab1138b73a3f35dfbeb86daef23 Mon Sep 17 00:00:00 2001 From: Godruoyi Date: Mon, 25 Nov 2024 14:54:22 +0800 Subject: [PATCH 1/2] chore: move the obtaining the version method to a separate class --- bin/pie | 50 ++++++++++------------------------------- src/Util/PieVersion.php | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 src/Util/PieVersion.php diff --git a/bin/pie b/bin/pie index 69d08f9f..b88f8cfc 100755 --- a/bin/pie +++ b/bin/pie @@ -11,6 +11,7 @@ use Php\Pie\Command\DownloadCommand; use Php\Pie\Command\InfoCommand; use Php\Pie\Command\InstallCommand; use Php\Pie\Command\ShowCommand; +use Php\Pie\Util\PieVersion; use Symfony\Component\Console\Application; use Symfony\Component\Console\CommandLoader\ContainerCommandLoader; use Symfony\Component\Console\Input\InputInterface; @@ -21,44 +22,17 @@ include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php'; $container = Container::factory(); -$application = new Application( - '🥧 PHP Installer for Extensions (PIE)', - (static function (): string { - $pieVersion = '@pie_version@'; +$application = new Application('🥧 PHP Installer for Extensions (PIE)', PieVersion::get()); - /** - * @psalm-suppress RedundantCondition - * @noinspection PhpConditionAlreadyCheckedInspection - */ - if ($pieVersion === '@pie_version'.'@') { - if (!class_exists(InstalledVersions::class)) { - /** - * Note: magic constant that causes Symfony Console to not display a version - * {@see Application::getLongVersion()} - */ - return 'UNKNOWN'; - } +$application->setCommandLoader(new ContainerCommandLoader($container, [ + 'download' => DownloadCommand::class, + 'build' => BuildCommand::class, + 'install' => InstallCommand::class, + 'info' => InfoCommand::class, + 'show' => ShowCommand::class, +])); - $installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']); - if ($installedVersion === null) { - return 'UNKNOWN'; - } - - return $installedVersion; - } - - /** @psalm-suppress NoValue */ - return $pieVersion; - })() +$application->run( + $container->get(InputInterface::class), + $container->get(OutputInterface::class), ); -$application->setCommandLoader(new ContainerCommandLoader( - $container, - [ - 'download' => DownloadCommand::class, - 'build' => BuildCommand::class, - 'install' => InstallCommand::class, - 'info' => InfoCommand::class, - 'show' => ShowCommand::class, - ] -)); -$application->run($container->get(InputInterface::class), $container->get(OutputInterface::class)); diff --git a/src/Util/PieVersion.php b/src/Util/PieVersion.php new file mode 100644 index 00000000..d07223b1 --- /dev/null +++ b/src/Util/PieVersion.php @@ -0,0 +1,41 @@ + Date: Tue, 26 Nov 2024 07:56:07 +0000 Subject: [PATCH 2/2] Added docblocks around PieVersion and how it behaves --- bin/pie | 25 ++++++++++++------------- src/Util/PieVersion.php | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/bin/pie b/bin/pie index b88f8cfc..88443670 100755 --- a/bin/pie +++ b/bin/pie @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Php\Pie; -use Composer\InstalledVersions; use Php\Pie\Command\BuildCommand; use Php\Pie\Command\DownloadCommand; use Php\Pie\Command\InfoCommand; @@ -24,15 +23,15 @@ $container = Container::factory(); $application = new Application('🥧 PHP Installer for Extensions (PIE)', PieVersion::get()); -$application->setCommandLoader(new ContainerCommandLoader($container, [ - 'download' => DownloadCommand::class, - 'build' => BuildCommand::class, - 'install' => InstallCommand::class, - 'info' => InfoCommand::class, - 'show' => ShowCommand::class, -])); - -$application->run( - $container->get(InputInterface::class), - $container->get(OutputInterface::class), -); +$application->setCommandLoader(new ContainerCommandLoader( + $container, + [ + 'download' => DownloadCommand::class, + 'build' => BuildCommand::class, + 'install' => InstallCommand::class, + 'info' => InfoCommand::class, + 'show' => ShowCommand::class, + ] +)); + +$application->run($container->get(InputInterface::class), $container->get(OutputInterface::class)); diff --git a/src/Util/PieVersion.php b/src/Util/PieVersion.php index d07223b1..5c44126c 100644 --- a/src/Util/PieVersion.php +++ b/src/Util/PieVersion.php @@ -8,28 +8,56 @@ use function class_exists; +/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ final class PieVersion { + /** + * Note: magic constant that causes Symfony Console to not display a version + * {@see Application::getLongVersion()} + */ + private const SYMFONY_MAGIC_CONST_UNKNOWN = 'UNKNOWN'; + + /** + * A static method to try to find the version of PIE you are currently + * running. If running in the PHAR built with Box, this should return a + * realistic-looking version; usually either a tag (e.g. `2.0.0`), or a tag + * and following commit short hash (e.g. `2.0.0@e558e33`). If not this will + * fall back to some other techniques to try to determine a version. + */ public static function get(): string { + /** + * This value is replaced dynamically by Box with the real version when + * we build the PHAR. It is based on the Git tag and/or version + * + * It will be replaced with `2.0.0` on an exact tag match, or something + * like `2.0.0@e558e33` on a commit following a tag. + * + * When running not in a PHAR, this will not be replaced, so this + * method needs additional logic to determine the version. + * + * @link https://box-project.github.io/box/configuration/#pretty-git-tag-placeholder-git + */ $pieVersion = '@pie_version@'; /** * @psalm-suppress RedundantCondition * @noinspection PhpConditionAlreadyCheckedInspection */ + // phpcs:ignore Generic.Strings.UnnecessaryStringConcat.Found if ($pieVersion === '@pie_version' . '@') { if (! class_exists(InstalledVersions::class)) { - /** - * Note: magic constant that causes Symfony Console to not display a version - * {@see Application::getLongVersion()} - */ - return 'UNKNOWN'; + return self::SYMFONY_MAGIC_CONST_UNKNOWN; } + /** + * This tries to determine the version based on Composer; if we are + * the root package (i.e. you're developing on it), this will most + * likely be something like `dev-main` (branch name). + */ $installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']); if ($installedVersion === null) { - return 'UNKNOWN'; + return self::SYMFONY_MAGIC_CONST_UNKNOWN; } return $installedVersion;