|
| 1 | +import sys |
| 2 | + |
| 3 | +# The GitHub label passed as an argument: patch, minor, major, release |
| 4 | +LABEL = sys.argv[1] |
| 5 | + |
| 6 | +# The file that stores the current version |
| 7 | +VERSION_FILE = "version.txt" |
| 8 | + |
| 9 | +def read_version(): |
| 10 | + """Read the current version from version.txt""" |
| 11 | + with open(VERSION_FILE, "r") as f: |
| 12 | + return f.read().strip() |
| 13 | + |
| 14 | +def write_version(version): |
| 15 | + """Write the new version back to version.txt""" |
| 16 | + with open(VERSION_FILE, "w") as f: |
| 17 | + f.write(version + "\n") |
| 18 | + |
| 19 | +def parse_version(v): |
| 20 | + """ |
| 21 | + Parse a version string like "0.5.0-draft" into parts: |
| 22 | + major, minor, patch, and optional suffix (e.g., "-draft"). |
| 23 | + """ |
| 24 | + parts = v.split(".") # Split into ['0', '5', '0-draft'] |
| 25 | + major = int(parts[0]) |
| 26 | + minor = int(parts[1]) |
| 27 | + |
| 28 | + # Handle patch number and suffix(if exist) |
| 29 | + if "-" in parts[2]: |
| 30 | + patch_str, suffix = parts[2].split("-", 1) |
| 31 | + patch = int(patch_str) |
| 32 | + suffix = "-" + suffix # Keep the dash in the suffix |
| 33 | + else: |
| 34 | + patch = int(parts[2]) |
| 35 | + suffix = "" |
| 36 | + |
| 37 | + return major, minor, patch, suffix |
| 38 | + |
| 39 | + |
| 40 | +def bump_version(major, minor, patch): |
| 41 | + """ |
| 42 | + Increment the version based on LABEL: |
| 43 | + - patch: increment patch |
| 44 | + - minor: increment minor, reset patch |
| 45 | + - major: increment major, reset minor and patch |
| 46 | + """ |
| 47 | + if LABEL == "patch": |
| 48 | + patch = patch + 1 |
| 49 | + elif LABEL == "minor": |
| 50 | + minor = minor + 1 |
| 51 | + patch = 0 |
| 52 | + elif LABEL == "major": |
| 53 | + major = major + 1 |
| 54 | + minor = 0 |
| 55 | + patch = 0 |
| 56 | + return major, minor, patch |
| 57 | + |
| 58 | +def main(): |
| 59 | + # Read current version |
| 60 | + old_version = read_version() |
| 61 | + |
| 62 | + # Parse current version |
| 63 | + major, minor, patch, suffix = parse_version(old_version) |
| 64 | + |
| 65 | + # If label is release, remove the "-draft" suffix |
| 66 | + if LABEL == "release": |
| 67 | + new_version = f"{major}.{minor}.{patch}" |
| 68 | + else: |
| 69 | + # Otherwise, bump version according to label |
| 70 | + new_major, new_minor, new_patch = bump_version(major, minor, patch) |
| 71 | + new_version = f"{new_major}.{new_minor}.{new_patch}-draft" |
| 72 | + |
| 73 | + # Prevent race conditions by only writing if version changed |
| 74 | + if new_version != old_version: |
| 75 | + write_version(new_version) |
| 76 | + print(f"Version updated: {old_version} -> {new_version}") |
| 77 | + else: |
| 78 | + print(f"No version change needed. Current version: {old_version}") |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments