-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathversion.py
More file actions
executable file
·88 lines (71 loc) · 3.07 KB
/
version.py
File metadata and controls
executable file
·88 lines (71 loc) · 3.07 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
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
#!/usr/bin/env -S uv run
# Run version.py to show the current version.
# Run `version.py <new_version>` to change the version.
# Run `version.py bump` to increment the last number in the version.
import os
import sys
def looks_like_version_string(s):
return s.count(".") == 2 and all(x.isdigit() for x in s.split("."))
def main(args=None):
# Find cargo.toml
python_dir = os.path.dirname(os.path.abspath(__file__))
acorn_dir = os.path.dirname(python_dir)
cargo_toml_path = os.path.join(acorn_dir, "Cargo.toml")
cargo_toml = open(cargo_toml_path).read()
# Find package.json
vscode_dir = os.path.join(acorn_dir, "vscode")
extension_dir = os.path.join(vscode_dir, "extension")
package_json_path = os.path.join(extension_dir, "package.json")
package_json = open(package_json_path).read()
# Find the version file
version_file_path = os.path.join(acorn_dir, "VERSION")
# Check what the current versions are
cargo_version = cargo_toml.split('version = "')[1].split('"')[0]
package_version = package_json.split('"version": "')[1].split('"')[0]
version_version = open(version_file_path).read().strip()
if not looks_like_version_string(cargo_version):
raise Exception("can't find version in Cargo.toml")
if not looks_like_version_string(package_version):
raise Exception("can't find version in package.json")
if not looks_like_version_string(version_version):
raise Exception("can't find version in VERSION")
if cargo_version != package_version:
raise Exception(
f"Cargo.toml ({cargo_version}) and package.json ({package_version}) versions don't match"
)
if cargo_version != version_version:
raise Exception(
f"Cargo.toml ({cargo_version}) and VERSION ({version_version}) versions don't match"
)
old_version = cargo_version
print("version:", old_version)
if args is None or len(args) < 1:
return
# Handle bump command
if args[0] == "bump":
# Split version into components and increment the last part
parts = old_version.split(".")
parts[-1] = str(int(parts[-1]) + 1)
new_version = ".".join(parts)
else:
new_version = args[0]
if not looks_like_version_string(new_version):
raise Exception(f"invalid version string: {new_version}")
# Update Cargo.toml
old_cargo_str = f'version = "{old_version}"'
assert cargo_toml.count(old_cargo_str) == 1
cargo_toml = cargo_toml.replace(old_cargo_str, f'version = "{new_version}"')
with open(cargo_toml_path, "w") as f:
f.write(cargo_toml)
# Update package.json
old_package_str = f'"version": "{old_version}"'
assert package_json.count(old_package_str) == 1
package_json = package_json.replace(old_package_str, f'"version": "{new_version}"')
with open(package_json_path, "w") as f:
f.write(package_json)
# Update version file
with open(version_file_path, "w") as f:
f.write(new_version)
print("changed to:", new_version)
if __name__ == "__main__":
main(sys.argv[1:])