mailwolt/app/Support/BuildMeta.php

78 lines
2.4 KiB
PHP

<?php
namespace App\Support;
use Illuminate\Support\Str;
final class BuildMeta
{
public string $version = 'dev';
public string $rev = '';
public string $short = '';
public ?string $updated = null;
public static function detect(
string $buildFile = '/etc/mailwolt/build.info',
string $basePath = null
): self {
$m = new self();
$basePath ??= base_path();
// 1) /etc/mailwolt/build.info (vom Updater)
if (is_file($buildFile) && is_readable($buildFile)) {
$lines = @file($buildFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
foreach ($lines as $line) {
if (!str_contains($line, '=')) continue;
[$k, $v] = explode('=', $line, 2);
$k = trim($k); $v = trim($v);
if ($k === 'version') $m->version = $v;
if ($k === 'rev') $m->rev = $v;
if ($k === 'updated') $m->updated = $v;
}
}
// 2) Fallback: .env APP_VERSION
if ($m->version === 'dev' && ($envVer = env('APP_VERSION'))) {
$m->version = trim($envVer);
}
// 3) Fallback: Git (ohne "-dirty")
if ($m->rev === '' || $m->version === 'dev') {
$head = $basePath.'/.git/HEAD';
if (is_file($head)) {
$ref = trim((string)@file_get_contents($head));
if (Str::startsWith($ref, 'ref:')) {
$refFile = $basePath.'/.git/'.substr($ref, 5);
$commit = @file_get_contents($refFile);
} else {
$commit = $ref;
}
$m->rev = $m->rev ?: trim((string)$commit);
if ($m->version === 'dev') {
$tag = @shell_exec('git -C '.escapeshellarg($basePath).' describe --tags --abbrev=0 2>/dev/null');
$m->version = $tag ? trim($tag) : 'dev';
}
}
}
// Sauber: "-dirty" entfernen
$m->version = preg_replace('/-dirty$/', '', $m->version);
// Kurz-Commit
$m->short = $m->rev ? substr($m->rev, 0, 7) : '';
return $m;
}
public function toArray(): array
{
return [
'version' => $this->version,
'rev' => $this->rev,
'short' => $this->short,
'updated' => $this->updated,
];
}
}