Skip to content

Commit d801384

Browse files
committed
feat(release): add atomic version preparation
1 parent 6b460e5 commit d801384

4 files changed

Lines changed: 503 additions & 1 deletion

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,7 @@
17671767
"release:plugins:npm:plan": "node --import tsx scripts/plugin-npm-release-plan.ts",
17681768
"release:verify-beta": "node --import tsx scripts/release-verify-beta.ts",
17691769
"release:prep": "node scripts/release-preflight.mjs --fix",
1770+
"release:version": "node --import tsx scripts/release-version.ts",
17701771
"runtime-sidecars:check": "node --import tsx scripts/generate-runtime-sidecar-paths-baseline.ts --check",
17711772
"runtime-sidecars:gen": "node --import tsx scripts/generate-runtime-sidecar-paths-baseline.ts --write",
17721773
"start": "node openclaw.mjs",

‎scripts/lib/android-version.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ export function resolveAndroidVersion(rootDir = path.resolve(".")): ResolvedAndr
182182
};
183183
}
184184

185-
export function renderAndroidVersionProperties(version: ResolvedAndroidVersion): string {
185+
export function renderAndroidVersionProperties(
186+
version: Pick<ResolvedAndroidVersion, "canonicalVersion" | "versionCode">,
187+
): string {
186188
return `# Shared Android version defaults.\n# Source of truth: apps/android/version.json\n# Generated by scripts/android-sync-versioning.ts.\n\nOPENCLAW_ANDROID_VERSION_NAME=${version.canonicalVersion}\nOPENCLAW_ANDROID_VERSION_CODE=${version.versionCode}\n`;
187189
}
188190

‎scripts/release-version.ts‎

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
// Release Version keeps the core and explicitly selected native release trains aligned.
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import {
5+
canonicalAndroidVersionCode,
6+
normalizeAndroidVersionCode,
7+
normalizePinnedAndroidVersion,
8+
renderAndroidVersionProperties,
9+
} from "./lib/android-version.ts";
10+
import { parseReleaseVersion } from "./lib/npm-publish-plan.mjs";
11+
12+
const MACOS_INFO_PLIST = "apps/macos/Sources/OpenClaw/Resources/Info.plist";
13+
const ANDROID_VERSION_FILE = "apps/android/version.json";
14+
const ANDROID_VERSION_PROPERTIES_FILE = "apps/android/Config/Version.properties";
15+
16+
type ReleaseVersionMode = "check" | "write";
17+
18+
type ReleaseVersionArgs = {
19+
android: boolean;
20+
help: boolean;
21+
mode: ReleaseVersionMode;
22+
rootDir: string;
23+
version: string | null;
24+
};
25+
26+
type ReleaseVersionChange = {
27+
currentContent: string;
28+
nextContent: string;
29+
path: string;
30+
};
31+
32+
type ReleaseVersionPlan = {
33+
changes: ReleaseVersionChange[];
34+
version: string;
35+
};
36+
37+
type AndroidVersionManifest = {
38+
version?: unknown;
39+
versionCode?: unknown;
40+
};
41+
42+
export function parseReleaseVersionArgs(argv: string[]): ReleaseVersionArgs {
43+
let android = false;
44+
let help = false;
45+
let mode: ReleaseVersionMode = "check";
46+
let rootDir = path.resolve(".");
47+
let version: string | null = null;
48+
49+
for (let index = 0; index < argv.length; index += 1) {
50+
const arg = argv[index];
51+
switch (arg) {
52+
case "--": {
53+
break;
54+
}
55+
case "--android": {
56+
android = true;
57+
break;
58+
}
59+
case "--check": {
60+
mode = "check";
61+
break;
62+
}
63+
case "--root": {
64+
rootDir = path.resolve(readOptionValue(argv, index, arg));
65+
index += 1;
66+
break;
67+
}
68+
case "--version": {
69+
version = readOptionValue(argv, index, arg);
70+
index += 1;
71+
break;
72+
}
73+
case "--write": {
74+
mode = "write";
75+
break;
76+
}
77+
case "-h":
78+
case "--help": {
79+
help = true;
80+
break;
81+
}
82+
default: {
83+
throw new Error(`Unknown argument: ${arg}`);
84+
}
85+
}
86+
}
87+
88+
return { android, help, mode, rootDir, version };
89+
}
90+
91+
export function planReleaseVersion(params: {
92+
android?: boolean;
93+
rootDir?: string;
94+
version: string;
95+
}): ReleaseVersionPlan {
96+
const rootDir = path.resolve(params.rootDir ?? ".");
97+
const parsedVersion = parseReleaseVersion(params.version);
98+
if (!parsedVersion) {
99+
throw new Error(
100+
`Invalid release version '${params.version}'. Expected YYYY.M.PATCH, YYYY.M.PATCH-alpha.N, YYYY.M.PATCH-beta.N, or YYYY.M.PATCH-N.`,
101+
);
102+
}
103+
104+
const changes = [
105+
planPackageJson(rootDir, parsedVersion.version),
106+
planMacosInfoPlist(rootDir, parsedVersion),
107+
];
108+
if (params.android) {
109+
changes.push(...planAndroidVersion(rootDir, parsedVersion.baseVersion));
110+
}
111+
112+
return {
113+
changes: changes.filter((change) => change.currentContent !== change.nextContent),
114+
version: parsedVersion.version,
115+
};
116+
}
117+
118+
export function applyReleaseVersionPlan(plan: ReleaseVersionPlan): void {
119+
const tempPaths: string[] = [];
120+
try {
121+
for (const [index, change] of plan.changes.entries()) {
122+
const tempPath = `${change.path}.release-version-${process.pid}-${index}.tmp`;
123+
fs.writeFileSync(tempPath, change.nextContent, "utf8");
124+
tempPaths.push(tempPath);
125+
}
126+
for (const [index, change] of plan.changes.entries()) {
127+
fs.renameSync(tempPaths[index], change.path);
128+
}
129+
} finally {
130+
for (const tempPath of tempPaths) {
131+
fs.rmSync(tempPath, { force: true });
132+
}
133+
}
134+
}
135+
136+
export function main(argv = process.argv.slice(2)): number {
137+
const args = parseReleaseVersionArgs(argv);
138+
if (args.help) {
139+
printUsage();
140+
return 0;
141+
}
142+
if (!args.version) {
143+
throw new Error("Missing required --version.");
144+
}
145+
146+
const plan = planReleaseVersion({
147+
android: args.android,
148+
rootDir: args.rootDir,
149+
version: args.version,
150+
});
151+
if (plan.changes.length === 0) {
152+
process.stdout.write(`Release version ${plan.version} is already aligned.\n`);
153+
return 0;
154+
}
155+
156+
const relativePaths = plan.changes.map((change) => path.relative(args.rootDir, change.path));
157+
if (args.mode === "check") {
158+
process.stderr.write(
159+
`Release version ${plan.version} requires updates:\n- ${relativePaths.join("\n- ")}\n`,
160+
);
161+
return 1;
162+
}
163+
164+
applyReleaseVersionPlan(plan);
165+
process.stdout.write(
166+
`Updated release version ${plan.version}:\n- ${relativePaths.join("\n- ")}\n`,
167+
);
168+
return 0;
169+
}
170+
171+
function planPackageJson(rootDir: string, version: string): ReleaseVersionChange {
172+
const filePath = path.join(rootDir, "package.json");
173+
const currentContent = fs.readFileSync(filePath, "utf8");
174+
const packageJson = JSON.parse(currentContent) as Record<string, unknown>;
175+
if (typeof packageJson.version !== "string" || !packageJson.version.trim()) {
176+
throw new Error(`Missing package.json version in ${filePath}.`);
177+
}
178+
packageJson.version = version;
179+
return {
180+
currentContent,
181+
nextContent: `${JSON.stringify(packageJson, null, 2)}\n`,
182+
path: filePath,
183+
};
184+
}
185+
186+
function planMacosInfoPlist(
187+
rootDir: string,
188+
releaseVersion: NonNullable<ReturnType<typeof parseReleaseVersion>>,
189+
): ReleaseVersionChange {
190+
const filePath = path.join(rootDir, MACOS_INFO_PLIST);
191+
const currentContent = fs.readFileSync(filePath, "utf8");
192+
const buildVersion = [
193+
String(releaseVersion.year),
194+
String(releaseVersion.month).padStart(2, "0"),
195+
String(releaseVersion.patch).padStart(2, "0"),
196+
"00",
197+
].join("");
198+
const shortVersionContent = replacePlistString(
199+
currentContent,
200+
"CFBundleShortVersionString",
201+
releaseVersion.baseVersion,
202+
MACOS_INFO_PLIST,
203+
);
204+
const nextContent = replacePlistString(
205+
shortVersionContent,
206+
"CFBundleVersion",
207+
buildVersion,
208+
MACOS_INFO_PLIST,
209+
);
210+
return { currentContent, nextContent, path: filePath };
211+
}
212+
213+
function planAndroidVersion(rootDir: string, baseVersion: string): ReleaseVersionChange[] {
214+
const versionPath = path.join(rootDir, ANDROID_VERSION_FILE);
215+
const propertiesPath = path.join(rootDir, ANDROID_VERSION_PROPERTIES_FILE);
216+
const versionContent = fs.readFileSync(versionPath, "utf8");
217+
const propertiesContent = fs.readFileSync(propertiesPath, "utf8");
218+
const manifest = JSON.parse(versionContent) as AndroidVersionManifest;
219+
const currentVersion =
220+
typeof manifest.version === "string" ? normalizePinnedAndroidVersion(manifest.version) : null;
221+
const currentVersionCode =
222+
typeof manifest.versionCode === "number" ? manifest.versionCode : Number.NaN;
223+
const versionCode =
224+
currentVersion === baseVersion
225+
? normalizeAndroidVersionCode(currentVersionCode, baseVersion)
226+
: canonicalAndroidVersionCode(baseVersion);
227+
const nextVersionContent = `${JSON.stringify({ version: baseVersion, versionCode }, null, 2)}\n`;
228+
const nextPropertiesContent = renderAndroidVersionProperties({
229+
canonicalVersion: baseVersion,
230+
versionCode,
231+
});
232+
233+
return [
234+
{
235+
currentContent: versionContent,
236+
nextContent: nextVersionContent,
237+
path: versionPath,
238+
},
239+
{
240+
currentContent: propertiesContent,
241+
nextContent: nextPropertiesContent,
242+
path: propertiesPath,
243+
},
244+
];
245+
}
246+
247+
function replacePlistString(content: string, key: string, value: string, filePath: string): string {
248+
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
249+
const pattern = new RegExp(
250+
`(<key>\\s*${escapedKey}\\s*</key>\\s*<string>)([^<]*)(</string>)`,
251+
"gu",
252+
);
253+
const matches = [...content.matchAll(pattern)];
254+
if (matches.length !== 1) {
255+
throw new Error(`${filePath} must contain exactly one string value for ${key}.`);
256+
}
257+
return content.replace(pattern, `$1${value}$3`);
258+
}
259+
260+
function readOptionValue(argv: string[], index: number, flag: string): string {
261+
const value = argv[index + 1];
262+
if (!value || value.startsWith("-")) {
263+
throw new Error(`Missing value for ${flag}.`);
264+
}
265+
return value;
266+
}
267+
268+
function printUsage(): void {
269+
process.stdout.write(
270+
[
271+
"Usage: node --import tsx scripts/release-version.ts --version <version> [--check|--write] [--android] [--root dir]",
272+
"",
273+
" --check report release version drift without writing (default)",
274+
" --write update all selected version files after validating them",
275+
" --android also align the independently pinned Android release train",
276+
"",
277+
].join("\n"),
278+
);
279+
}
280+
281+
if (import.meta.main) {
282+
try {
283+
process.exitCode = main();
284+
} catch (error) {
285+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
286+
process.exitCode = 1;
287+
}
288+
}

0 commit comments

Comments
 (0)