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 another app's launch job alone when an app is uninstalled

Țugui Dragoș4e270468284d
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
index 292f5a91..241610b2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
@@ -54,8 +54,14 @@ struct LeftoverMatcher: Sendable {
guard let item, other.path.hasPrefix(item) else { return false }
return PathComponents.isPath(other.path, atOrInside: item)
}
+ // A job belongs to the app whose program it runs, so another app's program outranks any name.
+ let program = url.flatMap { Self.program(ofJobAt: $0, kind: kind) }
+ .flatMap { PathComponents.isPath($0, atOrInside: bundlePath) ? nil : $0 }
for other in others where !goesWithTheItem(other) {
- guard let rival = other.evidence(for: candidate) else {
+ let runsItsProgram = program.map { PathComponents.isPath($0, atOrInside: other.path) } ?? false
+ let rival = runsItsProgram
+ ? Evidence(.launchdJob, .certain, specificity: 0) : other.evidence(for: candidate)
+ guard let rival else {
// Siblings from the same maker, like Firefox Nightly beside Firefox, use the same files.
if target.isSibling(of: other), other.sharesName(with: candidate) {
share(with: other)
@@ -100,6 +106,13 @@ struct LeftoverMatcher: Sendable {
}
}
+ /// The absolute program the launchd job at `url` runs, spelled to be compared, or nil for anything else.
+ static func program(ofJobAt url: URL, kind: SearchLocation.Kind) -> String? {
+ guard kind == .launchAgents || kind == .launchDaemons, url.pathExtension == "plist" else { return nil }
+ guard let program = JobDefinition(contentsOf: url)?.program, program.hasPrefix("/") else { return nil }
+ return PathPattern.comparablePath(of: URL(filePath: program))
+ }
+
/// The other installed apps with any claim on `fileName`, by bundle identifier, and the other copies of this app
/// with one. A path a Homebrew cask names is checked this way too, so one shared with another app or another
/// copy is never selected for this app.
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
index 61b748a1..fc88e511 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
@@ -416,10 +416,9 @@ public struct LeftoverScanner: Sendable {
job url: URL,
kind: SearchLocation.Kind
) -> LeftoverMatch? {
- guard kind == .launchAgents || kind == .launchDaemons, url.pathExtension == "plist" else { return nil }
- guard let program = JobDefinition(contentsOf: url)?.program, program.hasPrefix("/") else { return nil }
- let path = PathPattern.comparablePath(of: URL(filePath: program))
- guard PathComponents.isPath(path, atOrInside: bundle) else { return nil }
+ guard let program = LeftoverMatcher.program(ofJobAt: url, kind: kind),
+ PathComponents.isPath(program, atOrInside: bundle)
+ else { return nil }
return LeftoverMatch(reason: .launchdJob, confidence: .certain, sharedWith: [])
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift
index df791576..d9b58b79 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift
@@ -53,6 +53,28 @@ struct LeftoverMatcherTests {
#expect(match("\(identifier).plist", in: .preferences, for: pretender) == nil)
}
+ @Test func aJobRunningAnotherInstalledAppsProgramIsThatApps() throws {
+ let directory = try TemporaryDirectory()
+ let tide = app("org.example.tide", name: "Tide")
+ let sync = app("org.example.tidesync", name: "Tide Sync")
+ let relay = app("net.other.relay", name: "Relay")
+ func job(_ name: String, running app: InstalledApp) throws -> URL {
+ let program = app.url.appending(path: "Contents/MacOS/Agent").path(percentEncoded: false)
+ let plist = ["Label": "org.example.job", "Program": program]
+ return try directory.file(name, contents: PropertyListSerialization.data(
+ fromPropertyList: plist, format: .xml, options: 0
+ ))
+ }
+ let matcher = LeftoverMatcher(app: tide, installedApps: [tide, sync, relay]) { _ in nil }
+ func claim(_ job: URL) -> LeftoverMatch? {
+ matcher.match(fileName: job.lastPathComponent, kind: .launchAgents, at: job)
+ }
+
+ #expect(claim(try job("org.example.tide.sync.plist", running: sync)) == nil)
+ #expect(claim(try job("Tide.plist", running: relay))?.sharedWith.contains("net.other.relay") == true)
+ #expect(claim(try job("org.example.tide.helper.plist", running: tide))?.isRecommended == true)
+ }
+
/// The list of installed apps covers only `/Applications` and `~/Applications`. Chrome Canary kept on another
/// disk is still installed, so Launch Services is asked about a longer identifier before it is taken for one
/// of Chrome's own.