Ship Windows as a per-user NSIS installer
Replaces the Inno Setup script, which could not be built off Windows, with an NSIS installer that cross-compiles on Linux, installs under LOCALAPPDATA with no UAC prompt, and keeps the catalog on uninstall. build.mjs gains --installer and --no-installer, and --publish now ships the setup exe beside the portable zip.
This commit is contained in:
+162
-11
@@ -62,8 +62,11 @@ Usage: node windows/build.mjs [options]
|
||||
--launcher <kind> auto (default), ps or go. "go" needs a Go toolchain and
|
||||
produces Luminary.exe; "ps" always works.
|
||||
--allow-dirty skip the check that the checkout is clean outside windows/
|
||||
--publish copy the finished zip to windows/releases (plus a
|
||||
SHA256SUMS.txt) so it can be transferred to a Windows PC
|
||||
--installer also build Luminary-<ver>-win-x64-setup.exe with NSIS
|
||||
(fetches a private NSIS copy on Linux if makensis is absent)
|
||||
--no-installer skip the setup exe even when makensis is installed
|
||||
--publish copy the finished zip and setup exe to windows/releases
|
||||
(plus a SHA256SUMS.txt) so they can be transferred to a PC
|
||||
--webview2 <exe> ship a prebuilt LuminaryWindow.exe (native WebView2 window).
|
||||
Build it with:
|
||||
docker run --rm -v $PWD/windows/launcher:/src \\
|
||||
@@ -104,6 +107,7 @@ function parseArgs(argv) {
|
||||
launcher: 'auto',
|
||||
build: true,
|
||||
allowDirty: false,
|
||||
installer: 'auto',
|
||||
publish: false,
|
||||
webview2: null,
|
||||
help: false,
|
||||
@@ -127,6 +131,10 @@ function parseArgs(argv) {
|
||||
i += 1;
|
||||
} else if (arg === '--skip-build') {
|
||||
options.build = false;
|
||||
} else if (arg === '--installer') {
|
||||
options.installer = 'yes';
|
||||
} else if (arg === '--no-installer') {
|
||||
options.installer = 'no';
|
||||
} else if (arg === '--allow-dirty') {
|
||||
options.allowDirty = true;
|
||||
} else if (arg === '--publish') {
|
||||
@@ -595,6 +603,129 @@ function writeZip(sourceDir, zipPath) {
|
||||
return { files: count, bytes: statSync(zipPath).size };
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- installer ---- */
|
||||
|
||||
function dirSizeBytes(dir) {
|
||||
let total = 0;
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
const absolute = path.join(dir, entry.name);
|
||||
total += entry.isDirectory() ? dirSizeBytes(absolute) : statSync(absolute).size;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
function cachedMakensis(outDir) {
|
||||
const root = path.join(outDir, 'tools', 'nsis');
|
||||
const bin = path.join(root, 'usr', 'bin', 'makensis');
|
||||
if (!existsSync(bin)) return null;
|
||||
return { bin, nsisdir: path.join(root, 'usr', 'share', 'nsis') };
|
||||
}
|
||||
|
||||
function findMakensis(outDir) {
|
||||
if (process.env.MAKENSIS) {
|
||||
return { bin: process.env.MAKENSIS, nsisdir: process.env.NSISDIR || null };
|
||||
}
|
||||
const cached = cachedMakensis(outDir);
|
||||
if (cached) return cached;
|
||||
const which = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['makensis'], {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
const found = !which.error && which.status === 0 ? which.stdout.split(/\r?\n/)[0].trim() : '';
|
||||
return found ? { bin: found, nsisdir: process.env.NSISDIR || null } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* NSIS is a small cross-compiler: on Linux it produces the same .exe as it
|
||||
* would on Windows. Ubuntu ships it as two .debs, which unpack into the build
|
||||
* cache without root, so a fresh host can build the installer unattended.
|
||||
*/
|
||||
async function fetchMakensis(outDir) {
|
||||
if (process.platform !== 'linux' || process.arch !== 'x64') return null;
|
||||
const dpkg = spawnSync('dpkg-deb', ['--version'], { stdio: 'ignore' });
|
||||
if (dpkg.error || dpkg.status !== 0) return null;
|
||||
const packVersion = '3.09-4ubuntu1';
|
||||
const base = 'http://archive.ubuntu.com/ubuntu/pool/universe/n/nsis';
|
||||
const debs = path.join(outDir, 'cache', 'nsis-debs');
|
||||
const root = path.join(outDir, 'tools', 'nsis');
|
||||
mkdirSync(debs, { recursive: true });
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
for (const name of [`nsis_${packVersion}_amd64.deb`, `nsis-common_${packVersion}_all.deb`]) {
|
||||
const file = path.join(debs, name);
|
||||
if (!existsSync(file)) {
|
||||
try {
|
||||
writeFileSync(file, await download(`${base}/${name}`));
|
||||
} catch (error) {
|
||||
warn(`could not download ${name} (${error.message})`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
const unpack = spawnSync('dpkg-deb', ['-x', file, root], { encoding: 'utf8' });
|
||||
if (unpack.error || unpack.status !== 0) {
|
||||
warn(`could not unpack ${name}: ${(unpack.stderr || '').trim() || 'dpkg-deb failed'}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return cachedMakensis(outDir);
|
||||
}
|
||||
|
||||
async function resolveMakensis(outDir, required) {
|
||||
const found = findMakensis(outDir);
|
||||
if (found) return found;
|
||||
if (!required) return null;
|
||||
log('nsis', 'makensis not found; unpacking a private NSIS into the build cache');
|
||||
const fetched = await fetchMakensis(outDir);
|
||||
if (fetched) {
|
||||
log('nsis', `NSIS ${packVersionOf(fetched.bin)} ready (no root install needed)`);
|
||||
return fetched;
|
||||
}
|
||||
fail(
|
||||
'NSIS is required for --installer and was not found.\n' +
|
||||
' Ubuntu/Debian: sudo apt install nsis\n' +
|
||||
' macOS: brew install makensis\n' +
|
||||
' Windows: https://nsis.sourceforge.io\n' +
|
||||
' or set MAKENSIS=/path/to/makensis',
|
||||
);
|
||||
}
|
||||
|
||||
function packVersionOf(makensis) {
|
||||
const result = spawnSync(makensis, ['-VERSION'], { encoding: 'utf8' });
|
||||
return !result.error && result.status === 0 ? result.stdout.trim() : 'unknown version';
|
||||
}
|
||||
|
||||
function buildInstaller({ outDir, bundle, pkg, makensis }) {
|
||||
const script = path.join(HERE, 'installer.nsi');
|
||||
if (!existsSync(script)) fail(`installer script missing: ${script}`);
|
||||
const outputDir = path.join(outDir, 'installer');
|
||||
mkdirSync(outputDir, { recursive: true });
|
||||
const setup = path.join(outputDir, `Luminary-${pkg.version}-win-x64-setup.exe`);
|
||||
rmSync(setup, { force: true });
|
||||
|
||||
// NSIS wants four numeric fields; 0.5.1 -> 0.5.1.0
|
||||
const fields = pkg.version.split('.').map((n) => Number(n) || 0);
|
||||
while (fields.length < 4) fields.push(0);
|
||||
const version4 = fields.slice(0, 4).join('.');
|
||||
const installKb = Math.ceil(dirSizeBytes(bundle) / 1024);
|
||||
|
||||
log('installer', `makensis ${path.basename(script)} (${(installKb / 1024).toFixed(0)} MB payload)`);
|
||||
run(
|
||||
makensis.bin,
|
||||
[
|
||||
'-V2',
|
||||
`-DAPP_VERSION=${pkg.version}`,
|
||||
`-DAPP_VERSION4=${version4}`,
|
||||
`-DSOURCE_DIR=${bundle}`,
|
||||
`-DOUTPUT_DIR=${outputDir}`,
|
||||
`-DICON=${path.join(LAUNCHER_FILES, 'Luminary.ico')}`,
|
||||
`-DINSTALL_KB=${installKb}`,
|
||||
script,
|
||||
],
|
||||
ROOT,
|
||||
makensis.nsisdir ? { NSISDIR: makensis.nsisdir } : undefined,
|
||||
);
|
||||
if (!existsSync(setup)) fail('makensis reported success but produced no setup exe');
|
||||
return { path: setup, bytes: statSync(setup).size };
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- main ---- */
|
||||
|
||||
async function main() {
|
||||
@@ -653,9 +784,20 @@ async function main() {
|
||||
rmSync(stageRoot, { recursive: true, force: true });
|
||||
renameSync(workRoot, stageRoot);
|
||||
|
||||
const stagedBundle = path.join(stageRoot, 'Luminary');
|
||||
let installer = null;
|
||||
if (options.installer !== 'no') {
|
||||
const makensis = await resolveMakensis(options.out, options.installer === 'yes');
|
||||
if (makensis) {
|
||||
installer = buildInstaller({ outDir: options.out, bundle: stagedBundle, pkg, makensis });
|
||||
log('installer', `${path.basename(installer.path)} (${(installer.bytes / 1024 / 1024).toFixed(1)} MB)`);
|
||||
} else {
|
||||
warn('makensis is not installed; shipping the zip only (install NSIS for the setup exe)');
|
||||
}
|
||||
}
|
||||
|
||||
// The isolation contract: nothing outside windows/ may have changed.
|
||||
if (!options.allowDirty) checkRepoClean(options);
|
||||
const stagedBundle = path.join(stageRoot, 'Luminary');
|
||||
|
||||
// Hand-off copy. The staging and the zip stay outside the checkout; this is
|
||||
// the one write into the repository, into windows/releases, for transfer to a
|
||||
@@ -663,14 +805,16 @@ async function main() {
|
||||
let published = null;
|
||||
if (options.publish) {
|
||||
mkdirSync(RELEASES_DIR, { recursive: true });
|
||||
const target = path.join(RELEASES_DIR, path.basename(zipPath));
|
||||
cpSync(zipPath, target);
|
||||
writeFileSync(
|
||||
path.join(RELEASES_DIR, 'SHA256SUMS.txt'),
|
||||
`${sha256File(target)} ${path.basename(target)}\n`,
|
||||
);
|
||||
published = target;
|
||||
log('publish', path.relative(ROOT, target));
|
||||
const artifacts = [zipPath, ...(installer ? [installer.path] : [])];
|
||||
const sums = [];
|
||||
for (const artifact of artifacts) {
|
||||
const target = path.join(RELEASES_DIR, path.basename(artifact));
|
||||
cpSync(artifact, target);
|
||||
sums.push(`${sha256File(target)} ${path.basename(target)}`);
|
||||
log('publish', path.relative(ROOT, target));
|
||||
}
|
||||
writeFileSync(path.join(RELEASES_DIR, 'SHA256SUMS.txt'), `${sums.join('\n')}\n`);
|
||||
published = path.join(RELEASES_DIR, path.basename(zipPath));
|
||||
}
|
||||
|
||||
process.stdout.write(
|
||||
@@ -680,6 +824,13 @@ async function main() {
|
||||
` artifact ${zipPath}`,
|
||||
` size ${(zip.bytes / 1024 / 1024).toFixed(1)} MB (${zip.files} files)`,
|
||||
` sha256 ${sha256File(zipPath)}`,
|
||||
...(installer
|
||||
? [
|
||||
` setup ${installer.path}`,
|
||||
` setup MB ${(installer.bytes / 1024 / 1024).toFixed(1)}`,
|
||||
` setup sha ${sha256File(installer.path)}`,
|
||||
]
|
||||
: []),
|
||||
` node ${options.node} win-x64`,
|
||||
` launcher ${launcher === 'go' ? 'Luminary.exe' : 'Luminary.cmd / Luminary.vbs'}`,
|
||||
...(options.webview2 ? [` window LuminaryWindow.exe (native WebView2)`] : []),
|
||||
|
||||
Reference in New Issue
Block a user