scripts: Add more npm packages to update-version.py

This commit is contained in:
Pekka Enberg
2025-04-03 10:35:02 +03:00
parent a279056e88
commit 4342438801

View File

@@ -15,15 +15,25 @@ import os
from pathlib import Path
# Define all npm package paths in one place
NPM_PACKAGES = [
"bindings/javascript",
"bindings/javascript/npm/darwin-universal",
"bindings/javascript/npm/linux-x64-gnu",
"bindings/javascript/npm/win32-x64-msvc",
"bindings/wasm"
]
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()
@@ -41,15 +51,15 @@ def update_cargo_toml(new_version):
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:
@@ -59,61 +69,70 @@ def update_cargo_toml(new_version):
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:
@@ -132,22 +151,17 @@ def create_git_commit_and_tag(version):
try:
# Add files that exist and have changes
files_to_add = ["Cargo.toml", "Cargo.lock"]
js_package = "bindings/javascript/package.json"
js_lock = "bindings/javascript/package-lock.json"
wasm_package = "bindings/wasm/package.json"
wasm_lock = "bindings/wasm/package-lock.json"
# Add files only if they exist
if os.path.exists(js_package):
files_to_add.append(js_package)
if os.path.exists(js_lock):
files_to_add.append(js_lock)
if os.path.exists(wasm_package):
files_to_add.append(wasm_package)
if os.path.exists(wasm_lock):
files_to_add.append(wasm_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:
@@ -157,21 +171,21 @@ def create_git_commit_and_tag(version):
)
except subprocess.CalledProcessError:
print(f"Warning: Could not add {file} to git")
# Create commit
commit_message = f"Limbo {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}")
@@ -181,19 +195,16 @@ def create_git_commit_and_tag(version):
def main():
args = parse_args()
new_version = args.version
# Update Cargo.toml
update_cargo_toml(new_version)
# Update JavaScript package files
update_package_json("bindings/javascript", new_version)
# Update WebAssembly package files
update_package_json("bindings/wasm", 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)