|
6 | 6 | from pathlib import Path |
7 | 7 | from typing import Any |
8 | 8 | from typing import Callable |
| 9 | +from typing import Literal |
| 10 | +from typing import Optional |
| 11 | +from typing import TypeAlias |
9 | 12 |
|
10 | 13 |
|
11 | 14 | REPO_FOLDER: Path = Path(__file__).resolve().parent.parent |
12 | 15 |
|
| 16 | +Increment: TypeAlias = Literal["MAJOR", "MINOR", "PATCH", "PRERELEASE"] |
| 17 | + |
13 | 18 |
|
14 | 19 | class MissingDependencyError(Exception): |
15 | 20 | """Exception raised when a depedency is missing from the system running setup-repo.""" |
@@ -52,3 +57,83 @@ def remove_readonly(func: Callable[[str], Any], path: str, _: Any) -> None: |
52 | 57 | """ |
53 | 58 | Path(path).chmod(stat.S_IWRITE) |
54 | 59 | func(path) |
| 60 | + |
| 61 | + |
| 62 | +def get_package_version() -> str: |
| 63 | + """Gets the package version.""" |
| 64 | + result: subprocess.CompletedProcess = subprocess.run( |
| 65 | + ["uvx", "version", "-p"], |
| 66 | + cwd=REPO_FOLDER, |
| 67 | + capture_output=True |
| 68 | + ) |
| 69 | + return result.stdout.decode("utf-8").strip() |
| 70 | + |
| 71 | + |
| 72 | +def get_bumped_package_version(increment: Optional[Increment] = None) -> str: |
| 73 | + """Gets the bumped package version.""" |
| 74 | + args: list[str] = ["uvx", "cz", "bump", "--get-next", "--yes", "--dry-run"] |
| 75 | + if increment is not None: |
| 76 | + args.extend(["--increment", increment]) |
| 77 | + result: subprocess.CompletedProcess = subprocess.run(args, cwd=REPO_FOLDER, capture_output=True) |
| 78 | + return result.stdout.decode("utf-8").strip() |
| 79 | + |
| 80 | + |
| 81 | +def create_release_branch(new_version: str) -> None: |
| 82 | + """Creates a release branch.""" |
| 83 | + commands: list[list[str]] = [ |
| 84 | + ["git", "status", "--porcelain"], |
| 85 | + ["git", "branch", "-b", f"release/{new_version}", "develop"], |
| 86 | + ["git", "checkout", f"release/{new_version}"], |
| 87 | + ] |
| 88 | + for command in commands: |
| 89 | + subprocess.run(command, cwd=REPO_FOLDER, capture_output=True, check=True) |
| 90 | + |
| 91 | + |
| 92 | +def bump_version(increment: Optional[Increment] = None) -> None: |
| 93 | + """Bumps the package version.""" |
| 94 | + bump_cmd: list[str] = ["uvx", "cz", "bump", "--yes", "--files-only", "--changelog"] |
| 95 | + if increment is not None: |
| 96 | + bump_cmd.extend(["--increment", increment]) |
| 97 | + subprocess.run(bump_cmd, cwd=REPO_FOLDER, check=True) |
| 98 | + |
| 99 | + |
| 100 | +def get_latest_tag() -> Optional[str]: |
| 101 | + """Gets the latest git tag.""" |
| 102 | + sort_tags: list[str] = ["git", "tag", "--sort=-creatordate"] |
| 103 | + find_last: list[str] = ["grep", "-v", '"${GITHUB_REF_NAME}"'] |
| 104 | + echo_none: list[str] = ["echo", "''"] |
| 105 | + result: subprocess.CompletedProcess = subprocess.run( |
| 106 | + [*sort_tags, "|", *find_last, "|", "tail", "-n1", "||", *echo_none], |
| 107 | + cwd=REPO_FOLDER, |
| 108 | + capture_output=True |
| 109 | + ) |
| 110 | + tag: str = result.stdout.decode("utf-8").strip() |
| 111 | + if tag == "": |
| 112 | + return None |
| 113 | + return tag |
| 114 | + |
| 115 | + |
| 116 | +def get_latest_release_notes() -> None: |
| 117 | + """Gets the release notes. |
| 118 | +
|
| 119 | + Assumes the latest_tag hasn't been applied yet. |
| 120 | + """ |
| 121 | + latest_tag: Optional[str] = get_latest_tag() |
| 122 | + latest_version: str = get_package_version() |
| 123 | + if latest_tag == latest_version: |
| 124 | + raise ValueError( |
| 125 | + "The latest tag and version are the same. Please ensure the release notes are taken before tagging." |
| 126 | + ) |
| 127 | + rev_range: str = latest_version if latest_tag is None else f"{latest_tag}..{latest_version}" |
| 128 | + |
| 129 | + result: subprocess.CompletedProcess = subprocess.run( |
| 130 | + ["uvx", "cz", "changelog", rev_range, "--dry-run"], |
| 131 | + cwd=REPO_FOLDER, |
| 132 | + check=True |
| 133 | + ) |
| 134 | + return result.stdout.decode("utf-8") |
| 135 | + |
| 136 | + |
| 137 | +def tag_release() -> None: |
| 138 | + """Tags the release using commitizen bump with tag only.""" |
| 139 | + subprocess.run(["uvx", "cz", "bump", "--tag-only", "--yes"], cwd=REPO_FOLDER, check=True) |
0 commit comments