mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-08 19:12:48 +08:00
feat: 完善生成说明文件的代码结构和生成效果
This commit is contained in:
@@ -148,7 +148,7 @@ class VersionBase extends Command
|
|||||||
* 读取自上次tag到现在所有的提交说明.
|
* 读取自上次tag到现在所有的提交说明.
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function generateComment()
|
protected function generateComment()
|
||||||
{
|
{
|
||||||
$this->output->writeln('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
|
$this->output->writeln('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
|
||||||
// 获取最新的tag
|
// 获取最新的tag
|
||||||
@@ -164,34 +164,22 @@ class VersionBase extends Command
|
|||||||
|
|
||||||
$commits = explode("\n", $commits);
|
$commits = explode("\n", $commits);
|
||||||
|
|
||||||
// 按类型分组提交
|
$groupedCommits = $this->groupCommitsByType($commits, function ($message) {
|
||||||
$groupedCommits = [];
|
return trim($message);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成更新说明
|
// 生成更新说明
|
||||||
$comment = "版本更新说明:\n";
|
$comment = "版本更新说明:\n";
|
||||||
|
|
||||||
foreach ($groupedCommits as $type => $messages) {
|
foreach (static::COMMIT_TYPES as $type => $desc) {
|
||||||
$comment .= "\n【{$type}】\n";
|
if (!empty($groupedCommits[$desc])) {
|
||||||
foreach ($messages as $message) {
|
$comment .= "\n【{$desc}】\n";
|
||||||
$comment .= "- {$message}\n";
|
foreach ($groupedCommits[$desc] as $message) {
|
||||||
|
$comment .= "- {$message}\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->output->writeln($comment);
|
$this->output->writeln($comment);
|
||||||
|
|
||||||
// 生成数组格式文件
|
// 生成数组格式文件
|
||||||
@@ -229,6 +217,7 @@ class VersionBase extends Command
|
|||||||
if (empty($fromTag)) {
|
if (empty($fromTag)) {
|
||||||
if (count($tags) < 2) {
|
if (count($tags) < 2) {
|
||||||
$output->error('没有足够的tag来生成发布说明');
|
$output->error('没有足够的tag来生成发布说明');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,6 +238,7 @@ class VersionBase extends Command
|
|||||||
|
|
||||||
if ($toTag === null) {
|
if ($toTag === null) {
|
||||||
$output->error('没有找到比 ' . $fromTag . ' 更新的tag');
|
$output->error('没有找到比 ' . $fromTag . ' 更新的tag');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$output->info('从指定版本开始生成发布说明: ' . $fromTag);
|
$output->info('从指定版本开始生成发布说明: ' . $fromTag);
|
||||||
@@ -262,62 +252,42 @@ class VersionBase extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取从指定tag到目标tag的所有提交,包含提交哈希和提交日期
|
// 获取从指定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)) {
|
if (empty($commits)) {
|
||||||
$output->warning('从 ' . $fromTag . ' 到 ' . $toTag . ' 没有任何提交');
|
$output->warning('从 ' . $fromTag . ' 到 ' . $toTag . ' 没有任何提交');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$commits = explode("\n", $commits);
|
$commits = explode("\n", $commits);
|
||||||
|
|
||||||
// 按类型分组提交
|
$groupedCommits = $this->groupCommitsByType($commits, function ($message, $fullCommit) {
|
||||||
$groupedCommits = [];
|
$parts = explode('|', $fullCommit);
|
||||||
foreach ($commits as $commit) {
|
if (count($parts) >= 3) {
|
||||||
$matched = false;
|
return [
|
||||||
// 提取提交哈希和日期
|
'message' => trim($message),
|
||||||
preg_match('/^([a-f0-9]+)\s(\d{4}-\d{2}-\d{2})\s(.+)$/', $commit, $matches);
|
'hash' => $parts[0],
|
||||||
|
'date' => $parts[1],
|
||||||
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,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
// 生成发布说明
|
// 生成发布说明
|
||||||
$releaseTitle = "## {$currentVersion} 发布说明\n\n";
|
$releaseTitle = "## {$currentVersion} 发布说明\n\n";
|
||||||
$releaseDate = '发布日期: ' . date('Y-m-d') . "\n\n";
|
$releaseDate = '发布日期: ' . date('Y-m-d') . "\n\n";
|
||||||
$releaseContent = "本次发布包含了从 {$fromTag} 到 {$currentVersion} 的所有更新。\n\n";
|
$releaseContent = "本次发布包含了从 {$fromTag} 到 {$currentVersion} 的所有更新。\n\n";
|
||||||
|
|
||||||
foreach ($groupedCommits as $type => $messages) {
|
foreach (static::COMMIT_TYPES as $type => $desc) {
|
||||||
$releaseContent .= "### {$type}\n\n";
|
if (!empty($groupedCommits[$desc])) {
|
||||||
foreach ($messages as $item) {
|
$releaseContent .= "### {$desc}\n\n";
|
||||||
$releaseContent .= "- [{$item['date']}] {$item['message']} (#{$item['hash']})\n";
|
foreach ($groupedCommits[$desc] as $item) {
|
||||||
|
$releaseContent .= "- [{$item['date']}] {$item['message']} (#{$item['hash']})\n";
|
||||||
|
}
|
||||||
|
$releaseContent .= "\n";
|
||||||
}
|
}
|
||||||
$releaseContent .= "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$release = $releaseTitle . $releaseDate . $releaseContent;
|
$release = $releaseTitle . $releaseDate . $releaseContent;
|
||||||
@@ -327,4 +297,41 @@ class VersionBase extends Command
|
|||||||
file_put_contents($releaseFile, $release);
|
file_put_contents($releaseFile, $release);
|
||||||
$output->info('已生成发布说明文件: ' . $releaseFile);
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user