33
44import argparse
55import datetime
6+ import os
67import re
78import subprocess
89import sys
910from pathlib import Path
1011from typing import Tuple
1112
13+ PROJECT_DIR = Path (__file__ ).parent .parent
14+
1215parser = argparse .ArgumentParser (description = "Tags and releases the next version of this project." )
1316
1417parser .add_argument ("tag" , help = "Semantic version number to use as the tag." )
@@ -24,7 +27,7 @@ def parse_changelog(args: argparse.Namespace) -> Tuple[str, str]:
2427 """Return an updated changelog and and the list of changes."""
2528 match = re .match (
2629 pattern = r"(.*?## \[Unreleased]\n)(.+?\n)(\n*## \[.*)" ,
27- string = Path ( "CHANGELOG.md" ).read_text (encoding = "utf-8" ),
30+ string = ( PROJECT_DIR / "CHANGELOG.md" ).read_text (encoding = "utf-8" ),
2831 flags = re .DOTALL ,
2932 )
3033 assert match
@@ -41,6 +44,23 @@ def parse_changelog(args: argparse.Namespace) -> Tuple[str, str]:
4144 return "" .join ((header , tagged , tail )), changes
4245
4346
47+ def replace_unreleased_tags (tag : str , dry_run : bool ) -> None :
48+ match = re .match (r"\d+\.\d+" , tag )
49+ assert match
50+ short_tag = match .group ()
51+ for directory , _ , files in os .walk (PROJECT_DIR / "tcod" ):
52+ for filename in files :
53+ file = Path (directory , filename )
54+ if file .suffix != ".py" :
55+ continue
56+ text = file .read_text (encoding = "utf-8" )
57+ new_text = re .sub (r":: unreleased" , rf":: { short_tag } " , text )
58+ if text != new_text :
59+ print (f"Update tags in { file } " )
60+ if not dry_run :
61+ file .write_text (new_text , encoding = "utf-8" )
62+
63+
4464def main () -> None :
4565 if len (sys .argv ) == 1 :
4666 parser .print_help (sys .stderr )
@@ -54,8 +74,10 @@ def main() -> None:
5474 print ("--- New changelog:" )
5575 print (new_changelog )
5676
77+ replace_unreleased_tags (args .tag , args .dry_run )
78+
5779 if not args .dry_run :
58- Path ( "CHANGELOG.md" ).write_text (new_changelog , encoding = "utf-8" )
80+ ( PROJECT_DIR / "CHANGELOG.md" ).write_text (new_changelog , encoding = "utf-8" )
5981 edit = ["-e" ] if args .edit else []
6082 subprocess .check_call (["git" , "commit" , "-avm" , "Prepare %s release." % args .tag ] + edit )
6183 subprocess .check_call (["git" , "tag" , args .tag , "-am" , "%s\n \n %s" % (args .tag , changes )] + edit )
0 commit comments