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.

Leave a receipt to the installed app it names most closely

Țugui Dragoșeecb3d6c7e77
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
index e6706291..63799f5b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
@@ -28,6 +28,13 @@ public enum PackageReceipts {
return receipt == identifier || [".", "-", "_"].contains { receipt.hasPrefix(identifier + $0) }
}
+ /// The identifier among `identifiers` whose app `receipt` belongs to: the longest that proves it, since a receipt
+ /// named for one identifier and a separator can carry on into another app's (`…synth-fx.app.pkg` is Synth FX's,
+ /// not Synth's). All in lowercase.
+ public static func owner(of receipt: String, among identifiers: some Sequence<String>) -> String? {
+ identifiers.filter { proves(receipt, isThe: $0) }.max { $0.count < $1.count }
+ }
+
/// Returns the identifiers of all installed packages, in lowercase, without reading each receipt. That is
/// enough to find an app's own receipt and to tell whether a cask really belongs to an app.
@concurrent
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
index 28ef10ff..1d89732b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
@@ -60,7 +60,10 @@ public struct Uninstallation: Sendable {
)
}
scan = scan.adding(
- await receiptLeftovers(for: app, receipts: receipts, exclusions: exclusions, environment: environment)
+ await receiptLeftovers(
+ for: app, receipts: receipts, installedApps: installedApps, exclusions: exclusions,
+ environment: environment
+ )
)
scan = scan.adding(
await homebrewReceipt(
@@ -97,12 +100,14 @@ public struct Uninstallation: Sendable {
static func receiptLeftovers(
for app: InstalledApp,
receipts: Set<String>,
+ installedApps: [InstalledApp],
exclusions: Exclusions,
environment: SearchEnvironment
) async -> [Leftover] {
let identifier = app.bundleIdentifier.lowercased()
+ let identifiers = Set(installedApps.map { $0.bundleIdentifier.lowercased() } + [identifier])
var leftovers: [Leftover] = []
- for receipt in receipts.filter({ PackageReceipts.proves($0, isThe: identifier) }).sorted() {
+ for receipt in receipts.filter({ PackageReceipts.owner(of: $0, among: identifiers) == identifier }).sorted() {
for url in PackageActions.receiptFiles(of: receipt, onVolume: environment.rootDirectory)
where !exclusions.excludes(url) {
leftovers.append(await LeftoverScanner.leftover(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
index c893fbb7..d688bfa3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
@@ -744,6 +744,7 @@ struct UninstallationTests {
let found = await Uninstallation.receiptLeftovers(
for: app,
receipts: ["com.example.app.pkg", "com.example.app2.pkg", "com.other.thing"],
+ installedApps: [app],
exclusions: .none,
environment: environment
)
@@ -755,6 +756,36 @@ struct UninstallationTests {
#expect(found.map(\.kind) == [.receipts, .receipts])
}
+ /// A receipt whose name begins with this app's identifier can still be another installed app's, one whose
+ /// identifier carries on past the separator: `org.example.synth-fx.app.pkg` installed Synth FX, not Synth.
+ @Test func leavesAReceiptToTheAppItNamesMoreClosely() async throws {
+ let directory = try TemporaryDirectory()
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ let receipts = ["org.example.synth.app.pkg", "org.example.synth-fx.app.pkg"]
+ for name in receipts {
+ try directory.file("root/private/var/db/receipts/\(name).plist", bytes: 32)
+ }
+ let synth = InstalledApp(
+ url: URL(filePath: "/Applications/Synth.app"), bundleIdentifier: "org.example.synth", name: "Synth"
+ )
+ let effects = InstalledApp(
+ url: URL(filePath: "/Applications/Synth FX.app"), bundleIdentifier: "org.example.synth-fx", name: "Synth FX"
+ )
+
+ let found = await Uninstallation.receiptLeftovers(
+ for: synth,
+ receipts: Set(receipts),
+ installedApps: [synth, effects],
+ exclusions: .none,
+ environment: environment
+ )
+
+ #expect(found.map(\.url.lastPathComponent) == ["org.example.synth.app.pkg.plist"])
+ }
+
@Test func offersNoReceiptWhenNoneNamesTheApp() async throws {
let directory = try TemporaryDirectory()
let environment = SearchEnvironment(
@@ -766,6 +797,7 @@ struct UninstallationTests {
let found = await Uninstallation.receiptLeftovers(
for: app,
receipts: ["com.example.appointments"],
+ installedApps: [app],
exclusions: .none,
environment: environment
)