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.

Chrome's cache can be emptied in Space again

Țugui Dragoșba9060373a49
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
index 196fd19e..660dd8a1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
@@ -72,6 +72,9 @@ public enum DeveloperCaches {
let systemImage: String
let appBundleIdentifiers: [String]
let folders: [Folder]
+ /// The tool's own folder inside a folder its vendor shares with other products, such as
+ /// `Library/Caches/Google/AndroidStudio*`. Space leaves only that part of the vendor's folder to Developer.
+ var ownFolders: [String] = []
}
/// A folder a tool keeps, relative to the home folder, and what it holds.
@@ -99,20 +102,25 @@ public enum DeveloperCaches {
}
}
- /// Returns the names of the children of `folder` that this table lists, or lists something inside, as
- /// patterns such as `AndroidStudio*`. Space leaves these folders to the Developer page. Both folders are read
- /// as the kernel names them, so another spelling of the same folder (another case, a link) finds the same.
- static func namesListed(inside folder: URL, home: URL) -> [String] {
+ /// The tools' own folders inside `folder`, as the name patterns leading to each (`["Google", "AndroidStudio*"]`),
+ /// which Space leaves to the Developer page. Both folders are read as the kernel names them, so another spelling
+ /// of the same folder (another case, a link) finds the same.
+ static func foldersLeftToDeveloper(inside folder: URL, home: URL) -> [[String]] {
// Not through `comparablePath`: standardizing drops `/private` only from a path that exists, and most
// paths in the table don't.
let base = PathComponents.of(PathPattern.canonical(folder).path(percentEncoded: false))
let home = PathComponents.of(PathPattern.canonical(home).path(percentEncoded: false))
- let names = definitions.flatMap(\.folders).compactMap { folder -> String? in
- let full = home + PathComponents.of(folder.path)
- guard full.count > base.count, full.starts(with: base) else { return nil }
- return full[base.count]
+ let owned = definitions.flatMap { definition in
+ let ownFolders = definition.ownFolders.map { home + PathComponents.of($0) }
+ return definition.folders.compactMap { folder -> [String]? in
+ let full = home + PathComponents.of(folder.path)
+ guard full.count > base.count, full.starts(with: base) else { return nil }
+ let declared = ownFolders.first { $0.count > base.count && full.starts(with: $0) }
+ let own = declared ?? Array(full.prefix(base.count + 1))
+ return Array(own[base.count...])
+ }
}
- return Array(Set(names)).sorted()
+ return Set(owned).sorted { $0.joined(separator: "/") < $1.joined(separator: "/") }
}
/// The folders the Developer page offers, relative to the home folder. Only folders one tool owns outright
@@ -501,7 +509,7 @@ public enum DeveloperCaches {
Folder("Library/Caches/Google/AndroidStudio*/index", .cache, source: "https://developer.android.com/studio/troubleshoot"),
Folder("Library/Caches/Google/AndroidStudio*/tmp", .cache, source: "https://developer.android.com/studio/troubleshoot"),
Folder("Library/Logs/Google/AndroidStudio*", .logs, source: "https://developer.android.com/studio/troubleshoot"),
- ]),
+ ], ownFolders: ["Library/Caches/Google/AndroidStudio*", "Library/Logs/Google/AndroidStudio*"]),
Definition(id: "neovim", name: "Neovim", systemImage: "curlybraces", appBundleIdentifiers: [], folders: [
Folder(".cache/nvim", .cache, source: "https://github.com/neovim/neovim/blob/master/runtime/doc/starting.txt#L1404-L1405"),
Folder(".local/share/nvim/lazy", .environments, source: "https://github.com/folke/lazy.nvim/blob/main/lua/lazy/core/config.lua#L8"),
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
index 5dd9b09d..2c8f2647 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
@@ -2,22 +2,22 @@ import AppKit
public import Foundation
/// Plans the emptying of one of Space's folders. The folder itself is never removed (the guard refuses it, and
-/// macOS expects to find it), so its children are listed instead. Each child passes through the same guard and
-/// exclusions as anything else, and one named for an open app is left where it is, since the app may still be
-/// writing to it.
+/// macOS expects to find it), so its children are listed instead, and inside a vendor's folder that holds a tool's
+/// own, the vendor's other children. Each passes through the same guard and exclusions as anything else, and one
+/// named for an open app is left where it is, since the app may still be writing to it.
public enum SpaceRemoval {
public struct Plan: Sendable {
- /// Children that will be moved to the Trash.
+ /// Folders and files that will be moved to the Trash.
public let removable: [URL]
- /// Children left alone because the app that owns them is open, each with the name of that app.
+ /// Those left alone because the app that owns them is open, each with the name of that app.
public let inUse: [(url: URL, name: String)]
- /// Children left to Developer, which lists something inside each.
+ /// Folders left to Developer, each a tool's own folder with something inside that Developer lists.
public let leftToDeveloper: [URL]
- /// The size of each removable child, which is what emptying frees. A child whose size is not known is
- /// missing here but stays in `removable`.
+ /// The size of each removable item, which is what emptying frees. One whose size is not known is missing
+ /// here but stays in `removable`.
public let sizes: [URL: Int64]
- /// Why a removable child is left for the person to choose, from what measuring it saw. A child missing here
- /// holds nothing that keeps it from being selected.
+ /// Why a removable item is left for the person to choose, from what measuring it saw. One missing here holds
+ /// nothing that keeps it from being selected.
public var heldBack: [URL: HoldBack] = [:]
/// The open apps whose folders are left alone, each named once and sorted, by the names the user knows, not
@@ -26,7 +26,7 @@ public enum SpaceRemoval {
Set(inUse.map(\.name)).sorted()
}
- /// What Peel selects of this plan for the person: every child it could measure that nothing holds back.
+ /// What Peel selects of this plan for the person: every item it could measure that nothing holds back.
public var suggested: Set<URL> {
Set(sizes.keys).subtracting(heldBack.keys)
}
@@ -83,23 +83,32 @@ public enum SpaceRemoval {
running: [String: String]
) -> (removable: [URL], inUse: [(url: URL, name: String)], leftToDeveloper: [URL]) {
let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)
- let children = item.urls.flatMap { url in
- (try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)) ?? []
- }
-
- let developerNames = item.urls.flatMap { DeveloperCaches.namesListed(inside: $0, home: environment.homeDirectory) }
var removable: [URL] = []
var inUse: [(url: URL, name: String)] = []
var leftToDeveloper: [URL] = []
- for child in children where removalGuard.allowsRemoval(of: child) {
- if let name = running[Naming.normalized(child.lastPathComponent)] {
- inUse.append((child, name))
- } else if developerNames.contains(where: { fnmatch($0, child.lastPathComponent, FNM_CASEFOLD) == 0 }) {
- leftToDeveloper.append(child)
- } else {
- removable.append(child)
+ func sort(_ folder: URL, leaving owned: [[String]]) {
+ let children = try? FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)
+ for child in children ?? [] {
+ let name = child.lastPathComponent
+ let app = running[Naming.normalized(name)]
+ let listed = owned.filter { fnmatch($0[0], name, FNM_CASEFOLD) == 0 }
+ if app == nil, !listed.isEmpty, !listed.contains(where: { $0.count == 1 }), child.isRealFolder {
+ sort(child, leaving: listed.map { Array($0.dropFirst()) })
+ continue
+ }
+ guard removalGuard.allowsRemoval(of: child) else { continue }
+ if let app {
+ inUse.append((child, app))
+ } else if !listed.isEmpty {
+ leftToDeveloper.append(child)
+ } else {
+ removable.append(child)
+ }
}
}
+ for url in item.urls {
+ sort(url, leaving: DeveloperCaches.foldersLeftToDeveloper(inside: url, home: environment.homeDirectory))
+ }
return (removable, inUse, leftToDeveloper)
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
index 21f6e791..bfd3f544 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
@@ -70,11 +70,12 @@ struct DeveloperCachesTests {
try FileManager.default.createSymbolicLink(at: directory.url.appending(path: "link"), withDestinationURL: directory.url.appending(path: "home"))
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
- let asWritten = DeveloperCaches.namesListed(inside: home.appending(path: "Library/Caches", directoryHint: .isDirectory), home: home)
- let otherCase = DeveloperCaches.namesListed(inside: home.appending(path: "library/caches", directoryHint: .isDirectory), home: home)
- let throughALink = DeveloperCaches.namesListed(inside: directory.url.appending(path: "link/Library/Caches", directoryHint: .isDirectory), home: home)
+ let left = { (folder: URL) in DeveloperCaches.foldersLeftToDeveloper(inside: folder, home: home) }
+ let asWritten = left(home.appending(path: "Library/Caches", directoryHint: .isDirectory))
+ let otherCase = left(home.appending(path: "library/caches", directoryHint: .isDirectory))
+ let throughALink = left(directory.url.appending(path: "link/Library/Caches", directoryHint: .isDirectory))
- #expect(asWritten.contains("Coursier"))
+ #expect(asWritten.contains(["Coursier"]))
#expect(otherCase == asWritten, "another case found \(otherCase)")
#expect(throughALink == asWritten, "a link found \(throughALink)")
}
@@ -207,6 +208,16 @@ struct DeveloperCachesTests {
}
}
+ @Test func everyOwnFolderHoldsWhatItsToolLists() {
+ for definition in DeveloperCaches.definitions {
+ for own in definition.ownFolders {
+ let names = PathComponents.of(own)
+ let holds = definition.folders.contains { PathComponents.of($0.path).starts(with: names) }
+ #expect(holds, "\(definition.id)'s \(own) holds none of its folders")
+ }
+ }
+ }
+
@Test func everyDefinitionIsDistinctAndNoFolderIsCountedTwice() {
let identifiers = DeveloperCaches.definitions.map(\.id)
#expect(Set(identifiers).count == identifiers.count)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
index 56292efb..d7f19869 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
@@ -50,6 +50,49 @@ struct SpaceRemovalTests {
#expect(logged.leftToDeveloper.map(\.lastPathComponent) == ["JetBrains"])
}
+ @Test func offersTheRestOfAVendorsFolderThatHoldsAToolsOwn() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Library/Caches/Google/Chrome/Default/Cache/Cache_Data/data_0")
+ try directory.file("home/Library/Caches/Google/AndroidStudio2026.1.4/caches/index.db")
+ try directory.file("home/Library/Caches/Google/AndroidStudio2026.1.4/LocalHistory/changes.storageData")
+ try directory.file("home/Library/Caches/Google/AndroidStudio2025.3.1/caches/index.db")
+ try directory.file("home/Library/Caches/Coursier/jvm/adoptium@17/bin/java")
+ try directory.file("home/Library/Logs/Google/AndroidStudio2026.1.4/idea.log")
+ try directory.file("home/Library/Logs/Google/GoogleUpdater/updater.log")
+ let logs = SpaceItem(
+ id: "logs",
+ category: .library,
+ urls: [directory.url.appending(path: "home/Library/Logs", directoryHint: .isDirectory)],
+ size: 0,
+ handling: .trash
+ )
+ let named = { (urls: [URL]) in
+ urls.map { "\($0.deletingLastPathComponent().lastPathComponent)/\($0.lastPathComponent)" }.sorted()
+ }
+
+ let caches = await SpaceRemoval.plan(for: item(directory), environment: environment(directory), running: [:])
+ let logged = await SpaceRemoval.plan(for: logs, environment: environment(directory), running: [:])
+
+ #expect(named(caches.removable) == ["Google/Chrome"])
+ #expect(named(caches.leftToDeveloper) == ["Caches/Coursier", "Google/AndroidStudio2025.3.1"])
+ #expect(named(logged.removable) == ["Google/GoogleUpdater"])
+ #expect(named(logged.leftToDeveloper) == ["Google/AndroidStudio2026.1.4"])
+ }
+
+ @Test func neverGoesThroughALinkIntoAnotherFolder() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Projects/Site/index.html")
+ try directory.directory("home/Library/Caches")
+ let google = directory.url.appending(path: "home/Library/Caches/Google")
+ let projects = directory.url.appending(path: "home/Projects")
+ try FileManager.default.createSymbolicLink(at: google, withDestinationURL: projects)
+
+ let plan = await SpaceRemoval.plan(for: item(directory), environment: environment(directory), running: [:])
+
+ #expect(plan.removable.isEmpty, "offered \(plan.removable.map(\.path))")
+ #expect(plan.leftToDeveloper.map(\.lastPathComponent) == ["Google"])
+ }
+
@Test func leavesAloneWhatAnOpenAppIsStillUsing() async throws {
let directory = try TemporaryDirectory()
try directory.file("home/Library/Caches/com.spotify.client/audio.db")