-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathupdate-version.py
More file actions
executable file
·218 lines (170 loc) · 6.34 KB
/
update-version.py
File metadata and controls
executable file
·218 lines (170 loc) · 6.34 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
"""
Script to update version in Cargo.toml, package.json, and package-lock.json files for the Limbo project.
This script updates all occurrences of the version in the workspace configuration,
updates the JavaScript and WebAssembly bindings package.json and package-lock.json files,
uses cargo update to update Cargo.lock, creates a git commit, and adds a version tag.
"""
import re
import argparse
import sys
import json
import subprocess
import os
from pathlib import Path
# Define all npm package paths in one place
NPM_PACKAGES = [
".",
"npm/darwin-x64",
"npm/linux-arm64-musl",
"npm/linux-x64-gnu",
"npm/linux-x64-musl",
"npm/darwin-arm64",
"npm/linux-arm64-gnu",
"npm/win32-x64-msvc",
"npm/win32-arm64-msvc",
]
def parse_args():
parser = argparse.ArgumentParser(description="Update version in project files")
# Version argument
parser.add_argument(
"version",
help="The new version to set (e.g., 0.1.0)"
)
return parser.parse_args()
def extract_current_version(content):
"""Extract the current version from Cargo.toml content."""
# Look for the first version field (usually the package version)
match = re.search(r'version\s*=\s*"([^"]+)"', content)
if match:
return match.group(1)
return None
def update_cargo_toml(new_version):
"""Update all version references in Cargo.toml to the new version."""
try:
cargo_path = Path("Cargo.toml")
if not cargo_path.exists():
sys.exit(1)
content = cargo_path.read_text()
current_version = extract_current_version(content)
# Pattern to match version in various contexts while maintaining the quotes
pattern = r'(version\s*=\s*)"' + re.escape(current_version) + r'"'
updated_content = re.sub(pattern, fr'\1"{new_version}"', content)
cargo_path.write_text(updated_content)
return True
except Exception:
sys.exit(1)
def update_package_json(dir_path, new_version):
"""Update version in package.json and package-lock.json files."""
dir_path = Path(dir_path)
# Update package.json
try:
package_path = dir_path / "package.json"
if not package_path.exists():
return False
# Read and parse the package.json file
with open(package_path, 'r') as f:
package_data = json.load(f)
# Update version regardless of current value
package_data['version'] = new_version
# Write updated package.json
with open(package_path, 'w') as f:
json.dump(package_data, f, indent=2)
except Exception:
return False
# Update package-lock.json if it exists
try:
lock_path = dir_path / "package-lock.json"
if not lock_path.exists():
return True # package.json was updated successfully
# Read and parse the package-lock.json file
with open(lock_path, 'r') as f:
lock_data = json.load(f)
# Update version in multiple places in package-lock.json
if 'version' in lock_data:
lock_data['version'] = new_version
# Update version in packages section if it exists (npm >= 7)
if 'packages' in lock_data:
if '' in lock_data['packages']: # Root package
if 'version' in lock_data['packages']['']:
lock_data['packages']['']['version'] = new_version
# Update version in dependencies section if it exists (older npm)
package_name = package_data.get('name', '')
if 'dependencies' in lock_data and package_name in lock_data['dependencies']:
if 'version' in lock_data['dependencies'][package_name]:
lock_data['dependencies'][package_name]['version'] = new_version
# Write updated package-lock.json
with open(lock_path, 'w') as f:
json.dump(lock_data, f, indent=2)
return True
except Exception:
return False
def update_all_packages(new_version):
"""Update all npm packages with the new version."""
results = []
for package_path in NPM_PACKAGES:
result = update_package_json(package_path, new_version)
results.append((package_path, result))
return results
def run_cargo_update():
"""Run cargo update to update the Cargo.lock file."""
try:
# Run cargo update showing its output with verbose flag
subprocess.run(
["cargo", "update", "--workspace", "--verbose"],
check=True
)
return True
except Exception:
return False
def create_git_commit_and_tag(version):
"""Create a git commit with all changes and add a version tag."""
try:
# Add files that exist and have changes
files_to_add = ["Cargo.toml", "Cargo.lock"]
# Add all potential package.json and package-lock.json files
for package_path in NPM_PACKAGES:
package_json = f"{package_path}/package.json"
package_lock = f"{package_path}/package-lock.json"
if os.path.exists(package_json):
files_to_add.append(package_json)
if os.path.exists(package_lock):
files_to_add.append(package_lock)
# Add each file individually
for file in files_to_add:
try:
subprocess.run(
["git", "add", file],
check=True
)
except subprocess.CalledProcessError:
print(f"Warning: Could not add {file} to git")
# Create commit
commit_message = f"{version}"
subprocess.run(
["git", "commit", "-m", commit_message],
check=True
)
# Create tag
tag_name = f"v{version}"
subprocess.run(
["git", "tag", "-a", tag_name, "-m", f"Version {version}"],
check=True
)
return True
except Exception as e:
print(f"Error in git operations: {e}")
return False
def main():
args = parse_args()
new_version = args.version
# Update Cargo.toml
update_cargo_toml(new_version)
# Update all npm packages
update_all_packages(new_version)
# Update Cargo.lock using cargo update
run_cargo_update()
# Create git commit and tag
create_git_commit_and_tag(new_version)
if __name__ == "__main__":
main()