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
Installed packages are read from pkgutil's property lists
Țugui Dragoșc52f6448476e
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swiftindex d8ea4898..b4f00c90 100644--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift@@ -32,7 +32,14 @@ public enum PackageReceipts {/// enough to find an app's own receipt and to tell whether a cask really belongs to an app.@concurrentpublic static func identifiers() async -> Set<String> {- Set((await pkgutil(["--pkgs"]) ?? "").split(whereSeparator: \.isNewline).map { String($0).lowercased() })+ Set((packageList(in: await pkgutil(["--pkgs-plist"])) ?? []).map { $0.lowercased() })+ }++ /// Reads what `pkgutil --pkgs-plist` prints: an array of package identifiers. Nil for no answer, or an answer+ /// in any other form, which says nothing about what is installed.+ static func packageList(in answer: String?) -> [String]? {+ guard let answer else { return nil }+ return (try? PropertyListSerialization.propertyList(from: Data(answer.utf8), format: nil)) as? [String]}/// Nil when the tool could not be run, failed, or timed out: no answer is not "nothing installed".@@ -48,11 +55,10 @@ public enum PackageReceipts {static func list(exclusions: Exclusions, pkgutil: @escaping Pkgutil, environment: SearchEnvironment = .current) async -> PackageScan {let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)let reach = HelperReach(environment: environment)- guard let answer = await pkgutil(["--pkgs"]) else { return PackageScan(receipts: [], couldNotAsk: true) }- let identifiers = answer- .split(whereSeparator: \.isNewline)- .map(String.init)- .filter { !$0.hasPrefix("com.apple.") }+ guard let packages = packageList(in: await pkgutil(["--pkgs-plist"])) else {+ return PackageScan(receipts: [], couldNotAsk: true)+ }+ let identifiers = packages.filter { !$0.hasPrefix("com.apple.") }let receipts = await withTaskGroup(of: PackageReceipt.self) { group invar pending = identifiers.makeIterator()@@ -79,7 +85,7 @@ public enum PackageReceipts {await receipts(installing: url, exclusions: exclusions, pkgutil: pkgutil)}- /// `pkgutil --file-info` matches a path against the receipt's file list, which is relative to the package's+ /// `pkgutil --file-info-plist` matches a path against the receipt's file list, which is relative to the package's/// install location. With the install location `Applications`, an app is found as `/Example.app`, not as/// `/Applications/Example.app`. So the leading folders are dropped one at a time until a package answers,/// and the answer counts only if that package really installed this path.@@ -88,7 +94,7 @@ public enum PackageReceipts {let wanted = PathPattern.comparablePath(of: PathPattern.canonical(url))var identifiers: Set<String> = []for path in Self.spellings(of: wanted) where identifiers.isEmpty && !Task.isCancelled {- identifiers = Set(Self.packageIdentifiers(in: await pkgutil(["--file-info", path]) ?? ""))+ identifiers = Set(Self.packageIdentifiers(in: await pkgutil(["--file-info-plist", path])))}var receipts: [PackageReceipt] = []@@ -110,14 +116,13 @@ public enum PackageReceipts {return (0..<max(1, components.count)).map { "/" + components.dropFirst($0).joined(separator: "/") }}- static func packageIdentifiers(in output: String) -> [String] {- output- .split(whereSeparator: \.isNewline)- .compactMap { line -> String? in- guard line.hasPrefix("pkgid: ") else { return nil }- return String(line.dropFirst("pkgid: ".count)).trimmingCharacters(in: .whitespaces)- }- .filter { !$0.hasPrefix("com.apple.") }+ /// Reads what `pkgutil --file-info-plist` prints: a `path-info` array with the `pkgid` of each package that+ /// lists the path. Apple's own packages are left out.+ static func packageIdentifiers(in answer: String?) -> [String] {+ guard let answer,+ let plist = try? PropertyListSerialization.propertyList(from: Data(answer.utf8), format: nil),+ let packages = (plist as? [String: Any])?["path-info"] as? [[String: Any]] else { return [] }+ return packages.compactMap { $0["pkgid"] as? String }.filter { !$0.hasPrefix("com.apple.") }}/// A receipt and the full paths of everything it lists, so `installs(_:)` can tell whether it installed a path.diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swiftindex 549389b3..ab5d02d1 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift@@ -230,7 +230,7 @@ struct ExclusionsReachEveryScannerTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--file-info": "pkgid: com.example.pkg\n"+ case "--file-info-plist": PkgutilAnswer.fileInfo(arguments[1], packages: "com.example.pkg")case "--pkg-info-plist": infocase "--files": "Applications/Example.app\n"default: ""@@ -323,7 +323,7 @@ struct ExclusionsReachEveryScannerTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.pkg\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.pkg")case "--pkg-info-plist": infocase "--files": "Applications/Example.app\nLibrary/Application Support/Example\n"default: ""diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swiftindex 81dc9299..f34df4a6 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift@@ -70,9 +70,9 @@ struct PackageReceiptsTests {#expect(PackageReceipts.topLevel(files: files, installLocation: "/", exists: { _ in true }).offered.isEmpty)}- /// `pkgutil --file-info` matches a path against the receipt's file list, which leaves out the package's- /// install prefix (such as `Applications`). So the path is asked about again without its leading folders,- /// and the answer is believed only if the receipt really installed that path.+ /// `pkgutil --file-info-plist` matches a path against the receipt's file list, which leaves out the+ /// package's install prefix (such as `Applications`). So the path is asked about again without its leading+ /// folders, and the answer is believed only if the receipt really installed that path.@Test func findsThePackageThatInstalledAnAppUnderAnInstallPrefix() async throws {let directory = try TemporaryDirectory()let volume = PathPattern.canonical(directory.url).path(percentEncoded: false)@@ -83,10 +83,12 @@ struct PackageReceiptsTests {))let asked = Mutex<[String]>([])let pkgutil: PackageReceipts.Pkgutil = { arguments in- if arguments.first == "--file-info" {+ if arguments.first == "--file-info-plist" {asked.withLock { $0.append(arguments[1]) }// Answers only for the path as the receipt lists it: the app without the install prefix.- return arguments[1] == "/Example.app" ? "volume: /\npkgid: com.example.pkg\n" : "volume: /\n"+ return arguments[1] == "/Example.app"+ ? PkgutilAnswer.fileInfo(arguments[1], packages: "com.example.pkg")+ : PkgutilAnswer.fileInfo(arguments[1])}return switch arguments.first {case "--pkg-info-plist": info@@ -122,7 +124,7 @@ struct PackageReceiptsTests {@Test func aPackageKnownOnlyByNameStaysListedAndUnknown() async throws {let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.quiet\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.quiet")default: nil}}@@ -148,7 +150,9 @@ struct PackageReceiptsTests {let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {// The package installed an app of the same name elsewhere, not the one asked about.- case "--file-info": arguments[1] == "/Example.app" ? "pkgid: com.example.pkg\n" : ""+ case "--file-info-plist": arguments[1] == "/Example.app"+ ? PkgutilAnswer.fileInfo(arguments[1], packages: "com.example.pkg")+ : PkgutilAnswer.fileInfo(arguments[1])case "--pkg-info-plist": infocase "--files": "Example.app\n"default: ""@@ -176,7 +180,7 @@ struct PackageReceiptsTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.pkg\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.pkg")case "--pkg-info-plist": infocase "--files": "usr/local/bin/tool\nusr/local/bin/tool3\n"default: ""@@ -197,11 +201,23 @@ struct PackageReceiptsTests {#expect(silent.couldNotAsk)#expect(silent.receipts.isEmpty)- let empty = await PackageReceipts.list(exclusions: .none, pkgutil: { _ in "" })+ let empty = await PackageReceipts.list(exclusions: .none, pkgutil: { _ in PkgutilAnswer.packages() })#expect(!empty.couldNotAsk)#expect(empty.receipts.isEmpty)}+ /// An answer that is not the property list `pkgutil` prints says nothing about what is installed, so it is+ /// no answer, rather than a list of packages read from whatever text came back.+ @Test func anAnswerInAnotherFormIsNoAnswer() async {+ let error: PackageReceipts.Pkgutil = { _ in "Error: the receipts could not be read.\n" }+ let scan = await PackageReceipts.list(exclusions: .none, pkgutil: error)++ #expect(scan.couldNotAsk)+ #expect(scan.receipts.isEmpty)+ #expect(PackageReceipts.packageList(in: "com.example.pkg\n") == nil)+ #expect(PackageReceipts.packageList(in: PkgutilAnswer.packages("com.example.pkg")) == ["com.example.pkg"])+ }+/// "Receipt only" is a fact about the disk. Hiding an item behind an exclusion does not make it true.@Test func doesNotCallAPackageForgottenBecauseItsItemsWereFilteredOut() async throws {let directory = try TemporaryDirectory()@@ -213,7 +229,7 @@ struct PackageReceiptsTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.pkg\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.pkg")case "--pkg-info-plist": infocase "--files": "Applications/Example.app\n"default: ""@@ -241,7 +257,7 @@ struct PackageReceiptsTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.pkg\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.pkg")case "--pkg-info-plist": infodefault: nil}@@ -268,7 +284,7 @@ struct PackageReceiptsTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.pkg\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.pkg")case "--pkg-info-plist": infocase "--files": "Applications/Example.app\nhome/Library/Mail/Bundles/Example.mailbundle\n"default: ""@@ -304,7 +320,7 @@ struct PackageReceiptsTests {))let pkgutil: PackageReceipts.Pkgutil = { arguments inswitch arguments.first {- case "--pkgs": "com.example.pkg\n"+ case "--pkgs-plist": PkgutilAnswer.packages("com.example.pkg")case "--pkg-info-plist": infocase "--files": "root/Library/Application Support/Example\nroot/Library/Example\nroot/Applications/Example Tools\n"default: ""diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PkgutilAnswer.swift b/Packages/PeelCore/Tests/PeelCoreTests/PkgutilAnswer.swiftnew file mode 100644index 00000000..3930a206--- /dev/null+++ b/Packages/PeelCore/Tests/PeelCoreTests/PkgutilAnswer.swift@@ -0,0 +1,19 @@+import Foundation++/// What `pkgutil` prints as property lists, for the stand-ins the tests pass in its place.+enum PkgutilAnswer {+ /// `pkgutil --pkgs-plist`: the identifiers of the installed packages.+ static func packages(_ identifiers: String...) -> String {+ xml(identifiers)+ }++ /// `pkgutil --file-info-plist <path>`: the packages whose receipts list `path`.+ static func fileInfo(_ path: String, packages: String...) -> String {+ xml(["path": path, "path-info": packages.map { ["pkgid": $0] }] as [String: Any])+ }++ private static func xml(_ plist: Any) -> String {+ let data = try! PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)+ return String(decoding: data, as: UTF8.self)+ }+}