Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Name a removal in History by the apps it took

Țugui Dragoș5de75ac30c53
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
index 2fb89b82..d94e303c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
@@ -189,6 +189,18 @@ public struct BulkUninstallation: Sendable {
}
}
+ /// The chosen apps `urls` belong to: an app whose bundle is among them, and an app with a file of its own among
+ /// them. A file names the app by its identifier, which another copy of it shares, so only a bundle names a copy.
+ public func apps(owning urls: some Sequence<URL>) -> [InstalledApp] {
+ let paths = Set(urls.map(PathPattern.comparablePath))
+ let owners = Set(items.filter { paths.contains(PathPattern.comparablePath(of: $0.url)) }.flatMap(\.apps))
+ let copied = Set(Dictionary(grouping: apps, by: \.bundleIdentifier).filter { $0.value.count > 1 }.keys)
+ return apps.filter { app in
+ paths.contains(PathPattern.comparablePath(of: app.url))
+ || (owners.contains(app.bundleIdentifier) && !copied.contains(app.bundleIdentifier))
+ }
+ }
+
static func merge(_ uninstallations: [Uninstallation]) -> [Item] {
let chosen = Set(uninstallations.map(\.app.bundleIdentifier))
let chosenBundles = Set(uninstallations.map { PathPattern.comparablePath(of: $0.app.url) })
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
index d5b162b6..cae74290 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
@@ -297,6 +297,40 @@ struct BulkUninstallationTests {
}
}
+ @Test func namesTheAppsWhoseItemsMovedAndThoseWhoseItemsStayed() async throws {
+ let directory = try TemporaryDirectory()
+ let stays = InstalledApp(url: try directory.directory("Applications/Stays.app"), bundleIdentifier: "org.example.stays", name: "Stays")
+ let goes = InstalledApp(url: try directory.directory("Applications/Goes.app"), bundleIdentifier: "org.example.goes", name: "Goes")
+ let kept = InstalledApp(url: try directory.directory("Applications/Kept.app"), bundleIdentifier: "org.example.kept", name: "Kept")
+ let copy = InstalledApp(url: try directory.directory("Other/Goes.app"), bundleIdentifier: "org.example.goes", name: "Goes")
+ let staysCache = try directory.directory("home/Library/Caches/org.example.stays")
+ let goesCache = try directory.directory("home/Library/Caches/org.example.goes")
+ let keptCache = try directory.directory("home/Library/Caches/org.example.kept")
+ let bulk = BulkUninstallation(uninstallations: [
+ uninstallation(stays, [leftover(staysCache.path(percentEncoded: false), size: 1)]),
+ uninstallation(goes, [leftover(goesCache.path(percentEncoded: false), size: 1)]),
+ uninstallation(kept, [leftover(keptCache.path(percentEncoded: false), size: 1)]),
+ uninstallation(copy, []),
+ ])
+ let trash = try directory.directory("Trash")
+ let service = TrashService(
+ environment: SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ ) { url in
+ guard url != stays.url else { throw CocoaError(.fileWriteNoPermission) }
+ let destination = trash.appending(path: UUID().uuidString)
+ try FileManager.default.moveItem(at: url, to: destination)
+ return destination
+ }
+
+ let result = await bulk.move([stays.url, staysCache, goes.url, goesCache, keptCache], using: service)
+
+ #expect(bulk.apps(owning: result.trashed.map(\.originalURL)) == [goes, kept])
+ #expect(bulk.apps(owning: result.failures.map(\.url)) == [stays])
+ }
+
@Test func movesTheAppsBeforeTheirFiles() {
let first = app("com.example.one", "One")
let second = app("com.example.two", "Two")
diff --git a/Peel/Model/BulkRemovalPlan.swift b/Peel/Model/BulkRemovalPlan.swift
index 4f28496a..29893abe 100644
--- a/Peel/Model/BulkRemovalPlan.swift
+++ b/Peel/Model/BulkRemovalPlan.swift
@@ -122,14 +122,31 @@ final class BulkRemovalPlan {
return await bulk.move(request.urls, using: TrashService(exclusions: ExclusionsStore.shared.exclusions))
}
- /// The source History records: up to three app names, or a count in English that History shows in the
- /// user's language through `historySourceKey`.
- var historySource: String {
- guard apps.count > 3 else { return apps.map(\.name).joined(separator: ", ") }
- return String(AttributedString(localized: "^[\(apps.count) app](inflect: true)", locale: Locale(identifier: "en")).characters)
+ /// Records `result` in History, what moved named by the apps it belonged to and what stayed by its own apps.
+ func record(_ result: TrashResult, sizes: [URL: Int64], in history: RemovalHistoryStore) async {
+ var removal = RemovalInProgress()
+ let moved = bulk?.apps(owning: result.trashed.map(\.originalURL)) ?? []
+ let stayed = bulk?.apps(owning: result.failures.map(\.url)) ?? []
+ let parts = [
+ (TrashResult(trashed: result.trashed), part(naming: moved)),
+ (TrashResult(failures: result.failures), part(naming: stayed)),
+ ]
+ for (result, part) in parts {
+ await history.record(result, part: part, sizes: sizes, in: &removal)
+ }
+ history.finish(removal)
}
- var historySourceKey: String? {
- apps.count > 3 ? "apps.\(apps.count)" : nil
+ /// Up to three app names, or a count in English that History shows in the user's language through the key.
+ private func part(naming named: [InstalledApp]) -> RemovalPart {
+ let named = named.isEmpty ? apps : named
+ let tool = Tool.applications.rawValue
+ guard named.count > 3 else {
+ return RemovalPart(source: named.map(\.name).joined(separator: ", "), sourceKey: nil, tool: tool)
+ }
+ let count = AttributedString(
+ localized: "^[\(named.count) app](inflect: true)", locale: Locale(identifier: "en")
+ )
+ return RemovalPart(source: String(count.characters), sourceKey: "apps.\(named.count)", tool: tool)
}
}
diff --git a/Peel/Views/MultipleAppsView.swift b/Peel/Views/MultipleAppsView.swift
index f28fb786..1e1f4c3e 100644
--- a/Peel/Views/MultipleAppsView.swift
+++ b/Peel/Views/MultipleAppsView.swift
@@ -370,13 +370,7 @@ struct MultipleAppsView: View {
) {
home.record(appManagement: state)
}
- await history.record(
- result,
- tool: .applications,
- source: plan.historySource,
- sourceKey: plan.historySourceKey,
- sizes: request.sizes
- )
+ await plan.record(result, sizes: request.sizes, in: history)
return result
}
// When an app bundle moved, reloading the library changes the selection, which rebuilds this page. When