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 list of app folders away from the main thread
Țugui Dragoșad4fb11cb187
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swiftindex 52b66d45..deb2b3c8 100644--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift@@ -9,17 +9,21 @@ public struct AppScan: Sendable, Equatable {public let unreadable: [URL]/// Whether Full Disk Access would open what could not be read.public let needsFullDiskAccess: Bool- /// Whether the list of folders the person chose is there and could not be read, so the apps in those folders are- /// missing from `apps` too. Settings > General > App Folders starts it over.- public let listOfFoldersUnreadable: Bool+ /// The folders the person chose that the reading looked in, beside the Applications folders, or nil when their+ /// list is there and could not be read.+ public let chosenFolders: [URL]?- init(apps: [InstalledApp], unreadable: [URL], needsFullDiskAccess: Bool, listOfFoldersUnreadable: Bool = false) {+ init(apps: [InstalledApp], unreadable: [URL], needsFullDiskAccess: Bool, chosenFolders: [URL]? = []) {self.apps = appsself.unreadable = unreadableself.needsFullDiskAccess = needsFullDiskAccess- self.listOfFoldersUnreadable = listOfFoldersUnreadable+ self.chosenFolders = chosenFolders}+ /// Whether the list of folders the person chose could not be read, so the apps in those folders are missing from+ /// `apps` too. Settings > General > App Folders starts it over.+ public var listOfFoldersUnreadable: Bool { chosenFolders == nil }+/// Whether every app could be read. Until then nothing can be called orphaned, since the files of an app Peel/// can't see would look so.public var readEverything: Bool { unreadable.isEmpty && !listOfFoldersUnreadable }@@ -86,26 +90,18 @@ public enum AppCatalog {await scan(in: directories).apps}- /// Reads the apps in the Applications folders and in the folders the person chose. When the list of chosen- /// folders cannot be read, the reading says so (`listOfFoldersUnreadable`).+ /// Reads the apps in the Applications folders and in the folders the person chose, and says which of those it+ /// looked in (`chosenFolders`, nil when their list cannot be read).@concurrentpublic static func scan(choices: AppFolders = AppFolders()) async -> AppScan {- await scan(chosen: choices.load())- }-- /// The same reading, from a list of chosen folders already read: nil when it could not be read.- @concurrent- public static func scan(chosen: [URL]?) async -> AppScan {- guard let chosen else {- let standard = await scan(in: standardDirectories)- return AppScan(- apps: standard.apps,- unreadable: standard.unreadable,- needsFullDiskAccess: standard.needsFullDiskAccess,- listOfFoldersUnreadable: true- )- }- return await scan(in: directories(adding: chosen))+ let chosen = choices.load()+ let found = await scan(in: chosen.map(directories(adding:)) ?? standardDirectories)+ return AppScan(+ apps: found.apps,+ unreadable: found.unreadable,+ needsFullDiskAccess: found.needsFullDiskAccess,+ chosenFolders: chosen+ )}@concurrentdiff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swiftindex 4811c5f1..136c84eb 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift@@ -35,7 +35,7 @@ struct CommandLineTests {Output.unreadableNote(for: AppScan(apps: [], unreadable: [studio], needsFullDiskAccess: false))== "Peel couldn't look inside /Volumes/Studio/Apps, so something may be there that isn't listed.")- let listUnread = AppScan(apps: [], unreadable: [], needsFullDiskAccess: false, listOfFoldersUnreadable: true)+ let listUnread = AppScan(apps: [], unreadable: [], needsFullDiskAccess: false, chosenFolders: nil)#expect(Output.unreadableNote(for: listUnread) == "Peel can't read its list of app folders, so the apps in them aren't listed. Start it over in Settings > General > App Folders.")}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swiftindex 082b296c..42962cf5 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift@@ -699,7 +699,7 @@ struct FileCommandTests {let said = OrphansCommand.refusal(for: AppScan(apps: [], unreadable: many, needsFullDiskAccess: false))?.description#expect(said?.hasPrefix("Peel couldn't read /Volumes/Old/f1x, /Volumes/Old/f2x, /Volumes/Old/f3x, /Volumes/Old/f4x, and 56 more folders, so") == true)- let list = AppScan(apps: [], unreadable: [], needsFullDiskAccess: false, listOfFoldersUnreadable: true)+ let list = AppScan(apps: [], unreadable: [], needsFullDiskAccess: false, chosenFolders: nil)#expect(OrphansCommand.refusal(for: list)?.description == "Peel couldn't read its list of app folders, so it can't tell which apps are installed. Nothing is listed, since the files of an app it can't see would look orphaned. Start the list over in Settings > General > App Folders.")}diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swiftindex 7aaf2b6e..8707ce67 100644--- a/Peel/Model/AppLibrary.swift+++ b/Peel/Model/AppLibrary.swift@@ -70,6 +70,8 @@ final class AppLibrary {/// The folders the person chose for apps, beside the Applications folders, or nil when their list cannot be read./// `PeelApp` watches them and reads the apps again when they change.private(set) var folders: [URL]?+ /// Counts the changes the person makes to that list, so a reading that began before one never shows its older list.+ @ObservationIgnored private var folderChanges = 0private(set) var couldNotSaveFolders = false/// What the last reading of the folders could not read, so the apps in it are missing from `apps`/// (`AppScan.unreadable`).@@ -234,11 +236,12 @@ final class AppLibrary {if !hasLoaded {releaseNotes = await releaseNotesStore.load()}- let chosen = readFolders()+ let changes = folderChangesaskedForReading = true- let found = await scanRun.run { await AppCatalog.scan(chosen: chosen) }+ let found = await scanRun.run { [appFolders] in await AppCatalog.scan(choices: appFolders) }if !scanRun.isRunning { askedForReading = false }if let found {+ showFolders(of: found, readSince: changes)keepUnreadable(of: found)adopt(found.apps)}@@ -270,6 +273,7 @@ final class AppLibrary {func startFoldersOver() -> StartOver {let answer = appFolders.startOver()if answer == .started { couldNotSaveFolders = false }+ folderChanges += 1folders = appFolders.load()return answer}@@ -278,15 +282,14 @@ final class AppLibrary {private func changeFolders(_ change: (AppFolders) -> Bool) {couldNotSaveFolders = !change(appFolders)+ folderChanges += 1folders = appFolders.load()}- /// Reads the list of chosen folders for a reading of the apps, which looks in exactly these, so Settings and the- /// pages say the same of it. Set before the reading, so a change made in Settings meanwhile is never undone.- private func readFolders() -> [URL]? {- let read = appFolders.load()- if read != folders { folders = read }- return read+ /// Shows the list of chosen folders the reading looked in, so Settings and the pages say the same of it, unless+ /// the person changed the list since the reading began: that change brings a reading of its own.+ private func showFolders(of scan: AppScan, readSince changes: Int) {+ if changes == folderChanges, scan.chosenFolders != folders { folders = scan.chosenFolders }}/// Reads the folders again without emptying the list first, so an app installed while Peel is open@@ -298,10 +301,11 @@ final class AppLibrary {@discardableResultfunc refresh() async -> [InstalledApp] {lastRead = .now- let chosen = readFolders()- let read = await scanRun.run { await AppCatalog.scan(chosen: chosen) }+ let changes = folderChanges+ let read = await scanRun.run { [appFolders] in await AppCatalog.scan(choices: appFolders) }if !scanRun.isRunning { askedForReading = false }guard let scan = read else { return [] }+ showFolders(of: scan, readSince: changes)keepUnreadable(of: scan)let found = scan.appslet listed = AppCatalog.sorted(found + stillThere(revealed, beside: found))diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swiftindex 8b2004fe..3d9ed029 100644--- a/PeelTests/AppLibraryTests.swift+++ b/PeelTests/AppLibraryTests.swift@@ -956,4 +956,39 @@ private nonisolated final class TestClock: Sendable {library.recheck?.cancel()#expect(!library.isReadingAsAsked)}++ @Test func aReadingWaitsForTheListOfFoldersAwayFromTheMainActor() async throws {+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: folder) }+ let made = try MadeUpDefaults()+ defer { made.forget() }+ let list = AppFolders(url: folder.appending(path: "app-folders.json"))+ let library = AppLibrary(+ defaults: made.defaults,+ releaseNotesStore: ReleaseNotesStore(url: folder.appending(path: "release-notes.json")),+ appFolders: list,+ appMemory: AppMemory(url: folder.appending(path: "apps.json"))+ )+ // Held as `peel` holds it while it reads the list, until the main actor has been free during the reading.+ let lock = open(list.url.path(percentEncoded: false) + ".lock", O_CREAT | O_RDWR | O_CLOEXEC, 0o600)+ try #require(lock >= 0 && flock(lock, LOCK_EX) == 0)+ let mainWasFree = DispatchSemaphore(value: 0)+ async let releasedInTime = withCheckedContinuation { continuation in+ Thread {+ let inTime = mainWasFree.wait(timeout: .now() + 60) == .success+ flock(lock, LOCK_UN)+ close(lock)+ continuation.resume(returning: inTime)+ }.start()+ }++ let reading = Task { await library.refresh() }+ for _ in 0..<100 { await Task.yield() }+ let stillReading = library.scanRun.isRunning+ mainWasFree.signal()+ _ = await reading.value+ #expect(await releasedInTime)+ #expect(stillReading)+ }}