revamp version selector

This commit is contained in:
Aran-Fey
2024-11-02 17:45:35 +01:00
parent 5d07db4b45
commit 9d9c7627d5

View File

@@ -1,7 +1,9 @@
import subprocess
import sys
from dataclasses import dataclass
import revel
import typing_extensions as te
def main() -> None:
@@ -79,7 +81,7 @@ def make_new_release() -> None:
subprocess.run(["git", "push"], check=True)
# Create a tag
version = get_version()
version = str(get_version())
subprocess.run(["git", "tag", version], check=True)
subprocess.run(["git", "push", "origin", "tag", version], check=True)
@@ -89,40 +91,118 @@ def make_new_release() -> None:
def bump_version() -> None:
options = {
"patch": "patch",
"minor": "minor",
"major": "major",
}
version = get_version()
if "rc" in version:
options["release candidate"] = "rc"
version_part_to_bump = revel.select(
options,
prompt="Which version do you want to bump?",
)
options = dict[str, str]()
if version_part_to_bump != "rc":
if revel.select_yes_no("Make this a pre-release?"):
version_part_to_bump += ",rc"
if version.rc is None:
v = version.bump_patch()
options[f"Patch: {v}"] = str(v)
v = v.bump_rc()
options[f"Patch RC: {v}"] = str(v)
v = version.bump_minor()
options[f"Minor: {v}"] = str(v)
v = v.bump_rc()
options[f"Minor RC: {v}"] = str(v)
v = version.bump_major()
options[f"Major: {v}"] = str(v)
v = v.bump_rc()
options[f"Major RC: {v}"] = str(v)
else:
v = version.bump_rc()
options[f"RC: {v}"] = str(v)
v = version.drop_rc()
options[f"No RC: {v}"] = str(v)
new_version = revel.select(options, prompt="Select new version")
subprocess.run(
[sys.executable, "-m", "hatch", "version", version_part_to_bump],
[sys.executable, "-m", "hatch", "version", new_version],
check=True,
)
subprocess.run(["git", "commit", "--all", "-m", "bump version"], check=True)
def get_version() -> str:
return subprocess.run(
@dataclass(frozen=True)
class Version:
major: int
minor: int = 0
patch: int = 0
rc: int | None = 0
@classmethod
def parse(cls, version_string: str) -> te.Self:
head, _, tail = version_string.partition("rc")
if tail:
rc = int(tail)
else:
rc = None
parts = head.split(".")
parts += ("0", "0", "0")
return cls(
major=int(parts[0]), minor=int(parts[1]), patch=int(parts[2]), rc=rc
)
def bump_major(self) -> te.Self:
return type(self)(major=self.major + 1)
def bump_minor(self) -> te.Self:
return type(self)(major=self.major, minor=self.minor + 1)
def bump_patch(self) -> te.Self:
return type(self)(
major=self.major,
minor=self.minor,
patch=self.patch + 1,
)
def bump_rc(self) -> te.Self:
return type(self)(
major=self.major,
minor=self.minor,
patch=self.patch,
rc=0 if self.rc is None else self.rc + 1,
)
def drop_rc(self) -> te.Self:
return type(self)(
major=self.major,
minor=self.minor,
patch=self.patch,
rc=None,
)
def __str__(self):
version_string = f"{self.major}.{self.minor}"
if self.patch > 0:
version_string += f".{self.patch}"
if self.rc is not None:
version_string += f"rc{self.rc}"
return version_string
def get_version() -> Version:
version_string = subprocess.run(
[sys.executable, "-m", "hatch", "version"],
check=True,
capture_output=True,
text=True,
).stdout.strip()
return Version.parse(version_string)
if __name__ == "__main__":
main()