Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Read the app folders within a budget, each on a thread of its own
Țugui Dragoș185ba3a2e884
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swiftindex 397f8697..535f0753 100644--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift@@ -90,24 +90,48 @@ public enum AppCatalog {@concurrentpublic static func scan(in directories: [URL]) async -> AppScan {- let walks = directories.map { appBundles(in: $0) }- let bundles = Set(walks.flatMap(\.bundles))+ await scan(in: directories, within: FileSize.budget) { appBundles(in: $0, unless: $1) }+ }++ /// Walks one folder for `scan(in:)`, until `isGivenUp` says nobody waits anymore. A parameter so a test can stand+ /// in for a folder that never answers.+ typealias Walk = @Sendable (_ folder: URL, _ isGivenUp: @escaping @Sendable () -> Bool) -> (+ bundles: [URL], unreadable: [URL]+ )++ /// Each folder is walked on a thread of its own and waited for within `budget`: a folder on a disk that sleeps, or+ /// one a file provider holds, can keep a directory read in the kernel for minutes. One that doesn't answer is+ /// among what could not be read, and the others are listed meanwhile.+ @concurrent+ static func scan(+ in directories: [URL], within budget: TimeInterval, walking walk: @escaping Walk+ ) async -> AppScan {+ let walks = await withTaskGroup(of: (index: Int, walk: (bundles: [URL], unreadable: [URL])?).self) { group in+ for (index, directory) in directories.enumerated() {+ group.addTask { (index, await SlowRead.answer(within: budget) { walk(directory, $0) }) }+ }+ return await group.reduce(into: Array(repeating: nil, count: directories.count)) { $0[$1.index] = $1.walk }+ }+ let answered = zip(directories, walks).compactMap { directory, walk in walk.map { (directory, $0) } }+ let unanswered = zip(directories, walks).filter { $0.1 == nil }.map(\.0)+ let bundles = Set(answered.flatMap(\.1.bundles))let read = await withTaskGroup(of: (bundle: URL, app: InstalledApp?).self) { group infor bundle in bundles {- group.addTask { (bundle, Reading.of(bundle)) }+ group.addTask { (bundle, Task.isCancelled ? nil : Reading.of(bundle)) }}return await group.reduce(into: [(bundle: URL, app: InstalledApp?)]()) { $0.append($1) }}+ guard !Task.isCancelled else { return AppScan(apps: [], unreadable: [], needsFullDiskAccess: false) }- Reading.forget(inside: directories, except: bundles)+ Reading.forget(inside: answered.map(\.0), except: bundles)let closedApps = read.filter { $0.app == nil && AppInspector.isClosed($0.bundle) }.map(\.bundle)- let unreadable = (walks.flatMap(\.unreadable) + closedApps)- .sorted { $0.path(percentEncoded: false) < $1.path(percentEncoded: false) }+ let refused = answered.flatMap(\.1.unreadable) + closedAppsreturn AppScan(apps: sorted(read.compactMap(\.app)),- unreadable: unreadable,- needsFullDiskAccess: unreadable.contains { FullDiskAccess.canList($0) == .missing }+ unreadable: (refused + unanswered)+ .sorted { $0.path(percentEncoded: false) < $1.path(percentEncoded: false) },+ needsFullDiskAccess: refused.contains { FullDiskAccess.canList($0) == .missing })}@@ -193,7 +217,9 @@ public enum AppCatalog {/// The apps in `directory`, three levels deep, and the folders in it that are there and could not be read. A/// folder that is not there, such as an Applications folder nobody made, holds no app.- static func appBundles(in directory: URL, maximumDepth: Int = 3) -> (bundles: [URL], unreadable: [URL]) {+ static func appBundles(+ in directory: URL, maximumDepth: Int = 3, unless isStopped: () -> Bool = { false }+ ) -> (bundles: [URL], unreadable: [URL]) {var unreadable: [URL] = []guard let enumerator = FileManager.default.enumerator(at: directory,@@ -207,6 +233,7 @@ public enum AppCatalog {var bundles: [URL] = []for case let url as URL in enumerator {+ guard !isStopped() else { break }if url.pathExtension == "app" {bundles.append(url.standardizedFileURL)} else if enumerator.level >= maximumDepth {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swiftindex 1860f00b..4c3ef278 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift@@ -1,5 +1,6 @@import Foundation@testable import PeelCore+import Synchronizationimport Testingstruct AppCatalogTests {@@ -19,6 +20,56 @@ struct AppCatalogTests {)}+ @Test func aFolderThatDoesNotAnswerIsNamedAndTheOthersAreListed() async throws {+ let directory = try TemporaryDirectory()+ let answering = try directory.directory("Apps")+ try directory.file(+ "Apps/Tunewell.app/Contents/Info.plist",+ contents: PropertyListSerialization.data(+ fromPropertyList: ["CFBundleIdentifier": "org.example.tunewell", "CFBundleName": "Tunewell"],+ format: .xml, options: 0+ )+ )+ let sleeping = directory.url.appending(path: "Sleeping", directoryHint: .isDirectory)+ let released = DispatchSemaphore(value: 0)+ defer { released.signal() }++ let scan = await AppCatalog.scan(in: [answering, sleeping], within: 0.2) { folder, isGivenUp in+ guard folder == sleeping else { return AppCatalog.appBundles(in: folder, unless: isGivenUp) }+ _ = released.wait(timeout: .now() + 30)+ return ([], [])+ }++ #expect(scan.apps.map(\.name) == ["Tunewell"])+ #expect(scan.unreadable == [sleeping])+ #expect(!scan.needsFullDiskAccess)+ }++ @Test func aReadingStoppedWhileAFolderDoesNotAnswerReturnsAtOnce() async throws {+ let sleeping = URL(filePath: "/nowhere/Sleeping", directoryHint: .isDirectory)+ let asked = Mutex(false)+ let released = DispatchSemaphore(value: 0)+ defer { released.signal() }+ let clock = ContinuousClock()+ let reading = Task(priority: .userInitiated) {+ _ = await AppCatalog.scan(in: [sleeping], within: 3_600) { _, _ in+ asked.withLock { $0 = true }+ _ = released.wait(timeout: .now() + 30)+ return ([], [])+ }+ return clock.now+ }+ while !asked.withLock({ $0 }) {+ try await Task.sleep(for: .milliseconds(2))+ }++ let stopped = clock.now+ reading.cancel()+ let returned = await reading.value++ #expect(returned - stopped < .seconds(1))+ }+/// Opening an app changes when it was last opened and nothing about what is installed, so the pages that scan/// again whenever the apps change are not told they did.@Test func anAppOpenedAgainListsTheSameApps() {diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swiftindex 3fc85ee0..ec16dbde 100644--- a/Peel/Model/AppLibrary.swift+++ b/Peel/Model/AppLibrary.swift@@ -223,8 +223,8 @@ final class AppLibrary {}}- /// Calls `refresh()` unless the folders were read in the last 30 seconds. For Peel coming forward:- /// the folder watcher sees every real change, so a recent read is enough.+ /// Calls `refresh()` unless a reading of the folders started in the last 30 seconds, even one still running. For+ /// Peel coming forward: the folder watcher sees every real change, so a recent read is enough.func refreshUnlessRecent() async -> [InstalledApp] {if let lastRead, ContinuousClock.now - lastRead < .seconds(30) { return [] }return await refresh()@@ -257,8 +257,8 @@ final class AppLibrary {/// when it was last checked) is dropped rather than shown for a version it doesn't describe.@discardableResultfunc refresh() async -> [InstalledApp] {- guard let scan = await scanRun.run({ await AppCatalog.scan() }) else { return [] }lastRead = .now+ guard let scan = await scanRun.run({ await AppCatalog.scan() }) else { return [] }keepUnreadable(of: scan)let found = scan.appslet listed = AppCatalog.sorted(found + stillThere(revealed, beside: found))