feat(update): 新增 --dry-run 预览模式

This commit is contained in:
augushong
2026-05-25 21:35:47 +08:00
parent fb3f807877
commit a17ba88068
2 changed files with 60 additions and 34 deletions

View File

@@ -25,6 +25,8 @@ class AdminUpdateServiceBase
public $type = 'ulthon_admin'; public $type = 'ulthon_admin';
public $dryRun = false;
/** /**
* @var Input * @var Input
*/ */
@@ -261,33 +263,41 @@ class AdminUpdateServiceBase
} }
if (!empty($optional_update_waring_files)) { if (!empty($optional_update_waring_files)) {
foreach ($optional_update_waring_files as $file_path => $type) { if ($this->dryRun) {
$output->writeln($file_path . ' ' . $type);
}
$output->writeln('以上文件被您修改了,这些文件是默认的系统文件,并非您的主要业务代码,');
$output->writeln('您可能通过扩展机制修改了以上文件来定制系统代码的逻辑');
$output->writeln('您可能需要根据扩展规则查看系统逻辑是否发生了变化,如果发生了变化,您需要手动修改这些文件');
$is_udpate_optinal_files = $output->confirm($input, '确定要更新吗?(建议不更新)', false);
if ($is_udpate_optinal_files) {
$need_process_files = array_merge($need_process_files, $optional_update_waring_files); $need_process_files = array_merge($need_process_files, $optional_update_waring_files);
} else {
foreach ($optional_update_waring_files as $file_path => $type) {
$output->writeln($file_path . ' ' . $type);
}
$output->writeln('以上文件被您修改了,这些文件是默认的系统文件,并非您的主要业务代码,');
$output->writeln('您可能通过扩展机制修改了以上文件来定制系统代码的逻辑');
$output->writeln('您可能需要根据扩展规则查看系统逻辑是否发生了变化,如果发生了变化,您需要手动修改这些文件');
$is_udpate_optinal_files = $output->confirm($input, '确定要更新吗?(建议不更新)', false);
if ($is_udpate_optinal_files) {
$need_process_files = array_merge($need_process_files, $optional_update_waring_files);
}
} }
} }
if (!empty($force_update_waring_files)) { if (!empty($force_update_waring_files)) {
foreach ($force_update_waring_files as $file_path => $type) { if ($this->dryRun) {
$output->writeln($file_path . ' ' . $type);
}
$output->writeln('以上文件被您定制了,您不应该修改这些文件,');
$output->writeln('但您出于某些原因修改了他们,如果继续更新,会覆盖至最新版本,');
$output->writeln('这些改动不应该发生,继续自动升级可能会导致错误,');
$output->writeln('建议您选择更新,然后将这些改动的逻辑通过扩展的机制重新实现');
$is_udpate_force_files = $output->confirm($input, '确定要更新吗?(建议更新)', false);
if ($is_udpate_force_files) {
$need_process_files = array_merge($need_process_files, $force_update_waring_files); $need_process_files = array_merge($need_process_files, $force_update_waring_files);
} else {
foreach ($force_update_waring_files as $file_path => $type) {
$output->writeln($file_path . ' ' . $type);
}
$output->writeln('以上文件被您定制了,您不应该修改这些文件,');
$output->writeln('但您出于某些原因修改了他们,如果继续更新,会覆盖至最新版本,');
$output->writeln('这些改动不应该发生,继续自动升级可能会导致错误,');
$output->writeln('建议您选择更新,然后将这些改动的逻辑通过扩展的机制重新实现');
$is_udpate_force_files = $output->confirm($input, '确定要更新吗?(建议更新)', false);
if ($is_udpate_force_files) {
$need_process_files = array_merge($need_process_files, $force_update_waring_files);
}
} }
} }
@@ -300,23 +310,37 @@ class AdminUpdateServiceBase
// 处理需要更新的文件 // 处理需要更新的文件
foreach ($need_process_files as $file_path => $type) { if (!$this->dryRun) {
$now_file_path = $now_dir . '/' . $file_path; foreach ($need_process_files as $file_path => $type) {
$last_file_path = $last_version_dir . '/' . $file_path; $now_file_path = $now_dir . '/' . $file_path;
$last_file_path = $last_version_dir . '/' . $file_path;
PathTools::intiDir($now_file_path); PathTools::intiDir($now_file_path);
if ($type == 'delete') { if ($type == 'delete') {
$output->writeln('删除文件' . $now_file_path); $output->writeln('删除文件' . $now_file_path);
unlink($now_file_path); unlink($now_file_path);
} elseif ($type == 'add') { } elseif ($type == 'add') {
$output->writeln('增加文件' . $now_file_path); $output->writeln('增加文件' . $now_file_path);
copy($last_file_path, $now_file_path); copy($last_file_path, $now_file_path);
} elseif ($type == 'update') { } elseif ($type == 'update') {
$output->writeln('更新文件' . $now_file_path); $output->writeln('更新文件' . $now_file_path);
copy($last_file_path, $now_file_path); copy($last_file_path, $now_file_path);
}
} }
} }
if ($this->dryRun && !empty($need_process_files)) {
$add_count = count(array_filter($need_process_files, fn($t) => $t === 'add'));
$delete_count = count(array_filter($need_process_files, fn($t) => $t === 'delete'));
$update_count = count(array_filter($need_process_files, fn($t) => $t === 'update'));
$output->writeln('');
$output->writeln('[预览模式] 以下文件将被变更:');
foreach ($need_process_files as $file_path => $type) {
$output->writeln(" [{$type}] " . $file_path);
}
$output->writeln("统计: 新增 {$add_count}, 删除 {$delete_count}, 更新 {$update_count}");
}
// 检测now的composer依赖和最新的composer依赖 // 检测now的composer依赖和最新的composer依赖
$last_composer_json = file_get_contents($last_version_dir . '/composer.json'); $last_composer_json = file_get_contents($last_version_dir . '/composer.json');

View File

@@ -24,6 +24,7 @@ class UpdateBase extends Command
->addOption('reinstall', null, Option::VALUE_NONE, '重装版本') ->addOption('reinstall', null, Option::VALUE_NONE, '重装版本')
->addOption('update-ulthon', null, Option::VALUE_NONE, '更新 ulthon_admin') ->addOption('update-ulthon', null, Option::VALUE_NONE, '更新 ulthon_admin')
->addOption('update-master', null, Option::VALUE_NONE, '更新 ulthon_admin 的 master 分支') ->addOption('update-master', null, Option::VALUE_NONE, '更新 ulthon_admin 的 master 分支')
->addOption('dry-run', null, Option::VALUE_NONE, '预览模式,只输出变更不执行')
->setDescription('更新系统代码'); ->setDescription('更新系统代码');
} }
@@ -41,6 +42,7 @@ class UpdateBase extends Command
$update_service = new AdminUpdateService($repo); $update_service = new AdminUpdateService($repo);
$update_service->input = $input; $update_service->input = $input;
$update_service->output = $output; $update_service->output = $output;
$update_service->dryRun = (bool)$input->getOption('dry-run');
$update_service->update(); $update_service->update();
} }