Skip to content

Commit

Permalink
Merge pull request #131 from godruoyi/godruoyi/move-getversion-method…
Browse files Browse the repository at this point in the history
…-to-class

chore: move the obtaining the version method to a separate class
  • Loading branch information
asgrim authored Nov 26, 2024
2 parents 33f911a + 9c69fab commit 75dbc22
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
33 changes: 3 additions & 30 deletions bin/pie
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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;
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;
Expand All @@ -21,36 +21,8 @@ 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';
}

$installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']);
if ($installedVersion === null) {
return 'UNKNOWN';
}

return $installedVersion;
}

/** @psalm-suppress NoValue */
return $pieVersion;
})()
);
$application->setCommandLoader(new ContainerCommandLoader(
$container,
[
Expand All @@ -61,4 +33,5 @@ $application->setCommandLoader(new ContainerCommandLoader(
'show' => ShowCommand::class,
]
));

$application->run($container->get(InputInterface::class), $container->get(OutputInterface::class));
69 changes: 69 additions & 0 deletions src/Util/PieVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Php\Pie\Util;

use Composer\InstalledVersions;

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)) {
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 self::SYMFONY_MAGIC_CONST_UNKNOWN;
}

return $installedVersion;
}

/** @psalm-suppress NoValue */
return $pieVersion;
}
}

0 comments on commit 75dbc22

Please sign in to comment.