codeburn/app/scripts/stage-menubar.mjs
iamtoruk d59802c1ec feat(app): ship the tray app with the Windows desktop builds
stage-menubar.mjs is stage-cli.mjs for the Tauri tray app: it copies rather than
downloads, taking the local `cargo tauri build` output by default and whatever
CI fetched with --from. The two routes need different things staged, so it has
two shapes. The NSIS build gets the .msi and its .sha256, computing the digest
when a local build has none, so the installer's verification means the same
thing there as it does for a release. The Store build gets the executable, since
a packaged app cannot run msiexec at all.

build/menubar rides into both packages as win.extraResources. Unlike the CLI it
carries no node_modules, so electron-builder's production-dependency filter has
nothing to strip and the afterPack hook is not needed.

installer.nsh removes the tray app on uninstall only when the marker credits
this app with installing it, which is what keeps a manual `codeburn menubar`
install alive through a desktop uninstall. PowerShell reads the marker and pulls
the product code out of the uninstall string, because NSIS has no JSON and the
alternative was hand-parsing one in a macro.

appx-extensions.xml declares the tray app's launch at login as the package's own
windows.startupTask, so it appears in Settings > Apps > Startup like anything
else the Store installed. No Run value is written on that route.
2026-09-02 04:48:03 -07:00

113 lines
4.8 KiB
JavaScript

// Stage the Windows tray app into app/build/menubar so electron-builder ships it
// as extraResources. The counterpart of stage-cli.mjs, and it copies rather than
// downloads for the same reason: CI fetches the release assets in its own step
// and passes them with --from, and a developer points at a local build.
//
// Two layouts, because the two routes need different things:
//
// --target nsis (default)
// build/menubar/CodeBurn.Menubar_<version>_x64_en-US.msi
// build/menubar/CodeBurn.Menubar_<version>_x64_en-US.msi.sha256
//
// app/electron/menubar.ts hands that path to `codeburn menubar`, whose
// Windows installer verifies the digest before msiexec sees the file. The
// digest is copied when the source has one and computed when it does not, so
// a local `cargo tauri build` stages the same shape a release does.
//
// --target appx
// build/menubar/codeburn-menubar.exe (plus anything beside it the app needs)
//
// A packaged app cannot run msiexec, so the Store build launches the exe out
// of its own package and the manifest's startup task covers launch at login.
//
// Sources, in order: --from <dir>, then the local Tauri build output.
import { createHash } from 'node:crypto'
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs'
import { basename, dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url)) // app/scripts
const appDir = join(here, '..')
const root = join(appDir, '..')
const stage = join(appDir, 'build', 'menubar')
const MSI_PATTERN = /^CodeBurn\.Menubar_(.+)_x64_en-US\.msi$/
const TRAY_EXE = 'codeburn-menubar.exe'
/// What the Tauri build puts beside the exe and the app needs at runtime. Anything absent is
/// simply not copied: WebView2Loader ships only when the loader is not linked statically.
const TRAY_SIDECARS = ['WebView2Loader.dll']
function option(name, fallback) {
const index = process.argv.indexOf(name)
if (index === -1) return fallback
const value = process.argv[index + 1]
if (!value) throw new Error(`${name} requires a value`)
return value
}
const target = option('--target', 'nsis')
if (target !== 'nsis' && target !== 'appx') {
throw new Error(`stage-menubar: --target must be nsis or appx, not ${target}`)
}
const defaultSource = target === 'nsis'
? join(root, 'windows', 'src-tauri', 'target', 'release', 'bundle', 'msi')
: join(root, 'windows', 'src-tauri', 'target', 'release')
const source = option('--from', defaultSource)
function fail(message) {
throw new Error(
`stage-menubar: ${message}\n` +
` looked in ${source}\n` +
' build it with `cargo tauri build` in windows/, or pass --from <dir> with the\n' +
' downloaded windows-v* release assets.'
)
}
// The stage is rebuilt from scratch every time: a leftover .msi from an older version would
// otherwise ship beside the current one and be found first.
rmSync(stage, { recursive: true, force: true })
mkdirSync(stage, { recursive: true })
if (!existsSync(source) || !statSync(source).isDirectory()) fail('no source directory')
if (target === 'nsis') {
const msiName = readdirSync(source).find(name => MSI_PATTERN.test(name))
if (!msiName) fail('no CodeBurn.Menubar_<version>_x64_en-US.msi')
const msiSource = join(source, msiName)
copyFileSync(msiSource, join(stage, msiName))
// The release carries its own .sha256; a local build does not, and computing it here keeps
// the installer's verification meaningful in both cases rather than skipped in one.
const checksumSource = `${msiSource}.sha256`
if (existsSync(checksumSource)) {
copyFileSync(checksumSource, join(stage, `${msiName}.sha256`))
} else {
const digest = createHash('sha256').update(readFileSync(msiSource)).digest('hex')
writeFileSync(join(stage, `${msiName}.sha256`), `${digest} ${msiName}\n`)
}
const version = MSI_PATTERN.exec(msiName)[1]
const appVersion = JSON.parse(readFileSync(join(appDir, 'package.json'), 'utf8')).version
// A mismatch is not fatal: a desktop release can legitimately ship the newest tray build
// there is. It is worth saying out loud, because it is usually a stale local bundle.
if (version !== appVersion) {
console.warn(`stage-menubar: staging tray app ${version} into desktop app ${appVersion}`)
}
console.log(`stage-menubar: staged ${msiName} -> ${stage}`)
} else {
const exeSource = join(source, TRAY_EXE)
if (!existsSync(exeSource)) fail(`no ${TRAY_EXE}`)
copyFileSync(exeSource, join(stage, TRAY_EXE))
const copied = [basename(exeSource)]
for (const sidecar of TRAY_SIDECARS) {
const path = join(source, sidecar)
if (!existsSync(path)) continue
copyFileSync(path, join(stage, sidecar))
copied.push(sidecar)
}
console.log(`stage-menubar: staged ${copied.join(', ')} -> ${stage}`)
}