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
Register an app History puts back with Launch Services
Țugui Dragoș800bd94c52dd
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swiftindex 6fe7d4b5..8645940d 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift@@ -370,9 +370,11 @@ struct RestoreCommand: AsyncParsableCommand {let (restored, failures, forgotten) = await Uninterrupted.run {var restored: Set<UUID> = []var failures: [(RemovalRecord, RestoreFailure)] = []- for record in going.inPutBackOrder {- // `peel` never uses the helper: items that need administrator access are left for the Peel app.- if let failure = await service.restore(record.trashedItem, canUseHelper: false) {+ let ordered = going.inPutBackOrder+ // `peel` never uses the helper: items that need administrator access are left for the Peel app.+ let answers = await service.restore(ordered.map(\.trashedItem), canUseHelper: false)+ for (record, answer) in zip(ordered, answers) {+ if let failure = answer {failures.append((record, failure))} else {restored.insert(record.id)diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swiftindex e09beecc..2494b505 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift@@ -1,3 +1,4 @@+import CoreServicespublic import Foundationinternal import PeelPrivileged@@ -220,6 +221,7 @@ public struct TrashService: Sendable {typealias RunningInside = @Sendable (_ app: URL) async -> [LaunchdCleanup.Running]typealias StopRunning = @Sendable ([LaunchdCleanup.Running], _ canUseHelper: Bool) async -> [LaunchdCleanup.Running]typealias RegistrationAtLogin = @Sendable (_ app: URL) async -> StartsAtLogin.Registration?+ typealias RegisterApp = @Sendable (_ app: URL) async -> Voidlet environment: SearchEnvironmentprivate let removalGuard: RemovalGuard@@ -231,6 +233,7 @@ public struct TrashService: Sendable {private let runningInside: RunningInsideprivate let stopRunning: StopRunningprivate let registrationAtLogin: RegistrationAtLogin+ private let registerApp: RegisterAppprivate let moveToTrash: @Sendable (URL) throws -> URLprivate let ownMoves: OwnTrashMoves/// Where each item is written down as soon as it moves, so a removal cut short still reaches History.@@ -253,6 +256,7 @@ public struct TrashService: Sendable {guard #available(macOS 27, *) else { return nil }return await StartsAtLogin.registration(of: app)},+ registerApp: { app in _ = LSRegisterURL(app as CFURL, true) },moveToTrash: { try Self.moveToSystemTrash($0, refusal: removalGuard.refusal(of:)) },ownMoves: .shared,journal: RemovalJournal(beside: RemovalHistory.defaultURL)@@ -260,7 +264,8 @@ public struct TrashService: Sendable {}/// For tests. Unless a test passes its own, it stops and starts no launchd job, asks launchd nothing, forgets no- /// preference domain, acts as if there were no helper, changes no Dock, and writes no journal.+ /// preference domain, registers no app with Launch Services, acts as if there were no helper, changes no Dock,+ /// and writes no journal.init(environment: SearchEnvironment,exclusions: Exclusions = .none,@@ -272,6 +277,7 @@ public struct TrashService: Sendable {runningInside: @escaping RunningInside = { _ in [] },stopRunning: @escaping StopRunning = { _, _ in [] },registrationAtLogin: @escaping RegistrationAtLogin = { _ in nil },+ registerApp: @escaping RegisterApp = { _ in },ownMoves: OwnTrashMoves = OwnTrashMoves(),journal: RemovalJournal? = nil,moveToTrash: @escaping @Sendable (URL) throws -> URL@@ -287,6 +293,7 @@ public struct TrashService: Sendable {runningInside: runningInside,stopRunning: stopRunning,registrationAtLogin: registrationAtLogin,+ registerApp: registerApp,moveToTrash: moveToTrash,ownMoves: ownMoves,journal: journal@@ -304,6 +311,7 @@ public struct TrashService: Sendable {runningInside: @escaping RunningInside,stopRunning: @escaping StopRunning,registrationAtLogin: @escaping RegistrationAtLogin,+ registerApp: @escaping RegisterApp,moveToTrash: @escaping @Sendable (URL) throws -> URL,ownMoves: OwnTrashMoves,journal: RemovalJournal?@@ -318,6 +326,7 @@ public struct TrashService: Sendable {self.runningInside = runningInsideself.stopRunning = stopRunningself.registrationAtLogin = registrationAtLogin+ self.registerApp = registerAppself.moveToTrash = moveToTrashself.ownMoves = ownMovesself.journal = journal@@ -647,10 +656,27 @@ public struct TrashService: Sendable {TrashResult(failures: urls.map { TrashFailure(url: $0, reason: .needsHelper) })}+ /// Puts `items` back in the order given, each as `restore(_:canUseHelper:)` does, and answers for each why it+ /// stayed, or nil. Launch Services leaves out an app in the Trash and isn't told it moved back, so the apps among+ /// them are registered again, but only once every item is back: a registered app starts its extensions, which+ /// would make their containers again before the ones in the Trash went back.+ @concurrent+ public func restore(_ items: [TrashedItem], canUseHelper: Bool = false) async -> [RestoreFailure?] {+ var answers: [RestoreFailure?] = []+ for item in items {+ answers.append(await restore(item, canUseHelper: canUseHelper))+ }+ for (item, answer) in zip(items, answers)+ where answer == nil && item.originalURL.pathExtension.caseInsensitiveCompare("app") == .orderedSame {+ await registerApp(item.originalURL)+ }+ return answers+ }+/// Moves an item from the Trash back where it came from, through the helper when the folder needs/// administrator rights, and puts back the Dock tiles an uninstall took out for it.@concurrent- public func restore(_ item: TrashedItem, canUseHelper: Bool = false) async -> RestoreFailure? {+ func restore(_ item: TrashedItem, canUseHelper: Bool = false) async -> RestoreFailure? {let failure = await moveBack(item, canUseHelper: canUseHelper)if failure == nil {await putBackDockTiles(item.originalURL)diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swiftindex 86903371..c38410d9 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift@@ -452,6 +452,59 @@ struct TrashServiceTests {#expect(await forgotten.calls.last == ["home/Library/Preferences/com.example.app.plist", "com.example.app"])}+ @Test func anAppPutBackIsRegisteredWithLaunchServicesOnceItIsBack() async throws {+ let directory = try TemporaryDirectory()+ let app = try directory.directory("home/Applications/org.example.Studio.app")+ let cache = try directory.directory("home/Library/Caches/org.example.Studio")+ let trash = try directory.directory("home/.Trash")+ let registered = Mutex<[String]>([])+ let service = TrashService(+ environment: SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)+ ),+ registerApp: { url in registered.withLock { $0.append(url.path(percentEncoded: false)) } }+ ) { url in+ let destination = trash.appending(path: url.lastPathComponent)+ try FileManager.default.moveItem(at: url, to: destination)+ return destination+ }++ let moved = await service.trash([app, cache]).trashed+ #expect(registered.withLock { $0 }.isEmpty)+ #expect(await service.restore(moved) == [nil, nil])+ let appMoved = try #require(moved.first { $0.originalURL == app })+ #expect(await service.restore([appMoved]) == [.missingFromTrash])+ #expect(registered.withLock { $0 } == [app.path(percentEncoded: false)])+ }++ @Test func registersTheAppsPutBackOnlyOnceEveryItemIsBack() async throws {+ let directory = try TemporaryDirectory()+ let app = try directory.directory("home/Applications/org.example.Studio.app")+ let container = try directory.directory("home/Library/Containers/org.example.Studio.Extension")+ let trash = try directory.directory("home/.Trash")+ let containerWasBack = Mutex<[Bool]>([])+ let service = TrashService(+ environment: SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)+ ),+ registerApp: { _ in+ let isBack = FileManager.default.fileExists(atPath: container.path(percentEncoded: false))+ containerWasBack.withLock { $0.append(isBack) }+ }+ ) { url in+ let destination = trash.appending(path: url.lastPathComponent)+ try FileManager.default.moveItem(at: url, to: destination)+ return destination+ }++ let moved = await service.trash([app, container]).trashed+ let appFirst = moved.sorted { ($0.originalURL == app ? 0 : 1) < ($1.originalURL == app ? 0 : 1) }+ #expect(await service.restore(appFirst) == [nil, nil])+ #expect(containerWasBack.withLock { $0 } == [true])+ }+/// Putting an app back puts back the Dock tiles an uninstall took out for it, and only once it is back.@Test func puttingAnItemBackPutsBackItsDockTiles() async throws {let directory = try TemporaryDirectory()diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swiftindex 7ad913dc..60f63e39 100644--- a/Peel/Model/RemovalHistoryStore.swift+++ b/Peel/Model/RemovalHistoryStore.swift@@ -326,8 +326,10 @@ final class RemovalHistoryStore {await QuitGuard.shared.run {var restored: Set<UUID> = []var problems: [RemovalRecord.ID: RestoreFailure] = [:]- for record in records.inPutBackOrder {- if let failure = await service.restore(record.trashedItem, canUseHelper: canUseHelper) {+ let ordered = records.inPutBackOrder+ let answers = await service.restore(ordered.map(\.trashedItem), canUseHelper: canUseHelper)+ for (record, answer) in zip(ordered, answers) {+ if let failure = answer {problems[record.id] = failure} else {restored.insert(record.id)