|
| 1 | +import argparse |
| 2 | +import datetime |
| 3 | +import json |
| 4 | +import os |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | + |
| 8 | +MANIFEST_FILE = "custom_components/foodsharing/manifest.json" |
| 9 | + |
| 10 | + |
| 11 | +def get_current_version(): |
| 12 | + """Get the current version from git tags (preferred) or manifest.json.""" |
| 13 | + # 1. Try Git Tags (Strict CalVer) |
| 14 | + try: |
| 15 | + # Get all tags |
| 16 | + tags = ( |
| 17 | + subprocess.check_output(["git", "tag"], stderr=subprocess.DEVNULL) |
| 18 | + .decode() |
| 19 | + .splitlines() |
| 20 | + ) |
| 21 | + |
| 22 | + valid_tags = [] |
| 23 | + for tag in tags: |
| 24 | + tag = tag.strip() |
| 25 | + match = re.match(r"^(\d+)\.(\d+)\.(\d+)(?:(b)(\d+)|(-dev)(\d+))?$", tag) |
| 26 | + if match: |
| 27 | + y, m, p, b_p, b_n, d_p, d_n = match.groups() |
| 28 | + priority = 0 |
| 29 | + s_num = 0 |
| 30 | + if b_p: |
| 31 | + priority = 1 |
| 32 | + s_num = int(b_n) |
| 33 | + elif d_p: |
| 34 | + priority = 0 |
| 35 | + s_num = int(d_n) |
| 36 | + else: |
| 37 | + priority = 2 |
| 38 | + |
| 39 | + valid_tags.append( |
| 40 | + {"tag": tag, "key": (int(y), int(m), int(p), priority, s_num)} |
| 41 | + ) |
| 42 | + |
| 43 | + if valid_tags: |
| 44 | + # Sort by key descending |
| 45 | + valid_tags.sort(key=lambda x: x["key"], reverse=True) |
| 46 | + return valid_tags[0]["tag"] |
| 47 | + |
| 48 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 49 | + pass |
| 50 | + |
| 51 | + # 2. Try manifest.json |
| 52 | + if os.path.exists(MANIFEST_FILE): |
| 53 | + with open(MANIFEST_FILE, "r", encoding="utf-8") as f: |
| 54 | + data = json.load(f) |
| 55 | + v = data.get("version", "0.0.0") |
| 56 | + if v != "0.0.0": |
| 57 | + return v |
| 58 | + |
| 59 | + return "2024.1.0" # Safe baseline |
| 60 | + |
| 61 | + |
| 62 | +def write_version(version): |
| 63 | + """Write version to manifest.json.""" |
| 64 | + if os.path.exists(MANIFEST_FILE): |
| 65 | + with open(MANIFEST_FILE, "r", encoding="utf-8") as f: |
| 66 | + data = json.load(f) |
| 67 | + data["version"] = version |
| 68 | + with open(MANIFEST_FILE, "w", encoding="utf-8") as f: |
| 69 | + json.dump(data, f, indent=2) |
| 70 | + |
| 71 | + |
| 72 | +def calculate_version(release_type, now=None): |
| 73 | + current_version = get_current_version() |
| 74 | + if now is None: |
| 75 | + now = datetime.datetime.now() |
| 76 | + year = now.year |
| 77 | + month = now.month |
| 78 | + |
| 79 | + # Parse CalVer: YEAR.MONTH.PATCH and suffix |
| 80 | + # Supports formats like: 2026.1.1, 2026.1.1b1, 2026.1.1-dev1 |
| 81 | + match = re.match(r"^(\d+)\.(\d+)\.(\d+)(?:(b)(\d+)|(-dev)(\d+))?$", current_version) |
| 82 | + |
| 83 | + if match: |
| 84 | + curr_year, curr_month, curr_patch, b_prefix, b_num, dev_prefix, dev_num = ( |
| 85 | + match.groups() |
| 86 | + ) |
| 87 | + curr_year, curr_month, curr_patch = ( |
| 88 | + int(curr_year), |
| 89 | + int(curr_month), |
| 90 | + int(curr_patch), |
| 91 | + ) |
| 92 | + |
| 93 | + if b_prefix: |
| 94 | + suffix_type = "b" |
| 95 | + suffix_num = int(b_num) |
| 96 | + elif dev_prefix: |
| 97 | + suffix_type = "-dev" |
| 98 | + suffix_num = int(dev_num) |
| 99 | + else: |
| 100 | + suffix_type = None |
| 101 | + suffix_num = 0 |
| 102 | + else: |
| 103 | + # Fallback for invalid formats |
| 104 | + curr_year, curr_month, curr_patch = 0, 0, 0 |
| 105 | + suffix_type = None |
| 106 | + suffix_num = 0 |
| 107 | + |
| 108 | + # Logic: Reset patch if Year or Month changes |
| 109 | + is_new_cycle = year != curr_year or month != curr_month |
| 110 | + if is_new_cycle: |
| 111 | + patch = 0 |
| 112 | + else: |
| 113 | + patch = curr_patch |
| 114 | + |
| 115 | + if release_type == "stable": |
| 116 | + # If we have a suffix (beta/dev), stable means "cutting the release" of THIS patch |
| 117 | + if suffix_type is not None: |
| 118 | + return f"{year}.{month}.{patch}" |
| 119 | + |
| 120 | + # If it's a new cycle, we start at .0 |
| 121 | + if is_new_cycle: |
| 122 | + return f"{year}.{month}.0" |
| 123 | + |
| 124 | + # Increment patch |
| 125 | + return f"{year}.{month}.{patch + 1}" |
| 126 | + |
| 127 | + elif release_type == "beta": |
| 128 | + # If Year/Month changes, we start at .0b0 |
| 129 | + if is_new_cycle: |
| 130 | + return f"{year}.{month}.0b0" |
| 131 | + |
| 132 | + # Already in beta? Increment beta number |
| 133 | + if suffix_type == "b": |
| 134 | + return f"{year}.{month}.{patch}b{suffix_num + 1}" |
| 135 | + |
| 136 | + # Coming from stable or dev? |
| 137 | + # We want to increment patch if coming from stable of the same cycle |
| 138 | + # If it was 2026.2.0 (stable), next beta is 2026.2.1b0 |
| 139 | + if suffix_type is None: |
| 140 | + return f"{year}.{month}.{patch + 1}b0" |
| 141 | + |
| 142 | + # Coming from dev? Use current patch but change suffix |
| 143 | + return f"{year}.{month}.{patch}b0" |
| 144 | + |
| 145 | + elif release_type == "dev" or release_type == "nightly": |
| 146 | + # If Year/Month changes, we start at .0-dev0 |
| 147 | + if is_new_cycle: |
| 148 | + return f"{year}.{month}.0-dev0" |
| 149 | + |
| 150 | + # Already in dev? Increment dev number |
| 151 | + if suffix_type == "-dev": |
| 152 | + return f"{year}.{month}.{patch}-dev{suffix_num + 1}" |
| 153 | + |
| 154 | + # New dev? Increment patch if coming from stable |
| 155 | + if suffix_type is None: |
| 156 | + return f"{year}.{month}.{patch + 1}-dev0" |
| 157 | + |
| 158 | + # Coming from beta? Just change suffix |
| 159 | + return f"{year}.{month}.{patch}-dev0" |
| 160 | + |
| 161 | + else: |
| 162 | + raise ValueError(f"Unknown release type: {release_type}") |
| 163 | + |
| 164 | + |
| 165 | +def main(): |
| 166 | + parser = argparse.ArgumentParser(description="Manage project version.") |
| 167 | + parser.add_argument("action", choices=["get", "bump"], help="Action to perform") |
| 168 | + parser.add_argument( |
| 169 | + "--type", |
| 170 | + choices=["stable", "beta", "nightly", "dev"], |
| 171 | + help="Release type for bump", |
| 172 | + ) |
| 173 | + |
| 174 | + args = parser.parse_args() |
| 175 | + |
| 176 | + if args.action == "get": |
| 177 | + print(get_current_version()) |
| 178 | + elif args.action == "bump": |
| 179 | + if not args.type: |
| 180 | + print("Error: --type is required for bump action") |
| 181 | + exit(1) |
| 182 | + new_version = calculate_version(args.type) |
| 183 | + write_version(new_version) |
| 184 | + print(new_version) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + main() |
0 commit comments