2021-05-14 21:57:41 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-19 00:12:47 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-05-14 21:57:41 +00:00
|
|
|
namespace Rector\Core\Application;
|
|
|
|
|
2021-05-19 00:12:47 +00:00
|
|
|
use DateTimeInterface;
|
|
|
|
use Rector\Core\Exception\VersionException;
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use Symplify\PackageBuilder\Console\ShellCode;
|
|
|
|
|
2021-05-14 21:57:41 +00:00
|
|
|
/**
|
2021-05-19 00:12:47 +00:00
|
|
|
* 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
|
2021-05-14 21:57:41 +00:00
|
|
|
*/
|
|
|
|
final class VersionResolver
|
|
|
|
{
|
2021-05-19 00:12:47 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const PACKAGE_VERSION = '@package_version@';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const RELEASE_DATE = '@release_date@';
|
|
|
|
|
|
|
|
public static function resolvePackageVersion(): string
|
2021-05-14 21:57:41 +00:00
|
|
|
{
|
2021-05-19 00:12:47 +00:00
|
|
|
$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.'
|
|
|
|
);
|
2021-05-14 21:57:41 +00:00
|
|
|
}
|
2021-05-19 00:12:47 +00:00
|
|
|
return trim($process->getOutput());
|
2021-05-14 21:57:41 +00:00
|
|
|
}
|
2021-05-19 00:12:47 +00:00
|
|
|
|
|
|
|
public static function resolverReleaseDateTime(): DateTimeInterface
|
2021-05-14 21:57:41 +00:00
|
|
|
{
|
2021-05-19 00:12:47 +00:00
|
|
|
$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()));
|
2021-05-14 21:57:41 +00:00
|
|
|
}
|
|
|
|
}
|