feat(changelog): 使用统一的 CHANGELOG_HEADER 常量生成标准化头部

This commit is contained in:
2026-02-28 15:29:07 +08:00
parent 04410ea9e7
commit 5957d67bc3
3 changed files with 24 additions and 17 deletions

View File

@@ -5,6 +5,15 @@ use std::collections::HashMap;
use std::fs;
use std::path::Path;
pub const CHANGELOG_HEADER: &str = r#"# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
"#;
/// Changelog generator
pub struct ChangelogGenerator {
format: ChangelogFormat,
@@ -109,9 +118,10 @@ impl ChangelogGenerator {
};
let new_content = if existing.is_empty() {
format!("# Changelog\n\n{}", entry)
} else {
// Find position after header
format!("{}{}", CHANGELOG_HEADER, entry)
} else if existing.starts_with(CHANGELOG_HEADER) {
format!("{}{}", CHANGELOG_HEADER, entry)
} else if existing.starts_with("# Changelog") {
let lines: Vec<&str> = existing.lines().collect();
let mut header_end = 0;
@@ -129,6 +139,8 @@ impl ChangelogGenerator {
let rest = lines[header_end..].join("\n");
format!("{}\n{}\n{}", header, entry, rest)
} else {
format!("{}{}", CHANGELOG_HEADER, entry)
};
fs::write(changelog_path, new_content)
@@ -378,16 +390,7 @@ pub fn init_changelog(path: &Path) -> Result<()> {
anyhow::bail!("Changelog already exists at {:?}", path);
}
let content = r#"# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
"#;
fs::write(path, content)
fs::write(path, CHANGELOG_HEADER)
.with_context(|| format!("Failed to create changelog: {:?}", path))?;
Ok(())