Added git HEAD to version parsing

This commit is contained in:
Igor Scheller 2023-02-28 15:58:41 +01:00
parent fb8c05edad
commit 523e984122
5 changed files with 20 additions and 6 deletions

View File

@ -5,22 +5,27 @@ declare(strict_types=1);
namespace Engelsystem\Helpers; namespace Engelsystem\Helpers;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Illuminate\Support\Str;
class Version class Version
{ {
protected string $versionFile = 'VERSION'; public function __construct(protected string $gitRoot, protected string $storage, protected Config $config)
public function __construct(protected string $storage, protected Config $config)
{ {
} }
public function getVersion(): string public function getVersion(): string
{ {
$file = $this->storage . DIRECTORY_SEPARATOR . $this->versionFile; $file = $this->storage . DIRECTORY_SEPARATOR . 'VERSION';
$gitHead = implode(DIRECTORY_SEPARATOR, [$this->gitRoot, 'logs', 'HEAD']);
$version = 'n/a'; $version = 'n/a';
if (file_exists($file)) { if (file_exists($file)) {
$version = trim(file_get_contents($file)); $version = trim(file_get_contents($file));
} elseif (file_exists($gitHead)) {
$lines = file($gitHead) ?: [];
$lastLine = array_pop($lines) ?: '';
$words = explode(' ', $lastLine);
$version = Str::substr($words[1] ?? '', 0, 7);
} }
return $this->config->get('version', $version); return $this->config->get('version', $version);

View File

@ -13,5 +13,8 @@ class VersionServiceProvider extends ServiceProvider
$this->app->when(Version::class) $this->app->when(Version::class)
->needs('$storage') ->needs('$storage')
->give($this->app->get('path.storage.app')); ->give($this->app->get('path.storage.app'));
$this->app->when(Version::class)
->needs('$gitRoot')
->give($this->app->get('path') . DIRECTORY_SEPARATOR . '.git');
} }
} }

View File

@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 4242c4d5e6f71a2b3c4d5e6f71[...] R <a@b.c> 42 +0000 clone: from foo.git
4242c4d5e6f71a2b3c4d5e6f71a2b3c4d5e6f7af 1a2b3c4d5e53f133711311a53f[...] R <a@b.c> 43 +0000 checkout: moving from a to b

View File

@ -17,6 +17,7 @@ class VersionServiceProviderTest extends ServiceProviderTest
public function testRegister(): void public function testRegister(): void
{ {
$app = new Application(); $app = new Application();
$app->instance('path', '/tmp');
$app->instance('path.storage.app', '/tmp'); $app->instance('path.storage.app', '/tmp');
$serviceProvider = new VersionServiceProvider($app); $serviceProvider = new VersionServiceProvider($app);

View File

@ -17,13 +17,16 @@ class VersionTest extends ServiceProviderTest
public function testGetVersion(): void public function testGetVersion(): void
{ {
$config = new Config(); $config = new Config();
$version = new Version(__DIR__ . '/Stub', $config); $version = new Version(__DIR__ . '/Stub', __DIR__ . '/Stub', $config);
$this->assertEquals('n/a', $version->getVersion()); $this->assertEquals('n/a', $version->getVersion());
$version = new Version(__DIR__ . '/Stub/files', $config); $version = new Version(__DIR__ . '/Stub', __DIR__ . '/Stub/files', $config);
$this->assertEquals('0.42.0-testing', $version->getVersion()); $this->assertEquals('0.42.0-testing', $version->getVersion());
$version = new Version(__DIR__ . '/Stub/files', __DIR__ . '/Stub', $config);
$this->assertEquals('1a2b3c4', $version->getVersion());
$config->set('version', '1.2.3-dev'); $config->set('version', '1.2.3-dev');
$this->assertEquals('1.2.3-dev', $version->getVersion()); $this->assertEquals('1.2.3-dev', $version->getVersion());
} }