Files
Agent-Workbench/scripts/bundle-desktop.mjs

52 lines
4.4 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
const digest = file => createHash('sha256').update(fs.readFileSync(file)).digest('hex');
export function planDesktopBundle(root) {
const meta = JSON.parse(fs.readFileSync(path.join(root, 'node_modules/electron/package.json'), 'utf8'));
if (meta.version !== '45.2.0') throw new Error('Desktop packaging requires the fixed Electron 45.2.0 runtime.');
const installed = path.join(root, 'node_modules/electron/dist');
const directory = fs.existsSync(path.join(installed, 'electron.exe')) ? installed : path.join(root, 'runtime/electron');
const executable = path.join(directory, 'electron.exe');
if (!fs.existsSync(executable) || digest(executable) !== '010ea51bfb528c8143513439aa93fb40754403daa2023f9decd4517da71706ab') throw new Error('The actual Windows Electron runtime differs from the fixed binary.');
for (const file of ['LICENSE', 'LICENSES.chromium.html']) if (!fs.existsSync(path.join(directory, file))) throw new Error('Electron runtime license missing.');
return { directory, version: meta.version, sha256: digest(executable), bytes: fs.statSync(executable).size };
}
export function buildNativeLauncher(root, output) {
const compiler = path.join(process.env.SystemRoot || 'C:\\Windows', 'Microsoft.NET/Framework64/v4.0.30319/csc.exe');
if (!fs.existsSync(compiler)) throw new Error('The Windows .NET Framework compiler is required to build the fixed desktop launcher.');
fs.mkdirSync(path.dirname(output), { recursive: true });
const inputs = { compiler: digest(compiler), source: digest(path.join(root, 'src/desktop/Launcher.cs')), icon: digest(path.join(root, 'public/workbench-icon.ico')) };
const recordFile = output + '.build.json';
if (fs.existsSync(output) && fs.existsSync(recordFile)) {
const cached = JSON.parse(fs.readFileSync(recordFile, 'utf8'));
if (JSON.stringify(cached.inputs) === JSON.stringify(inputs) && cached.sha256 === digest(output)) return cached;
}
try { execFileSync(compiler, ['/nologo', '/target:winexe', '/platform:x64', '/optimize+', '/utf8output',
'/reference:System.Web.Extensions.dll', '/reference:System.Windows.Forms.dll', '/win32icon:' + path.join(root, 'public/workbench-icon.ico'),
'/out:' + output, path.join(root, 'src/desktop/Launcher.cs')], { cwd: root, windowsHide: true, timeout: 30000, stdio: 'pipe' }); }
catch (error) { fs.writeFileSync(output + '.compile-failure.txt', Buffer.concat([error.stdout ?? Buffer.alloc(0), error.stderr ?? Buffer.alloc(0)])); throw error; }
const record = { bytes: fs.statSync(output).size, sha256: digest(output), inputs, compiler: 'Windows .NET Framework C# compiler; source-bound cached original output, no deterministic-build claim.' };
fs.writeFileSync(recordFile, JSON.stringify(record, null, 2)); return record;
}
export function copyDesktopBundle(root, output, plan, { source = false, launcher = null } = {}) {
if (source && digest(path.join(plan.directory, 'resources/default_app.asar')) !== 'eac55d6cfcf4eab7e31a45356567f549ef7eb669e3699226b48a8492ad34f510') {
throw new Error('The source Electron runtime requires its fixed default application loader.');
}
const destination = path.join(output, source ? 'runtime/electron' : 'desktop');
fs.cpSync(plan.directory, destination, { recursive: true, filter: file => source || path.basename(file) !== 'default_app.asar' });
if (!source) {
fs.renameSync(path.join(destination, 'electron.exe'), path.join(destination, 'AgentWorkbench.exe'));
const app = path.join(destination, 'resources/app'); fs.mkdirSync(app, { recursive: true });
fs.writeFileSync(path.join(app, 'package.json'), JSON.stringify({ name: 'agent-workbench-desktop', version: JSON.parse(fs.readFileSync(path.join(root, 'package.json'))).version, main: 'main.cjs' }) + '\n');
fs.writeFileSync(path.join(app, 'main.cjs'), "require('../../../dist-desktop/main.cjs');\n");
if (launcher) fs.copyFileSync(launcher, path.join(output, 'AgentWorkbench.exe'));
}
const identity = { version: plan.version, sha256: plan.sha256, bytes: plan.bytes, executable: source ? 'runtime/electron/electron.exe' : 'desktop/AgentWorkbench.exe',
scope: 'Fixed original Windows Electron runtime, full license texts, isolated application loader and native bootstrap; original executable hash unchanged.' };
fs.writeFileSync(path.join(output, 'DESKTOP-RUNTIME.json'), JSON.stringify(identity, null, 2) + '\n');
return identity;
}