54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
from argparse import ArgumentParser
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
@dataclass
|
|
class Version:
|
|
major: int = 1
|
|
minor: int = 0
|
|
patch: int = 0
|
|
|
|
def __str__(self):
|
|
return f"{self.major}.{self.minor}.{self.patch}"
|
|
|
|
def __init__(self, version_str: str):
|
|
try:
|
|
match list(map(int, version_str.split("."))):
|
|
case [major, minor, patch]:
|
|
self.major, self.minor, self.patch = major, minor, patch
|
|
return
|
|
case _:
|
|
raise ValueError("Version file must contain 3 parts (SemVer)!")
|
|
except ValueError as e:
|
|
raise ValueError(f"Version must contain numbers only!") from e
|
|
|
|
def parse_args():
|
|
parser = ArgumentParser("build.py", description="Docker multi-arch build helper.")
|
|
version_group = parser.add_mutually_exclusive_group()
|
|
version_group.add_argument("--major", "-m", action="store_true", help="Increase the major version before building.")
|
|
version_group.add_argument("--minor", "-n", action="store_true", help="Increase the minor version before building.")
|
|
version_group.add_argument("--patch", "-p", action="store_true", help="Increase the patch version before building.")
|
|
parser.add_argument("--file", "-f", type=str, default="version.txt", help="File to store the current version in.")
|
|
return parser.parse_args()
|
|
|
|
def save_version(version_file: Path, version: Version):
|
|
with version_file.open("w", encoding="utf-8") as f:
|
|
f.write(str(version))
|
|
|
|
def load_version(version_file: Path):
|
|
try:
|
|
with version_file.open("r", encoding="utf-8") as f:
|
|
return Version(f.read().strip())
|
|
except FileNotFoundError:
|
|
return Version()
|
|
|
|
def main():
|
|
args = parse_args()
|
|
version_file = Path(args.file)
|
|
version = load_version(version_file)
|
|
save_version(version_file, version)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|