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,99 @@
import "dart:io";
import "mac_installer.dart";
import "models.dart";
import "windows_installer.dart";
/// The part that differs per platform: where downloads go, how a verified
/// archive becomes the installed app, and how the app is started again.
abstract class Installer {
/// "windows" | "macos" | "android", what the check is asked for
String get platform;
/// where the `.part` file and the finished download live
Directory get staging;
/// builds that failed to start here and must not be offered again
List<int> blockedBuilds();
/// Turns the verified [archive] into the installed app. Does not restart.
Future<void> apply(File archive, UpdateRelease release, {required int runningBuild});
/// Starts the app again. The caller ends this process afterwards when
/// [exitsOnRestart] says so.
Future<void> relaunch(List<String> args);
/// false on android, where the system installer does the restarting
bool get exitsOnRestart;
/// Counts a start of [build]. true = the previous build was just put back
/// because this one kept dying, relaunch.
Future<bool> noteStart(int build);
Future<void> markHealthy();
}
typedef ProcessRunner = Future<ProcessResult> Function(String exe, List<String> args);
typedef ProcessStarter = Future<void> Function(String exe, List<String> args);
Future<ProcessResult> defaultRunner(String exe, List<String> args) => Process.run(exe, args);
Future<void> defaultStarter(String exe, List<String> args) async {
// detached, it has to outlive us
await Process.start(exe, args, mode: ProcessStartMode.detached);
}
/// What [detectInstall] found. Exactly one of the two is set.
class InstallDetection {
const InstallDetection.found(Installer this.installer) : reason = null;
const InstallDetection.none(String this.reason) : installer = null;
final Installer? installer;
/// why this copy of the app isnt one we can update
final String? reason;
}
/// Works out whether this process is running from somewhere the updater put
/// it. A dev build, `flutter run`, `dart run`, a folder that isnt
/// `app-<build>`, a translocated or read only mac bundle: all "none", and the
/// updater then leaves the app completly alone.
InstallDetection detectInstall({
String? executable,
Map<String, String>? environment,
String? platform,
bool? product,
}) {
// debug + profile builds and anything run off the jit. a release build is
// the only thing that should ever get swapped
product ??= const bool.fromEnvironment("dart.vm.product");
if (!product) return const InstallDetection.none("this isn't a release build");
final exe = executable ?? Platform.resolvedExecutable;
final env = environment ?? Platform.environment;
switch (platform ?? Platform.operatingSystem) {
case "windows":
return WindowsInstaller.locate(exe, environment: env);
case "macos":
return MacInstaller.locate(exe, environment: env);
default:
return InstallDetection.none("no installer for ${platform ?? Platform.operatingSystem} here");
}
}
/// last bit of a path, whichever way the slashes lean
String baseName(String path) {
var p = path;
while (p.length > 1 && (p.endsWith("/") || p.endsWith("\\"))) {
p = p.substring(0, p.length - 1);
}
final cut = p.lastIndexOf(RegExp(r"[/\\]"));
return cut < 0 ? p : p.substring(cut + 1);
}
String joinPath(String a, String b, [String? c]) {
final sep = Platform.pathSeparator;
final ab = a.endsWith(sep) ? "$a$b" : "$a$sep$b";
return c == null ? ab : "$ab$sep$c";
}