Skip to content

Commit 93bdf8c

Browse files
committed
refactor(scripts): share update manifest logic and fix build arch detection
1 parent b85cee3 commit 93bdf8c

6 files changed

Lines changed: 337 additions & 498 deletions

‎scripts/lib/build-target-arch.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ describe("build-target-arch", () => {
4444

4545
assert.equal(arch, "arm64");
4646
});
47+
48+
it("does not apply Windows host env heuristics for non-Windows targets", () => {
49+
const arch = getDefaultBuildArch(
50+
"linux",
51+
"x64",
52+
{
53+
PROCESSOR_ARCHITECTURE: "AMD64",
54+
PROCESSOR_ARCHITEW6432: "ARM64",
55+
},
56+
{ archChoices: ["x64", "arm64"] },
57+
);
58+
59+
assert.equal(arch, "x64");
60+
});
4761
});

‎scripts/lib/build-target-arch.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export function getDefaultBuildArch(
3939
env: NodeJS.ProcessEnv,
4040
platformConfig: PlatformConfig,
4141
): BuildArch {
42-
const nodePlatform: NodeJS.Platform =
42+
const hostPlatform: NodeJS.Platform =
4343
platform === "win" ? "win32" : platform === "mac" ? "darwin" : "linux";
44-
const hostArch = resolveHostProcessArch(nodePlatform, processArch, env);
44+
const hostArch = resolveHostProcessArch(hostPlatform, processArch, env);
4545
if (hostArch && platformConfig.archChoices.includes(hostArch)) {
4646
return hostArch;
4747
}

‎scripts/lib/update-manifest.ts‎

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
export interface UpdateManifestFile {
2+
readonly url: string;
3+
readonly sha512: string;
4+
readonly size: number;
5+
}
6+
7+
export type UpdateManifestScalar = string | number | boolean;
8+
9+
export interface UpdateManifest {
10+
readonly version: string;
11+
readonly releaseDate: string;
12+
readonly files: ReadonlyArray<UpdateManifestFile>;
13+
readonly extras: Readonly<Record<string, UpdateManifestScalar>>;
14+
}
15+
16+
interface MutableUpdateManifestFile {
17+
url?: string;
18+
sha512?: string;
19+
size?: number;
20+
}
21+
22+
function stripSingleQuotes(value: string): string {
23+
if (value.startsWith("'") && value.endsWith("'")) {
24+
return value.slice(1, -1).replace(/''/g, "'");
25+
}
26+
return value;
27+
}
28+
29+
function parseFileRecord(
30+
currentFile: MutableUpdateManifestFile | null,
31+
sourcePath: string,
32+
lineNumber: number,
33+
platformLabel: string,
34+
): UpdateManifestFile | null {
35+
if (currentFile === null) {
36+
return null;
37+
}
38+
if (
39+
typeof currentFile.url !== "string" ||
40+
typeof currentFile.sha512 !== "string" ||
41+
typeof currentFile.size !== "number"
42+
) {
43+
throw new Error(
44+
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: incomplete file entry.`,
45+
);
46+
}
47+
return {
48+
url: currentFile.url,
49+
sha512: currentFile.sha512,
50+
size: currentFile.size,
51+
};
52+
}
53+
54+
function parseScalarValue(rawValue: string): UpdateManifestScalar {
55+
const trimmed = rawValue.trim();
56+
const isQuoted = trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2;
57+
const value = isQuoted ? trimmed.slice(1, -1).replace(/''/g, "'") : trimmed;
58+
if (isQuoted) return value;
59+
if (value === "true") return true;
60+
if (value === "false") return false;
61+
if (/^-?\d+(?:\.\d+)?$/.test(value)) {
62+
return Number(value);
63+
}
64+
return value;
65+
}
66+
67+
export function parseUpdateManifest(
68+
raw: string,
69+
sourcePath: string,
70+
platformLabel: string,
71+
): UpdateManifest {
72+
const lines = raw.split(/\r?\n/);
73+
const files: UpdateManifestFile[] = [];
74+
const extras: Record<string, UpdateManifestScalar> = {};
75+
let version: string | null = null;
76+
let releaseDate: string | null = null;
77+
let inFiles = false;
78+
let currentFile: MutableUpdateManifestFile | null = null;
79+
80+
for (const [index, rawLine] of lines.entries()) {
81+
const lineNumber = index + 1;
82+
const line = rawLine.trimEnd();
83+
if (line.length === 0) continue;
84+
85+
const fileUrlMatch = line.match(/^ - url:\s*(.+)$/);
86+
if (fileUrlMatch?.[1]) {
87+
const finalized = parseFileRecord(currentFile, sourcePath, lineNumber, platformLabel);
88+
if (finalized) files.push(finalized);
89+
currentFile = { url: stripSingleQuotes(fileUrlMatch[1].trim()) };
90+
inFiles = true;
91+
continue;
92+
}
93+
94+
const fileShaMatch = line.match(/^ sha512:\s*(.+)$/);
95+
if (fileShaMatch?.[1]) {
96+
if (currentFile === null) {
97+
throw new Error(
98+
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: sha512 without a file entry.`,
99+
);
100+
}
101+
currentFile.sha512 = stripSingleQuotes(fileShaMatch[1].trim());
102+
continue;
103+
}
104+
105+
const fileSizeMatch = line.match(/^ size:\s*(\d+)$/);
106+
if (fileSizeMatch?.[1]) {
107+
if (currentFile === null) {
108+
throw new Error(
109+
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: size without a file entry.`,
110+
);
111+
}
112+
currentFile.size = Number(fileSizeMatch[1]);
113+
continue;
114+
}
115+
116+
if (line === "files:") {
117+
inFiles = true;
118+
continue;
119+
}
120+
121+
if (inFiles && currentFile !== null) {
122+
const finalized = parseFileRecord(currentFile, sourcePath, lineNumber, platformLabel);
123+
if (finalized) files.push(finalized);
124+
currentFile = null;
125+
}
126+
inFiles = false;
127+
128+
const topLevelMatch = line.match(/^([A-Za-z][A-Za-z0-9]*):\s*(.+)$/);
129+
if (!topLevelMatch?.[1] || topLevelMatch[2] === undefined) {
130+
throw new Error(
131+
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: unsupported line '${line}'.`,
132+
);
133+
}
134+
135+
const [, key, rawValue] = topLevelMatch;
136+
const value = parseScalarValue(rawValue);
137+
138+
if (key === "version") {
139+
if (typeof value !== "string") {
140+
throw new Error(
141+
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: version must be a string.`,
142+
);
143+
}
144+
version = value;
145+
continue;
146+
}
147+
148+
if (key === "releaseDate") {
149+
if (typeof value !== "string") {
150+
throw new Error(
151+
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: releaseDate must be a string.`,
152+
);
153+
}
154+
releaseDate = value;
155+
continue;
156+
}
157+
158+
if (key === "path" || key === "sha512") {
159+
continue;
160+
}
161+
162+
extras[key] = value;
163+
}
164+
165+
const finalized = parseFileRecord(currentFile, sourcePath, lines.length, platformLabel);
166+
if (finalized) files.push(finalized);
167+
168+
if (!version) {
169+
throw new Error(`Invalid ${platformLabel} update manifest at ${sourcePath}: missing version.`);
170+
}
171+
if (!releaseDate) {
172+
throw new Error(
173+
`Invalid ${platformLabel} update manifest at ${sourcePath}: missing releaseDate.`,
174+
);
175+
}
176+
if (files.length === 0) {
177+
throw new Error(`Invalid ${platformLabel} update manifest at ${sourcePath}: missing files.`);
178+
}
179+
180+
return {
181+
version,
182+
releaseDate,
183+
files,
184+
extras,
185+
};
186+
}
187+
188+
function mergeExtras(
189+
primary: Readonly<Record<string, UpdateManifestScalar>>,
190+
secondary: Readonly<Record<string, UpdateManifestScalar>>,
191+
platformLabel: string,
192+
): Record<string, UpdateManifestScalar> {
193+
const merged: Record<string, UpdateManifestScalar> = { ...primary };
194+
195+
for (const [key, value] of Object.entries(secondary)) {
196+
const existing = merged[key];
197+
if (existing !== undefined && existing !== value) {
198+
throw new Error(
199+
`Cannot merge ${platformLabel} update manifests: conflicting '${key}' values ('${existing}' vs '${value}').`,
200+
);
201+
}
202+
merged[key] = value;
203+
}
204+
205+
return merged;
206+
}
207+
208+
export function mergeUpdateManifests(
209+
primary: UpdateManifest,
210+
secondary: UpdateManifest,
211+
platformLabel: string,
212+
): UpdateManifest {
213+
if (primary.version !== secondary.version) {
214+
throw new Error(
215+
`Cannot merge ${platformLabel} update manifests with different versions (${primary.version} vs ${secondary.version}).`,
216+
);
217+
}
218+
219+
const filesByUrl = new Map<string, UpdateManifestFile>();
220+
for (const file of [...primary.files, ...secondary.files]) {
221+
const existing = filesByUrl.get(file.url);
222+
if (existing && (existing.sha512 !== file.sha512 || existing.size !== file.size)) {
223+
throw new Error(
224+
`Cannot merge ${platformLabel} update manifests: conflicting file entry for ${file.url}.`,
225+
);
226+
}
227+
filesByUrl.set(file.url, file);
228+
}
229+
230+
return {
231+
version: primary.version,
232+
releaseDate:
233+
primary.releaseDate >= secondary.releaseDate ? primary.releaseDate : secondary.releaseDate,
234+
files: [...filesByUrl.values()],
235+
extras: mergeExtras(primary.extras, secondary.extras, platformLabel),
236+
};
237+
}
238+
239+
function quoteYamlString(value: string): string {
240+
return `'${value.replace(/'/g, "''")}'`;
241+
}
242+
243+
function serializeScalarValue(value: UpdateManifestScalar): string {
244+
if (typeof value === "string") {
245+
return quoteYamlString(value);
246+
}
247+
return String(value);
248+
}
249+
250+
export function serializeUpdateManifest(
251+
manifest: UpdateManifest,
252+
options: {
253+
readonly quoteVersion: boolean;
254+
readonly platformLabel: string;
255+
},
256+
): string {
257+
const version = options.quoteVersion ? quoteYamlString(manifest.version) : manifest.version;
258+
const lines = [`version: ${version}`, "files:"];
259+
260+
for (const file of manifest.files) {
261+
lines.push(` - url: ${file.url}`);
262+
lines.push(` sha512: ${file.sha512}`);
263+
lines.push(` size: ${file.size}`);
264+
}
265+
266+
for (const key of Object.keys(manifest.extras).toSorted()) {
267+
const value = manifest.extras[key];
268+
if (value === undefined) {
269+
throw new Error(
270+
`Cannot serialize ${options.platformLabel} update manifest: missing value for '${key}'.`,
271+
);
272+
}
273+
lines.push(`${key}: ${serializeScalarValue(value)}`);
274+
}
275+
276+
lines.push(`releaseDate: ${quoteYamlString(manifest.releaseDate)}`);
277+
lines.push("");
278+
return lines.join("\n");
279+
}

‎scripts/merge-mac-update-manifests.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,20 @@ releaseDate: '2026-03-07T10:36:07.540Z'
105105
assert.equal(manifest.extras.minimumSystemVersion, "13.0");
106106
assert.equal(manifest.extras.stagingPercentage, 50);
107107
});
108+
109+
it("serializes versions without quotes for compatibility", () => {
110+
const original = parseMacUpdateManifest(
111+
`version: '1.0'
112+
files:
113+
- url: T3-Code-1.0-x64.zip
114+
sha512: zipsha
115+
size: 1
116+
releaseDate: '2026-03-07T10:36:07.540Z'
117+
`,
118+
"latest-mac.yml",
119+
);
120+
121+
const serialized = serializeMacUpdateManifest(original);
122+
assert.ok(serialized.includes("version: 1.0"));
123+
});
108124
});

0 commit comments

Comments
 (0)