use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct VersionProgress {
pub version: String,
pub done: usize,
pub total: usize,
}
pub fn is_version_line(line: &str) -> Option<String> {
let t = line.trim();
if t.starts_with("## [") {
if let Some(end) = t.find(']') {
let ver = t["## [".len()..end].trim().trim_start_matches('v');
if !ver.is_empty() {
return Some(ver.to_string());
}
}
}
None
}
#[derive(Debug)]
pub struct Issue {
pub line: usize,
pub scope: String,
pub message: String,
}
pub fn resolve_roadmap_path(repo_path: &Path, scope: Option<&str>) -> PathBuf {
let c = crate::contract::load(repo_path);
match scope {
Some(name) if !name.is_empty() => {
if let Some(s) = c.scopes.iter().find(|s| s.name == name) {
repo_path.join(&s.dir).join("ROADMAP.md")
} else {
repo_path.join(name).join("ROADMAP.md")
}
}
_ => {
let current_dir = std::env::current_dir().unwrap_or_else(|_| repo_path.to_path_buf());
if let Some(s) = c.find_scope_by_path(¤t_dir) {
repo_path.join(&s.dir).join("ROADMAP.md")
} else {
repo_path.join("ROADMAP.md")
}
}
}
}
pub fn parse_roadmap(path: &Path) -> Result<Vec<VersionProgress>, String> {
let content = std::fs::read_to_string(path)
.map_err(|e| format!("读取 {} 失败: {}", path.display(), e))?;
let mut versions: Vec<VersionProgress> = Vec::new();
let mut current_version: Option<String> = None;
let mut done = 0usize;
let mut total = 0usize;
for line in content.lines() {
let trimmed = line.trim();
if let Some(ver) = is_version_line(trimmed) {
if let Some(v) = current_version.take() {
versions.push(VersionProgress {
version: v,
done,
total,
});
}
done = 0;
total = 0;
current_version = Some(ver);
continue;
}
if trimmed.starts_with("- [x]") || trimmed.starts_with("- [X]") {
total += 1;
done += 1;
} else if trimmed.starts_with("- [ ]") {
total += 1;
}
}
if let Some(ver) = current_version {
versions.push(VersionProgress {
version: ver,
done,
total,
});
}
Ok(versions)
}
pub fn print_status(repo_path: &Path, scope: Option<&str>) -> Result<(), String> {
let mut stdout = std::io::stdout();
print_status_to(&mut stdout, repo_path, scope)
}
pub fn print_status_to(
writer: &mut impl std::io::Write,
repo_path: &Path,
scope: Option<&str>,
) -> Result<(), String> {
let roadmap_path = resolve_roadmap_path(repo_path, scope);
if !roadmap_path.exists() {
writeln!(writer, " 未创建规划文件: {}", roadmap_path.display()).ok();
return Ok(());
}
let versions = parse_roadmap(&roadmap_path)?;
if versions.is_empty() {
writeln!(writer, " 未找到规划条目").ok();
return Ok(());
}
let scope_label = scope.unwrap_or("(auto)");
writeln!(writer, " [{}] 规划进度", scope_label).ok();
writeln!(writer, " {}", "-".repeat(40)).ok();
let mut total_done = 0usize;
let mut total_all = 0usize;
for v in &versions {
let rate = if v.total > 0 {
v.done as f64 / v.total as f64 * 100.0
} else {
0.0
};
writeln!(
writer,
" [{:<8}] {:>2}/{:>2} 完成 ({:.0}%)",
v.version, v.done, v.total, rate
)
.ok();
total_done += v.done;
total_all += v.total;
}
let overall = if total_all > 0 {
total_done as f64 / total_all as f64 * 100.0
} else {
0.0
};
writeln!(writer, " {}", "-".repeat(40)).ok();
writeln!(
writer,
" 总计: {}/{} 完成 ({:.0}%)",
total_done, total_all, overall
)
.ok();
Ok(())
}
const CATEGORIES: &[&str] = &[
"### Added",
"### Changed",
"### Fixed",
"### Removed",
"### Deprecated",
"### Security",
];
fn is_done_item(line: &str) -> bool {
let t = line.trim();
t.starts_with("- [x]") || t.starts_with("- [X]")
}
fn is_category_header(line: &str) -> bool {
let t = line.trim();
CATEGORIES
.iter()
.any(|c| t == *c || t.eq_ignore_ascii_case(c))
}
fn is_version_header(line: &str) -> bool {
is_version_line(line).is_some()
}
pub fn clean_roadmap(path: &Path) -> Result<usize, String> {
let content = std::fs::read_to_string(path)
.map_err(|e| format!("读取 {} 失败: {}", path.display(), e))?;
let original_len = content.len();
let mut lines: Vec<&str> = content.lines().collect();
lines.retain(|l| !is_done_item(l));
let mut i = 0;
while i + 1 < lines.len() {
if is_category_header(lines[i]) {
let next = lines[i + 1].trim();
if next.is_empty() || is_category_header(next) || is_version_header(next) {
lines.remove(i);
continue;
}
}
i += 1;
}
if let Some(last) = lines.last() {
if is_category_header(last) {
lines.pop();
}
}
let mut i = 0;
while i + 1 < lines.len() {
if is_version_header(lines[i]) {
let next = lines[i + 1].trim();
if next.is_empty() || is_version_header(next) {
lines.remove(i);
continue;
}
}
i += 1;
}
if let Some(last) = lines.last() {
if is_version_header(last) {
lines.pop();
}
}
while let Some(last) = lines.last() {
if last.trim().is_empty() {
lines.pop();
} else {
break;
}
}
if lines.is_empty() {
std::fs::write(path, "").map_err(|e| format!("写入失败: {}", e))?;
return Ok(original_len);
}
let mut output = String::new();
for line in &lines {
output.push_str(line);
output.push('\n');
}
std::fs::write(path, &output).map_err(|e| format!("写入失败: {}", e))?;
Ok(original_len.saturating_sub(output.len()))
}
pub fn validate_roadmap(path: &Path, scope: &str) -> Result<Vec<Issue>, String> {
let content = std::fs::read_to_string(path)
.map_err(|e| format!("读取 {} 失败: {}", path.display(), e))?;
let mut issues: Vec<Issue> = Vec::new();
for (idx, raw_line) in content.lines().enumerate() {
let line_num = idx + 1;
let trimmed = raw_line.trim();
if is_version_line(trimmed).is_some() {
let raw_ver = trimmed
.trim_start_matches("## [")
.split(']')
.next()
.unwrap_or("")
.trim();
if raw_ver.starts_with('v') {
issues.push(Issue {
line: line_num,
scope: scope.to_string(),
message: format!("版本号不应有 v 前缀: {}", raw_ver),
});
}
}
if trimmed.starts_with("### ") {
let lowered = trimmed.to_lowercase();
if let Some(standard) = CATEGORIES.iter().find(|c| c.to_lowercase() == lowered) {
if trimmed != *standard {
issues.push(Issue {
line: line_num,
scope: scope.to_string(),
message: format!("分类标题大小写: 应为 '{}',当前 '{}'", standard, trimmed),
});
}
}
}
let has_any_box =
trimmed.contains("[x]") || trimmed.contains("[X]") || trimmed.contains("[ ]");
let is_standard = trimmed.starts_with("- [x] ")
|| trimmed.starts_with("- [X] ")
|| trimmed.starts_with("- [ ] ");
if has_any_box && !is_standard {
issues.push(Issue {
line: line_num,
scope: scope.to_string(),
message: format!("checkbox 格式异常: {}", trimmed),
});
}
}
Ok(issues)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
fn write_roadmap(content: &str) -> tempfile::TempDir {
let d = tempfile::tempdir().unwrap();
let mut f = std::fs::File::create(d.path().join("ROADMAP.md")).unwrap();
write!(f, "{}", content).unwrap();
d
}
fn read_roadmap(d: &Path) -> String {
std::fs::read_to_string(d.join("ROADMAP.md")).unwrap_or_default()
}
#[test]
fn test_parse_empty() {
let d = write_roadmap("");
let v = parse_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert!(v.is_empty());
}
#[test]
fn test_parse_single_version() {
let d = write_roadmap(
"## [0.1.0]\n\
\n\
### Added\n\
- [x] feature a\n\
- [ ] feature b\n\
### Fixed\n\
- [x] bug c\n",
);
let v = parse_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(v.len(), 1);
assert_eq!(v[0].version, "0.1.0");
assert_eq!(v[0].done, 2);
assert_eq!(v[0].total, 3);
}
#[test]
fn test_parse_multi_version() {
let d = write_roadmap(
"## [0.2.0]\n\
- [x] done\n\
- [ ] todo\n\
\n\
## [0.1.0]\n\
- [x] a\n\
- [x] b\n",
);
let v = parse_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(v.len(), 2);
assert_eq!(v[0].version, "0.2.0");
assert_eq!(v[0].done, 1);
assert_eq!(v[0].total, 2);
assert_eq!(v[1].version, "0.1.0");
assert_eq!(v[1].done, 2);
assert_eq!(v[1].total, 2);
}
#[test]
fn test_parse_v_prefix() {
let d = write_roadmap("## [v0.1.0]\n- [x] item\n");
let v = parse_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(v[0].version, "0.1.0");
}
#[test]
fn test_parse_no_checkboxes() {
let d = write_roadmap("## [0.1.0]\n\njust text\n");
let v = parse_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(v.len(), 1);
assert_eq!(v[0].done, 0);
assert_eq!(v[0].total, 0);
}
#[test]
fn test_parse_version_with_suffix() {
let d = write_roadmap("## [0.1.0] — 已发布\n- [x] done\n- [ ] todo\n");
let v = parse_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(v.len(), 1);
assert_eq!(v[0].version, "0.1.0");
assert_eq!(v[0].done, 1);
assert_eq!(v[0].total, 2);
}
#[test]
fn test_clean_version_with_suffix() {
let d = write_roadmap("## [0.1.0] — 已发布\n- [x] done\n");
clean_roadmap(&d.path().join("ROADMAP.md")).unwrap();
let content = read_roadmap(d.path());
assert!(!content.contains("0.1.0"), "空版本应被清理");
}
#[test]
fn test_parse_file_not_found() {
let d = tempfile::tempdir().unwrap();
let result = parse_roadmap(&d.path().join("NONEXISTENT.md"));
assert!(result.is_err());
}
#[test]
fn test_resolve_path_with_contract_scope() {
let d = tempfile::tempdir().unwrap();
let contract_dir = d.path().join(".quanttide/devops");
std::fs::create_dir_all(&contract_dir).unwrap();
std::fs::write(
contract_dir.join("contract.yaml"),
"scopes:\n cli:\n dir: src/cli\n language: rust\n",
)
.unwrap();
let path = resolve_roadmap_path(d.path(), Some("cli"));
assert!(path.to_string_lossy().ends_with("src/cli/ROADMAP.md"));
}
#[test]
fn test_resolve_path_fallback_to_name() {
let d = tempfile::tempdir().unwrap();
let path = resolve_roadmap_path(d.path(), Some("custom"));
assert!(path.to_string_lossy().ends_with("custom/ROADMAP.md"));
}
#[test]
fn test_resolve_path_no_scope_no_contract() {
let d = tempfile::tempdir().unwrap();
let path = resolve_roadmap_path(d.path(), None);
assert_eq!(path, d.path().join("ROADMAP.md"));
}
#[test]
fn test_clean_removes_done_items() {
let d = write_roadmap(
"## [0.1.0]\n\
### Added\n\
- [x] done item\n\
- [ ] todo item\n\
### Fixed\n\
- [x] fixed bug\n",
);
let removed = clean_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert!(removed > 0);
let content = read_roadmap(d.path());
assert!(!content.contains("done item"));
assert!(!content.contains("fixed bug"));
assert!(content.contains("todo item"));
}
#[test]
fn test_clean_empty_file() {
let d = write_roadmap("");
let removed = clean_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(removed, 0);
}
#[test]
fn test_clean_all_done_empties_file() {
let d = write_roadmap("## [0.1.0]\n### Added\n- [x] done\n");
clean_roadmap(&d.path().join("ROADMAP.md")).unwrap();
let content = read_roadmap(d.path());
assert!(content.is_empty());
}
#[test]
fn test_clean_no_done_items_no_change() {
let d = write_roadmap("## [0.1.0]\n- [ ] todo\n");
let removed = clean_roadmap(&d.path().join("ROADMAP.md")).unwrap();
assert_eq!(removed, 0);
}
#[test]
fn test_clean_trailing_newlines_removed() {
let d = write_roadmap("## [0.1.0]\n- [ ] todo\n\n\n");
clean_roadmap(&d.path().join("ROADMAP.md")).unwrap();
let content = read_roadmap(d.path());
assert_eq!(content.trim_end().lines().count(), 2); }
#[test]
fn test_validate_v_prefix() {
let d = write_roadmap("## [v0.1.0]\n- [ ] item\n");
let issues = validate_roadmap(&d.path().join("ROADMAP.md"), "test").unwrap();
assert!(issues.iter().any(|f| f.message.contains("v 前缀")));
}
#[test]
fn test_validate_category_case() {
let d = write_roadmap("## [0.1.0]\n### added\n- [ ] item\n");
let issues = validate_roadmap(&d.path().join("ROADMAP.md"), "test").unwrap();
assert!(issues.iter().any(|f| f.message.contains("大小写")));
}
#[test]
fn test_validate_clean_file_no_issues() {
let d = write_roadmap("## [0.1.0]\n### Added\n- [ ] item\n");
let issues = validate_roadmap(&d.path().join("ROADMAP.md"), "test").unwrap();
assert!(issues.is_empty());
}
#[test]
fn test_validate_does_not_modify_file() {
let original = "## [v0.1.0]\n### added\n- [x] bad format\n";
let d = write_roadmap(original);
let _issues = validate_roadmap(&d.path().join("ROADMAP.md"), "test").unwrap();
assert_eq!(read_roadmap(d.path()), original);
}
#[test]
fn test_print_status_file_not_found() {
let d = tempfile::tempdir().unwrap();
let mut buf = Vec::new();
print_status_to(&mut buf, d.path(), None).unwrap();
let output = String::from_utf8_lossy(&buf);
assert!(output.contains("未创建规划文件"));
}
#[test]
fn test_print_status_empty_roadmap() {
let d = write_roadmap("");
let mut buf = Vec::new();
print_status_to(&mut buf, d.path(), None).unwrap();
let output = String::from_utf8_lossy(&buf);
assert!(output.contains("未找到规划条目"));
}
#[test]
fn test_print_status_with_data() {
let d =
write_roadmap("## [0.2.0]\n- [x] done\n- [ ] todo\n\n## [0.1.0]\n- [x] a\n- [x] b\n");
let mut buf = Vec::new();
print_status_to(&mut buf, d.path(), None).unwrap();
let output = String::from_utf8_lossy(&buf);
assert!(output.contains("(auto)"));
assert!(output.contains("0.2.0"));
assert!(output.contains("0.1.0"));
assert!(output.contains("3/4"));
assert!(output.contains("总计"));
}
}