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.

peel uninstall writes down what it refused before the move

Țugui Dragoș627f69cf352a
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
index 593fd896..697b088b 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
@@ -430,6 +430,9 @@ struct UninstallCommand: AsyncParsableCommand {
// The removal guard judges each item before printing, so the list shows what the move will really do.
let plan = UninstallPlan.make(uninstallation, keepLeftovers: keepLeftovers, refusal: service.refusal(of:))
if let refusal = plan.appStays {
+ if !dryRun {
+ _ = await plan.record(TrashResult(), from: target.name)
+ }
throw CommandFailure("Peel won't move \(Output.plain(Output.path(target.url))): \(refusal.summary). Nothing of \(Output.plain(target.name)) was touched.")
}
let unreadable = uninstallation.scan.unreadableLocations
@@ -465,8 +468,7 @@ struct UninstallCommand: AsyncParsableCommand {
// The privacy reset comes before the move, because `tccutil` only finds an app that is still in place.
let reset = resetPrivacy ? await PrivacyReset.reset(bundleIdentifier: target.bundleIdentifier) : nil
let result = await plan.move(using: service)
- let sizes = [URL: Int64](measured: plan.items.map { ($0.url, $0.size) })
- return (reset, result, await Removals.record(result, from: target.name, sizes: sizes, tool: "applications"))
+ return (reset, result, await plan.record(result, from: target.name))
}
let privacy = reset.map { Self.privacyOutcome($0, app: target, after: result) }
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
index d6c2c9fd..0fbfc146 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
@@ -57,6 +57,26 @@ struct UninstallPlan {
)
}
+ /// Writes `result` into History, with what the removal guard refused before the move beside what the move
+ /// reports, as `Cleanup` does, so `peel history --refused` and the app's Not Moved list it. False when History
+ /// could not be written.
+ func record(
+ _ result: TrashResult,
+ from source: String,
+ in log: RemovalLog = RemovalLog(),
+ refusals: RefusalLog = RefusalLog()
+ ) async -> Bool {
+ let refused = staying.compactMap { item in item.refusal.map { TrashFailure(url: item.url, reason: $0) } }
+ return await Removals.record(
+ TrashResult(trashed: result.trashed, failures: result.failures + refused),
+ from: source,
+ sizes: [URL: Int64](measured: items.map { ($0.url, $0.size) }),
+ tool: "applications",
+ in: log,
+ refusals: refusals
+ )
+ }
+
/// Moves the bundle to the Trash on its own, then the rest only if the bundle really moved. Call it only when
/// `appStays` is nil: it takes the first item that moves to be the bundle.
func move(using service: TrashService) async -> TrashResult {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift
index cbbc2c4d..60672862 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift
@@ -202,4 +202,33 @@ struct CleanupTests {
#expect(await RemovalLog(url: log.url).load().records?.map(\.originalURL) == [cache])
#expect(await RefusalLog(url: refusals.url).load().records.map(\.url) == [documents])
}
+
+ /// `peel uninstall` writes down what the guard refused before the move beside what moved, as every other
+ /// command does, and an app the guard keeps where it is is written down too, though nothing moved.
+ @Test func anUninstallWritesDownWhatTheGuardRefusedBeforeTheMove() async throws {
+ let directory = try TemporaryDirectory()
+ let app = try directory.directory("home/Applications/Editor.app")
+ let cache = try directory.file("home/Library/Caches/com.example.editor/blob", bytes: 20)
+ let documents = try directory.directory("home/Documents")
+ let log = RemovalLog(url: directory.url.appending(path: "Peel/removals.json"))
+ let refusals = RefusalLog(url: directory.url.appending(path: "Peel/refusals.json"))
+ let service = try service(in: directory)
+ let plan = UninstallPlan(app: app, items: [
+ UninstallPlan.Item(url: app, size: 4_096, refusal: nil),
+ UninstallPlan.Item(url: cache, size: 20, refusal: nil),
+ UninstallPlan.Item(url: documents, size: nil, refusal: service.refusal(of: documents)),
+ ], needsAdministrator: 0, needsReview: 0)
+
+ #expect(await plan.record(await plan.move(using: service), from: "Editor", in: log, refusals: refusals))
+
+ let moved = await RemovalLog(url: log.url).load().records?.map(\.originalURL.lastPathComponent)
+ #expect(Set(moved ?? []) == ["Editor.app", "blob"])
+ #expect(await RefusalLog(url: refusals.url).load().records.map(\.url) == [documents])
+
+ let kept = UninstallPlan(app: documents, items: [
+ UninstallPlan.Item(url: documents, size: nil, refusal: service.refusal(of: documents)),
+ ], needsAdministrator: 0, needsReview: 0)
+ #expect(await kept.record(TrashResult(), from: "Documents", in: log, refusals: refusals))
+ #expect(await RefusalLog(url: refusals.url).load().records.map(\.url) == [documents, documents])
+ }
}