|
| 1 | +import datetime |
| 2 | +import re |
| 3 | +import sys |
| 4 | + |
| 5 | +# Read existing changelog |
| 6 | +with open("Changelog.md") as f: |
| 7 | + changelog_lines = list(s.rstrip() for s in f) |
| 8 | + |
| 9 | +# Determine new version |
| 10 | +this_version = sys.argv[1] |
| 11 | +today = datetime.date.today() |
| 12 | +today_str = f"{today.strftime('%Y')}-{today.strftime('%m')}-{today.strftime('%d')}" |
| 13 | + |
| 14 | +# Determine last version |
| 15 | +matches = re.finditer(r"^## \[v?(.*)\].*$", "\n".join(changelog_lines), re.MULTILINE) |
| 16 | +last_version = next(matches)[1] |
| 17 | + |
| 18 | +# Stop the script if new version is already the latest |
| 19 | +if last_version == this_version: |
| 20 | + print(f"Version {this_version} is already up-to-date.") |
| 21 | + sys.exit(0) |
| 22 | + |
| 23 | +# Find line with "## Unreleased" heading |
| 24 | +unreleased_idx = next( |
| 25 | + i for i, line in enumerate(changelog_lines) if line.startswith("## Unreleased") |
| 26 | +) |
| 27 | + |
| 28 | +# Find line with the last release (i.e. "## x.y.z" heading) |
| 29 | +last_release_idx = next( |
| 30 | + i |
| 31 | + for i, line in enumerate(changelog_lines) |
| 32 | + if line.startswith("## ") and i > unreleased_idx |
| 33 | +) |
| 34 | + |
| 35 | +# Clean up unreleased notes (i.e. remove empty sections) |
| 36 | +released_notes = "\n".join(changelog_lines[(unreleased_idx + 2) : last_release_idx]) |
| 37 | + |
| 38 | +release_section_fragments = re.split("\n### (.*)\n", released_notes, re.MULTILINE) |
| 39 | +release_notes_intro = release_section_fragments[0] |
| 40 | +release_sections = list( |
| 41 | + zip(release_section_fragments[1::2], release_section_fragments[2::2]) |
| 42 | +) |
| 43 | +nonempty_release_sections = [ |
| 44 | + (section, content) for section, content in release_sections if content.strip() != "" |
| 45 | +] |
| 46 | + |
| 47 | +cleaned_release_notes = ( |
| 48 | + "\n".join( |
| 49 | + [release_notes_intro] |
| 50 | + + [ |
| 51 | + f"### {section}\n{content}" |
| 52 | + for section, content in nonempty_release_sections |
| 53 | + ] |
| 54 | + ) |
| 55 | +).split("\n") |
| 56 | + |
| 57 | +# Update changelog |
| 58 | +lines_to_insert = [ |
| 59 | + "## Unreleased", |
| 60 | + f"[Commits](https://github.com/scalableminds/webknossos-libs/compare/v{this_version}...HEAD)", |
| 61 | + "", |
| 62 | + "### Breaking Changes", |
| 63 | + "", |
| 64 | + "### Added", |
| 65 | + "", |
| 66 | + "### Changed", |
| 67 | + "", |
| 68 | + "### Fixed", |
| 69 | + "", |
| 70 | + "", |
| 71 | + f"## [{this_version}](https://github.com/scalableminds/webknossos-libs/releases/tag/v{this_version}) - {today_str}", |
| 72 | + f"[Commits](https://github.com/scalableminds/webknossos-libs/compare/v{last_version}...v{this_version})", |
| 73 | +] |
| 74 | +changelog_lines = ( |
| 75 | + changelog_lines[:unreleased_idx] |
| 76 | + + lines_to_insert |
| 77 | + + cleaned_release_notes |
| 78 | + + [""] |
| 79 | + + changelog_lines[(last_release_idx):] |
| 80 | + + [""] |
| 81 | +) |
| 82 | + |
| 83 | +with open("Changelog.md", "wt") as f: |
| 84 | + f.write("\n".join(changelog_lines)) |
0 commit comments