readest/apps/readest-app/scripts/tauri.mjs
Huang Xin 2ce0522743
Some checks are pending
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
CodeQL Advanced / Analyze (rust) (push) Waiting to run
Publish Docker image / build (linux/amd64, ubuntu-latest) (push) Waiting to run
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm) (push) Waiting to run
Publish Docker image / merge (push) Blocked by required conditions
Build with Nix / tests (push) Waiting to run
PR checks / nix_flake_check (push) Waiting to run
PR checks / rust_lint (push) Waiting to run
PR checks / koplugin_rust_lint (push) Waiting to run
PR checks / build_web_app (push) Waiting to run
PR checks / test_web_app (1) (push) Waiting to run
PR checks / test_web_app (2) (push) Waiting to run
PR checks / test_extensions (push) Waiting to run
PR checks / build_tauri_app (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Deploy to vercel on merge / build_and_deploy (push) Waiting to run
chore(linux): catch up the CEF runtime with upstream feat/cef, closes #6397 (#6487)
2026-09-30 04:07:58 +08:00

118 lines
4.7 KiB
JavaScript
Executable file

#!/usr/bin/env node
// `pnpm tauri <command> [tauri args] [-- cargo args]`.
//
// Every platform but Linux goes straight to the npm @tauri-apps/cli, which
// builds on the readest tauri fork (the wry runtime). On Linux the desktop
// commands (dev, build, bundle) run on the CEF runtime instead: this uses the
// tauri CLI published from upstream's `feat/cef` branch and adds what the CEF
// build needs on top of the normal tauri command line:
// - `--features cef` so the CLI bundles the CEF distribution and cargo
// compiles the CEF runtime (`cef = ["dep:tauri-runtime-cef"]` in src-tauri/Cargo.toml);
// - `--no-default-features` (cargo side) to drop the `wry` runtime, see
// src-tauri/Cargo.toml;
// - `--config src-tauri/.cargo/cef.toml` (cargo side) to take tauri and the
// plugins from the `feat/cef` branches instead of the readest fork.
//
// Cargo resolves that graph differently from the fork graph every other
// platform builds, and it can only write one Cargo.lock. So for the duration
// of the command the repo's Cargo.cef.lock is swapped in as Cargo.lock and the
// original is put back afterwards; the CEF resolution is saved back to
// Cargo.cef.lock, which is committed like Cargo.lock. The original is parked
// in Cargo.lock.wry meanwhile, created exclusively so two CEF commands in one
// checkout cannot swap over each other; if a run is killed hard (SIGKILL)
// before the swap back, `mv Cargo.lock.wry Cargo.lock` restores it.
//
// Mobile commands (`tauri android ...`, `tauri ios ...`) and the tauri
// webdriver test harness (scripts/test-tauri.sh, which needs WebKitWebDriver)
// keep the wry runtime on Linux too.
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const CEF_CLI = '@tauri-apps/cli-cef@3.0.0-alpha.27';
// Offline builds (Flatpak) cannot `pnpm dlx`; they point this at an unpacked
// copy of the CLI package's `tauri.js` instead.
const localCefCli = process.env['TAURI_CEF_CLI'];
// CI builds the CLI from readest/tauri instead (same command line); see the
// "install the CEF tauri CLI" step in the workflows for why.
const cargoCefCli = process.env['TAURI_CEF_CARGO'] === '1';
const appDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const repoRoot = path.resolve(appDir, '../..');
const cargoConfig = path.join(appDir, 'src-tauri/.cargo/cef.toml');
const lockPath = path.join(repoRoot, 'Cargo.lock');
const cefLockPath = path.join(repoRoot, 'Cargo.cef.lock');
const savedLockPath = path.join(repoRoot, 'Cargo.lock.wry');
const [command, ...rest] = process.argv.slice(2);
const useCef = process.platform === 'linux' && ['dev', 'build', 'bundle'].includes(command);
const run = (file, args, cleanup = () => {}) => {
const child = spawn(file, args, { stdio: 'inherit' });
child.on('exit', (code, signal) => {
cleanup();
process.exit(code ?? (signal ? 1 : 0));
});
child.on('error', (error) => {
cleanup();
console.error(error);
process.exit(1);
});
};
if (!useCef) {
const tauriCli = createRequire(import.meta.url).resolve('@tauri-apps/cli/tauri.js');
run('node', [tauriCli, ...process.argv.slice(2)]);
} else {
const split = rest.indexOf('--');
const tauriArgs = split === -1 ? rest : rest.slice(0, split);
const cargoArgs = split === -1 ? [] : rest.slice(split + 1);
const args = [
command,
'--features',
'cef',
...tauriArgs,
'--',
'--no-default-features',
'--config',
cargoConfig,
...cargoArgs,
];
try {
fs.writeFileSync(savedLockPath, fs.readFileSync(lockPath), { flag: 'wx' });
} catch (error) {
if (error.code !== 'EEXIST') throw error;
console.error(
`${savedLockPath} exists: another CEF build is running from this checkout, or an earlier ` +
'one was killed before it could restore Cargo.lock. Wait for it to finish, or run ' +
'`mv Cargo.lock.wry Cargo.lock`.',
);
process.exit(1);
}
if (fs.existsSync(cefLockPath)) {
fs.copyFileSync(cefLockPath, lockPath);
}
let swapped = true;
const restoreLock = () => {
if (!swapped) return;
fs.copyFileSync(lockPath, cefLockPath);
fs.renameSync(savedLockPath, lockPath);
swapped = false;
};
// Ctrl-C reaches the child through the process group; stay alive until it
// has exited so the lock swap is undone.
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
process.on(signal, () => {});
}
if (cargoCefCli) {
run('cargo', ['tauri', ...args], restoreLock);
} else if (localCefCli) {
run('node', [localCefCli, ...args], restoreLock);
} else {
run('pnpm', ['dlx', CEF_CLI, ...args], restoreLock);
}
}