2021-05-14 21:57:41 +00:00
< ? php
2022-05-29 20:46:31 +00:00
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\Core\Application ;
2021-05-14 21:57:41 +00:00
2021-05-19 08:27:31 +00:00
use DateTime ;
2022-06-06 17:12:56 +00:00
use Rector\Core\Exception\VersionException ;
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 / 6587715 d0f8cae0cd39073b3bc5f018d0e6b84fe / src / Composer / Compiler . php #L208
2022-05-29 18:55:20 +00:00
*
* @ see \Rector\Core\Tests\Application\VersionResolverTest
2021-05-14 21:57:41 +00:00
*/
final class VersionResolver
{
2021-05-19 00:12:47 +00:00
/**
* @ var string
*/
2022-06-07 07:41:04 +00:00
public const PACKAGE_VERSION = '39c7ae65e9489b88a886f035a22db8f427db0e16' ;
2022-04-15 07:29:32 +00:00
/**
* @ var string
*/
2022-06-07 07:41:04 +00:00
public const RELEASE_DATE = '2022-06-07 09:34:38' ;
2022-05-29 20:07:14 +00:00
/**
* @ var int
*/
private const SUCCESS_CODE = 0 ;
2022-05-29 20:46:31 +00:00
public static function resolvePackageVersion () : string
2021-05-14 21:57:41 +00:00
{
2022-05-29 18:55:20 +00:00
// resolve current tag
2022-05-29 20:46:31 +00:00
\exec ( 'git tag --points-at' , $tagExecOutput , $tagExecResultCode );
2022-05-29 20:07:14 +00:00
if ( $tagExecResultCode !== self :: SUCCESS_CODE ) {
2022-06-06 17:12:56 +00:00
throw new \Rector\Core\Exception\VersionException ( 'Ensure to run compile from composer git repository clone and that git binary is available.' );
2022-04-14 21:57:42 +00:00
}
2022-05-29 18:55:20 +00:00
if ( $tagExecOutput !== []) {
$tag = $tagExecOutput [ 0 ];
if ( $tag !== '' ) {
return $tag ;
}
2022-04-14 21:57:42 +00:00
}
2022-05-29 20:46:31 +00:00
\exec ( 'git log --pretty="%H" -n1 HEAD' , $commitHashExecOutput , $commitHashResultCode );
2022-05-29 20:07:14 +00:00
if ( $commitHashResultCode !== 0 ) {
2022-06-06 17:12:56 +00:00
throw new \Rector\Core\Exception\VersionException ( 'Ensure to run compile from composer git repository clone and that git binary is available.' );
2021-05-14 21:57:41 +00:00
}
2022-05-29 20:46:31 +00:00
$version = \trim ( $commitHashExecOutput [ 0 ]);
return \trim ( $version , '"' );
2021-05-14 21:57:41 +00:00
}
2022-06-06 17:12:56 +00:00
public static function resolverReleaseDateTime () : \DateTime
2021-05-14 21:57:41 +00:00
{
2022-05-29 20:46:31 +00:00
\exec ( 'git log -n1 --pretty=%ci HEAD' , $output , $resultCode );
if ( $resultCode !== self :: SUCCESS_CODE ) {
2022-06-06 17:12:56 +00:00
throw new \Rector\Core\Exception\VersionException ( 'You must ensure to run compile from composer git repository clone and that git binary is available.' );
2021-05-19 00:12:47 +00:00
}
2022-06-06 17:12:56 +00:00
return new \DateTime ( \trim ( $output [ 0 ]));
2021-05-14 21:57:41 +00:00
}
}