pruneCaches function
Removes helper binaries of other glint versions from ~/.glint/bin, runner builds of Xcode versions that are gone and runner builds beyond the newest three from ~/.glint/xctest, at most once a day unless force; nothing a live process runs, or any helper or build used in the last kPruneGrace, is touched.
Implementation
Future<PruneReport> pruneCaches({bool dryRun = false, bool force = false, String? home, String? version}) async {
final root = '${home ?? Platform.environment['HOME'] ?? '.'}/.glint';
final stamp = File('$root/.pruned');
if (!force && !dryRun && stamp.existsSync()) {
final age = DateTime.now().difference(stamp.lastModifiedSync());
if (age < kPruneEvery) return const PruneReport([], dryRun: false);
}
final table = await ProcessTable.read();
final running = table.rows.map((r) => r.command).toList();
bool inUse(String path) => running.any((c) => c.contains(path));
final goneBuilds = await _goneRunnerBuilds('$root/xctest', running);
final items = [
..._oldHelpers('$root/bin', version ?? glintVersion, inUse),
...goneBuilds,
..._staleRunnerBuilds('$root/xctest', running, skip: {for (final g in goneBuilds) g.path}),
];
if (!dryRun) {
for (final item in items) {
try {
final type = FileSystemEntity.typeSync(item.path);
if (type == FileSystemEntityType.directory) {
Directory(item.path).deleteSync(recursive: true);
} else if (type != FileSystemEntityType.notFound) {
File(item.path).deleteSync();
}
} on Object {
continue;
}
}
try {
Directory(root).createSync(recursive: true);
stamp.writeAsStringSync('${DateTime.now().toIso8601String()}\n');
} on Object {
return PruneReport(items, dryRun: dryRun);
}
}
return PruneReport(items, dryRun: dryRun);
}