35 lines
1.8 KiB
JavaScript
35 lines
1.8 KiB
JavaScript
// 编译小壹启动程序(小壹.exe)。程序只用 Rust 标准库;图标和版本信息由 windres 写入。
|
||
import fs from 'node:fs/promises';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
import { execFileSync } from 'node:child_process';
|
||
import { fileURLToPath } from 'node:url';
|
||
|
||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||
const icon = path.resolve(here, '../../../detail-photocraft/assets/jiansun/icon.ico');
|
||
|
||
export async function buildLauncher({ version, output, windres = process.env.WINDRES || 'windres', rustc = 'rustc' }) {
|
||
const parts = /^(\d+)\.(\d+)\.(\d+)$/.exec(version || '');
|
||
if (!parts || !output) throw Error('用法:build-launcher.mjs <x.y.z 版本> <输出 exe 路径>');
|
||
const work = await fs.mkdtemp(path.join(os.tmpdir(), 'xiaoyi-launcher-'));
|
||
try {
|
||
const rc = (await fs.readFile(path.join(here, 'launcher.rc.in'), 'utf8'))
|
||
.replaceAll('@VERSION_COMMA@', [...parts.slice(1), '0'].join(','))
|
||
.replaceAll('@VERSION@', version);
|
||
await fs.writeFile(path.join(work, 'launcher.rc'), rc);
|
||
await fs.copyFile(icon, path.join(work, 'icon.ico'));
|
||
execFileSync(windres, ['-c', '65001', '-O', 'coff', '-i', 'launcher.rc', '-o', 'launcher-res.o'], { cwd: work, windowsHide: true, stdio: 'inherit' });
|
||
output = path.resolve(output);
|
||
execFileSync(rustc, ['--edition', '2021', '-C', 'opt-level=s', '-C', 'panic=abort', '-C', 'strip=symbols',
|
||
'-C', `link-arg=${path.join(work, 'launcher-res.o')}`, '-o', output, path.join(here, 'launcher.rs')], { windowsHide: true, stdio: 'inherit' });
|
||
return output;
|
||
} finally {
|
||
await fs.rm(work, { recursive: true, force: true });
|
||
}
|
||
}
|
||
|
||
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||
const [version, output] = process.argv.slice(2);
|
||
console.log(await buildLauncher({ version, output }));
|
||
}
|