277c691b2c
The Linux app now downloads the release archive to a temporary folder, verifies its checksum, extracts it beside the install folder, swaps the bundle in with two renames (keeping <install>.previous for rollback) and relaunches. A read-only install location is reported with how to fix it. The prompt and Settings button say "Install and restart".
117 lines
4.0 KiB
Dart
117 lines
4.0 KiB
Dart
import 'dart:io';
|
|
|
|
/// Installs a downloaded Linux release archive over the running app.
|
|
///
|
|
/// The archive holds one `pipelines-linux-x64/` bundle (executable `pipelines-app`,
|
|
/// `data/`, `lib/`). It is extracted beside the install folder, checked, and
|
|
/// swapped in with two renames; the previous bundle is kept as
|
|
/// `<install>.previous` until the next update so a failed swap rolls back.
|
|
/// The running process keeps its already-open files, so it can relaunch the
|
|
/// new executable and exit.
|
|
class LinuxSelfUpdate {
|
|
LinuxSelfUpdate({
|
|
required this.installDir,
|
|
this.executableName = 'pipelines-app',
|
|
Future<ProcessResult> Function(String, List<String>)? run,
|
|
}) : _run = run ?? ((exe, args) => Process.run(exe, args));
|
|
|
|
/// The folder holding the running executable.
|
|
final Directory installDir;
|
|
final String executableName;
|
|
final Future<ProcessResult> Function(String, List<String>) _run;
|
|
|
|
/// The install folder of this process.
|
|
static LinuxSelfUpdate current() =>
|
|
LinuxSelfUpdate(installDir: File(Platform.resolvedExecutable).parent);
|
|
|
|
/// Why this install cannot update itself, or null when it can.
|
|
Future<String?> problem() async {
|
|
final parent = installDir.parent;
|
|
try {
|
|
final probe = File(
|
|
'${parent.path}/.pipelines-update-probe-${DateTime.now().microsecondsSinceEpoch}',
|
|
);
|
|
await probe.writeAsString('');
|
|
await probe.delete();
|
|
} on FileSystemException {
|
|
return 'Pipelines is installed in ${installDir.path}, which this user '
|
|
'cannot change. Reinstall it in your home folder to update it from '
|
|
'the app.';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Extracts [archive] and swaps it in; returns the new executable path.
|
|
Future<String> install(File archive, {required int versionCode}) async {
|
|
final reason = await problem();
|
|
if (reason != null) throw FileSystemException(reason, installDir.path);
|
|
final parent = installDir.parent.path;
|
|
final name = installDir.uri.pathSegments.where((s) => s.isNotEmpty).last;
|
|
final staging = Directory('$parent/.$name-update-$versionCode');
|
|
if (await staging.exists()) await staging.delete(recursive: true);
|
|
await staging.create();
|
|
try {
|
|
final result = await _run('tar', [
|
|
'-xzf',
|
|
archive.path,
|
|
'-C',
|
|
staging.path,
|
|
]);
|
|
if (result.exitCode != 0) {
|
|
throw FileSystemException(
|
|
'The update archive could not be extracted: ${'${result.stderr}'.trim()}',
|
|
archive.path,
|
|
);
|
|
}
|
|
final bundle = await _bundleIn(staging);
|
|
final previous = Directory('$parent/$name.previous');
|
|
if (await previous.exists()) await previous.delete(recursive: true);
|
|
await installDir.rename(previous.path);
|
|
try {
|
|
await bundle.rename(installDir.path);
|
|
} catch (_) {
|
|
await previous.rename(installDir.path);
|
|
rethrow;
|
|
}
|
|
return '${installDir.path}/$executableName';
|
|
} finally {
|
|
if (await staging.exists()) await staging.delete(recursive: true);
|
|
}
|
|
}
|
|
|
|
/// The extracted bundle: the folder holding the executable and its data.
|
|
Future<Directory> _bundleIn(Directory staging) async {
|
|
final candidates = [
|
|
staging,
|
|
...await staging
|
|
.list()
|
|
.where((e) => e is Directory)
|
|
.cast<Directory>()
|
|
.toList(),
|
|
];
|
|
for (final dir in candidates) {
|
|
final exe = File('${dir.path}/$executableName');
|
|
if (await exe.exists() &&
|
|
await Directory('${dir.path}/data/flutter_assets').exists() &&
|
|
await Directory('${dir.path}/lib').exists()) {
|
|
return dir;
|
|
}
|
|
}
|
|
throw FileSystemException(
|
|
'The update archive has no complete $executableName bundle.',
|
|
staging.path,
|
|
);
|
|
}
|
|
|
|
/// Starts [executable] on its own and ends this process.
|
|
static Future<Never> relaunch(String executable) async {
|
|
await Process.start(
|
|
executable,
|
|
const [],
|
|
mode: ProcessStartMode.detached,
|
|
workingDirectory: File(executable).parent.path,
|
|
);
|
|
exit(0);
|
|
}
|
|
}
|