-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathupdate_version.py
executable file
·171 lines (152 loc) · 5.4 KB
/
update_version.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
A utility script to update the version in multiple files.
simply run `python update_version.py <version>` to update the version in the files.
"""
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Literal
def default_version_string(v: str, count: str, hash: str, mod: str, num: str):
mod = f"-{mod}" if len(mod) > 0 else ""
num = f"-{num}" if len(num) > 0 else ""
count = f"-{count}" if len(count) > 0 else ""
hash = f"-{hash}" if len(hash) > 0 else ""
return f"{v}{mod}{num}" if len(mod) > 0 else f"{v}{count}{hash}"
def cargo_version_string(v: str, count: str, hash: str, mod: str, num: str):
return f"{v}-{mod}.{num}" if len(mod) > 0 else v
@dataclass(frozen=True, slots=True)
class VersionLocation:
relative_path: Path
version_path: list[str | int]
file_format: Literal["json2", "json4", "toml"]
function: Callable[[str, str, str, str, str], str] = default_version_string
prefix: str = ""
suffix: str = ""
LOCATIONS: list[VersionLocation] = [
VersionLocation(
relative_path=Path("package.json"),
version_path=["version"],
file_format="json2",
),
VersionLocation(
relative_path=Path("choreolib/ChoreoLib2025.json"),
version_path=["version"],
file_format="json2",
),
VersionLocation(
relative_path=Path("choreolib/ChoreoLib2025.json"),
version_path=["javaDependencies", 0, "version"],
file_format="json2",
),
VersionLocation(
relative_path=Path("choreolib/ChoreoLib2025.json"),
version_path=["cppDependencies", 0, "version"],
file_format="json2",
),
VersionLocation(
relative_path=Path("src-tauri/tauri.conf.json"),
version_path=["package", "version"],
file_format="json2",
),
VersionLocation(
relative_path=Path("src-tauri/tauri.conf.json"),
version_path=["tauri", "windows", 0, "title"],
file_format="json2",
prefix="Choreo v",
),
VersionLocation(
relative_path=Path("src-tauri/Cargo.toml"),
version_path=["package", "version"],
file_format="toml",
function=cargo_version_string,
),
VersionLocation(
relative_path=Path("src-cli/Cargo.toml"),
version_path=["package", "version"],
file_format="toml",
function=cargo_version_string,
),
VersionLocation(
relative_path=Path("src-core/Cargo.toml"),
version_path=["package", "version"],
file_format="toml",
function=cargo_version_string,
),
]
def update_version(version: str) -> None:
# Formats:
#
# v2024.2.3-193-g869a3ef
# v2025.0.0
# v2025.0.0-alpha-1
# v2025.0.0-beta-2
m = re.search(
r"""
^v
( [0-9]+\.[0-9]+\.[0-9]+ ) # group 1 semver
( -([0-9]+) )? # group 3 commits since last tag
( -([a-z\d]+) )? # group 5 commit hash (if group 2) or alpha or beta (if not group 2)
( -([0-9]+) )? # group 7 alpha or beta number
""",
version,
re.X,
)
version = m.group(1)
count = m.group(3) if m.group(2) else ""
hash = m.group(5) if m.group(4) and m.group(2) else ""
pre = m.group(5) if m.group(4) and not m.group(2) else ""
num = m.group(7) if m.group(6) else ""
for location in LOCATIONS:
file_path = Path(__file__).parent / location.relative_path
if location.file_format == "json2" or location.file_format == "json4":
import json
with open(file_path, "r") as f:
data = json.load(f)
og = data
version_str = (
location.prefix
+ location.function(version, count, hash, pre, num)
+ location.suffix
)
try:
for key in location.version_path[:-1]:
data = data[key]
data[location.version_path[-1]] = version_str
except KeyError as e:
print(
f"Version path not found: {location.version_path} in {location.relative_path}"
)
raise e
with open(file_path, "w") as f:
json.dump(og, f, indent=int(location.file_format[-1]))
f.write("\n")
elif location.file_format == "toml":
import tomlkit
with open(file_path, "r") as f:
data = tomlkit.load(f)
og = data
version_str = (
location.prefix
+ location.function(version, count, hash, pre, num)
+ location.suffix
)
try:
for key in location.version_path[:-1]:
data = data[key]
data[location.version_path[-1]] = version_str
except KeyError as e:
print(
f"Version path not found: {location.version_path} in {location.relative_path}"
)
raise e
with open(file_path, "w") as f:
tomlkit.dump(og, f)
else:
raise ValueError(f"Unsupported file format: {location.file_format}")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Update version in files")
parser.add_argument("version", type=str, help="Version to set")
args = parser.parse_args()
update_version(args.version)