|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from typing import List |
| 6 | + |
| 7 | + |
| 8 | +def die(msg: str): |
| 9 | + print(msg, file=sys.stderr) |
| 10 | + sys.exit(1) |
| 11 | + |
| 12 | + |
| 13 | +def cmd_exec(cmd: List[str]) -> str: |
| 14 | + result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 15 | + if result.returncode != 0: |
| 16 | + die(f"Error: Failed to execute command: {' '.join(cmd)}\n{result.stderr}") |
| 17 | + return result.stdout.strip() |
| 18 | + |
| 19 | + |
| 20 | +def check_git_status(): |
| 21 | + stdout = cmd_exec(["git", "status", "--porcelain"]) |
| 22 | + if len(stdout) > 0: |
| 23 | + die("Error: There are uncommitted changes. Please commit or stash them before proceeding.") |
| 24 | + |
| 25 | + stdout = cmd_exec(["git", "tag", "--points-at", "HEAD"]) |
| 26 | + if len(stdout) > 0: |
| 27 | + die("Error: HEAD is already tagged.") |
| 28 | + |
| 29 | + branch = cmd_exec(["git", "branch", "--show-current"]) |
| 30 | + if branch != "main": |
| 31 | + die("Error: You must be on the main branch to update the version.") |
| 32 | + |
| 33 | + |
| 34 | +def update_version(part: str) -> str: |
| 35 | + init_file = os.path.join(os.path.dirname(__file__), "src", "lara_sdk", "__init__.py") |
| 36 | + with open(init_file, "r") as f: |
| 37 | + lines = f.readlines() |
| 38 | + |
| 39 | + new_version = None |
| 40 | + |
| 41 | + for i, line in enumerate(lines): |
| 42 | + if line.startswith("__version__"): |
| 43 | + version = line.split("=")[1].strip().strip("\"'") |
| 44 | + |
| 45 | + major, minor, patch = version.split(".") |
| 46 | + if part == "major": |
| 47 | + major = str(int(major) + 1) |
| 48 | + minor = "0" |
| 49 | + patch = "0" |
| 50 | + elif part == "minor": |
| 51 | + minor = str(int(minor) + 1) |
| 52 | + patch = "0" |
| 53 | + elif part == "patch": |
| 54 | + patch = str(int(patch) + 1) |
| 55 | + else: |
| 56 | + die("Error: Invalid version part.") |
| 57 | + |
| 58 | + new_version = f"{major}.{minor}.{patch}" |
| 59 | + lines[i] = f"__version__ = \"{new_version}\"\n" |
| 60 | + break |
| 61 | + |
| 62 | + if new_version is None: |
| 63 | + die("Error: Could not find version in __init__.py.") |
| 64 | + |
| 65 | + with open(init_file, "w") as f: |
| 66 | + f.writelines(lines) |
| 67 | + |
| 68 | + return new_version |
| 69 | + |
| 70 | + |
| 71 | +def git_tag(version: str): |
| 72 | + cmd_exec(["git", "add", "."]) |
| 73 | + cmd_exec(["git", "commit", "-m", f"v{version}"]) |
| 74 | + cmd_exec(["git", "tag", f"v{version}"]) |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + args = sys.argv[1:] |
| 79 | + |
| 80 | + if len(args) != 1 or args[0] not in ["major", "minor", "patch"]: |
| 81 | + print("Usage: python3 version [major|minor|patch]") |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + check_git_status() |
| 85 | + version = update_version(args[0]) |
| 86 | + git_tag(version) |
| 87 | + |
| 88 | + print(f"Tag v{version} created.") |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + main() |
0 commit comments