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.

Make peel exclusions say what it really did

Țugui Dragoș25ba5cfb4b6f
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
index 1ff5dfc4..1fcfcac6 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
@@ -102,20 +102,34 @@ struct ExclusionsCommand: AsyncParsableCommand {
}
func run(in store: ExclusionStore, among apps: [InstalledApp]? = nil) async throws {
- _ = try await ExclusionsCommand.current(in: store)
+ let current = try await ExclusionsCommand.current(in: store)
// The app is looked up before the list is changed, since looking it up can take a while.
// An app with no identifier is left alone by where it is.
var identifier: String?
var added = paths.map(URL.init(argument:))
if let app {
- let found = try await ExclusionsCommand.app(app, among: apps)
- if let found = found.bundleIdentifier { identifier = found } else { added.append(found.url) }
+ let installed = if let apps { apps } else { await AppCatalog.installedApps() }
+ // An identifier leaves every copy alone, so two copies that share one need no choice between them.
+ if let shared = installed.lazy.compactMap(\.bundleIdentifier)
+ .first(where: { $0.caseInsensitiveCompare(app) == .orderedSame }) {
+ identifier = shared
+ } else {
+ let found = try await ExclusionsCommand.app(app, among: installed)
+ if let found = found.bundleIdentifier { identifier = found } else { added.append(found.url) }
+ }
+ }
+ for url in added where url.isMissing {
+ Output.note("\(Output.plain(Output.path(url))) isn't there yet. Peel will leave it alone once it is.")
}
+ var after = current
+ after.add(added)
+ let newIdentifier = identifier.map { !current.bundleIdentifiers.contains($0) } ?? false
+ let new = after.paths.count - current.paths.count + (newIdentifier ? 1 : 0)
try await ExclusionsCommand.change(in: store) { [identifier, added] exclusions in
exclusions.add(added)
if let identifier { exclusions.bundleIdentifiers.insert(identifier) }
}
- Output.line("Peel will leave \(Output.count(paths.count + (app == nil ? 0 : 1), "item", "items")) alone.")
+ Output.line(new == 0 ? "Peel was already leaving that alone." : "Peel will leave \(Output.count(new, "item", "items")) alone.")
}
}
@@ -157,6 +171,12 @@ struct ExclusionsCommand: AsyncParsableCommand {
}
var remaining = before
guard remaining.remove(removed) || !before.bundleIdentifiers.isDisjoint(with: identifiers) else {
+ // What is inside a folder on the list stays alone until the folder comes off.
+ if let (url, folder) = removed.lazy.compactMap({ url in
+ before.reasons(excluding: url, app: nil).folders.first.map { (url, $0) }
+ }).first {
+ throw CommandFailure("Peel leaves \(Output.plain(Output.path(url))) alone because it is inside \(Output.plain(Output.path(folder))), which is on the list. Remove that instead.")
+ }
throw CommandFailure("Peel wasn't leaving that alone. See `peel exclusions list`.")
}
try await ExclusionsCommand.change(in: store) { [identifiers, removed] exclusions in
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift
index 9d9ac094..212b02d2 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift
@@ -148,6 +148,49 @@ struct ExclusionCommandTests {
#expect(await saved(store).isEmpty, "the app stayed excluded while the command said it was not")
}
+ @Test func saysAPathIsNotThereYetAndCountsOnlyWhatWentOn() async throws {
+ let directory = try TemporaryDirectory()
+ let store = store(in: directory)
+ let kept = try directory.directory("home/Keep")
+ let missing = directory.url.appending(path: "home/Later")
+ try await add([kept.path(percentEncoded: false)], in: store)
+ let collected = Output.Collected()
+
+ try await Output.$collected.withValue(collected) {
+ try await add([kept.path(percentEncoded: false), missing.path(percentEncoded: false)], in: store)
+ }
+
+ #expect(collected.output.contains("Peel will leave 1 item alone."))
+ #expect(collected.notes.contains("Later isn't there yet. Peel will leave it alone once it is."))
+ }
+
+ @Test func namesTheFolderThatLeavesAPathAlone() async throws {
+ let directory = try TemporaryDirectory()
+ let store = store(in: directory)
+ let kept = try directory.directory("home/Keep")
+ try await add([kept.path(percentEncoded: false)], in: store)
+
+ let failure = await #expect(throws: CommandFailure.self) {
+ try await remove([kept.appending(path: "notes.txt").path(percentEncoded: false)], in: store)
+ }
+
+ #expect(failure?.description.contains("inside \(Output.plain(Output.path(kept)))") == true)
+ }
+
+ @Test func addsAnAppWithTwoCopiesByItsIdentifier() async throws {
+ let directory = try TemporaryDirectory()
+ let store = store(in: directory)
+ let copy = InstalledApp(
+ url: URL(filePath: "/Users/me/Applications/Editor.app", directoryHint: .isDirectory),
+ bundleIdentifier: "com.example.editor",
+ name: "Editor"
+ )
+
+ try await add(["--app", "com.example.editor"], in: store, among: [editor, copy])
+
+ #expect(await saved(store).bundleIdentifiers == ["com.example.editor"])
+ }
+
@Test func refusesToRemoveWhatWasNotThere() async throws {
let directory = try TemporaryDirectory()
let store = store(in: directory)