Files
bworld/tools/desktop_all.ts
T
2026-11-12 20:41:13 -03:00

117 lines
3.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// deno task desktop-all
// builds the desktop app for windows and linux, from whichever of them it runs on, and zips each into
// dist/windows.zip and dist/linux.zip for the title screen's download button. each zip has the app's bworld/ folder
import { walk } from "@std/fs";
import { relative } from "@std/path";
import { Zip, ZipDeflate } from "fflate";
import { is_elf, wrap_linux_launcher } from "../desktop/linux_launcher.ts";
const APP_NAME = "bworld";
const TARGETS = [
{ name: "windows", target: "x86_64-pc-windows-msvc" },
{ name: "linux", target: "x86_64-unknown-linux-gnu" },
];
// same as deno task desktop
const DESKTOP_ARGS = [
"--allow-read",
"--allow-write",
"--allow-net",
"--allow-ffi",
"--allow-env=CUBI_SERVER,CUBI_CLIENT_UPDATES,HOME,USERPROFILE,APPDATA,XDG_DATA_HOME,DEBUG",
"--include",
"build",
"--include",
"server_mods",
"--include",
"server/game/worker.ts",
"--include",
"server/content_worker.ts",
"--include",
"desktop/config.json",
"desktop/main.ts",
];
async function run(args: string[]) {
const { success } = await new Deno.Command(Deno.execPath(), { args, stdout: "inherit", stderr: "inherit" })
.output();
if (!success) {
throw new Error(`deno ${args.join(" ")} failed`);
}
}
function remove(path: string) {
try {
Deno.removeSync(path, { recursive: true });
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e;
}
}
// zip is built on any os, so linux's executables are found by what they start with: elf binaries and scripts
function is_executable(path: string) {
if (is_elf(path)) return true;
const head = new Uint8Array(2);
const file = Deno.openSync(path);
file.readSync(head);
file.close();
return head[0] === 0x23 && head[1] === 0x21; // #!
}
// zips folder into out with the folder itself at its root. unix_modes keeps linux's executables executable
async function zip_folder(folder: string, out: string, unix_modes: boolean) {
const parent = `${folder}/..`;
const output = await Deno.open(out, { write: true, create: true, truncate: true });
// the zip's chunks are written one after another, in order
let writes = Promise.resolve();
const done = Promise.withResolvers<void>();
const zip = new Zip((error, chunk, final) => {
if (error) {
done.reject(error);
return;
}
writes = writes.then(() => write_all(output, chunk));
if (final) done.resolve();
});
for await (const entry of walk(folder, { includeDirs: false })) {
const file = new ZipDeflate(relative(parent, entry.path).replaceAll("\\", "/"), { level: 6 });
if (unix_modes) {
file.os = 3;
file.attrs = (is_executable(entry.path) ? 0o100755 : 0o100644) << 16;
}
zip.add(file);
file.push(await Deno.readFile(entry.path), true);
}
zip.end();
try {
await done.promise;
await writes;
} finally {
output.close();
}
}
async function write_all(file: Deno.FsFile, data: Uint8Array) {
for (let written = 0; written < data.length;) {
written += await file.write(data.subarray(written));
}
}
await run(["run", "-A", "build.ts", "--once"]);
for (const { name, target } of TARGETS) {
const dir = `dist/${name}`;
const app = `${dir}/${APP_NAME}`;
remove(dir);
console.log(`Building ${name} (${target})`);
await run(["desktop", "--target", target, "--output", app, ...DESKTOP_ARGS]);
if (name === "linux") {
wrap_linux_launcher(app);
}
const zip = `dist/${name}.zip`;
await zip_folder(app, zip, name === "linux");
console.log(`Zipped ${zip}`);
}