feat: 完善生成说明文件的代码结构和生成效果

This commit is contained in:
augushong
2025-03-29 10:35:18 +08:00
parent 09f6e35793
commit 389a5b3f3c

View File

@@ -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;
}
}