Added build script

This commit is contained in:
Michael Chen 2023-01-27 15:20:10 +01:00
parent 64fe3d69dd
commit 72275c491a
Signed by: cnml
GPG Key ID: 5845BF3F82D5F629
2 changed files with 54 additions and 0 deletions

53
PongGame/build.py Normal file
View File

@ -0,0 +1,53 @@
#!/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()

1
PongGame/version.txt Normal file
View File

@ -0,0 +1 @@
1.0.0