From 389a5b3f3cfb8b406d7fa7fcb7286328ba6fd99a Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 29 Mar 2025 10:35:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E7=94=9F=E6=88=90?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E6=96=87=E4=BB=B6=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=92=8C=E7=94=9F=E6=88=90=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/common/command/admin/VersionBase.php | 133 +++++++++--------- 1 file changed, 70 insertions(+), 63 deletions(-) diff --git a/extend/base/common/command/admin/VersionBase.php b/extend/base/common/command/admin/VersionBase.php index c1469df..3960a1e 100644 --- a/extend/base/common/command/admin/VersionBase.php +++ b/extend/base/common/command/admin/VersionBase.php @@ -148,7 +148,7 @@ class VersionBase extends Command * 读取自上次tag到现在所有的提交说明. * @return string */ - public function generateComment() + protected function generateComment() { $this->output->writeln('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); // 获取最新的tag @@ -164,34 +164,22 @@ class VersionBase extends Command $commits = explode("\n", $commits); - // 按类型分组提交 - $groupedCommits = []; - foreach ($commits as $commit) { - $matched = false; - foreach (static::COMMIT_TYPES as $type => $desc) { - if (preg_match("/^{$type}:/i", $commit)) { - $message = preg_replace("/^{$type}:/i", '', $commit); - $groupedCommits[$desc][] = trim($message); - $matched = true; - break; - } - } - - // 如果没有匹配到任何类型,归类为"其他" - if (!$matched) { - $groupedCommits['其他'][] = trim($commit); - } - } + $groupedCommits = $this->groupCommitsByType($commits, function ($message) { + return trim($message); + }); // 生成更新说明 $comment = "版本更新说明:\n"; - foreach ($groupedCommits as $type => $messages) { - $comment .= "\n【{$type}】\n"; - foreach ($messages as $message) { - $comment .= "- {$message}\n"; + foreach (static::COMMIT_TYPES as $type => $desc) { + if (!empty($groupedCommits[$desc])) { + $comment .= "\n【{$desc}】\n"; + foreach ($groupedCommits[$desc] as $message) { + $comment .= "- {$message}\n"; + } } } + $this->output->writeln($comment); // 生成数组格式文件 @@ -229,6 +217,7 @@ class VersionBase extends Command if (empty($fromTag)) { if (count($tags) < 2) { $output->error('没有足够的tag来生成发布说明'); + return; } @@ -249,6 +238,7 @@ class VersionBase extends Command if ($toTag === null) { $output->error('没有找到比 ' . $fromTag . ' 更新的tag'); + return; } $output->info('从指定版本开始生成发布说明: ' . $fromTag); @@ -262,62 +252,42 @@ class VersionBase extends Command } // 获取从指定tag到目标tag的所有提交,包含提交哈希和提交日期 - $commits = shell_exec("git log {$fromTag}..{$toTag} --pretty=format:\"%h %ad %s\" --date=short"); + $commits = shell_exec("git log {$fromTag}..{$toTag} --pretty=format:\"%h|%ad|%s\" --date=short"); if (empty($commits)) { $output->warning('从 ' . $fromTag . ' 到 ' . $toTag . ' 没有任何提交'); + return; } $commits = explode("\n", $commits); - // 按类型分组提交 - $groupedCommits = []; - foreach ($commits as $commit) { - $matched = false; - // 提取提交哈希和日期 - preg_match('/^([a-f0-9]+)\s(\d{4}-\d{2}-\d{2})\s(.+)$/', $commit, $matches); - - if (count($matches) >= 4) { - $hash = $matches[1]; - $date = $matches[2]; - $message = $matches[3]; - - foreach (static::COMMIT_TYPES as $type => $desc) { - if (preg_match("/^{$type}:/i", $message)) { - $cleanMessage = preg_replace("/^{$type}:/i", '', $message); - $groupedCommits[$desc][] = [ - 'message' => trim($cleanMessage), - 'hash' => $hash, - 'date' => $date, - ]; - $matched = true; - break; - } - } - - // 如果没有匹配到任何类型,归类为"其他" - if (!$matched) { - $groupedCommits['其他'][] = [ - 'message' => trim($message), - 'hash' => $hash, - 'date' => $date, - ]; - } + $groupedCommits = $this->groupCommitsByType($commits, function ($message, $fullCommit) { + $parts = explode('|', $fullCommit); + if (count($parts) >= 3) { + return [ + 'message' => trim($message), + 'hash' => $parts[0], + 'date' => $parts[1], + ]; } - } + + return null; + }); // 生成发布说明 $releaseTitle = "## {$currentVersion} 发布说明\n\n"; $releaseDate = '发布日期: ' . date('Y-m-d') . "\n\n"; $releaseContent = "本次发布包含了从 {$fromTag} 到 {$currentVersion} 的所有更新。\n\n"; - foreach ($groupedCommits as $type => $messages) { - $releaseContent .= "### {$type}\n\n"; - foreach ($messages as $item) { - $releaseContent .= "- [{$item['date']}] {$item['message']} (#{$item['hash']})\n"; + foreach (static::COMMIT_TYPES as $type => $desc) { + if (!empty($groupedCommits[$desc])) { + $releaseContent .= "### {$desc}\n\n"; + foreach ($groupedCommits[$desc] as $item) { + $releaseContent .= "- [{$item['date']}] {$item['message']} (#{$item['hash']})\n"; + } + $releaseContent .= "\n"; } - $releaseContent .= "\n"; } $release = $releaseTitle . $releaseDate . $releaseContent; @@ -327,4 +297,41 @@ class VersionBase extends Command file_put_contents($releaseFile, $release); $output->info('已生成发布说明文件: ' . $releaseFile); } + + protected function groupCommitsByType($commits, $formatCallback) + { + $groupedCommits = []; + foreach (static::COMMIT_TYPES as $type => $desc) { + $groupedCommits[$desc] = []; + } + $groupedCommits['其他'] = []; // 添加"其他"类别 + + foreach ($commits as $commit) { + $parts = explode('|', $commit); + $message = $parts[2] ?? $commit; + $matched = false; + foreach (static::COMMIT_TYPES as $type => $desc) { + if (preg_match("/^{$type}:/i", $message)) { + $cleanMessage = preg_replace("/^{$type}:/i", '', $message); + $groupedCommits[$desc][] = $formatCallback($cleanMessage, $commit); + $matched = true; + break; + } + } + + // 如果没有匹配到任何类型,归类为"其他" + if (!$matched) { + $groupedCommits['其他'][] = $formatCallback($message, $commit); + } + } + + // 清空空分组 + foreach (array_keys($groupedCommits) as $key) { + if (empty($groupedCommits[$key])) { + unset($groupedCommits[$key]); + } + } + + return $groupedCommits; + } }