|
| 1 | +"""Helpers for ZeroVer release automation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import TYPE_CHECKING, Literal |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from pathlib import Path |
| 11 | + |
| 12 | +INITIAL_RELEASE_VERSION = "0.1.0" |
| 13 | +TAG_PREFIX = "v" |
| 14 | +ZEROVER_PART_COUNT = 3 |
| 15 | +ReleaseKind = Literal["patch", "minor"] |
| 16 | + |
| 17 | +_PYPROJECT_VERSION_PATTERN = re.compile(r'^version = "([^"]+)"$') |
| 18 | + |
| 19 | + |
| 20 | +@dataclass(frozen=True, order=True) |
| 21 | +class ZeroVer: |
| 22 | + """A parsed ZeroVer version.""" |
| 23 | + |
| 24 | + minor: int |
| 25 | + patch: int |
| 26 | + |
| 27 | + def __str__(self) -> str: |
| 28 | + """Render the version as 0.Y.Z.""" |
| 29 | + return f"0.{self.minor}.{self.patch}" |
| 30 | + |
| 31 | + |
| 32 | +def parse_zerover(value: str) -> ZeroVer: |
| 33 | + """Parse a ZeroVer version string.""" |
| 34 | + parts = value.split(".") |
| 35 | + if len(parts) != ZEROVER_PART_COUNT: |
| 36 | + msg = f"expected a 0.Y.Z version, got {value!r}" |
| 37 | + raise ValueError(msg) |
| 38 | + |
| 39 | + major, minor_text, patch_text = parts |
| 40 | + if major != "0" or not minor_text.isdigit() or not patch_text.isdigit(): |
| 41 | + msg = f"expected a 0.Y.Z version, got {value!r}" |
| 42 | + raise ValueError(msg) |
| 43 | + |
| 44 | + return ZeroVer(minor=int(minor_text), patch=int(patch_text)) |
| 45 | + |
| 46 | + |
| 47 | +def parse_release_tag(tag: str) -> ZeroVer: |
| 48 | + """Parse a release tag of the form v0.Y.Z.""" |
| 49 | + if not tag.startswith(TAG_PREFIX): |
| 50 | + msg = f"expected a release tag starting with {TAG_PREFIX!r}, got {tag!r}" |
| 51 | + raise ValueError(msg) |
| 52 | + |
| 53 | + return parse_zerover(tag.removeprefix(TAG_PREFIX)) |
| 54 | + |
| 55 | + |
| 56 | +def find_latest_release_tag(tags: list[str]) -> str | None: |
| 57 | + """Return the highest ZeroVer release tag from a list of tags.""" |
| 58 | + release_tags = [tag for tag in tags if tag.startswith(TAG_PREFIX)] |
| 59 | + if not release_tags: |
| 60 | + return None |
| 61 | + |
| 62 | + _, latest_tag = max( |
| 63 | + ((parse_release_tag(tag), tag) for tag in release_tags), |
| 64 | + key=lambda item: item[0], |
| 65 | + ) |
| 66 | + return latest_tag |
| 67 | + |
| 68 | + |
| 69 | +def determine_next_version( |
| 70 | + latest_release_tag: str | None, |
| 71 | + release_kind: ReleaseKind, |
| 72 | +) -> str: |
| 73 | + """Determine the next release version from the latest release tag.""" |
| 74 | + if latest_release_tag is None: |
| 75 | + return INITIAL_RELEASE_VERSION |
| 76 | + |
| 77 | + latest_version = parse_release_tag(latest_release_tag) |
| 78 | + if release_kind == "minor": |
| 79 | + return str(ZeroVer(minor=latest_version.minor + 1, patch=0)) |
| 80 | + |
| 81 | + return str(ZeroVer(minor=latest_version.minor, patch=latest_version.patch + 1)) |
| 82 | + |
| 83 | + |
| 84 | +def read_pyproject_version(pyproject_path: Path) -> str: |
| 85 | + """Read project.version from pyproject.toml.""" |
| 86 | + project_section_found = False |
| 87 | + |
| 88 | + for line in pyproject_path.read_text(encoding="utf-8").splitlines(): |
| 89 | + stripped = line.strip() |
| 90 | + if stripped == "[project]": |
| 91 | + project_section_found = True |
| 92 | + continue |
| 93 | + |
| 94 | + if project_section_found and stripped.startswith("["): |
| 95 | + break |
| 96 | + |
| 97 | + if project_section_found: |
| 98 | + match = _PYPROJECT_VERSION_PATTERN.match(stripped) |
| 99 | + if match is not None: |
| 100 | + return match.group(1) |
| 101 | + |
| 102 | + msg = f"could not find [project].version in {pyproject_path}" |
| 103 | + raise ValueError(msg) |
| 104 | + |
| 105 | + |
| 106 | +def write_pyproject_version(pyproject_path: Path, version: str) -> None: |
| 107 | + """Write project.version in pyproject.toml.""" |
| 108 | + parse_zerover(version) |
| 109 | + lines = pyproject_path.read_text(encoding="utf-8").splitlines() |
| 110 | + project_section_found = False |
| 111 | + |
| 112 | + for index, line in enumerate(lines): |
| 113 | + stripped = line.strip() |
| 114 | + if stripped == "[project]": |
| 115 | + project_section_found = True |
| 116 | + continue |
| 117 | + |
| 118 | + if project_section_found and stripped.startswith("["): |
| 119 | + break |
| 120 | + |
| 121 | + if project_section_found and _PYPROJECT_VERSION_PATTERN.match(stripped): |
| 122 | + lines[index] = f'version = "{version}"' |
| 123 | + pyproject_path.write_text("\n".join(lines) + "\n", encoding="utf-8") |
| 124 | + return |
| 125 | + |
| 126 | + msg = f"could not find [project].version in {pyproject_path}" |
| 127 | + raise ValueError(msg) |
0 commit comments