Install the update itself, instead of asking for a drag

Build 115. Downloading and then telling the operator to quit, mount and drag
was the friction the updater was supposed to remove.

An app cannot replace itself while running, so the swap is done by a small
script that outlives the process: it waits for us to exit, moves the old bundle
aside, copies the new one in, clears the quarantine flag and relaunches.

Moved aside, not deleted. If the copy fails half way — a full disk, a truncated
download, the volume going away — the old app goes straight back and is
reopened. The failure this is written against is a campus Mac with no working
app and a service in an hour, which is far worse than not updating. Tested in a
sandbox across all three paths: a clean swap, a copy that fails mid-way (old app
restored, no leftovers), and a disk image with no app in it (aborts before
touching the installed one).

It refuses while translation is live: quitting stops every listener's captions
and no update is worth that. Stop translating first.

No sudo — /Applications is admin-writable and the bundle belongs to the
operator. If that is ever untrue the copy fails and the rollback runs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Waters Church
2026-09-24 12:50:05 -04:00
co-authored by Claude Opus 5
parent 6753a94e1c
commit 4aa30699c9
5 changed files with 115 additions and 11 deletions
+91
View File
@@ -2259,6 +2259,97 @@ ipcMain.handle('update:download', async (_e, url, name) => {
});
});
/*
* ─────────────────────────── installing it, in place ──────────────────────
*
* An app cannot replace itself while it is running, so the swap is done by a
* small script that outlives us: it waits for this process to exit, moves the
* old bundle aside, copies the new one in, and relaunches.
*
* MOVED ASIDE, NOT DELETED. If the copy fails half way — a full disk, a bad
* download, the volume going away — the old app is put straight back and
* relaunched. The failure mode this is written against is a campus Mac with no
* working app and a service in an hour, which is far worse than not updating.
*
* No sudo: /Applications is admin-writable on macOS and the bundle belongs to
* the operator, so nothing here needs a password. If that is ever not true the
* copy fails, the old app comes back, and the log says so.
*/
ipcMain.handle('update:install', async (_e, dmgPath) => {
const dmg = String(dmgPath || '');
if (!dmg || !fs.existsSync(dmg)) return { ok: false, error: 'That installer is no longer on disk.' };
/* Never mid-service. Quitting the app stops every listener's captions, and an
update is never worth that — the operator stops translation first. */
if (engineIsLive) {
return { ok: false, error: 'Translation is live. Press Stop translating first, then install.' };
}
const appPath = app.getPath('exe').replace(/\/Contents\/MacOS\/[^/]+$/, '');
if (!/\.app$/.test(appPath)) {
return { ok: false, error: 'This copy is not running from an .app bundle, so it cannot replace itself.' };
}
const tmp = path.join(require('os').tmpdir(), 'ct-install-' + process.pid + '.sh');
const mount = '/Volumes/ChurchTranslateUpdate';
const aside = appPath + '.replacing-' + process.pid;
const script = [
'#!/bin/bash',
'# Written by Church Translate to swap itself. Safe to delete.',
'set -u',
'DMG=' + JSON.stringify(dmg),
'APP=' + JSON.stringify(appPath),
'ASIDE=' + JSON.stringify(aside),
'MNT=' + JSON.stringify(mount),
'PID=' + process.pid,
'LOG="$HOME/Library/Application Support/Church Translate/data/logs/install.log"',
'mkdir -p "$(dirname "$LOG")"',
'say(){ echo "$(date "+%Y-%m-%d %H:%M:%S") $*" >> "$LOG"; }',
'say "installing $DMG into $APP"',
'# wait for the app to actually be gone (up to 30s)',
'for i in $(seq 1 60); do kill -0 "$PID" 2>/dev/null || break; sleep 0.5; done',
'if kill -0 "$PID" 2>/dev/null; then say "app did not exit; aborting"; exit 1; fi',
'hdiutil detach "$MNT" -quiet 2>/dev/null',
'if ! hdiutil attach "$DMG" -nobrowse -quiet -mountpoint "$MNT"; then say "could not mount"; open -a "$APP"; exit 1; fi',
'NEW="$MNT/Church Translate.app"',
'# refuse to touch the installed app unless the replacement is really there',
'if [ ! -f "$NEW/Contents/Info.plist" ]; then say "disk image has no app"; hdiutil detach "$MNT" -quiet; open -a "$APP"; exit 1; fi',
'rm -rf "$ASIDE"',
'if ! mv "$APP" "$ASIDE"; then say "could not move the old app aside"; hdiutil detach "$MNT" -quiet; open -a "$APP"; exit 1; fi',
'if cp -R "$NEW" "$APP"; then',
' xattr -dr com.apple.quarantine "$APP" 2>/dev/null',
' say "installed $( /usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$APP/Contents/Info.plist" 2>/dev/null )"',
' rm -rf "$ASIDE"',
'else',
' say "copy failed — putting the old app back"',
' rm -rf "$APP"; mv "$ASIDE" "$APP"',
'fi',
'hdiutil detach "$MNT" -quiet 2>/dev/null',
'open -a "$APP"',
'say "relaunched"',
'rm -f "$0"'
].join('\n');
try {
fs.writeFileSync(tmp, script, { mode: 0o755 });
} catch (e) {
return { ok: false, error: 'Could not prepare the installer: ' + e.message };
}
/* Detached and unref'd, or it dies with us the moment we quit. */
try {
const child = spawn('/bin/bash', [tmp], { detached: true, stdio: 'ignore' });
child.unref();
} catch (e) {
return { ok: false, error: 'Could not start the installer: ' + e.message };
}
/* Give the renderer a moment to show the message before we go. */
forceQuit = true;
setTimeout(() => { try { app.quit(); } catch (_) {} }, 600);
return { ok: true };
});
ipcMain.handle('update:reveal', (_e, p2) => { try { shell.showItemInFolder(String(p2)); return true; } catch (_) { return false; } });
ipcMain.handle('config:get', () => loadConfig());