Files
speedBB/app/Console/Commands/VersionFetch.php
tracer 225dc391ff
All checks were successful
CI/CD Pipeline / stamp_build (push) Successful in 3s
CI/CD Pipeline / deploy (push) Successful in 24s
CI/CD Pipeline / promote_stable (push) Successful in 3s
Use composer.json as version/build source and stamp build in CI
2026-02-24 22:55:49 +01:00

79 lines
1.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Setting;
use Illuminate\Console\Command;
class VersionFetch extends Command
{
protected $signature = 'version:fetch';
protected $description = 'Sync version/build metadata into settings using composer.json as source of truth.';
public function handle(): int
{
$meta = $this->resolveComposerMetadata();
if ($meta === null) {
$this->error('Unable to determine version/build from composer.json.');
return self::FAILURE;
}
$version = $meta['version'];
$build = $meta['build'];
Setting::updateOrCreate(
['key' => 'version'],
['value' => $version],
);
Setting::updateOrCreate(
['key' => 'build'],
['value' => (string) $build],
);
$this->info("Version/build synced: {$version} (build {$build}).");
return self::SUCCESS;
}
private function resolveComposerMetadata(): ?array
{
$composerPath = base_path('composer.json');
if (!is_file($composerPath) || !is_readable($composerPath)) {
return null;
}
$raw = file_get_contents($composerPath);
if ($raw === false) {
return null;
}
$data = json_decode($raw, true);
if (!is_array($data)) {
return null;
}
$version = trim((string) ($data['version'] ?? ''));
$buildRaw = trim((string) ($data['build'] ?? ''));
if ($version === '' || $buildRaw === '') {
return null;
}
if (!preg_match('/^\d+\.\d+(?:\.\d+)?(?:[-._][0-9A-Za-z.-]+)?$/', $version)) {
return null;
}
if (!ctype_digit($buildRaw)) {
return null;
}
return [
'version' => $version,
'build' => (int) $buildRaw,
];
}
}