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.

Count Intel code once though a job or a command runs it

Țugui Dragoș17a652c5eecc
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
index 4e6cff53..0d5157b3 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
@@ -22,6 +22,8 @@ public struct IntelFinding: Sendable, Hashable, Identifiable {
public let size: Int64?
/// For a plug-in, its kind, which says where its Intel code can load (`Plugin.Category.intelHost`).
public var pluginCategory: Plugin.Category?
+ /// The labels of the launchd jobs that run a program at it or inside it.
+ public var backgroundItems: [String] = []
public var id: URL { url }
}
@@ -194,8 +196,16 @@ public enum IntelInspector {
))
}
+ // A job or a command that runs what is listed already, or something inside it, is that code's, and a job is
+ // named on its row.
+ var listed = findings.map { PathPattern.comparablePath(of: $0.url) }
for item in backgroundItems.items where !Task.isCancelled {
guard let program = item.program.map({ URL(filePath: $0) }), !exclusions.excludes(program) else { continue }
+ let path = PathPattern.comparablePath(of: program)
+ if let index = listed.firstIndex(where: { PathComponents.isPath(path, atOrInside: $0) }) {
+ findings[index].backgroundItems.append(item.label)
+ continue
+ }
guard isIntelOnly(executable: program, unreadable: &unreadable), isNew(program) else { continue }
findings.append(IntelFinding(
url: program,
@@ -204,14 +214,13 @@ public enum IntelInspector {
owner: item.plistURL?.lastPathComponent,
size: await measure(program),
))
+ listed.append(path)
}
- // A command that leads into an app listed already is that app's.
- let apps = findings.filter { $0.kind == .app }.map { PathPattern.comparablePath(of: $0.url) }
for tool in tools(in: toolDirectories(environment), unreadable: &unreadable)
where !Task.isCancelled && !exclusions.excludes(tool.url) {
let path = PathPattern.comparablePath(of: tool.url)
- guard !apps.contains(where: { PathComponents.isPath(path, inside: $0) }) else { continue }
+ guard !listed.contains(where: { PathComponents.isPath(path, atOrInside: $0) }) else { continue }
guard isIntelOnly(executable: tool.url, unreadable: &unreadable), isNew(tool.url) else { continue }
findings.append(IntelFinding(
url: tool.url,
@@ -472,9 +481,10 @@ public enum IntelInspector {
}
extension IntelFinding {
- /// Whether `query` matches what the row shows: the name, or what the finding belongs to.
+ /// Whether `query` matches what the row shows, the name or what the finding belongs to, or a job that runs it.
public func matches(_ query: String) -> Bool {
guard !query.isEmpty else { return true }
return SearchText.matches(name, query) || owner.map { SearchText.matches($0, query) } == true
+ || backgroundItems.contains { SearchText.matches($0, query) }
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
index 504491ce..d1465895 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
@@ -503,6 +503,49 @@ struct IntelInspectorTests {
#expect(!IntelInspector.embeddedDirectories.contains("Contents/Frameworks"))
}
+ @Test func countsCodeOnceThoughAJobOrACommandRunsIt() async throws {
+ let directory = try TemporaryDirectory()
+ let intelApp = try bundle(directory, "Apps/Intel.app", cpuTypes: [intel])
+ let agent = try directory.file("Apps/Intel.app/Contents/MacOS/Agent", contents: intelHeader(fileType: 2))
+ try directory.setPermissions(0o755, of: agent)
+ let universal = try bundle(directory, "Apps/Universal.app", cpuTypes: [intel, appleSilicon])
+ let helper = try bundle(directory, "Apps/Universal.app/Contents/Library/LoginItems/Helper.app", cpuTypes: [intel])
+ func job(_ label: String, running program: URL) -> BackgroundItem {
+ BackgroundItem(
+ label: label, kind: .agent, source: .userLibrary, plistURL: nil,
+ program: program.path(percentEncoded: false), runsAtLoad: true, keepsAlive: false,
+ ownerBundleIdentifier: nil, ownerName: nil, ownerURL: nil, isOrphan: false, state: .loaded,
+ isDisabled: false
+ )
+ }
+ let bin = try directory.directory("root/usr/local/bin")
+ try FileManager.default.createSymbolicLink(
+ at: bin.appending(path: "helper"), withDestinationURL: helper.appending(path: "Contents/MacOS/Helper")
+ )
+
+ let scan = await IntelInspector.scan(
+ installedApps: [
+ InstalledApp(url: intelApp, bundleIdentifier: "org.example.intel", name: "Intel", architectures: [.x86_64]),
+ InstalledApp(url: universal, bundleIdentifier: "org.example.universal", name: "Universal", architectures: [.x86_64, .arm64]),
+ ],
+ backgroundItems: BackgroundItems.Scan(items: [
+ job("org.example.intel.agent", running: agent),
+ job("org.example.universal.helper", running: helper.appending(path: "Contents/MacOS/Helper")),
+ ]),
+ exclusions: .none,
+ environment: environment(in: directory),
+ measure: { _ in 1 }
+ )
+
+ let rows = Dictionary(uniqueKeysWithValues: scan.findings.map { ($0.name, ($0.kind, $0.backgroundItems)) })
+ #expect(rows.count == 2, "listed \(scan.findings.map(\.name))")
+ #expect(rows["Intel"]?.0 == .app)
+ #expect(rows["Intel"]?.1 == ["org.example.intel.agent"])
+ #expect(rows["Helper.app"]?.0 == .insideApp)
+ #expect(rows["Helper.app"]?.1 == ["org.example.universal.helper"])
+ #expect(scan.findings.filter { $0.matches("intel.agent") }.map(\.name) == ["Intel"])
+ }
+
@Test func readsThePlugInFoldersItself() async throws {
let directory = try TemporaryDirectory()
_ = try bundle(directory, "home/Library/Audio/Plug-Ins/VST3/Old.vst3", cpuTypes: [intel])
diff --git a/Peel/Views/Intel/IntelDetailView.swift b/Peel/Views/Intel/IntelDetailView.swift
index 58f6ffc8..16770533 100644
--- a/Peel/Views/Intel/IntelDetailView.swift
+++ b/Peel/Views/Intel/IntelDetailView.swift
@@ -80,6 +80,12 @@ struct IntelDetailView: View {
LabeledContent("Size") {
Text(finding.size.byteCount).monospacedDigit()
}
+ if !finding.backgroundItems.isEmpty {
+ LabeledContent("Background Items") {
+ Text(verbatim: finding.backgroundItems.formatted(.list(type: .and)))
+ .textSelection(.enabled)
+ }
+ }
if let lastOpened = lastOpened(of: app) {
LabeledContent("Last opened") {
Text(lastOpened, format: .relative(presentation: .named))