diff --git a/app/Console/Commands/VersionFetch.php b/app/Console/Commands/VersionFetch.php new file mode 100644 index 0000000..f4c2a0c --- /dev/null +++ b/app/Console/Commands/VersionFetch.php @@ -0,0 +1,97 @@ +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; + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index c3928c5..5be197b 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -11,6 +11,9 @@ return Application::configure(basePath: dirname(__DIR__)) commands: __DIR__.'/../routes/console.php', health: '/up', ) + ->withCommands([ + __DIR__.'/../app/Console/Commands', + ]) ->withMiddleware(function (Middleware $middleware): void { // })