Use composer.json as version/build source and stamp build in CI
This commit is contained in:
@@ -4,28 +4,23 @@ namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class VersionFetch extends Command
|
||||
{
|
||||
protected $signature = 'version:fetch';
|
||||
|
||||
protected $description = 'Sync version/build metadata into settings using composer.json version and git build count.';
|
||||
protected $description = 'Sync version/build metadata into settings using composer.json as source of truth.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$version = $this->resolveComposerVersion();
|
||||
$build = $this->resolveBuildCount();
|
||||
|
||||
if ($version === null) {
|
||||
$this->error('Unable to determine version from composer.json.');
|
||||
$meta = $this->resolveComposerMetadata();
|
||||
if ($meta === null) {
|
||||
$this->error('Unable to determine version/build from composer.json.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if ($build === null) {
|
||||
$this->error('Unable to determine build number from git.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
$version = $meta['version'];
|
||||
$build = $meta['build'];
|
||||
|
||||
Setting::updateOrCreate(
|
||||
['key' => 'version'],
|
||||
@@ -37,17 +32,12 @@ class VersionFetch extends Command
|
||||
['value' => (string) $build],
|
||||
);
|
||||
|
||||
if (!$this->syncComposerBuild($build)) {
|
||||
$this->error('Failed to sync version/build to composer.json.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->info("Version/build synced: {$version} (build {$build}).");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function resolveComposerVersion(): ?string
|
||||
private function resolveComposerMetadata(): ?array
|
||||
{
|
||||
$composerPath = base_path('composer.json');
|
||||
|
||||
@@ -66,7 +56,9 @@ class VersionFetch extends Command
|
||||
}
|
||||
|
||||
$version = trim((string) ($data['version'] ?? ''));
|
||||
if ($version === '') {
|
||||
$buildRaw = trim((string) ($data['build'] ?? ''));
|
||||
|
||||
if ($version === '' || $buildRaw === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -74,58 +66,13 @@ class VersionFetch extends Command
|
||||
return null;
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
if (!ctype_digit($buildRaw)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private function resolveBuildCount(): ?int
|
||||
{
|
||||
$commands = [
|
||||
['git', 'rev-list', '--count', 'master'],
|
||||
['git', 'rev-list', '--count', 'HEAD'],
|
||||
return [
|
||||
'version' => $version,
|
||||
'build' => (int) $buildRaw,
|
||||
];
|
||||
|
||||
foreach ($commands as $command) {
|
||||
$process = new Process($command, base_path());
|
||||
$process->run();
|
||||
|
||||
if ($process->isSuccessful()) {
|
||||
$output = trim($process->getOutput());
|
||||
if (is_numeric($output)) {
|
||||
return (int) $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function syncComposerBuild(int $build): bool
|
||||
{
|
||||
$composerPath = base_path('composer.json');
|
||||
|
||||
if (!is_file($composerPath) || !is_readable($composerPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$raw = file_get_contents($composerPath);
|
||||
if ($raw === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data['build'] = (string) $build;
|
||||
|
||||
$encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
if ($encoded === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$encoded .= "\n";
|
||||
|
||||
return file_put_contents($composerPath, $encoded) !== false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user