Prepare Linux builds and validate native desktop runtime

This commit is contained in:
2026-10-14 18:06:22 -07:00
parent edef2520af
commit 30cbfeedd6
15 changed files with 481 additions and 27 deletions
@@ -0,0 +1,25 @@
const supported = { platform: ['win32', 'linux'], arch: ['x64', 'arm64'] };
export function packageTarget(args, defaults = process) {
const target = { platform: defaults.platform, arch: defaults.arch };
const seen = new Set();
for (const argument of args) {
const match = /^--(platform|arch)=(.+)$/.exec(argument);
if (!match || !supported[match[1]].includes(match[2]) || seen.has(match[1])) {
throw new Error('Use --platform=win32|linux and --arch=x64|arm64, at most once each.');
}
seen.add(match[1]);
target[match[1]] = match[2];
}
for (const key of Object.keys(supported)) {
// Preserve Electron's native macOS packaging default; explicit cross-build
// options remain the Windows/Linux targets supported by this project.
if (key === 'platform' && target.platform === 'darwin' && !seen.has('platform')) continue;
if (!supported[key].includes(target[key])) throw new Error(`Unsupported Browser ${key}: ${target[key]}`);
}
return target;
}
export function executableName(platform) {
return platform === 'win32' ? 'RuneStone-Browser.exe' : 'RuneStone-Browser';
}
+7 -7
View File
@@ -1,12 +1,12 @@
import { packager } from '@electron/packager';
const options = Object.fromEntries(process.argv.slice(2).map(arg => {
const match = /^--(platform|arch)=(win32|linux|x64|arm64)$/.exec(arg);
if (!match) throw new Error('Use --platform=win32|linux and --arch=x64|arm64.');
return [match[1], match[2]];
}));
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { packageTarget } from './package-target.mjs';
const project = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const target = packageTarget(process.argv.slice(2));
const outputs = await packager({
dir: '.', name: 'RuneStone-Browser', out: 'dist', overwrite: true,
platform: options.platform || process.platform, arch: options.arch || process.arch,
dir: project, name: 'RuneStone-Browser', out: resolve(project, 'dist'), overwrite: true,
...target,
ignore: [/^\/(test|scripts|output|plugin|releases|\.github)(\/|$)/, /^\/src\/(ui\/|host\.mjs|main\.mjs|preload\.cjs)/],
});
for (const output of outputs) console.log(output);
+8 -6
View File
@@ -1,11 +1,13 @@
import { cp, mkdir, access } from 'node:fs/promises';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { packageTarget } from './package-target.mjs';
import { stageRuntime } from './stage-runtime.mjs';
const project = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const host = resolve(process.env.RUNESTONE_ROOT || resolve(project, '..'));
const source = resolve(project, `dist/RuneStone-Browser-${process.platform}-${process.arch}`);
await access(resolve(source, process.platform === 'win32' ? 'RuneStone-Browser.exe' : 'RuneStone-Browser'));
const destination = resolve(host, 'src-tauri/browser-engine');
await mkdir(destination, { recursive: true });
await cp(source, destination, { recursive: true });
const target = packageTarget(process.argv.slice(2));
if (target.platform !== process.platform || target.arch !== process.arch) {
throw new Error('Stage the Browser runtime on the same platform and architecture as the native host build.');
}
const { destination, backup } = await stageRuntime({ project, host, ...target });
console.log(`Browser runtime staged in ${destination}`);
if (backup) console.log(`Previous runtime retained in ${backup}`);
+48
View File
@@ -0,0 +1,48 @@
import { cp, mkdir, access, mkdtemp, rename, rm, lstat, chmod } from 'node:fs/promises';
import { constants } from 'node:fs';
import { resolve, join } from 'node:path';
import { executableName } from './package-target.mjs';
// Copy into a new tree instead of merging Windows and Linux runtime files.
// Retain the previous tree in output/ so a failed switch can be recovered.
export async function stageRuntime({ project, host, platform, arch }) {
const source = resolve(project, `dist/RuneStone-Browser-${platform}-${arch}`);
const executable = join(source, executableName(platform));
const stat = await lstat(executable);
if (!stat.isFile()) throw new Error(`Browser executable is not a regular file: ${executable}`);
await access(executable, platform === 'win32' ? constants.R_OK : constants.R_OK | constants.X_OK);
const parent = resolve(host, 'src-tauri');
await mkdir(parent, { recursive: true });
const destination = join(parent, 'browser-engine');
const temporary = await mkdtemp(join(parent, '.browser-stage-'));
// Copy to a new child so cp preserves the source root mode, not mkdtemp's 0700.
const staged = join(temporary, 'browser-engine');
let backup;
let moved = false;
try {
await cp(source, staged, { recursive: true });
// Electron Packager can leave its temporary root at 0700. Installed resources
// must remain traversable after the package manager changes ownership to root.
if (platform === 'linux') await chmod(staged, 0o755);
await access(join(staged, executableName(platform)), platform === 'win32' ? constants.R_OK : constants.X_OK);
try {
await lstat(destination);
const backups = resolve(host, 'output/browser-stage-backups');
await mkdir(backups, { recursive: true });
backup = join(await mkdtemp(join(backups, 'previous-')), 'browser-engine');
await rename(destination, backup);
moved = true;
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
try {
await rename(staged, destination);
} catch (error) {
if (moved) await rename(backup, destination);
throw error;
}
return { destination, backup: moved ? backup : undefined };
} finally {
await rm(temporary, { recursive: true, force: true });
}
}