feat: derive build from git commit count
This commit is contained in:
97
app/Console/Commands/VersionFetch.php
Normal file
97
app/Console/Commands/VersionFetch.php
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
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 = 'Update the build number based on the git commit count of master.';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$version = Setting::where('key', 'version')->value('value');
|
||||||
|
$build = $this->resolveBuildCount();
|
||||||
|
|
||||||
|
if ($version === null) {
|
||||||
|
$this->error('Unable to determine version from settings.');
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($build === null) {
|
||||||
|
$this->error('Unable to determine build number from git.');
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Setting::updateOrCreate(
|
||||||
|
['key' => 'build'],
|
||||||
|
['value' => (string) $build],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!$this->syncComposerMetadata($version, $build)) {
|
||||||
|
$this->error('Failed to sync version/build to composer.json.');
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->info("Build number updated to {$build}.");
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveBuildCount(): ?int
|
||||||
|
{
|
||||||
|
$commands = [
|
||||||
|
['git', 'rev-list', '--count', 'master'],
|
||||||
|
['git', 'rev-list', '--count', 'HEAD'],
|
||||||
|
];
|
||||||
|
|
||||||
|
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 syncComposerMetadata(string $version, 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['version'] = $version;
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,9 @@ return Application::configure(basePath: dirname(__DIR__))
|
|||||||
commands: __DIR__.'/../routes/console.php',
|
commands: __DIR__.'/../routes/console.php',
|
||||||
health: '/up',
|
health: '/up',
|
||||||
)
|
)
|
||||||
|
->withCommands([
|
||||||
|
__DIR__.'/../app/Console/Commands',
|
||||||
|
])
|
||||||
->withMiddleware(function (Middleware $middleware): void {
|
->withMiddleware(function (Middleware $middleware): void {
|
||||||
//
|
//
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user