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.

Read why a developer cache stays from the location itself

Țugui Dragoș36cb00b6318e
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index e8923b8e..a474ba2c 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -381,33 +381,23 @@ struct CachesCommand: AsyncParsableCommand {
environment.locations.map { [Output.size($0.size), Output.path($0.url), $0.kind.summary, note(for: $0)] }
}
- /// Why `location` stays, or nil when `--remove` takes it. It checks the same conditions as `isRecommended`, and
- /// the two must never disagree.
+ /// Why `location` stays, in the words the list says it, or nil when `--remove` takes it.
static func reason(for location: DeveloperEnvironment.Location) -> String? {
- if isKeptByDefault(location.kind) { return "kept by default" }
- if let heldBack = location.heldBack { return heldBack.summary }
- if !location.isTheTools { return "nothing shows the tool made it" }
- if let workspace = location.workspace, workspace.mayStillBeInUse {
- return workspace.lastUsed == nil
- ? "Xcode wrote no date for its workspace" : "its workspace was opened in the last 7 days"
+ location.keptBecause.map { kept in
+ switch kept {
+ case .keptByDefault: "kept by default"
+ case .heldBack(let heldBack): heldBack.summary
+ case .notTheTools: "nothing shows the tool made it"
+ case .workspaceMayBeInUse(let lastUsed):
+ lastUsed == nil ? "Xcode wrote no date for its workspace" : "its workspace was opened in the last 7 days"
+ }
}
- guard location.size == nil else { return nil }
- let unread: HoldBack = location.closedByPermissions ? .closedByPermissions
- : location.couldNotBeRead ? .couldNotBeRead : .notMeasured
- return unread.summary
}
/// The list's last column: "suggested", or why the folder stays.
static func note(for location: DeveloperEnvironment.Location) -> String {
guard let reason = reason(for: location) else { return "suggested" }
- return isKeptByDefault(location.kind) ? reason : "\(reason), kept"
- }
-
- private static func isKeptByDefault(_ kind: DeveloperEnvironment.ContentKind) -> Bool {
- switch kind {
- case .buildData, .downloads, .cache, .logs: false
- case .deviceSupport, .archives, .models, .environments, .keptDownloads, .projectState: true
- }
+ return location.keptBecause == .keptByDefault ? reason : "\(reason), kept"
}
/// What `--remove` leaves of the tools it moves from, each folder with why.
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
index 7b809a71..e6d8ea9e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
@@ -77,12 +77,30 @@ public struct DeveloperEnvironment: Sendable, Hashable, Identifiable {
/// Whether Peel recommends this location, which Select Recommended selects and `peel caches --remove` moves:
/// only content that tools make or fetch again, or logs, and only once measured.
- public var isRecommended: Bool {
- guard isTheTools, workspace?.mayStillBeInUse != true, heldBack == nil else { return false }
- return switch kind {
- case .buildData, .downloads, .cache, .logs: size != nil
- case .deviceSupport, .archives, .models, .environments, .keptDownloads, .projectState: false
+ public var isRecommended: Bool { keptBecause == nil }
+
+ /// Why Peel doesn't recommend this location, or nil when it does.
+ public var keptBecause: Kept? {
+ switch kind {
+ case .buildData, .downloads, .cache, .logs: break
+ case .deviceSupport, .archives, .models, .environments, .keptDownloads, .projectState: return .keptByDefault
}
+ if let heldBack { return .heldBack(heldBack) }
+ if !isTheTools { return .notTheTools }
+ if let workspace, workspace.mayStillBeInUse { return .workspaceMayBeInUse(lastUsed: workspace.lastUsed) }
+ guard size == nil else { return nil }
+ if closedByPermissions { return .heldBack(.closedByPermissions) }
+ return .heldBack(couldNotBeRead ? .couldNotBeRead : .notMeasured)
+ }
+
+ public enum Kept: Sendable, Equatable {
+ /// Its kind is kept unless chosen: what a tool can't simply make again.
+ case keptByDefault
+ case heldBack(HoldBack)
+ /// Nothing shows its tool made it.
+ case notTheTools
+ /// Build data of an Xcode workspace opened in the last week, or with no date.
+ case workspaceMayBeInUse(lastUsed: Date?)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
index abb7d2aa..a43c83b1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
@@ -116,6 +116,34 @@ struct DeveloperCachesTests {
#expect(androidStudio.appBundleIdentifiers.contains("com.google.android.studio-EAP"))
}
+ @Test func saysWhyALocationStaysByTheRuleThatRecommendsIt() {
+ func location(_ kind: DeveloperEnvironment.ContentKind, size: Int64? = 10) -> DeveloperEnvironment.Location {
+ DeveloperEnvironment.Location(
+ url: URL(filePath: "/Users/me/Library/Caches/tool"), kind: kind, size: size, source: "https://example.com"
+ )
+ }
+ var held = location(.cache)
+ held.heldBack = .holdsAWallet
+ var another = location(.cache)
+ another.isTheTools = false
+ var opened = location(.buildData)
+ opened.workspace = DeveloperEnvironment.Workspace(name: "App", lastUsed: nil)
+ var closed = location(.cache, size: nil)
+ (closed.couldNotBeRead, closed.closedByPermissions) = (true, true)
+ let unmeasured = location(.cache, size: nil)
+
+ #expect(location(.cache).keptBecause == nil)
+ #expect(location(.models).keptBecause == .keptByDefault)
+ #expect(held.keptBecause == .heldBack(.holdsAWallet))
+ #expect(another.keptBecause == .notTheTools)
+ #expect(opened.keptBecause == .workspaceMayBeInUse(lastUsed: nil))
+ #expect(closed.keptBecause == .heldBack(.closedByPermissions))
+ #expect(unmeasured.keptBecause == .heldBack(.notMeasured))
+ for sample in [location(.cache), location(.models), held, another, opened, closed, unmeasured] {
+ #expect(sample.isRecommended == (sample.keptBecause == nil))
+ }
+ }
+
@Test func everyChannelOfZedKeepsItsFoldersWhileItRuns() throws {
let zed = try #require(DeveloperCaches.definitions.first { $0.id == "zed" })