File size: 1,588 Bytes
9a58bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""Bump the minor version of Sunnyware. Every push = new minor version.

Usage:
  python scripts/bump_version.py             # bump and print
  python scripts/bump_version.py --dry-run   # print without writing
  python scripts/bump_version.py --patch     # bump patch instead of minor
"""
import re
import sys
from pathlib import Path

REPO = Path(__file__).resolve().parent.parent
INIT_FILE = REPO / "src" / "sunnyware" / "__init__.py"


def read_current():
    t = INIT_FILE.read_text(encoding="utf-8")
    m = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', t)
    if not m:
        raise SystemExit(f"Could not parse __version__ from {INIT_FILE}")
    return m.group(1), t


def bump(version, patch=False):
    parts = version.split(".")
    while len(parts) < 3:
        parts.append("0")
    major, minor, ptch = int(parts[0]), int(parts[1]), int(parts[2])
    if patch:
        return f"{major}.{minor}.{ptch + 1}"
    return f"{major}.{minor + 1}.0"


def main():
    dry_run = "--dry-run" in sys.argv
    patch = "--patch" in sys.argv

    old, t = read_current()
    new = bump(old, patch=patch)

    if dry_run:
        print(f"[bump] {old} -> {new} (dry-run, not written)")
        return 0

    t = re.sub(
        r'__version__\s*=\s*["\'][^"\']+["\']',
        f'__version__ = "{new}"',
        t,
        count=1,
    )
    INIT_FILE.write_text(t, encoding="utf-8")
    print(f"[bump] {old} -> {new}")
    # Print just the version for CI to capture
    print(f"NEW_VERSION={new}")
    return 0


if __name__ == "__main__":
    sys.exit(main())