Penambahan Tagging Auto Command #11

Merged
arbi merged 1 commits from main into development 2026-09-04 00:35:26 +07:00
+92
View File
@@ -0,0 +1,92 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;
/**
* Versi PHP dari scripts/release.sh — supaya bisa dijalankan tanpa bash/WSL.
* Membuat tag versi baru (naik PATCH) dari branch main lalu push ke origin.
*/
class ReleaseCommand extends Command
{
protected $signature = 'release {message? : Pesan tag rilis} {--y|yes : Lewati konfirmasi}';
protected $description = 'Buat tag versi baru (naik PATCH) dari branch main dan push ke origin';
public function handle(): int
{
$git = fn (array $args) => trim(Process::path(base_path())->run(array_merge(['git'], $args))->output());
$branch = $git(['rev-parse', '--abbrev-ref', 'HEAD']);
if ($branch !== 'main') {
$this->error("Harus dijalankan dari branch 'main' (sekarang di '{$branch}').");
$this->line('Jalankan: git checkout main && git pull origin main');
return self::FAILURE;
}
if ($git(['status', '--porcelain']) !== '') {
$this->error('Ada perubahan yang belum di-commit. Bereskan dulu sebelum tagging.');
return self::FAILURE;
}
$this->info('Mengambil update terbaru dari origin/main...');
Process::path(base_path())->run(['git', 'fetch', 'origin', 'main', '--tags']);
if ($git(['rev-parse', 'HEAD']) !== $git(['rev-parse', 'origin/main'])) {
$this->error('Branch main lokal belum sinkron dengan origin/main.');
$this->line('Jalankan: git pull origin main');
return self::FAILURE;
}
$latest = collect(explode("\n", $git(['tag', '--list', 'v*', '--sort=-v:refname'])))
->map(fn ($t) => trim($t))->filter()->first();
if (! $latest) {
$new = 'v0.1.0';
} else {
[$major, $minor, $patch] = array_pad(explode('.', ltrim($latest, 'v')), 3, '0');
$new = sprintf('v%d.%d.%d', (int) $major, (int) $minor, ((int) $patch) + 1);
}
$this->line('Tag terakhir : '.($latest ?: 'tidak ada'));
$this->line("Tag baru : {$new}");
if (! $this->option('yes') && ! $this->confirm("Buat dan push tag '{$new}'?", false)) {
$this->line('Dibatalkan.');
return self::SUCCESS;
}
$message = $this->argument('message') ?: "Rilis {$new}";
$tag = Process::path(base_path())->run(['git', 'tag', '-a', $new, '-m', $message]);
if (! $tag->successful()) {
$this->error('Gagal membuat tag: '.trim($tag->errorOutput()));
return self::FAILURE;
}
$push = Process::path(base_path())->run(['git', 'push', 'origin', $new]);
if (! $push->successful()) {
$this->error('Gagal push tag: '.trim($push->errorOutput()));
return self::FAILURE;
}
$this->info("Selesai. Tag {$new} sudah dibuat dan dikirim ke origin.");
$repo = preg_replace('/\.git$/', '', $git(['remote', 'get-url', 'origin']));
if ($latest && $repo) {
$this->newLine();
$this->line('Lihat file yang berubah di rilis ini:');
$this->line("{$repo}/compare/{$latest}...{$new}");
}
return self::SUCCESS;
}
}