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 caches leaves the caches of an app that is open

Țugui Dragoș97173aaec74d
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index 16e243f7..2f88ed48 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -253,15 +253,24 @@ struct CachesCommand: AsyncParsableCommand {
recordingIn log: RemovalLog = RemovalLog(),
refusals: RefusalLog = RefusalLog()
) async throws {
- let locations = environments.flatMap { environment in environment.locations.filter(\.isRecommended) }
+ // 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) }
+ 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) }
guard !locations.isEmpty else {
+ if let first = open.first {
+ throw CommandFailure(Self.quitFirst(first))
+ }
Output.line("Nothing here is safe to suggest. See `peel caches`.")
return
}
let cleanup = Cleanup.of(
locations.map { (url: $0.url, size: $0.size) },
- source: environments.count == 1 ? environments[0].name : "Developer caches",
- sourceKey: environments.count == 1 ? nil : "tool",
+ source: closed.count == 1 ? closed[0].name : "Developer caches",
+ sourceKey: closed.count == 1 ? nil : "tool",
tool: "developer",
service: service
)
@@ -271,9 +280,19 @@ struct CachesCommand: AsyncParsableCommand {
yes: removal.yes,
using: service,
recordingIn: log,
- refusals: refusals
+ refusals: refusals,
+ checkingAgain: {
+ // The question takes time, and an app may have been opened meanwhile.
+ if let reopened = closed.first(where: { $0.runningApp != nil }) {
+ throw CommandFailure(Self.quitFirst(reopened))
+ }
+ }
)
}
+
+ private static func quitFirst(_ environment: DeveloperEnvironment) -> String {
+ "Quit \(environment.runningApp ?? environment.name) first: \(environment.name)'s caches stay while it runs."
+ }
}
struct ProjectsCommand: AsyncParsableCommand {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
index b1bc652e..931d5df1 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
@@ -62,7 +62,8 @@ struct Cleanup {
using service: TrashService,
recordingIn log: RemovalLog = RemovalLog(),
refusals: RefusalLog = RefusalLog(),
- move: (TrashService, [URL]) async -> TrashResult = { await $0.trash($1) }
+ move: (TrashService, [URL]) async -> TrashResult = { await $0.trash($1) },
+ checkingAgain: () async throws -> Void = {}
) async throws {
if let note = UnreadableHistory.note(at: log.url) {
Output.note(note)
@@ -90,6 +91,7 @@ struct Cleanup {
if !yes {
try Output.confirm(question)
}
+ try await checkingAgain()
let (result, recorded) = await Uninterrupted.run {
let result = await move(service, moving.map(\.url))
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
index f094ee16..196fd19e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
@@ -1,4 +1,5 @@
public import Foundation
+import AppKit
internal import PeelPrivileged
public struct DeveloperEnvironment: Sendable, Hashable, Identifiable {
@@ -54,10 +55,13 @@ public struct DeveloperEnvironment: Sendable, Hashable, Identifiable {
SizeTotal(locations.map(\.size))
}
- /// The name of one of the environment's apps that is running, which is the one to ask the user to quit.
- /// `name` answers for a bundle identifier with the name of a running copy, or nil when none runs.
- public func runningApp(named name: (String) -> String?) -> String? {
- appBundleIdentifiers.lazy.compactMap(name).first
+ /// The name of one of the environment's apps that is running, which is the one to ask the user to quit, or nil
+ /// when none runs. Nothing of the environment moves while one runs, from the app or `peel`: it writes in these
+ /// folders. Asked each time, since an app can be opened at any moment.
+ public var runningApp: String? {
+ appBundleIdentifiers.lazy.compactMap { identifier in
+ NSRunningApplication.runningApplications(withBundleIdentifier: identifier).lazy.compactMap(\.localizedName).first
+ }.first
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
index de9796bf..5785409a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
@@ -46,17 +46,19 @@ struct DeveloperCachesTests {
#expect(!refused.isRecommended && !slow.isRecommended)
}
- /// Xcode's caches wait on Simulator too, so the request to quit names whichever of the two is running: the
- /// environment's name would ask for Xcode while only Simulator runs, and the request would keep coming back.
+ /// Xcode's caches wait on Simulator too, so the request to quit names whichever of an environment's apps is
+ /// running: the environment's name would ask for Xcode while only Simulator runs, and the request would keep
+ /// coming back. Finder runs on every Mac, and an identifier nobody uses stands for an app that is closed.
@Test func namesTheAppThatRunsRatherThanTheEnvironment() throws {
let xcode = try #require(DeveloperCaches.definitions.first { $0.id == "xcode" })
- let environment = DeveloperEnvironment(
- id: xcode.id, name: xcode.name, systemImage: xcode.systemImage, appBundleIdentifiers: xcode.appBundleIdentifiers, locations: []
- )
+ #expect(xcode.appBundleIdentifiers.contains("com.apple.dt.Xcode"))
+ #expect(xcode.appBundleIdentifiers.contains("com.apple.iphonesimulator"))
+ func environment(_ apps: [String]) -> DeveloperEnvironment {
+ DeveloperEnvironment(id: "tools", name: "Tools", systemImage: "hammer", appBundleIdentifiers: apps, locations: [])
+ }
- #expect(environment.runningApp { $0 == "com.apple.iphonesimulator" ? "Simulator" : nil } == "Simulator")
- #expect(environment.runningApp { $0 == "com.apple.dt.Xcode" ? "Xcode" : nil } == "Xcode")
- #expect(environment.runningApp { _ in nil } == nil)
+ #expect(environment(["org.example.closed", "com.apple.finder"]).runningApp == "Finder")
+ #expect(environment(["org.example.closed"]).runningApp == nil)
}
/// Space asks about a folder however it was spelled, so another spelling of the same folder (another case, or
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
index a688a80e..cfe583a1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
@@ -36,12 +36,18 @@ struct FileCommandTests {
// MARK: Developer caches
- private func environment(_ name: String, id: String, locations: [(String, DeveloperEnvironment.ContentKind)], in directory: borrowing TemporaryDirectory) throws -> DeveloperEnvironment {
+ private func environment(
+ _ name: String,
+ id: String,
+ apps: [String] = [],
+ locations: [(String, DeveloperEnvironment.ContentKind)],
+ in directory: borrowing TemporaryDirectory
+ ) throws -> DeveloperEnvironment {
DeveloperEnvironment(
id: id,
name: name,
systemImage: "hammer",
- appBundleIdentifiers: [],
+ appBundleIdentifiers: apps,
locations: try locations.map { path, kind in
DeveloperEnvironment.Location(url: try directory.file("home/\(path)/blob", bytes: 30).deletingLastPathComponent(), kind: kind, size: 30, source: "https://example.com/\(path)")
}
@@ -77,6 +83,31 @@ struct FileCommandTests {
#expect(FileManager.default.fileExists(atPath: directory.url.appending(path: "home/Library/Models/llama").path(percentEncoded: false)))
}
+ /// 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 {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let open = try environment("Finder Tools", id: "finder", apps: ["com.apple.finder"], locations: [("Library/Caches/finder-tool", .cache)], in: directory)
+ let closed = try environment("Rust", id: "rust", locations: [("Library/Caches/cargo", .cache)], in: directory)
+ #expect(open.runningApp == "Finder")
+ #expect(closed.runningApp == nil)
+
+ 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"))
+ #expect(FileManager.default.fileExists(atPath: directory.url.appending(path: "home/Library/Caches/finder-tool").path(percentEncoded: false)))
+
+ await #expect(throws: CommandFailure.self) {
+ try await (command(["caches", "--remove", "-y"]) as CachesCommand)
+ .clean([open], using: try service(in: directory), recordingIn: logs.removals, refusals: logs.refusals)
+ }
+ }
+
@Test func aDryRunOnCachesMovesNothing() async throws {
let directory = try TemporaryDirectory()
let logs = logs(in: directory)
diff --git a/Peel/Model/DeveloperLibrary.swift b/Peel/Model/DeveloperLibrary.swift
index aa689db7..95a550e7 100644
--- a/Peel/Model/DeveloperLibrary.swift
+++ b/Peel/Model/DeveloperLibrary.swift
@@ -1,4 +1,3 @@
-import AppKit
import Foundation
import Observation
import PeelCore
@@ -32,14 +31,6 @@ final class DeveloperLibrary {
}
}
- /// The name of one of the environment's apps that is running, or nil when none is. Asked each time rather
- /// than stored, since an app can be opened while the page is on screen.
- func runningApp(of environment: DeveloperEnvironment) -> String? {
- environment.runningApp { identifier in
- NSRunningApplication.runningApplications(withBundleIdentifier: identifier).lazy.compactMap(\.localizedName).first
- }
- }
-
private func environment(of page: CarriedSelection.Page) -> DeveloperEnvironment? {
environments?.first { $0.id == page.scope }
}
@@ -61,13 +52,13 @@ extension DeveloperLibrary: CarriesSelection {
}
func appToQuit(for part: CarriedSelection.Part) -> String? {
- environment(of: part.page).flatMap(runningApp(of:))
+ environment(of: part.page)?.runningApp
}
/// Moves nothing while one of the environment's apps is open: files never move out from under a running app.
func move(_ part: CarriedSelection.Part, apps: AppLibrary) async -> TrashResult? {
guard let environment = environment(of: part.page) else { return TrashResult() }
- guard runningApp(of: environment) == nil else { return nil }
+ guard environment.runningApp == nil else { return nil }
isRemoving = true
defer { isRemoving = false }
let urls = environment.locations.map(\.url).filter { selectedURLs.contains($0) && part.sizes.keys.contains($0) }