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.

An exclusion is one place however it is written

Țugui Dragoș0e06ed88d92f
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
index 92824def..59157e7b 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
@@ -108,9 +108,9 @@ struct ExclusionsCommand: AsyncParsableCommand {
if let app {
identifier = try await ExclusionsCommand.identifier(of: app, among: apps)
}
- let added = paths.map { URL(argument: $0).standardizedFileURL }
+ let added = paths.map(URL.init(argument:))
try await ExclusionsCommand.change(in: store) { [identifier] exclusions in
- exclusions.paths.formUnion(added)
+ 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.")
@@ -138,7 +138,7 @@ struct ExclusionsCommand: AsyncParsableCommand {
func run(in store: ExclusionStore, among apps: [InstalledApp]? = nil) async throws {
let before = try await ExclusionsCommand.current(in: store)
- let removed = Set(paths.map { URL(argument: $0).standardizedFileURL })
+ let removed = paths.map(URL.init(argument:))
// `app` comes off exactly as written, so an app that isn't installed can still come off the list. Only
// when the list has no such identifier is `app` looked up among the installed apps.
var identifiers: Set<String> = []
@@ -149,11 +149,12 @@ struct ExclusionsCommand: AsyncParsableCommand {
identifiers.insert(identifier)
}
}
- guard !before.paths.isDisjoint(with: removed) || !before.bundleIdentifiers.isDisjoint(with: identifiers) else {
+ var remaining = before
+ guard remaining.remove(removed) || !before.bundleIdentifiers.isDisjoint(with: identifiers) else {
throw CommandFailure("Peel wasn't leaving that alone. See `peel exclusions list`.")
}
try await ExclusionsCommand.change(in: store) { [identifiers] exclusions in
- exclusions.paths.subtract(removed)
+ exclusions.remove(removed)
exclusions.bundleIdentifiers.subtract(identifiers)
}
Output.line("Peel will treat it like anything else again.")
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift
index 13962a98..a9cf37f4 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift
@@ -119,9 +119,11 @@ func isAFolder(_ url: URL) -> Bool {
}
extension URL {
- /// A folder or app given on the command line, relative to the current directory.
+ /// A file, folder, or app given on the command line, relative to the current directory. What is there is
+ /// named as a file or as a folder, as the disk says, so it compares equal to the same item named anywhere else.
init(argument: String) {
let currentDirectory = URL(filePath: FileManager.default.currentDirectoryPath, directoryHint: .isDirectory)
- self = URL(filePath: (argument as NSString).expandingTildeInPath, directoryHint: .isDirectory, relativeTo: currentDirectory).standardizedFileURL
+ let path = (argument as NSString).expandingTildeInPath
+ self = URL(filePath: path, directoryHint: .checkFileSystem, relativeTo: currentDirectory).standardizedFileURL
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
index 760f8a7c..04102219 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
@@ -82,6 +82,25 @@ public struct Exclusions: Sendable, Codable, Hashable {
public var isEmpty: Bool { paths.isEmpty && bundleIdentifiers.isEmpty }
+ /// Adds `urls`, leaving out any already on the list under another spelling, as `/private/var` is `/var`.
+ public mutating func add(_ urls: [URL]) {
+ for url in urls where !paths.contains(where: { Self.isSamePlace($0, url) }) {
+ paths.insert(url.standardizedFileURL)
+ }
+ }
+
+ /// Takes `urls` off the list, however each entry was written. Returns whether anything came off.
+ @discardableResult
+ public mutating func remove(_ urls: [URL]) -> Bool {
+ let removed = paths.filter { entry in urls.contains { Self.isSamePlace(entry, $0) } }
+ paths.subtract(removed)
+ return !removed.isEmpty
+ }
+
+ private static func isSamePlace(_ one: URL, _ other: URL) -> Bool {
+ !spellings(of: one).isDisjoint(with: spellings(of: other))
+ }
+
/// True when `url` is excluded or sits inside something excluded. Compared name by name: a slash and a
/// combining mark after it are one `Character`, so a name beginning with the mark would never read as
/// inside its folder to a comparison of characters.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift
index d010cbd2..5e61dcea 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift
@@ -80,6 +80,21 @@ struct ExclusionCommandTests {
#expect(await saved(store).isEmpty)
}
+ /// One place is one exclusion however it is written: a file the app saved, the same path in capitals, never
+ /// added twice and taken off by any of its spellings.
+ @Test func treatsEverySpellingOfAPathAsOne() async throws {
+ let directory = try TemporaryDirectory()
+ let store = store(in: directory)
+ let note = try directory.file("home/Documents/note.txt")
+ #expect(await store.save(Exclusions(paths: [note])))
+
+ try await add([note.path(percentEncoded: false).uppercased()], in: store)
+ #expect(await saved(store).paths.count == 1)
+
+ try await remove([note.path(percentEncoded: false)], in: store)
+ #expect(await saved(store).isEmpty)
+ }
+
/// An app that is not installed stays in the list, so it can be removed by the identifier saved there.
@Test func removesAnAppThatIsNoLongerInstalled() async throws {
let directory = try TemporaryDirectory()
diff --git a/Peel/Model/ExclusionsStore.swift b/Peel/Model/ExclusionsStore.swift
index fa43db02..894159e6 100644
--- a/Peel/Model/ExclusionsStore.swift
+++ b/Peel/Model/ExclusionsStore.swift
@@ -39,8 +39,7 @@ final class ExclusionsStore {
}
func add(paths: [URL]) async {
- let added = paths.map(\.standardizedFileURL)
- await change { $0.paths.formUnion(added) }
+ await change { $0.add(paths) }
}
func add(bundleIdentifier: String) async {
@@ -48,8 +47,7 @@ final class ExclusionsStore {
}
func remove(paths: [URL]) async {
- let removed = paths.map(\.standardizedFileURL)
- await change { $0.paths.subtract(removed) }
+ await change { $0.remove(paths) }
}
func remove(bundleIdentifiers: [String]) async {