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 blockedBuilds(); /// Turns the verified [archive] into the installed app. Does not restart. Future apply(File archive, UpdateRelease release, {required int runningBuild}); /// Starts the app again. The caller ends this process afterwards when /// [exitsOnRestart] says so. Future relaunch(List 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 noteStart(int build); Future markHealthy(); } typedef ProcessRunner = Future Function(String exe, List args); typedef ProcessStarter = Future Function(String exe, List args); Future defaultRunner(String exe, List args) => Process.run(exe, args); Future defaultStarter(String exe, List 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-`, a translocated or read only mac bundle: all "none", and the /// updater then leaves the app completly alone. InstallDetection detectInstall({ String? executable, Map? 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"; }