Skip to content

Commit

Permalink
resolve version on release
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 19, 2021
1 parent 4c8fbea commit e2bd8de
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 38 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"symfony/dependency-injection": "^5.2",
"symfony/finder": "^5.2",
"symfony/http-kernel": "^5.2",
"symfony/process": "^5.2",
"symfony/yaml": "^5.2",
"symplify/astral": "^9.3.10",
"symplify/autowire-array-parameter": "^9.3.10",
Expand Down
16 changes: 16 additions & 0 deletions scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Compiler\PhpScoper\StaticEasyPrefixer;
use Rector\Compiler\Unprefixer;
use Rector\Compiler\ValueObject\ScoperOption;
use Rector\Core\Application\VersionResolver;

require_once __DIR__ . '/vendor/autoload.php';

Expand Down Expand Up @@ -86,6 +87,21 @@ function (string $filePath, string $prefix, string $content): string {
return $content;
},


function (string $filePath, string $prefix, string $content): string {
if (! Strings::endsWith($filePath, 'src/Application/VersionResolver.php')) {
return $content;
}

return strtr(
$content,
[
'@package_version@' => VersionResolver::resolvePackageVersion(),
'@release_date@' => VersionResolver::resolverReleaseDate(),
]
);
},

// unprefixed SmartFileInfo
function (string $filePath, string $prefix, string $content): string {
return Strings::replace(
Expand Down
67 changes: 31 additions & 36 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,47 @@

namespace Rector\Core\Application;

use Composer\InstalledVersions;
use Nette\Utils\Strings;
use DateTimeInterface;
use Rector\Core\Exception\VersionException;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\ShellCode;

/**
* Inspired by https://github.com/symplify/symplify/pull/3179/files
* Local resolver is needed, because PHPStan is unprefixing its InstalledVersion classes and the API is changing way too often.
* This makes sure it works without dependency on external conditions.
* Inspired by https://github.com/composer/composer/blob/master/src/Composer/Composer.php
* See https://github.com/composer/composer/blob/6587715d0f8cae0cd39073b3bc5f018d0e6b84fe/src/Composer/Compiler.php#L208
*/
final class VersionResolver
{
public function resolve(): string
{
// give local IntalledVersions a priority above anything else
$intalledVersionsFilepath = __DIR__ . '/../../vendor/composer/InstalledVersions.php';
if (file_exists($intalledVersionsFilepath)) {
require_once $intalledVersionsFilepath;
}

$installedRawData = InstalledVersions::getRawData();

$rectorPackageData = $this->resolvePackageData($installedRawData);
if ($rectorPackageData === null) {
return 'Unknown';
}

if (isset($rectorPackageData['replaced'])) {
return 'replaced@' . $rectorPackageData['replaced'][0];
}
/**
* @var string
*/
public const PACKAGE_VERSION = '@package_version@';

if ($rectorPackageData['version'] === 'dev-main') {
$reference = $rectorPackageData['reference'] ?? null;
if ($reference === null) {
return 'dev-main';
}
/**
* @var string
*/
public const RELEASE_DATE = '@release_date@';

return 'dev-main@' . Strings::substring($rectorPackageData['reference'], 0, 7);
public static function resolvePackageVersion(): string
{
$process = new Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
if ($process->run() !== ShellCode::SUCCESS) {
throw new VersionException(
'You must ensure to run compile from composer git repository clone and that git binary is available.'
);
}

return $rectorPackageData['version'];
return trim($process->getOutput());
}

/**
* @param mixed[] $installedRawData
*/
private function resolvePackageData(array $installedRawData): ?array
public static function resolverReleaseDate(): DateTimeInterface
{
return $installedRawData['versions']['rector/rector-src'] ?? $installedRawData['versions']['rector/rector'] ?? $installedRawData['root'] ?? null;
$process = new Process(['git', 'log', '-n1', '--pretty=%ci', 'HEAD'], __DIR__);
if ($process->run() !== ShellCode::ERROR) {
throw new VersionException(
'You must ensure to run compile from composer git repository clone and that git binary is available.'
);
}

return new \DateTime(trim($process->getOutput()));
}
}
4 changes: 2 additions & 2 deletions src/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ final class ConsoleApplication extends Application
/**
* @param Command[] $commands
*/
public function __construct(VersionResolver $versionResolver, CommandNaming $commandNaming, array $commands = [])
public function __construct(CommandNaming $commandNaming, array $commands = [])
{
$version = $versionResolver->resolve();
$version = VersionResolver::PACKAGE_VERSION;
parent::__construct(self::NAME, $version);

foreach ($commands as $command) {
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/VersionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Exception;

use Exception;

final class VersionException extends Exception
{
}

1 comment on commit e2bd8de

@TomasVotruba
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result 🥳

image

Please sign in to comment.