返回 CodeWhale
prepare-release.sh
根目录 / scripts / release / prepare-release.sh
1 #!/usr/bin/env bash
2 # Bump every version-bearing file for a release in one shot.
3 #
4 # Usage: ./scripts/release/prepare-release.sh <new-version>
5 #
6 # Touches: Cargo.toml (workspace version), crates/*/Cargo.toml (internal
7 # codewhale-* dependency pins), npm/codewhale/package.json (version +
8 # codewhaleBinaryVersion), npm/runtime-sdk/package.json, the VS Code extension
9 # package and lock, the root npm lock workspace records, README.md and
10 # docs/i18n/README*.md install-tag examples when present, the public fact matrix's
11 # source-candidate version, Cargo.lock, crates/tui/CHANGELOG.md (via
12 # sync-changelog.sh), web/lib/facts.generated.ts (via derive-facts.mjs), and
13 # the two version-stamped contract fixtures
14 # (crates/config/src/route/providers-export.golden.json and
15 # crates/tui/src/commands/contract/fixtures/config_policy/status.json).
16 # The website's changelog and install-guide modules are not tracked; the web
17 # build and test runs derive them from CHANGELOG.md and docs/INSTALL.md.
18 #
19 # It does NOT write the CHANGELOG entry — add the `## [X.Y.Z] - YYYY-MM-DD`
20 # section first (see docs/RELEASE_CHECKLIST.md), then run this script, then
21 # let check-versions.sh (run at the end here) confirm everything agrees.
22 set -euo pipefail
23
24 new="${1:?usage: $0 <new-version>}"
25 if ! [[ "${new}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
26 echo "error: '${new}' is not X.Y.Z or X.Y.Z-prerelease" >&2
27 exit 1
28 fi
29
30 repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
31 cd "${repo}"
32 readmes=(README.md docs/i18n/README.*.md)
33
34 # Release preparation spans generated files and two package managers. Preserve
35 # the exact starting bytes so any validation, generator, or downstream gate
36 # failure cannot strand a half-bumped checkout. The backup lives outside the
37 # repository and is removed on both success and failure.
38 transaction_dir="$(mktemp -d)"
39 transaction_active=1
40 transaction_existing="${transaction_dir}/existing"
41 transaction_missing="${transaction_dir}/missing"
42 : >"${transaction_existing}"
43 : >"${transaction_missing}"
44
45 transaction_paths=(
46 Cargo.toml
47 Cargo.lock
48 vendor/codewhale-ratatui/Cargo.toml
49 vendor/codewhale-ratatui/Cargo.lock
50 pet/tui/Cargo.lock
51 package-lock.json
52 npm/codewhale/package.json
53 npm/runtime-sdk/package.json
54 extensions/vscode/package.json
55 extensions/vscode/package-lock.json
56 docs/public-surface-facts.json
57 docs/INSTALL.md
58 "${readmes[@]}"
59 crates/tui/CHANGELOG.md
60 web/lib/facts.generated.ts
61 web/data/features.generated.json
62 web/data/release-baseline.json
63 crates/config/src/route/providers-export.golden.json
64 crates/tui/src/commands/contract/fixtures/config_policy/status.json
65 )
66 for manifest in crates/*/Cargo.toml tests/*/Cargo.toml; do
67 transaction_paths+=("${manifest}")
68 done
69 for path in "${transaction_paths[@]}"; do
70 if [[ -e "${path}" ]]; then
71 mkdir -p "${transaction_dir}/files/$(dirname "${path}")"
72 cp -p "${path}" "${transaction_dir}/files/${path}"
73 printf '%s\n' "${path}" >>"${transaction_existing}"
74 else
75 printf '%s\n' "${path}" >>"${transaction_missing}"
76 fi
77 done
78
79 finish_transaction() {
80 status=$?
81 trap - EXIT
82 if [[ "${transaction_active}" == "1" && "${status}" != "0" ]]; then
83 while IFS= read -r path; do
84 [[ -n "${path}" ]] || continue
85 cp -p "${transaction_dir}/files/${path}" "${path}"
86 done <"${transaction_existing}"
87 while IFS= read -r path; do
88 [[ -n "${path}" ]] || continue
89 rm -f -- "${path}"
90 done <"${transaction_missing}"
91 echo "Release preparation failed; restored the checkout's pre-run release files." >&2
92 fi
93 rm -rf -- "${transaction_dir}"
94 exit "${status}"
95 }
96 trap finish_transaction EXIT
97
98 old="$(grep -E '^version = "' Cargo.toml | head -n1 | sed -E 's/^version = "([^"]+)".*/\1/')"
99 if ! grep -q "^## \[${new}\]" CHANGELOG.md; then
100 echo "warning: CHANGELOG.md has no '## [${new}]' entry yet — add it before tagging" >&2
101 fi
102 unstamped=""
103 if [[ -f docs/features.toml ]]; then
104 unstamped="$(awk '/^id = "/ { id = $3 } /^since = "unreleased"/ { gsub(/"/, "", id); print " " id }' docs/features.toml)"
105 fi
106 if [[ -n "${unstamped}" ]]; then
107 echo "warning: docs/features.toml rows still since = \"unreleased\"; stamp each one this release ships with since = \"${new}\" (docs/RELEASE_CHECKLIST.md section 1):" >&2
108 echo "${unstamped}" >&2
109 fi
110
111 if [[ "${old}" != "${new}" ]]; then
112 echo "Bumping ${old} -> ${new}"
113
114 OLD_VERSION="${old}" NEW_VERSION="${new}" python3 - "${readmes[@]}" <<'PY'
115 import json, os, pathlib, re, sys
116
117 old, new = os.environ["OLD_VERSION"], os.environ["NEW_VERSION"]
118 old_re = re.escape(old)
119 readmes = sys.argv[1:]
120
121 def bump(path, pattern, repl, minimum):
122 p = pathlib.Path(path)
123 text = p.read_text()
124 out, n = re.subn(pattern, repl, text, flags=re.MULTILINE)
125 if n < minimum:
126 sys.exit(f"error: expected >= {minimum} replacement(s) in {path}, made {n}")
127 p.write_text(out)
128 print(f" {path}: {n} replacement(s)")
129
130 # Validate every versioned README install tag before writing any file. A README
131 # with no pinned tag is valid; if a tag exists, it must match the workspace so
132 # the release helper cannot silently preserve stale public install instructions.
133 release_tag_pattern = re.compile(r"--tag v([0-9]+\.[0-9]+\.[0-9]+)\b")
134 for readme in readmes:
135 versions = sorted(set(release_tag_pattern.findall(pathlib.Path(readme).read_text())))
136 stale = [version for version in versions if version != old]
137 if stale:
138 found = ", ".join(stale)
139 sys.exit(
140 f"error: {readme} has release tag version(s) {found}; expected {old}"
141 )
142
143 # 1) Workspace version.
144 bump("Cargo.toml", rf'^version = "{old_re}"$', f'version = "{new}"', 1)
145
146 # 2) Internal codewhale-* dependency pins in every crate manifest.
147 total = 0
148 manifests = list(pathlib.Path("crates").glob("*/Cargo.toml"))
149 kit_manifest = pathlib.Path("vendor/codewhale-ratatui/Cargo.toml")
150 if kit_manifest.is_file():
151 manifests.append(kit_manifest)
152 for manifest in sorted(manifests):
153 text = manifest.read_text()
154 out, n = re.subn(
155 rf'(codewhale-[a-z0-9-]+\s*=\s*\{{[^}}]*version = "){old_re}(")',
156 rf"\g<1>{new}\g<2>",
157 text,
158 )
159 if n:
160 manifest.write_text(out)
161 print(f" {manifest}: {n} pin(s)")
162 total += n
163 if total == 0:
164 sys.exit("error: no internal dependency pins were bumped — wrong old version?")
165
166 if "-" in old or "-" in new:
167 normalized = 0
168 for manifest in sorted(pathlib.Path("tests").glob("*/Cargo.toml")):
169 text = manifest.read_text()
170 out, n = re.subn(
171 r'(codewhale-[a-z0-9-]+\s*=\s*\{[^}]*version = ")[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?(")',
172 rf"\g<1>{new}\g<3>",
173 text,
174 )
175 if n:
176 manifest.write_text(out)
177 print(f" {manifest}: {n} test-harness pin(s)")
178 normalized += n
179 if normalized == 0:
180 sys.exit("error: no test-harness internal pins were normalized — wrong workspace layout?")
181
182 for lock_path in (pathlib.Path("vendor/codewhale-ratatui/Cargo.lock"), pathlib.Path("pet/tui/Cargo.lock")):
183 if not lock_path.is_file():
184 continue
185 text = lock_path.read_text()
186 out, count = re.subn(rf'(name = "codewhale-protocol"\nversion = "){old_re}("\n)', rf"\g<1>{new}\g<2>", text)
187 if count:
188 lock_path.write_text(out)
189 print(f" {lock_path}: {count} Protocol record(s)")
190
191 # 3) npm wrapper.
192 bump(
193 "npm/codewhale/package.json",
194 rf'("(?:version|codewhaleBinaryVersion)": "){old_re}(")',
195 rf"\g<1>{new}\g<2>",
196 2,
197 )
198
199 # The runtime SDK and VS Code extension are versioned release artifacts too.
200 bump(
201 "npm/runtime-sdk/package.json",
202 rf'^( "version": "){old_re}(",?)$',
203 rf"\g<1>{new}\g<2>",
204 1,
205 )
206 bump(
207 "extensions/vscode/package.json",
208 rf'^( "version": "){old_re}(",?)$',
209 rf"\g<1>{new}\g<2>",
210 1,
211 )
212
213 # 4) README install-tag examples (all translations, when present).
214 for readme in readmes:
215 p = pathlib.Path(readme)
216 text = p.read_text()
217 out, n = re.subn(rf"--tag v{old_re}\b", f"--tag v{new}", text)
218 if n:
219 p.write_text(out)
220 print(f" {readme}: {n} install-tag replacement(s)")
221 else:
222 print(f" {readme}: no versioned install-tag example; skipped")
223
224 # 5) Legacy numeric install/version snippets, if a branch still carries them.
225 # Current docs deliberately describe installed output generically, so zero
226 # matches is valid. If numeric forms exist, validate that they agree with
227 # the old workspace version before replacing them.
228 version_doc_files = [*readmes, "docs/INSTALL.md"]
229 for doc in version_doc_files:
230 p = pathlib.Path(doc)
231 text = p.read_text()
232 versions = sorted(set(re.findall(r"codewhale --version\s+#\s*([0-9]+\.[0-9]+\.[0-9]+)\b", text)))
233 stale = [version for version in versions if version != old]
234 if stale:
235 sys.exit(
236 f"error: {doc} has version-comment value(s) {', '.join(stale)}; expected {old}"
237 )
238 out, n = re.subn(
239 rf"(codewhale --version\s+#\s*){old_re}\b", rf"\g<1>{new}", text
240 )
241 if n:
242 p.write_text(out)
243 print(f" {doc}: {n} version-comment replacement(s)")
244
245 install = pathlib.Path("docs/INSTALL.md")
246 install_text = install.read_text()
247 pointer_versions = sorted(set(re.findall(r"wrapper is published at\s+v([0-9]+\.[0-9]+\.[0-9]+)\b", install_text)))
248 stale_pointers = [version for version in pointer_versions if version != old]
249 if stale_pointers:
250 sys.exit(
251 "error: docs/INSTALL.md has npm-wrapper publish pointer version(s) "
252 f"{', '.join(stale_pointers)}; expected {old}"
253 )
254 install_out, pointer_hits = re.subn(
255 rf"(wrapper is published at\s+)v{old_re}\b",
256 rf"\g<1>v{new}",
257 install_text,
258 )
259 # web/lib/public-surface-contract.test.ts asserts docs/INSTALL.md contains
260 # `v${FACTS.version} source candidate`, so the phrase has to track the bump or
261 # Web Frontend goes red on the next push.
262 install_out, candidate_hits = re.subn(
263 rf"\bv{old_re}( source candidate)",
264 rf"v{new}\g<1>",
265 install_out,
266 )
267 if candidate_hits > 1:
268 sys.exit(
269 "error: docs/INSTALL.md names 'v{0} source candidate' {1} times; "
270 "exactly one occurrence is expected".format(old, candidate_hits)
271 )
272 if pointer_hits or candidate_hits:
273 install.write_text(install_out)
274 print(
275 f" docs/INSTALL.md: {pointer_hits} publish-pointer replacement(s), "
276 f"{candidate_hits} source-candidate replacement(s)"
277 )
278
279 # 6) npm lock workspace records. Keep dependency records byte-stable.
280 lock = pathlib.Path("package-lock.json")
281 lock_text = lock.read_text()
282 lock_out, wrapper_lock_hits = re.subn(
283 rf'("npm/codewhale"\s*:\s*\{{[\s\S]*?"version"\s*:\s*"){old_re}(")',
284 rf"\g<1>{new}\g<2>",
285 lock_text,
286 count=1,
287 )
288 if wrapper_lock_hits != 1:
289 sys.exit(
290 "error: expected package-lock.json packages['npm/codewhale'].version "
291 f"to be {old}; made {wrapper_lock_hits} replacement(s)"
292 )
293 lock_out, sdk_lock_hits = re.subn(
294 rf'("npm/runtime-sdk"\s*:\s*\{{[\s\S]*?"version"\s*:\s*"){old_re}(")',
295 rf"\g<1>{new}\g<2>",
296 lock_out,
297 count=1,
298 )
299 if sdk_lock_hits != 1:
300 sys.exit(
301 "error: expected package-lock.json packages['npm/runtime-sdk'].version "
302 f"to be {old}; made {sdk_lock_hits} replacement(s)"
303 )
304 lock.write_text(lock_out)
305 print(" package-lock.json: 2 npm workspace replacements")
306
307 vscode_lock = pathlib.Path("extensions/vscode/package-lock.json")
308 vscode_lock_text = vscode_lock.read_text()
309 vscode_lock_json = json.loads(vscode_lock_text)
310 if vscode_lock_json.get("version") != old:
311 sys.exit(
312 "error: extensions/vscode/package-lock.json root version is "
313 f"{vscode_lock_json.get('version')!r}; expected {old!r}"
314 )
315 workspace_record = vscode_lock_json.get("packages", {}).get("", {})
316 if workspace_record.get("version") != old:
317 sys.exit(
318 "error: extensions/vscode/package-lock.json packages[''].version is "
319 f"{workspace_record.get('version')!r}; expected {old!r}"
320 )
321 vscode_lock_out, vscode_lock_hits = re.subn(
322 rf'("version"\s*:\s*"){old_re}(")',
323 rf"\g<1>{new}\g<2>",
324 vscode_lock_text,
325 count=2,
326 )
327 if vscode_lock_hits != 2:
328 sys.exit(
329 "error: expected two VS Code package-lock version replacements; "
330 f"made {vscode_lock_hits}"
331 )
332 vscode_lock_after = json.loads(vscode_lock_out)
333 if (
334 vscode_lock_after.get("version") != new
335 or vscode_lock_after.get("packages", {}).get("", {}).get("version") != new
336 ):
337 sys.exit("error: VS Code package-lock version replacement hit wrong records")
338 vscode_lock.write_text(vscode_lock_out)
339 print(" extensions/vscode/package-lock.json: 2 workspace replacements")
340
341 # 7) Public facts distinguish source candidate from latest published release.
342 # Change only sourceCandidate.version; the published object and screenshot
343 # provenance must remain untouched until separately evidenced.
344 facts = pathlib.Path("docs/public-surface-facts.json")
345 facts_text = facts.read_text()
346 facts_json = json.loads(facts_text)
347 candidate = facts_json.get("sourceCandidate", {})
348 if candidate.get("version") != old:
349 sys.exit(
350 "error: docs/public-surface-facts.json sourceCandidate.version is "
351 f"{candidate.get('version')!r}; expected {old!r}"
352 )
353 published_before = facts_json.get("latestPublishedRelease")
354 facts_out, facts_hits = re.subn(
355 rf'("sourceCandidate"\s*:\s*\{{[\s\S]*?"version"\s*:\s*"){old_re}(")',
356 rf"\g<1>{new}\g<2>",
357 facts_text,
358 count=1,
359 )
360 if facts_hits != 1:
361 sys.exit("error: failed to update sourceCandidate.version exactly once")
362 # The trust matrix states the telemetry posture in prose that names the source
363 # candidate, and web/lib/public-surface-contract.test.ts asserts the two agree
364 # (`Codewhale ${matrix.sourceCandidate.version} counts anonymous usage by
365 # default`). Bumping the field without the sentence reddens Web Frontend on the
366 # next push, which is how it broke at 0.9.14.
367 facts_out, telemetry_hits = re.subn(
368 rf"(Codewhale ){old_re}( counts anonymous usage by default)",
369 rf"\g<1>{new}\g<2>",
370 facts_out,
371 )
372 if telemetry_hits > 1:
373 sys.exit(
374 "error: the trust.telemetry source-candidate sentence appears {0} times; "
375 "exactly one occurrence is expected".format(telemetry_hits)
376 )
377 facts_after = json.loads(facts_out)
378 if facts_after.get("latestPublishedRelease") != published_before:
379 sys.exit("error: release preparation must not change latestPublishedRelease")
380 facts.write_text(facts_out)
381 print(" docs/public-surface-facts.json: 1 source-candidate replacement")
382
383 # 8) Version-stamped contract fixtures. The golden export and the config
384 # policy status baseline are compared against the workspace version by
385 # their own tests; check-versions.sh (run below) guards them too. Move
386 # them with every bump or CI's test job reddens on the new version.
387 golden_fixture = pathlib.Path("crates/config/src/route/providers-export.golden.json")
388 if golden_fixture.exists():
389 text = golden_fixture.read_text()
390 out, n = re.subn(rf'("runtimeVersion"\s*:\s*"){old_re}(")', rf"\g<1>{new}\g<2>", text)
391 if n != 1:
392 sys.exit(f"error: expected exactly one runtimeVersion in {golden_fixture}, made {n}")
393 golden_fixture.write_text(out)
394 print(f" {golden_fixture}: 1 runtimeVersion replacement")
395 status_fixture = pathlib.Path(
396 "crates/tui/src/commands/contract/fixtures/config_policy/status.json"
397 )
398 if status_fixture.exists():
399 text = status_fixture.read_text()
400 out, n = re.subn(rf"\b(codewhale\s+){old_re}\b", rf"\g<1>{new}", text, count=1)
401 if n != 1:
402 sys.exit(
403 f"error: expected the {status_fixture} header to carry {old}, "
404 f"made {n} replacement(s)"
405 )
406 status_fixture.write_text(out)
407 print(f" {status_fixture}: 1 version replacement")
408 PY
409
410 echo "Refreshing Cargo.lock..."
411 cargo update --workspace --offline >/dev/null
412 else
413 echo "Workspace is already at ${new}; refreshing generated release state and rerunning gates."
414 NEW_VERSION="${new}" python3 - <<'PY'
415 import json, os, pathlib, sys
416
417 expected = os.environ["NEW_VERSION"]
418 facts = pathlib.Path("docs/public-surface-facts.json")
419 candidate = json.loads(facts.read_text()).get("sourceCandidate", {})
420 actual = candidate.get("version")
421 if actual != expected:
422 sys.exit(
423 "error: docs/public-surface-facts.json sourceCandidate.version is "
424 f"{actual!r}; expected {expected!r}"
425 )
426 PY
427 fi
428
429 echo "Regenerating crates/tui/CHANGELOG.md slice..."
430 ./scripts/sync-changelog.sh
431
432 echo "Regenerating web/lib/facts.generated.ts..."
433 node web/scripts/derive-facts.mjs
434
435 echo "Validating..."
436 ./scripts/release/check-versions.sh
437 ./scripts/release/check-ohos-deps.sh
438 transaction_active=0
439 echo "Done. Review 'git diff', commit, and follow docs/RELEASE_CHECKLIST.md."
440
440 lines BASH