From a17ba8806858c78f6e27e6db519b542d685b68f1 Mon Sep 17 00:00:00 2001 From: augushong Date: Mon, 25 May 2026 21:35:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(update):=20=E6=96=B0=E5=A2=9E=20--dry-run?= =?UTF-8?q?=20=E9=A2=84=E8=A7=88=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/service/AdminUpdateServiceBase.php | 92 ++++++++++++------- .../base/common/command/admin/UpdateBase.php | 2 + 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/extend/base/admin/service/AdminUpdateServiceBase.php b/extend/base/admin/service/AdminUpdateServiceBase.php index cdd69f2..23eeebb 100644 --- a/extend/base/admin/service/AdminUpdateServiceBase.php +++ b/extend/base/admin/service/AdminUpdateServiceBase.php @@ -25,6 +25,8 @@ class AdminUpdateServiceBase public $type = 'ulthon_admin'; + public $dryRun = false; + /** * @var Input */ @@ -261,33 +263,41 @@ class AdminUpdateServiceBase } if (!empty($optional_update_waring_files)) { - 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) { + if ($this->dryRun) { $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)) { - 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) { + if ($this->dryRun) { $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) { - $now_file_path = $now_dir . '/' . $file_path; - $last_file_path = $last_version_dir . '/' . $file_path; + if (!$this->dryRun) { + foreach ($need_process_files as $file_path => $type) { + $now_file_path = $now_dir . '/' . $file_path; + $last_file_path = $last_version_dir . '/' . $file_path; - PathTools::intiDir($now_file_path); - if ($type == 'delete') { - $output->writeln('删除文件' . $now_file_path); - unlink($now_file_path); - } elseif ($type == 'add') { - $output->writeln('增加文件' . $now_file_path); - copy($last_file_path, $now_file_path); - } elseif ($type == 'update') { - $output->writeln('更新文件' . $now_file_path); - copy($last_file_path, $now_file_path); + PathTools::intiDir($now_file_path); + if ($type == 'delete') { + $output->writeln('删除文件' . $now_file_path); + unlink($now_file_path); + } elseif ($type == 'add') { + $output->writeln('增加文件' . $now_file_path); + copy($last_file_path, $now_file_path); + } elseif ($type == 'update') { + $output->writeln('更新文件' . $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依赖 $last_composer_json = file_get_contents($last_version_dir . '/composer.json'); diff --git a/extend/base/common/command/admin/UpdateBase.php b/extend/base/common/command/admin/UpdateBase.php index 8a584d2..ae5f522 100644 --- a/extend/base/common/command/admin/UpdateBase.php +++ b/extend/base/common/command/admin/UpdateBase.php @@ -24,6 +24,7 @@ class UpdateBase extends Command ->addOption('reinstall', null, Option::VALUE_NONE, '重装版本') ->addOption('update-ulthon', null, Option::VALUE_NONE, '更新 ulthon_admin') ->addOption('update-master', null, Option::VALUE_NONE, '更新 ulthon_admin 的 master 分支') + ->addOption('dry-run', null, Option::VALUE_NONE, '预览模式,只输出变更不执行') ->setDescription('更新系统代码'); } @@ -41,6 +42,7 @@ class UpdateBase extends Command $update_service = new AdminUpdateService($repo); $update_service->input = $input; $update_service->output = $output; + $update_service->dryRun = (bool)$input->getOption('dry-run'); $update_service->update(); }