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.

Own a background item by the bundle its program sits in

Țugui Dragoș6f63c7719603
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index 5914253b..36b3d5e9 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -180,7 +180,7 @@ public enum AppInspector {
infoDictionary(in: AppBundleLayout(of: url).infoFolder)?["CFBundleIdentifier"] as? String
}
- private static func displayName(of url: URL, identifier: String?) -> String {
+ static func displayName(of url: URL, identifier: String?) -> String {
let name = identifier.map { isKnown(url, as: $0) } == true
? FileManager.default.displayName(atPath: url.path(percentEncoded: false)) : url.lastPathComponent
return name.hasSuffix(".app") ? String(name.dropLast(4)) : name
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
index 85812dbc..06dce11a 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
@@ -10,7 +10,8 @@ public struct BackgroundItemOwnership: Sendable {
/// Where the app is, or nil when it is gone.
public let url: URL?
/// True when macOS registered the job for the app, or its program is signed by the app's team or sits
- /// inside the app. False when only a name points to the app: a label, or a bundle anyone can make.
+ /// inside the app (outside the scanned folders, an app signed by the program's team). False when only a name
+ /// points to the app: a label, or a bundle anyone can make.
public var isConfirmed = true
public var isInstalled: Bool { url != nil }
@@ -58,10 +59,22 @@ public struct BackgroundItemOwnership: Sendable {
.first(where: { $0.teamIdentifier == team }) {
return Owner(bundleIdentifier: app.bundleIdentifier, name: app.name, url: app.url)
}
- if let identifier = Self.bundleIdentifier(inProgramPath: program) {
- var owner = owner(forIdentifier: identifier)
- owner.isConfirmed = byIdentifier[identifier.lowercased()].map { proves(program, $0, team: team) } ?? false
- return owner
+ if let bundle = Self.bundle(holding: program) {
+ if let app = byIdentifier[bundle.identifier.lowercased()] {
+ return Owner(
+ bundleIdentifier: app.bundleIdentifier,
+ name: app.name,
+ url: app.url,
+ isConfirmed: proves(program, app, team: team)
+ )
+ }
+ let url = URL(filePath: bundle.path, directoryHint: .isDirectory)
+ return Owner(
+ bundleIdentifier: bundle.identifier,
+ name: AppInspector.displayName(of: url, identifier: bundle.identifier),
+ url: AppInspector.isInATrash(url) ? nil : url,
+ isConfirmed: team != nil && teamOfProgram(bundle.path) == team
+ )
}
if let app = appMatching(label: label) {
return Owner(
@@ -179,9 +192,15 @@ public struct BackgroundItemOwnership: Sendable {
.max { $0.key.count < $1.key.count }?.value
}
- static func bundleIdentifier(inProgramPath path: String?) -> String? {
- guard let path, let range = path.range(of: ".app/") else { return nil }
- let bundle = URL(filePath: String(path[..<range.lowerBound]) + ".app", directoryHint: .isDirectory)
- return AppInspector.infoDictionary(in: bundle.appending(path: "Contents", directoryHint: .isDirectory))?["CFBundleIdentifier"] as? String
+ /// The outermost app bundle `program` sits in, with the identifier its Info.plist declares.
+ static func bundle(holding program: String?) -> (path: String, identifier: String)? {
+ guard let program, let range = program.range(of: ".app/") else { return nil }
+ let path = String(program[..<range.lowerBound]) + ".app"
+ let contents = URL(filePath: path, directoryHint: .isDirectory)
+ .appending(path: "Contents", directoryHint: .isDirectory)
+ guard let identifier = AppInspector.infoDictionary(in: contents)?["CFBundleIdentifier"] as? String else {
+ return nil
+ }
+ return (path, identifier)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
index 0d5a568f..700c942a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
@@ -151,6 +151,35 @@ struct BackgroundItemOwnershipTests {
#expect(!planted.isConfirmed)
}
+ @Test func ownsAJobByTheBundleItsProgramSitsIn() throws {
+ let directory = try TemporaryDirectory()
+ for folder in ["Vendor", ".Trash"] {
+ try directory.file("\(folder)/Updater.app/Contents/Info.plist", contents: PropertyListSerialization.data(
+ fromPropertyList: ["CFBundleIdentifier": "org.example.updater"], format: .xml, options: 0))
+ }
+ let bundle = directory.url.appending(path: "Vendor/Updater.app").path(percentEncoded: false)
+ let program = bundle + "/Contents/MacOS/Updater"
+ func owner(_ teams: [String: String], program: String = program) -> BackgroundItemOwnership.Owner? {
+ BackgroundItemOwnership(installedApps: [], teamOfProgram: { teams[$0] })
+ .owner(label: "org.example.updater.agent", associated: [], program: program)
+ }
+
+ let signed = try #require(owner([program: "ABCDE12345", bundle: "ABCDE12345"]))
+ #expect(signed.bundleIdentifier == "org.example.updater")
+ #expect(signed.name == "Updater")
+ #expect(signed.url == URL(filePath: bundle, directoryHint: .isDirectory))
+ #expect(signed.isConfirmed)
+
+ let unsigned = try #require(owner([:]))
+ #expect(unsigned.url == URL(filePath: bundle, directoryHint: .isDirectory))
+ #expect(!unsigned.isConfirmed, "a bundle anyone can make proves nothing")
+ #expect(owner([program: "ABCDE12345", bundle: "ZZZZZ99999"])?.isConfirmed == false)
+ #expect(owner([program: "ABCDE12345"])?.isConfirmed == false)
+
+ let trashed = directory.url.appending(path: ".Trash/Updater.app/Contents/MacOS/Updater").path(percentEncoded: false)
+ #expect(owner([:], program: trashed)?.url == nil, "a copy in a Trash isn't installed")
+ }
+
@Test func knowsWhereTheOwnerIs() {
#expect(ownership.owner(label: "com.other.tool.helper", associated: [], program: nil)?.url == URL(filePath: "/Applications/Tool.app"))
let associated = ownership.owner(label: "org.unrelated.label", associated: ["com.example.app"], program: "/usr/local/bin/example-helper")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LaunchctlTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LaunchctlTests.swift
index 73aa7e5f..0e7d38ec 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LaunchctlTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LaunchctlTests.swift
@@ -184,7 +184,7 @@ struct LaunchctlTests {
.write(to: directory.file("Example.app/Contents/Info.plist"))
let helper = directory.url.appending(path: "Example.app/Contents/MacOS/Helper").path(percentEncoded: false)
- #expect(BackgroundItemOwnership.bundleIdentifier(inProgramPath: helper) == "com.example.app")
- #expect(BackgroundItemOwnership.bundleIdentifier(inProgramPath: "/usr/bin/true") == nil)
+ #expect(BackgroundItemOwnership.bundle(holding: helper)?.identifier == "com.example.app")
+ #expect(BackgroundItemOwnership.bundle(holding: "/usr/bin/true") == nil)
}
}