Repository navigation
Expand file tree
/
Copy pathcollect_release_notes.jac
More file actions
168 lines (156 loc) · 5.65 KB
/
Copy pathcollect_release_notes.jac
File metadata and controls
168 lines (156 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""Collect release note fragments and assemble them into release notes files.
Reads fragment files from release_notes/unreleased/<package>/
Fragment files follow the flat naming convention: <PR#>.<category>.md
(e.g. 1234.bugfix.md, 5678.feature.md)
Usage:
Called by release.jac during the release process.
Can also be run standalone for testing:
jac run scripts/collect_release_notes.jac --package jaclang --version 0.13.6 --dry-run
"""
import argparse;
import re;
import from pathlib { Path }
import from release_utils { PACKAGES }
glob UNRELEASED_DIR: Path = Path("release_notes/unreleased");
"""Read all fragment files from a package's unreleased directory.
Fragments follow the flat naming convention: <PR#>.<category>.md
Returns a dict mapping category name to list of entry strings.
"""
def collect_fragments(package_dir: Path) -> dict[(str, list[str])] {
categories: dict[(str, list[str])] = {
"feature": [],
"bugfix": [],
"breaking": [],
"refactor": [],
"docs": []
};
for fragment in sorted(package_dir.`glob("*.md")) {
if (fragment.name == "README.md") {
continue;
}
parts = fragment.stem.split(".");
if ((len(parts) != 2) or (parts[1] not in categories)) {
continue;
}
category = parts[1];
content = fragment.read_text(encoding="utf-8").strip();
# Fragment links must be written relative to the assembled page
# (jac/jaclang/cli/docs/community/release_notes/<pkg>.md) -- e.g.
# (../breaking-changes.md). The corpus link test validates them
# after assembly.
if content {
categories[category].append(content);
}
}
return {
k: v
for (k, v) in categories.items()
if v
};
}
"""Build the markdown section for a new release version."""
def build_section(
display_name: str, version: str, categories: dict[(str, list[str])]
) -> str {
lines = [f"## {display_name} {version} (Latest Release)", ""];
category_headings = {
"breaking": "### Breaking Changes",
"feature": "### New Features",
"bugfix": "### Bug Fixes",
"refactor": "### Refactors",
"docs": "### Documentation"
};
for cat_key in ("breaking", "feature", "bugfix", "refactor", "docs") {
entries = categories.get(cat_key, []);
if not entries {
continue;
}
lines.append(category_headings[cat_key]);
lines.append("");
for `entry in entries {
lines.append(`entry);
}
lines.append("");
}
return "\n".join(lines);
}
"""Insert the new version section into the release notes file.
Demotes the existing (Latest Release) tag and inserts the new section
right after the file header (title + description paragraph).
"""
def inject_section(release_notes_path: Path, display_name: str, new_section: str) {
content = release_notes_path.read_text(encoding="utf-8");
content = content.replace(" (Latest Release)", "");
re_match = re.search(r"^## ", content, re.MULTILINE);
if re_match {
insert_pos = re_match.start();
content = content[:insert_pos] + new_section + "\n" + content[insert_pos:];
} else {
content = content.rstrip() + "\n\n" + new_section + "\n";
}
release_notes_path.write_text(content, encoding="utf-8");
}
"""Delete all fragment files (but keep README.md)."""
def delete_fragments(package_dir: Path) -> list[str] {
deleted = [];
for fragment in package_dir.`glob("*.md") {
if (fragment.name == "README.md") {
continue;
}
fragment.unlink();
deleted.append(str(fragment));
}
return deleted;
}
"""Assemble fragments for a single package into its release notes file.
Returns True if fragments were found and assembled, False if no fragments.
"""
def assemble_package(
repo_root: Path, pkg_key: str, version: str, dry_run: bool = False
) -> bool {
pkg_info = PACKAGES[pkg_key];
if not pkg_info.release_notes {
return False;
}
package_dir = repo_root / UNRELEASED_DIR / pkg_info.notes_display;
if not package_dir.is_dir() {
print(f" No unreleased directory for {pkg_key}");
return False;
}
categories = collect_fragments(package_dir);
if not categories {
print(f" No fragments found for {pkg_key}");
return False;
}
total = sum(len(v) for v in categories.values());
print(f" Found {total} fragment(s) for {pkg_key}");
section = build_section(pkg_info.notes_display, version, categories);
if dry_run {
print(f" [dry-run] Would insert into {pkg_info.release_notes}:");
print(section);
return True;
}
release_notes_path = repo_root / pkg_info.release_notes;
inject_section(release_notes_path, pkg_info.notes_display, section);
print(f" Inserted into {pkg_info.release_notes}");
deleted = delete_fragments(package_dir);
print(f" Deleted {len(deleted)} fragment file(s)");
return True;
}
"""Standalone entry point for testing."""
def main {
parser = argparse.ArgumentParser(description="Collect release note fragments");
parser.add_argument("--package", required=True, help="Package key (e.g., jaclang)");
parser.add_argument(
"--version", required=True, help="Version string (e.g., 0.13.6)"
);
parser.add_argument(
"--dry-run", action="store_true", help="Print without modifying"
);
args = parser.parse_args();
repo_root = Path(__file__).resolve().parent.parent;
assemble_package(repo_root, args.package, args.version, args.dry_run);
}
with entry:__main__ {
main();
}