-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate_version.py
More file actions
47 lines (40 loc) · 1.69 KB
/
update_version.py
File metadata and controls
47 lines (40 loc) · 1.69 KB
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
"""Updates the version in Cargo.toml to match the version in the main CMakeLists.txt"""
import os
import re
from pathlib import Path
LBUG_RS_ROOT = Path(__file__).parent
LBUG_ROOT = LBUG_RS_ROOT.parent.parent
def get_lbug_version():
cmake_file = LBUG_ROOT / "CMakeLists.txt"
with open(cmake_file) as f:
for line in f:
if line.startswith("project(Lbug VERSION"):
version = line.split(" ")[2].strip()
# Make version semver-compatible
components = version.split(".")
if len(components) >= 4:
version = ".".join(components[0:3]) + "-pre." + ".".join(components[3:])
return version
if __name__ == "__main__":
version = get_lbug_version()
version_changed = False
with open(LBUG_RS_ROOT / "Cargo.toml", encoding="utf-8") as file:
data = file.readlines()
section = None
for index, line in enumerate(data):
if line.startswith("["):
section = line.strip().strip("[]")
if line.startswith("version = ") and section == "package":
toml_version = re.match('version = "(.*)"', line).group(1)
if toml_version != version:
version_changed = True
print(
f"Updating version in Cargo.toml from {toml_version} to {version}"
)
data[index] = re.sub(
'version = ".*"', f'version = "{version}"', line
)
break
if version_changed:
with open(LBUG_RS_ROOT / "Cargo.toml", "w", encoding="utf-8") as file:
file.writelines(data)