feat(garage_updates): an auto updater for garage apps

Asks Garage for the newest release of a project, downloads it with resume,
checks the sha256, installs it beside the old one and restarts into it.
Splash on garage_ui, a native stub and a first time installer for windows,
bundle swap on mac, the system installer on android. No-op on web.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
This commit is contained in:
ImBenji
2026-10-12 06:37:02 +01:00
co-authored by Claude Opus 5.5
parent 444933b004
commit d3b232f0ee
56 changed files with 9031 additions and 0 deletions
@@ -0,0 +1,164 @@
import "dart:io";
import "package:garage_updates/core.dart";
// A pretend windows install in a temp folder. The "stub" is a shell script
// with the stubs name: it writes down the arguments it was given, and for
// --garage-apply does the bookkeeping the real one would.
const _stubScript = r'''#!/bin/sh
dir="$(cd "$(dirname "$0")" && pwd)"
printf '%s\n' "$@" >> "$dir/stub-args.txt"
echo "--" >> "$dir/stub-args.txt"
if [ "$1" = "--garage-apply" ]; then
if [ -f "$dir/stub-exit" ]; then
exit "$(cat "$dir/stub-exit")"
fi
if [ ! -f "$dir/stub-lazy" ]; then
mkdir -p "$dir/app-$3"
cp "$2" "$dir/app-$3/unpacked-from.zip"
printf '{ "exe": "garage_stash.exe", "current": %s, "previous": 12, "failed_starts": 0, "blocked": [] }' "$3" > "$dir/garage_updates.json"
fi
fi
exit 0
''';
class FakeWindowsInstall {
FakeWindowsInstall._(this.tmp);
final Directory tmp;
Directory get root => Directory("${tmp.path}/Garage Stash");
Directory get versionFolder => Directory("${root.path}/app-12");
File get stub => File("${root.path}/garage_stash.exe");
File get exe => File("${versionFolder.path}/garage_stash.exe");
File get stateFile => File("${root.path}/garage_updates.json");
File get argsFile => File("${root.path}/stub-args.txt");
static FakeWindowsInstall create({String? state}) {
final f = FakeWindowsInstall._(Directory.systemTemp.createTempSync("gu_win_"));
f.versionFolder.createSync(recursive: true);
f.exe.writeAsStringSync("the real app, build 12");
File("${f.versionFolder.path}/stash-agent.exe").writeAsStringSync("the agent");
f.stub.writeAsStringSync(_stubScript);
Process.runSync("/bin/chmod", ["+x", f.stub.path]);
f.stateFile.writeAsStringSync(
state ?? '{ "exe": "garage_stash.exe", "current": 12, "previous": 9, "failed_starts": 1, "blocked": [11] }',
);
return f;
}
/// what LOCALAPPDATA is for an installer made by [installer]
Directory get local => Directory("${tmp.path}/Local");
File get userStateFile => File("${local.path}/Garage Stash/garage_updates.json");
/// `readOnly` is an install in Program Files. Nothing is chmodded, the
/// installer is just told it cant write, and [plainLaunch] behaves the same
WindowsInstaller installer({bool readOnly = false}) => WindowsInstaller(
root: root,
stub: stub,
runningFolderBuild: 12,
environment: {"LOCALAPPDATA": local.path},
rootWritable: () => !readOnly,
);
/// The stub being started with no flags, as far as the counting goes: the
/// same sums garage_stub.cpp does, in root when it can write there and in
/// the users own file when it cant. Returns the build it would start.
int plainLaunch({bool readOnly = false}) {
final state = StateFile(stateFile);
final s = state.read()!;
bool there(int? build) => build != null && Directory("${root.path}/app-$build").existsSync();
if (!readOnly) {
if (s.failedStarts >= 3 && there(s.previous)) {
if (!s.blocked.contains(s.current)) s.blocked.add(s.current!);
s.current = s.previous;
s.previous = null;
s.failedStarts = 0;
}
s.failedStarts++;
state.write(s);
return s.current!;
}
final mine = UserStateFile(userStateFile);
final u = mine.read() ?? UserState();
u.settleFor(s.current);
if (u.runInstead != null) {
if (u.runInstead != s.current && there(u.runInstead)) return u.runInstead!;
u.runInstead = null;
u.failedStarts = 0;
}
var run = s.current!;
if (u.failedStarts >= 3 && there(s.previous) && s.previous != s.current && !u.blocked.contains(s.previous)) {
if (!u.blocked.contains(s.current)) u.blocked.add(s.current!);
u.runInstead = s.previous;
u.failedStarts = 0;
run = s.previous!;
}
if (run == s.current) u.failedStarts++;
mine.write(u);
return run;
}
/// every run of the stub, as the list of arguments it got
List<List<String>> get runs {
if (!argsFile.existsSync()) return [];
final out = <List<String>>[];
var cur = <String>[];
for (final line in argsFile.readAsLinesSync()) {
if (line == "--") {
out.add(cur);
cur = <String>[];
} else {
cur.add(line);
}
}
return out;
}
void dispose() {
// a test may have made the root read only
Process.runSync("/bin/chmod", ["-R", "u+w", tmp.path]);
tmp.deleteSync(recursive: true);
}
}
/// A pretend mac app. `Foo.app/Contents/{Info.plist, MacOS/Foo}` with a
/// marker file so a test can tell which build is sitting where.
Directory makeApp(Directory parent, {String name = "Foo.app", String id = "net.imbenji.foo", required String marker}) {
final app = Directory("${parent.path}/$name");
Directory("${app.path}/Contents/MacOS").createSync(recursive: true);
Directory("${app.path}/Contents/Resources").createSync(recursive: true);
File("${app.path}/Contents/Info.plist").writeAsStringSync('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>Foo</string>
<key>CFBundleIdentifier</key>
<string>$id</string>
</dict>
</plist>
''');
File("${app.path}/Contents/MacOS/Foo").writeAsStringSync("#!/bin/sh\necho $marker\n");
File("${app.path}/Contents/Resources/marker.txt").writeAsStringSync(marker);
return app;
}
String markerOf(Directory app) => File("${app.path}/Contents/Resources/marker.txt").readAsStringSync();
/// zips [app] the way a release would be, with the .app at the top
File zipApp(Directory app, File to) {
final res = Process.runSync("/usr/bin/ditto", ["-c", "-k", "--keepParent", app.path, to.path]);
if (res.exitCode != 0) throw StateError("ditto couldnt zip ${app.path}: ${res.stderr}");
return to;
}