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.

Let an open app stop peel caches only for what it would move

Țugui Dragoșad783cfd3406
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index c392499a..53516e45 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -464,13 +464,16 @@ struct CachesCommand: AsyncParsableCommand {
recordingIn log: RemovalLog = RemovalLog(),
refusals: RefusalLog = RefusalLog()
) async throws {
- // Nothing of a tool moves while its app runs, since the app writes in those folders.
- let open = environments.filter { $0.runningApp != nil && $0.locations.contains(where: \.isRecommended) }
+ // Nothing of a tool moves while its app runs, since the app writes in those folders. A tool nothing would move
+ // from holds nothing up, whether its app runs or not.
+ let moving = environments.filter { $0.locations.contains(where: \.isRecommended) }
+ let open = moving.filter { $0.runningApp != nil }
for environment in open {
Output.note(Self.quitFirst(environment))
}
let closed = environments.filter { environment in !open.contains { $0.id == environment.id } }
- let locations = closed.flatMap { environment in environment.locations.filter(\.isRecommended) }
+ let closedMoving = moving.filter { environment in !open.contains { $0.id == environment.id } }
+ let locations = closedMoving.flatMap { environment in environment.locations.filter(\.isRecommended) }
Self.notes(for: environments).forEach(Output.note)
// Nobody asked for a folder Peel doesn't suggest on its own, so it is said here and never recorded as refused.
let staying = Self.staying(in: closed)
@@ -484,8 +487,8 @@ struct CachesCommand: AsyncParsableCommand {
}
let cleanup = Cleanup.of(
locations.map { (url: $0.url, size: $0.size) },
- source: closed.count == 1 ? closed[0].name : "Developer caches",
- sourceKey: closed.count == 1 ? nil : "tool",
+ source: closedMoving.count == 1 ? closedMoving[0].name : "Developer caches",
+ sourceKey: closedMoving.count == 1 ? nil : "tool",
tool: "developer",
service: service
)
@@ -499,7 +502,7 @@ struct CachesCommand: AsyncParsableCommand {
refusals: refusals,
checkingAgain: {
// The question takes time, and an app may have been opened meanwhile.
- if let reopened = closed.first(where: { $0.runningApp != nil }) {
+ if let reopened = closedMoving.first(where: { $0.runningApp != nil }) {
throw CommandFailure(Self.quitFirst(reopened))
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
index dd378170..b2f3824b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
@@ -94,6 +94,31 @@ struct FileCommandTests {
#expect(FileManager.default.fileExists(atPath: directory.url.appending(path: "home/Library/Models/llama").path(percentEncoded: false)))
}
+ @Test func anAppThatRunsHoldsNothingBackWhenNoneOfItsFoldersIsInThePlan() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let open = try environment(
+ "Finder Tools", id: "finder", apps: ["com.apple.finder"],
+ locations: [("Library/Application Support/models", .models)], in: directory
+ )
+ let closed = try environment("Rust", id: "rust", locations: [("Library/Caches/cargo", .cache)], in: directory)
+ #expect(open.runningApp == "Finder")
+
+ let collected = Output.Collected()
+ try await Output.$collected.withValue(collected) {
+ try await (command(["caches", "--remove", "-y"]) as CachesCommand)
+ .clean(
+ [open, closed],
+ using: try service(in: directory),
+ recordingIn: logs.removals,
+ refusals: logs.refusals
+ )
+ }
+
+ #expect(await moved(logs) == ["cargo"])
+ #expect(!collected.notes.contains("Quit Finder first"))
+ }
+
/// Nothing of a tool moves while its app runs, since the app writes in those folders: the Developer page waits,
/// and so does `peel caches`. Finder runs on every Mac, so it stands for an app that is open.
@Test func leavesTheCachesOfAnAppThatRuns() async throws {