|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Renames boa crate package names to the `obeli-sk-` prefix and sets the workspace |
| 4 | +version, so they can be published to crates.io without conflicting with upstream. |
| 5 | +
|
| 6 | +Usage: python3 scripts/rename_for_obeli_sk.py <version> |
| 7 | +Example: python3 scripts/rename_for_obeli_sk.py 1.0.0-obeli-sk.1 |
| 8 | +""" |
| 9 | + |
| 10 | +import re |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +if len(sys.argv) != 2: |
| 15 | + print(f"Usage: {sys.argv[0]} <version>", file=sys.stderr) |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | +version = sys.argv[1] |
| 19 | + |
| 20 | +# Map: original package name -> published crate name (hyphens for crates.io) |
| 21 | +RENAMES = { |
| 22 | + "boa_ast": "obeli-sk-boa-ast", |
| 23 | + "boa_engine": "obeli-sk-boa-engine", |
| 24 | + "boa_gc": "obeli-sk-boa-gc", |
| 25 | + "boa_icu_provider": "obeli-sk-boa-icu-provider", |
| 26 | + "boa_interner": "obeli-sk-boa-interner", |
| 27 | + "boa_macros": "obeli-sk-boa-macros", |
| 28 | + "boa_parser": "obeli-sk-boa-parser", |
| 29 | + "boa_runtime": "obeli-sk-boa-runtime", |
| 30 | + "boa_string": "obeli-sk-boa-string", |
| 31 | + "boa_wintertc": "obeli-sk-boa-wintertc", |
| 32 | + "small_btree": "obeli-sk-small-btree", |
| 33 | + "tag_ptr": "obeli-sk-tag-ptr", |
| 34 | +} |
| 35 | + |
| 36 | +# Map: original package name -> path relative to repo root |
| 37 | +CRATE_PATHS = { |
| 38 | + "boa_ast": "core/ast", |
| 39 | + "boa_engine": "core/engine", |
| 40 | + "boa_gc": "core/gc", |
| 41 | + "boa_icu_provider": "core/icu_provider", |
| 42 | + "boa_interner": "core/interner", |
| 43 | + "boa_macros": "core/macros", |
| 44 | + "boa_parser": "core/parser", |
| 45 | + "boa_runtime": "core/runtime", |
| 46 | + "boa_string": "core/string", |
| 47 | + "boa_wintertc": "core/wintertc", |
| 48 | + "small_btree": "utils/small_btree", |
| 49 | + "tag_ptr": "utils/tag_ptr", |
| 50 | +} |
| 51 | + |
| 52 | +root = Path(__file__).parent.parent |
| 53 | + |
| 54 | +# ── 1. Update workspace Cargo.toml ────────────────────────────────────────── |
| 55 | + |
| 56 | +root_cargo = root / "Cargo.toml" |
| 57 | +content = root_cargo.read_text() |
| 58 | + |
| 59 | +# Update [workspace.package] version |
| 60 | +content = re.sub( |
| 61 | + r'^(version\s*=\s*)"[^"]+"', |
| 62 | + rf'\1"{version}"', |
| 63 | + content, |
| 64 | + flags=re.MULTILINE, |
| 65 | +) |
| 66 | + |
| 67 | +# For each renamed crate, update its entry in [workspace.dependencies]: |
| 68 | +# Before: boa_ast = { version = "~1.0.0-dev", path = "core/ast" } |
| 69 | +# After: boa_ast = { package = "obeli-sk-boa-ast", version = "1.0.0-obeli-sk.1", path = "core/ast" } |
| 70 | +# |
| 71 | +# The workspace-dep key stays as the original name so that `boa_ast.workspace = true` |
| 72 | +# in individual crates continues to work; `package` tells cargo the real crate name. |
| 73 | + |
| 74 | +for old_name, new_name in RENAMES.items(): |
| 75 | + # Match an inline-table workspace dependency line for this crate. |
| 76 | + pattern = rf'^({re.escape(old_name)}\s*=\s*\{{)([^\n\}}]+)(\}})' |
| 77 | + |
| 78 | + def make_replacer(new_name=new_name): |
| 79 | + def replacer(m): |
| 80 | + prefix, body, suffix = m.group(1), m.group(2), m.group(3) |
| 81 | + # Insert `package` if not already present |
| 82 | + if "package" not in body: |
| 83 | + body = f' package = "{new_name}",' + body |
| 84 | + # Replace the version constraint |
| 85 | + body = re.sub(r'version\s*=\s*"[^"]*"', f'version = "{version}"', body) |
| 86 | + return prefix + body + suffix |
| 87 | + return replacer |
| 88 | + |
| 89 | + content = re.sub(pattern, make_replacer(), content, flags=re.MULTILINE) |
| 90 | + |
| 91 | +root_cargo.write_text(content) |
| 92 | +print(f"Updated {root_cargo}") |
| 93 | + |
| 94 | +# ── 2. Rename [package] name in each individual crate ──────────────────────── |
| 95 | + |
| 96 | +for old_name, new_name in RENAMES.items(): |
| 97 | + cargo_toml = root / CRATE_PATHS[old_name] / "Cargo.toml" |
| 98 | + if not cargo_toml.exists(): |
| 99 | + print(f"WARNING: {cargo_toml} not found, skipping") |
| 100 | + continue |
| 101 | + |
| 102 | + content = cargo_toml.read_text() |
| 103 | + updated = re.sub( |
| 104 | + rf'^(name\s*=\s*)"({re.escape(old_name)})"', |
| 105 | + rf'\1"{new_name}"', |
| 106 | + content, |
| 107 | + flags=re.MULTILINE, |
| 108 | + ) |
| 109 | + if updated == content: |
| 110 | + print(f"WARNING: no name replacement made in {cargo_toml}") |
| 111 | + else: |
| 112 | + cargo_toml.write_text(updated) |
| 113 | + print(f" {old_name!s:30s} -> {new_name}") |
| 114 | + |
| 115 | +print(f"\nDone. Version set to {version!r}.") |
0 commit comments