Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
peel's JSON says what --remove leaves and where Peel could not look
Țugui Dragoșfd5d6df483c3
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swiftindex 2ecaae38..f1af23d9 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift@@ -79,17 +79,27 @@ struct LeftoversCommand: AsyncParsableCommand {@OptionGroup var output: OutputOptions- private struct Report: Encodable {+ struct Report: Encodable {let app: AppRecord/// Nil when the app bundle couldn't be measured. Written as `null`, never left out, so a script always/// finds the key.let appSize: Int64?let leftovers: [LeftoverRecord]+ /// The folders macOS kept Peel out of, where the app may have left more.+ let unreadableLocations: [String]++ init(app: InstalledApp, appSize: Int64?, leftovers: [Leftover], unreadableLocations: [SearchLocation]) {+ self.app = AppRecord(app)+ self.appSize = appSize+ self.leftovers = leftovers.map(LeftoverRecord.init)+ self.unreadableLocations = unreadableLocations.map { Output.path($0.url) }+ }private enum CodingKeys: String, CodingKey {case appcase appSizecase leftovers+ case unreadableLocations}func encode(to encoder: any Encoder) throws {@@ -97,6 +107,7 @@ struct LeftoversCommand: AsyncParsableCommand {try container.encode(app, forKey: .app)try container.encode(appSize, forKey: .appSize)try container.encode(leftovers, forKey: .leftovers)+ try container.encode(unreadableLocations, forKey: .unreadableLocations)}}@@ -120,7 +131,8 @@ struct LeftoversCommand: AsyncParsableCommand {Self.notes(uninstallation, homebrew: homebrew, hidden: uninstallation.scan.leftovers.count - leftovers.count)if output.json {let appSize = uninstallation.isAppMeasured ? uninstallation.appSize : nil- try Output.json(Report(app: AppRecord(target), appSize: appSize, leftovers: leftovers.map(LeftoverRecord.init)))+ let unread = uninstallation.scan.unreadableLocations+ try Output.json(Report(app: target, appSize: appSize, leftovers: leftovers, unreadableLocations: unread))return}let appSize = uninstallation.isAppMeasured ? Output.size(uninstallation.appSize) : "unknown"diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swiftindex d3c724d3..b42d8df9 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift@@ -16,12 +16,39 @@ struct OrphansCommand: AsyncParsableCommand {@OptionGroup var output: OutputOptions- private struct Record: Encodable {- let identifier: String- let size: MeasuredSize- let confidence: String- let why: String- let files: [FileRecord]+ /// What `--json` writes: the groups, and the folders macOS kept Peel out of, where there may be more.+ struct Report: Encodable {+ let groups: [Group]+ let unreadableLocations: [String]++ struct Group: Encodable {+ let identifier: String+ let size: MeasuredSize+ let confidence: String+ let why: String+ let files: [File]+ }++ struct File: Encodable {+ let path: String+ let size: MeasuredSize+ /// Why `--remove` leaves the file where it is, or `null` when it would move it.+ let heldBack: String?++ private enum CodingKeys: String, CodingKey {+ case path+ case size+ case heldBack+ }++ /// Encodes a missing `heldBack` as `null`, for the reason `AppRecord` gives.+ func encode(to encoder: any Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(path, forKey: .path)+ try container.encode(size, forKey: .size)+ try container.encode(heldBack, forKey: .heldBack)+ }+ }}func validate() throws {@@ -47,15 +74,7 @@ struct OrphansCommand: AsyncParsableCommand {}if output.json {- try Output.json(scan.groups.map { group in- Record(- identifier: group.identifier,- size: MeasuredSize(group.total),- confidence: group.confidence.levelName,- why: group.confidence.summary,- files: group.items.map { FileRecord(path: Output.path($0.url), size: MeasuredSize($0.size)) }- )- })+ try Output.json(Self.report(for: scan))} else if scan.groups.isEmpty {Output.line("No orphaned files found.")} else {@@ -69,6 +88,24 @@ struct OrphansCommand: AsyncParsableCommand {}}+ static func report(for scan: OrphanScan) -> Report {+ Report(+ groups: scan.groups.map { group in+ Report.Group(+ identifier: group.identifier,+ size: MeasuredSize(group.total),+ confidence: group.confidence.levelName,+ why: group.confidence.summary,+ files: group.items.map { item in+ let size = MeasuredSize(item.size)+ return Report.File(path: Output.path(item.url), size: size, heldBack: item.heldBack?.rawValue)+ }+ )+ },+ unreadableLocations: scan.unreadableLocations.map { Output.path($0.url) }+ )+ }+static func heading(for group: OrphanGroup) -> String {"\(group.identifier) \(Output.size(group.total)) \(group.confidence.levelName): \(group.confidence.summary)"}@@ -151,6 +188,8 @@ struct CachesCommand: AsyncParsableCommand {let size: MeasuredSizelet kind: Stringlet source: String+ /// Whether `--remove` would move it.+ let suggested: Bool}}@@ -184,7 +223,13 @@ struct CachesCommand: AsyncParsableCommand {static func records(for environments: [DeveloperEnvironment]) -> [Record] {environments.map { environment inRecord(tool: environment.name, size: MeasuredSize(environment.total), locations: environment.locations.map {- Record.Location(path: Output.path($0.url), size: MeasuredSize($0.size), kind: $0.kind.rawValue, source: $0.source)+ Record.Location(+ path: Output.path($0.url),+ size: MeasuredSize($0.size),+ kind: $0.kind.rawValue,+ source: $0.source,+ suggested: $0.isRecommended+ )})}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swiftindex 65275e2e..8c8384d0 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift@@ -199,6 +199,18 @@ struct CommandLineTests {#expect(Set(records[0].keys) == Set(records[1].keys))}+ /// `peel leftovers --json` names the folders macOS kept Peel out of, where the app may have left more.+ @Test func theLeftoversReportSaysWhereItCouldNotLook() throws {+ let app = InstalledApp(url: URL(filePath: "/Applications/Editor.app", directoryHint: .isDirectory), bundleIdentifier: "com.example.editor", name: "Editor")+ let unread = SearchLocation(kind: .containers, url: URL(filePath: "/Users/me/Library/Containers", directoryHint: .isDirectory))++ let report = LeftoversCommand.Report(app: app, appSize: nil, leftovers: [], unreadableLocations: [unread])++ let json = try #require(JSONSerialization.jsonObject(with: Data(try Output.jsonText(report).utf8)) as? [String: Any])+ #expect(json["unreadableLocations"] as? [String] == ["/Users/me/Library/Containers"])+ #expect(json["appSize"] is NSNull)+ }+/// A script adds sizes up, so an unknown size is `null` under the same key: never zero, and never a missing/// key. In JSON a total with an unknown part is `null` too, and in text it reads "over" the known part.@Test func writesASizeNobodyKnowsAsNull() throws {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swiftindex b9f0f6e5..71a8abb2 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift@@ -376,6 +376,41 @@ struct FileCommandTests {])}+ /// `peel orphans --json` says for each file whether `--remove` leaves it and why, and which folders macOS kept+ /// Peel out of, so a script can tell an empty list from one that could not look everywhere.+ @Test func theOrphansListSaysWhatStaysAndWhereItCouldNotLook() throws {+ let directory = try TemporaryDirectory()+ let group = OrphanGroup(identifier: "com.example.gone", items: [+ try orphan("com.example.gone", in: directory),+ try orphan("com.example.gone.wallet", in: directory, heldBack: .holdsAWallet),+ ])+ let unread = SearchLocation(kind: .containers, url: URL(filePath: "/Users/me/Library/Containers", directoryHint: .isDirectory))++ let json = try Output.jsonText(OrphansCommand.report(for: OrphanScan(groups: [group], unreadableLocations: [unread])))++ let report = try #require(JSONSerialization.jsonObject(with: Data(json.utf8)) as? [String: Any])+ let files = try #require((report["groups"] as? [[String: Any]])?.first?["files"] as? [[String: Any]])+ #expect(files[0]["heldBack"] is NSNull)+ #expect(files[1]["heldBack"] as? String == "holdsAWallet")+ #expect(report["unreadableLocations"] as? [String] == ["/Users/me/Library/Containers"])+ }++ /// `peel caches --json` says which folders `--remove` would take.+ @Test func theCachesListSaysWhatRemoveWouldTake() throws {+ let directory = try TemporaryDirectory()+ let tool = try environment("Rust", id: "rust", locations: [+ ("Library/Caches/cargo", .cache),+ ("Library/Models/llama", .models),+ ], in: directory)++ let json = try Output.jsonText(CachesCommand.records(for: [tool]))++ let records = try #require(JSONSerialization.jsonObject(with: Data(json.utf8)) as? [[String: Any]])+ let locations = try #require(records.first?["locations"] as? [[String: Any]])+ #expect(locations[0]["suggested"] as? Bool == true)+ #expect(locations[1]["suggested"] as? Bool == false)+ }+/// A script reading `peel caches --json` gets, for each folder, where its tool documents it.@Test func tellsWhereEachCacheIsDocumented() throws {let directory = try TemporaryDirectory()