askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
build-mac.mjs · 6.2 KB · 116 lines
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * The Mac app people download: dbbasic-browser.app, zipped. |
| 4 | * |
| 5 | * npm run build:mac → release/dbbasic-browser.app |
| 6 | * release/dbbasic-browser-<version>-macos-<arch>.zip (+ a .json beside it) |
| 7 | * npm run build:mac -- --allow-dirty a trial build from uncommitted changes, named so it can't pass for a release |
| 8 | * |
| 9 | * It is Electron's own Electron.app (the one npm installed and checked against Electron's published checksums) with |
| 10 | * our name, icon and version, and the standalone build (scripts/build-standalone.mjs) in Contents/Resources/app. |
| 11 | * It is for the processor of the Electron that npm installed, so build on an Apple-chip Mac for macos-arm64. |
| 12 | * |
| 13 | * A release is built from one commit, the one its tag points at: the working tree must be clean, and the build says |
| 14 | * so when HEAD has no version tag yet. The .json records the file's name, size, SHA-256, the commit and the tag. |
| 15 | * |
| 16 | * The app is signed ad hoc only (no Apple Developer ID), so macOS warns the first time it is opened. |
| 17 | * The zip is made with ditto, which keeps the bundle's symlinks and permissions; other zip tools can break the app. |
| 18 | */ |
| 19 | import { execFileSync } from "node:child_process"; |
| 20 | import { createHash } from "node:crypto"; |
| 21 | import { cpSync, createReadStream, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 22 | import { createRequire } from "node:module"; |
| 23 | import { dirname, join } from "node:path"; |
| 24 | import { fileURLToPath } from "node:url"; |
| 25 | |
| 26 | if (process.platform !== "darwin") { |
| 27 | console.error("build-mac: run this on a Mac (it uses ditto, codesign and the Mac Electron.app)"); |
| 28 | process.exit(2); |
| 29 | } |
| 30 | |
| 31 | const root = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 32 | const NAME = "dbbasic-browser"; |
| 33 | const BUNDLE_ID = "com.askrobots.dbbasic-browser"; |
| 34 | const allowDirty = process.argv.includes("--allow-dirty"); |
| 35 | const run = (cmd, args, opts = {}) => (execFileSync(cmd, args, { cwd: root, encoding: "utf8", ...opts }) ?? "").trim(); |
| 36 | |
| 37 | // ---- which commit, which version ---- |
| 38 | const commit = run("git", ["rev-parse", "HEAD"]); |
| 39 | const dirty = run("git", ["status", "--porcelain"]) !== ""; |
| 40 | if (dirty && !allowDirty) { |
| 41 | console.error("build-mac: the working tree has uncommitted changes. A release is built from one commit: commit first,\n" + |
| 42 | "or pass --allow-dirty for a trial build (its name says so)."); |
| 43 | process.exit(1); |
| 44 | } |
| 45 | const { displayVersion } = await import(join(root, "src", "version.ts")); |
| 46 | const semver = JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version; |
| 47 | const shown = displayVersion(semver); |
| 48 | const tags = run("git", ["tag", "--points-at", "HEAD"]).split("\n").filter(Boolean); |
| 49 | const tagged = tags.includes(shown); |
| 50 | |
| 51 | // ---- the Electron.app to start from ---- |
| 52 | const electronBin = createRequire(import.meta.url)("electron"); |
| 53 | const srcApp = join(dirname(electronBin), "..", ".."); |
| 54 | const electronVersion = readFileSync(join(srcApp, "..", "version"), "utf8").trim(); |
| 55 | const archs = run("lipo", ["-archs", electronBin]).split(/\s+/); |
| 56 | const arch = archs.length === 1 && archs[0] === "arm64" ? "arm64" : archs.length === 1 && archs[0] === "x86_64" ? "x64" : null; |
| 57 | if (!arch) { |
| 58 | console.error(`build-mac: expected an arm64 or x86_64 Electron, found: ${archs.join(" ")}`); |
| 59 | process.exit(1); |
| 60 | } |
| 61 | |
| 62 | // ---- build the app's files ---- |
| 63 | run("npm", ["run", "build:standalone"], { stdio: "inherit" }); |
| 64 | |
| 65 | const out = join(root, "release"); |
| 66 | const appDir = join(out, `${NAME}.app`); |
| 67 | rmSync(out, { recursive: true, force: true }); |
| 68 | mkdirSync(out, { recursive: true }); |
| 69 | try { |
| 70 | run("cp", ["-cR", srcApp, appDir]); // an APFS clone where possible: fast, no extra disk |
| 71 | } catch { |
| 72 | cpSync(srcApp, appDir, { recursive: true, verbatimSymlinks: true }); |
| 73 | } |
| 74 | const res = join(appDir, "Contents", "Resources"); |
| 75 | rmSync(join(res, "default_app.asar"), { force: true }); // Electron's sample app; ours goes in Resources/app |
| 76 | cpSync(join(root, "standalone"), join(res, "app"), { recursive: true }); |
| 77 | cpSync(join(root, "assets", "icon.icns"), join(res, "electron.icns")); // CFBundleIconFile names this file |
| 78 | |
| 79 | const plist = join(appDir, "Contents", "Info.plist"); |
| 80 | const pb = (cmd) => execFileSync("/usr/libexec/PlistBuddy", ["-c", cmd, plist], { stdio: ["ignore", "ignore", "pipe"] }); |
| 81 | const setKey = (key, value) => { |
| 82 | try { pb(`Delete :${key}`); } catch { /* not there yet */ } |
| 83 | pb(`Add :${key} string ${value}`); |
| 84 | }; |
| 85 | setKey("CFBundleName", NAME); |
| 86 | setKey("CFBundleDisplayName", NAME); |
| 87 | setKey("CFBundleIdentifier", BUNDLE_ID); |
| 88 | setKey("CFBundleShortVersionString", semver.replace(/-.*/, "")); // macOS wants plain numbers here |
| 89 | setKey("CFBundleVersion", semver.replace(/-.*/, "")); |
| 90 | setKey("LSApplicationCategoryType", "public.app-category.productivity"); |
| 91 | setKey("DBBasicCommit", commit); |
| 92 | try { pb("Delete :ElectronAsarIntegrity"); } catch { /* the sample app's hash; there is no asar now */ } |
| 93 | |
| 94 | // Changing the bundle breaks Electron's signature, and an Apple-chip Mac won't run unsigned code: sign it ad hoc. |
| 95 | run("codesign", ["--force", "--deep", "--sign", "-", appDir], { stdio: "ignore" }); |
| 96 | run("codesign", ["--verify", "--deep", "--strict", appDir]); |
| 97 | |
| 98 | // ---- the zip, and what it is ---- |
| 99 | const suffix = dirty ? `-dev-${commit.slice(0, 7)}` : ""; |
| 100 | const zipName = `${NAME}-${shown}${suffix}-macos-${arch}.zip`; |
| 101 | const zip = join(out, zipName); |
| 102 | run("ditto", ["-c", "-k", "--sequesterRsrc", "--keepParent", appDir, zip]); |
| 103 | const sha256 = await new Promise((resolve, reject) => { |
| 104 | const h = createHash("sha256"); |
| 105 | createReadStream(zip).on("data", (d) => h.update(d)).on("end", () => resolve(h.digest("hex"))).on("error", reject); |
| 106 | }); |
| 107 | const size = statSync(zip).size; |
| 108 | const info = { file: zipName, platform: `macos-${arch}`, size, sha256, commit, tag: tagged ? shown : null, version: shown, electron: electronVersion, dirty }; |
| 109 | writeFileSync(join(out, `${zipName}.json`), JSON.stringify(info, null, 2) + "\n"); |
| 110 | |
| 111 | console.log(`\n${zip}`); |
| 112 | console.log(` ${(size / 1024 / 1024).toFixed(1)} MB (${size} bytes)`); |
| 113 | console.log(` sha256 ${sha256}`); |
| 114 | console.log(` commit ${commit}${dirty ? " plus uncommitted changes (trial build, not for upload)" : ""}`); |
| 115 | if (!dirty && !tagged) console.log(` HEAD has no ${shown} tag yet: tag this commit before uploading the zip to its release.`); |
| 116 | if (existsSync(appDir)) console.log(` app: ${appDir}`); |