-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommon.ts
More file actions
509 lines (421 loc) · 15.1 KB
/
Copy pathcommon.ts
File metadata and controls
509 lines (421 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/**
* Common build utilities shared across all platforms
*/
import { $ } from 'bun';
import { execSync } from 'child_process';
import { existsSync, mkdirSync, rmSync, copyFileSync, readdirSync } from 'fs';
import { join, dirname } from 'path';
import { createHash } from 'crypto';
export type Platform = 'darwin' | 'win32' | 'linux';
export type Arch = 'x64' | 'arm64';
export interface BuildConfig {
platform: Platform;
arch: Arch;
upload: boolean;
uploadLatest: boolean;
uploadScript: boolean;
rootDir: string;
electronDir: string;
}
/**
* Bun version to bundle with the app.
* Update this when upgrading Bun. Check latest at: https://github.com/oven-sh/bun/releases
* This should match or be close to the version used in CI (setup-bun action).
*/
export const BUN_VERSION = 'bun-v1.3.9';
/**
* uv version to bundle with the app.
* Update this when upgrading uv. Check latest at: https://github.com/astral-sh/uv/releases
*/
export const UV_VERSION = '0.10.6';
/**
* Get platform key for resources/bin folder naming.
*/
export function getPlatformKey(platform: Platform, arch: Arch): string {
return `${platform}-${arch}`;
}
/**
* Get the Bun download filename for a platform/arch combination
*/
export function getBunDownloadName(platform: Platform, arch: Arch): string {
const archMap: Record<Arch, string> = {
x64: 'x64',
arm64: 'aarch64',
};
const platformMap: Record<Platform, string> = {
darwin: 'darwin',
win32: 'windows',
linux: 'linux',
};
const bunArch = archMap[arch];
const bunPlatform = platformMap[platform];
// Windows and Linux x64 use baseline build for broader CPU compatibility (no AVX2 requirement)
if ((platform === 'win32' || platform === 'linux') && arch === 'x64') {
return `bun-${bunPlatform}-x64-baseline`;
}
return `bun-${bunPlatform}-${bunArch}`;
}
/**
* Get uv release artifact filename for a platform/arch combination.
*/
export function getUvDownloadName(platform: Platform, arch: Arch): string {
if (platform === 'darwin' && arch === 'arm64')
return 'uv-aarch64-apple-darwin.tar.gz';
if (platform === 'darwin' && arch === 'x64')
return 'uv-x86_64-apple-darwin.tar.gz';
if (platform === 'linux' && arch === 'arm64')
return 'uv-aarch64-unknown-linux-gnu.tar.gz';
if (platform === 'linux' && arch === 'x64')
return 'uv-x86_64-unknown-linux-gnu.tar.gz';
if (platform === 'win32' && arch === 'arm64')
return 'uv-aarch64-pc-windows-msvc.zip';
if (platform === 'win32' && arch === 'x64')
return 'uv-x86_64-pc-windows-msvc.zip';
throw new Error(`Unsupported uv target: ${platform}-${arch}`);
}
/**
* Verify SHA256 checksum of a file
*/
export async function verifySha256(
filePath: string,
expectedHash: string,
): Promise<boolean> {
const file = Bun.file(filePath);
const buffer = await file.arrayBuffer();
const hash = createHash('sha256').update(Buffer.from(buffer)).digest('hex');
return hash.toLowerCase() === expectedHash.toLowerCase();
}
/**
* Download and verify Bun binary
* Uses curl for downloads (more reliable in CI than fetch + Bun.write)
*/
export async function downloadBun(config: BuildConfig): Promise<void> {
const { platform, arch, electronDir } = config;
const bunDownload = getBunDownloadName(platform, arch);
const vendorDir = join(electronDir, 'vendor', 'bun');
console.log(`Downloading Bun ${BUN_VERSION} for ${platform}-${arch}...`);
// Create vendor directory
mkdirSync(vendorDir, { recursive: true });
// Create temp directory
const tempDir = join(electronDir, '.bun-download-temp');
mkdirSync(tempDir, { recursive: true });
try {
const zipUrl = `https://github.com/oven-sh/bun/releases/download/${BUN_VERSION}/${bunDownload}.zip`;
const checksumUrl = `https://github.com/oven-sh/bun/releases/download/${BUN_VERSION}/SHASUMS256.txt`;
// Download files using curl (more reliable in CI than fetch + Bun.write)
const zipPath = join(tempDir, `${bunDownload}.zip`);
const checksumPath = join(tempDir, 'SHASUMS256.txt');
console.log(` Downloading ${zipUrl}...`);
await $`curl -fsSL --retry 3 --retry-delay 2 -o ${zipPath} ${zipUrl}`;
console.log(' Download complete');
console.log(' Downloading checksums...');
await $`curl -fsSL --retry 3 --retry-delay 2 -o ${checksumPath} ${checksumUrl}`;
// Verify checksum
console.log(' Verifying checksum...');
const checksumContent = await Bun.file(checksumPath).text();
const expectedHash = checksumContent
.split('\n')
.find((line) => line.includes(`${bunDownload}.zip`))
?.split(' ')[0];
if (!expectedHash) {
throw new Error(`Checksum not found for ${bunDownload}.zip`);
}
const isValid = await verifySha256(zipPath, expectedHash);
if (!isValid) {
throw new Error('Checksum verification failed!');
}
console.log(' Checksum verified ✓');
// Extract
console.log(' Extracting...');
await $`unzip -o ${zipPath} -d ${tempDir}`.quiet();
// Copy binary
const bunBinary = platform === 'win32' ? 'bun.exe' : 'bun';
const sourcePath = join(tempDir, bunDownload, bunBinary);
const destPath = join(vendorDir, bunBinary);
copyFileSync(sourcePath, destPath);
// Make executable on Unix
if (platform !== 'win32') {
await $`chmod +x ${destPath}`.quiet();
}
console.log(` Bun installed to ${destPath} ✓`);
} finally {
// Cleanup temp directory
rmSync(tempDir, { recursive: true, force: true });
}
}
/**
* Find the first matching file recursively under a directory.
*/
function findFileRecursive(root: string, fileName: string): string | null {
const entries = readdirSync(root, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(root, entry.name);
if (entry.isFile() && entry.name === fileName) {
return fullPath;
}
if (entry.isDirectory()) {
const nested = findFileRecursive(fullPath, fileName);
if (nested) return nested;
}
}
return null;
}
/**
* Download and verify uv binary, then install it to resources/bin/<platform-arch>/uv(.exe).
*/
export async function downloadUv(config: BuildConfig): Promise<void> {
const { platform, arch, electronDir } = config;
const uvDownload = getUvDownloadName(platform, arch);
const uvBinaryName = platform === 'win32' ? 'uv.exe' : 'uv';
const platformKey = getPlatformKey(platform, arch);
const targetDir = join(electronDir, 'resources', 'bin', platformKey);
const targetPath = join(targetDir, uvBinaryName);
// Skip when already provisioned
if (existsSync(targetPath)) {
console.log(`uv already present at ${targetPath}`);
return;
}
console.log(`Downloading uv ${UV_VERSION} for ${platformKey}...`);
mkdirSync(targetDir, { recursive: true });
const tempDir = join(electronDir, '.uv-download-temp');
rmSync(tempDir, { recursive: true, force: true });
mkdirSync(tempDir, { recursive: true });
try {
const assetUrl = `https://github.com/astral-sh/uv/releases/download/${UV_VERSION}/${uvDownload}`;
const checksumUrl = `${assetUrl}.sha256`;
const assetPath = join(tempDir, uvDownload);
const checksumPath = join(tempDir, `${uvDownload}.sha256`);
const extractDir = join(tempDir, 'extract');
console.log(` Downloading ${assetUrl}...`);
await $`curl -fsSL --retry 3 --retry-delay 2 -o ${assetPath} ${assetUrl}`;
console.log(' Downloading checksum...');
await $`curl -fsSL --retry 3 --retry-delay 2 -o ${checksumPath} ${checksumUrl}`;
console.log(' Verifying checksum...');
const checksumContent = await Bun.file(checksumPath).text();
const hashMatch = checksumContent.match(/[a-fA-F0-9]{64}/);
if (!hashMatch) {
throw new Error(`Unable to parse checksum from ${checksumPath}`);
}
const isValid = await verifySha256(assetPath, hashMatch[0]);
if (!isValid) {
throw new Error('uv checksum verification failed');
}
console.log(' Checksum verified ✓');
mkdirSync(extractDir, { recursive: true });
if (uvDownload.endsWith('.zip')) {
// Use PowerShell on Windows for consistent extraction support.
await $`powershell -NoProfile -ExecutionPolicy Bypass -Command "Expand-Archive -LiteralPath '${assetPath}' -DestinationPath '${extractDir}' -Force"`;
} else {
await $`tar -xzf ${assetPath} -C ${extractDir}`;
}
const extractedUv = findFileRecursive(extractDir, uvBinaryName);
if (!extractedUv) {
throw new Error(`Unable to locate ${uvBinaryName} in extracted archive`);
}
copyFileSync(extractedUv, targetPath);
if (platform !== 'win32') {
await $`chmod +x ${targetPath}`.quiet();
}
console.log(` uv installed to ${targetPath} ✓`);
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
}
/**
* Clean previous build artifacts
*/
export function cleanBuildArtifacts(config: BuildConfig): void {
const { electronDir } = config;
console.log('Cleaning previous builds...');
const foldersToClean = [
join(electronDir, 'vendor'),
join(electronDir, 'packages'),
join(electronDir, 'release'),
];
for (const folder of foldersToClean) {
if (existsSync(folder)) {
rmSync(folder, { recursive: true, force: true });
}
}
}
/**
* Install dependencies
* On Windows, uses hoisted linker to avoid .bun symlink directory
*/
export async function installDependencies(config: BuildConfig): Promise<void> {
const { rootDir, platform } = config;
if (platform === 'win32') {
// Use hoisted linker on Windows - Bun's default isolated mode creates
// node_modules/.bun/ with symlinks that esbuild can't traverse on Windows
// ("Access is denied" errors with junction points)
// Hoisted mode creates flat npm-style node_modules without .bun
console.log('Installing dependencies (Windows hoisted mode)...');
await $`cd ${rootDir} && bun install --linker=hoisted`.quiet();
} else {
console.log('Installing dependencies...');
await $`cd ${rootDir} && bun install`.quiet();
}
}
/**
* Copy Session MCP Server to packaged app resources.
* The session server provides session-scoped tools (SubmitPlan, config_validate, etc.) for agent sessions.
*/
export function copySessionServer(config: BuildConfig): void {
const { rootDir, electronDir } = config;
const sessionSource = join(
rootDir,
'packages',
'session-mcp-server',
'dist',
'index.js',
);
const sessionDest = join(
electronDir,
'resources',
'session-mcp-server',
'index.js',
);
if (!existsSync(sessionSource)) {
console.warn(
`Warning: Session server not found at ${sessionSource}. Session-scoped tools will not work.`,
);
return;
}
console.log('Copying Session MCP Server...');
mkdirSync(dirname(sessionDest), { recursive: true });
copyFileSync(sessionSource, sessionDest);
}
/**
* Build MCP helper servers.
* Shared across all platforms to avoid drift.
*/
export function buildMcpServers(config: BuildConfig): void {
const { rootDir } = config;
const sessionDir = join(rootDir, 'packages', 'session-mcp-server');
const sessionOut = join(sessionDir, 'dist', 'index.js');
console.log('Building MCP server...');
mkdirSync(join(sessionDir, 'dist'), { recursive: true });
execSync(
`bun build ${join(sessionDir, 'src', 'index.ts')} --outfile ${sessionOut} --target node --format cjs`,
{ cwd: rootDir, stdio: 'inherit', shell: true },
);
if (!existsSync(sessionOut)) {
throw new Error(`Session MCP server output not found at ${sessionOut}`);
}
}
/**
* Build the WhatsApp worker subprocess (Baileys + Node runtime bundle).
* Output ships as an extraResource at resources/messaging-whatsapp-worker/worker.cjs
* and is spawned by WhatsAppAdapter. See electron-builder.yml `extraResources`.
*/
export function buildWhatsAppWorker(config: BuildConfig): void {
const { rootDir } = config;
const workerOut = join(
rootDir,
'packages',
'messaging-whatsapp-worker',
'dist',
'worker.cjs',
);
console.log('Building WhatsApp worker...');
execSync('bun run build:wa-worker', {
cwd: rootDir,
stdio: 'inherit',
shell: true,
});
if (!existsSync(workerOut)) {
throw new Error(`WhatsApp worker output not found at ${workerOut}`);
}
}
/**
* Verify MCP helper server is present in packaged resources.
*/
export function verifyMcpServersExist(config: BuildConfig): void {
const { electronDir } = config;
const sessionPath = join(
electronDir,
'resources',
'session-mcp-server',
'index.js',
);
if (!existsSync(sessionPath)) {
throw new Error(`Session MCP server not found at ${sessionPath}`);
}
}
/**
* Build the Electron app (main, preload, renderer)
*/
export async function buildElectronApp(config: BuildConfig): Promise<void> {
const { rootDir } = config;
console.log('Building Electron app...');
await $`cd ${rootDir} && bun run electron:build`;
}
/**
* Create manifest.json for upload
*/
export async function createManifest(config: BuildConfig): Promise<string> {
const { rootDir, electronDir } = config;
const packageJson = await Bun.file(join(electronDir, 'package.json')).json();
const version = packageJson.version;
const uploadDir = join(rootDir, '.build', 'upload');
mkdirSync(uploadDir, { recursive: true });
const manifestPath = join(uploadDir, 'manifest.json');
await Bun.write(manifestPath, JSON.stringify({ version }, null, 2));
console.log(`Created manifest.json (version: ${version})`);
return version;
}
/**
* Upload to S3
*/
export async function uploadToS3(config: BuildConfig): Promise<void> {
const { rootDir, upload, uploadLatest, uploadScript } = config;
if (!upload) return;
// Check for required env vars
const required = [
'S3_VERSIONS_BUCKET_ENDPOINT',
'S3_VERSIONS_BUCKET_ACCESS_KEY_ID',
'S3_VERSIONS_BUCKET_SECRET_ACCESS_KEY',
];
const missing = required.filter((key) => !process.env[key]);
if (missing.length > 0) {
throw new Error(`Missing S3 credentials: ${missing.join(', ')}`);
}
console.log('\n=== Uploading to S3 ===');
const flags = ['--electron'];
if (uploadLatest) flags.push('--latest');
if (uploadScript) flags.push('--script');
await $`cd ${rootDir} && bun run scripts/upload.ts ${flags}`;
console.log('Upload complete ✓');
}
/**
* Load environment variables from .env file
*/
export async function loadEnvFile(config: BuildConfig): Promise<void> {
const envPath = join(config.rootDir, '.env');
if (existsSync(envPath)) {
const content = await Bun.file(envPath).text();
for (const line of content.split('\n')) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const [key, ...valueParts] = trimmed.split('=');
if (key && valueParts.length > 0) {
const value = valueParts.join('=').replace(/^["']|["']$/g, '');
process.env[key] = value;
}
}
}
}
}
/**
* Get output artifact name for a platform/arch
*/
export function getArtifactName(platform: Platform, arch: Arch): string {
switch (platform) {
case 'darwin':
return `Qwen-Code-Desktop-${arch}.dmg`;
case 'win32':
return `Qwen-Code-Desktop-${arch}.exe`;
case 'linux':
return `Qwen-Code-Desktop-${arch}.AppImage`;
}
}