web-api/app/Console/Commands/AppVersionUpdateCommand.php
梁朝伟 d43464180d
All checks were successful
Deploy to Production / deploy (push) Successful in 16s
init
2024-11-13 19:38:27 +08:00

49 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class AppVersionUpdateCommand extends Command
{
protected $signature = 'app:version {version?} {--auto}';
protected $description = 'update .env 文件中的 APP_VERSION 版本号';
public function handle()
{
$envPath = base_path('.env');
if (!file_exists($envPath)) {
$this->error('.env 文件不存在');
return;
}
$current = env('APP_VERSION');
$version = $this->argument('version');
if ($version) {
if (!preg_match('/^\d+\.\d+\.\d+$/', $version)) {
$this->error('版本号格式错误');
return;
}
} else {
$version = preg_replace_callback('/(\d+)$/', fn ($matches) => $matches[1] + 1, $current);
}
$isAuto = $this->option('auto');
if ($isAuto) {
file_put_contents($envPath, preg_replace('/APP_VERSION=.*/', 'APP_VERSION=' . $version, file_get_contents($envPath)));
$this->info('更新成功 ' . $current . ' -> ' . $version);
return Command::SUCCESS;
}
$confirmed = $this->confirm('当前版本号:' . $current . ' 新版号 ' . $version . ' 确认更新么', true);
if (!$confirmed) {
$this->line('已取消');
return Command::SUCCESS;
}
file_put_contents($envPath, preg_replace('/APP_VERSION=.*/', 'APP_VERSION=' . $version, file_get_contents($envPath)));
$this->info('更新成功 ' . $current . ' -> ' . $version);
return Command::SUCCESS;
}
}