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.

Name what runs under an orphaned group's identifier one way everywhere

Țugui Dragoșe029e5756ced
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index e80e4cc2..7aa14271 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -104,12 +104,7 @@ struct OrphansCommand: AsyncParsableCommand {
// The same signals the app uses (the apps Peel remembers, the apps running, and what uninstalls left for an
// administrator), so both judge these files alike. What Peel remembers is only read here, never written.
let remembered = await AppMemory().load()
- let running = Dictionary(
- await RunningCopies.current.compactMap { process in
- process.bundleIdentifier.map { ($0, process.name ?? $0) }
- },
- uniquingKeysWith: { first, _ in first }
- )
+ let running = await RunningCopies.current
let exclusions = await UnreadableExclusions.load()
let scanner = OrphanScanner(exclusions: exclusions)
let owners = OrphanOwners().load()
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift b/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift
index d737b557..04405194 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift
@@ -25,6 +25,14 @@ public enum RunningCopies {
}
}
+ /// `processes` by their bundle identifier, the first of each, as the judgments of orphaned files read them.
+ public static func byIdentifier(_ processes: [Process]) -> [String: Process] {
+ Dictionary(
+ processes.compactMap { process in process.bundleIdentifier.map { ($0, process) } },
+ uniquingKeysWith: { first, _ in first }
+ )
+ }
+
@MainActor
public static var current: [Process] {
NSWorkspace.shared.runningApplications.compactMap { running in
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
index 246e1a81..9174c77d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
@@ -66,15 +66,15 @@ public struct OrphanConfidence: Sendable, Hashable {
public static func judge(
_ group: OrphanGroup,
installedTeams: Set<String> = [],
- running: [String: String] = [:],
+ running: [String: RunningCopies.Process] = [:],
now: Date = .now
) -> OrphanConfidence {
let declared = group.rememberedApp?.bundleIdentifier.lowercased()
let isTheGroups = { (identifier: String) in
isOneApp(identifier.lowercased(), group.identifier.lowercased(), declared: declared)
}
- if let match = running.keys.sorted().first(where: isTheGroups) {
- return OrphanConfidence(level: .unsure, reasons: [.running(running[match] ?? match)])
+ if let match = running.keys.filter(isTheGroups).min() {
+ return OrphanConfidence(level: .unsure, reasons: [.running(running[match]?.name ?? match)])
}
// A plug-in's row says so and isn't recommended, while what else the app left is judged by the signs below.
if group.items.allSatisfy({ $0.heldBack == .loadedByOtherApps }) {
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
index 3861b1eb..2b7e99cd 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
@@ -61,14 +61,14 @@ public struct OrphanScanner: Sendable {
///
/// `installedApps` must be the complete list: anything it does not claim can be reported as orphaned.
/// `remembered` holds the apps Peel has seen installed before, so a group can be named after the app that
- /// left it. `running` names what is running now by its bundle identifier: a helper or an agent can keep a
+ /// left it. `running` is what runs now, known by its bundle identifier: a helper or an agent can keep a
/// folder that no app bundle claims, and while it runs, its group is marked unsure. `owners` are the groups the
/// person said belong to an app (`OrphanOwners`), left out while that app is installed.
@concurrent
public func scan(
installedApps: [InstalledApp],
remembered: [RememberedApp] = [],
- running: [String: String] = [:],
+ running: [RunningCopies.Process] = [],
owners: [String: String] = [:],
leftForAnAdministrator records: [LeftForAnAdministrator.Record] = []
) async -> OrphanScan {
@@ -213,7 +213,7 @@ public struct OrphanScanner: Sendable {
$0.url.path(percentEncoded: false) < $1.url.path(percentEncoded: false)
}
let (groups, apart) = Self.settingApart(
- Self.group(kept, remembered: gone, installedTeams: teams, running: running)
+ Self.group(kept, remembered: gone, installedTeams: teams, running: RunningCopies.byIdentifier(running))
)
let recorded = await recorded(
records,
@@ -509,7 +509,7 @@ public struct OrphanScanner: Sendable {
_ found: [(identifier: String, item: OrphanItem)],
remembered: [RememberedApp] = [],
installedTeams: Set<String> = [],
- running: [String: String] = [:],
+ running: [String: RunningCopies.Process] = [:],
now: Date = .now
) -> [OrphanGroup] {
let roots = Set(found.map { groupingKey(for: $0.identifier).lowercased() }).sorted { $0.count < $1.count }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift
index 7957bf0c..ddd9e006 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift
@@ -75,11 +75,35 @@ struct OrphanConfidenceTests {
@Test("Something running under that identifier settles it, whatever else is true")
func runningOutranksEverything() {
let old = group(written: 400 * 24 * 60 * 60, remembered: left("Example"))
- let judged = OrphanConfidence.judge(old, running: ["com.example.app": "Example"], now: now)
+ let judged = OrphanConfidence.judge(old, running: running("com.example.app", bundle: "Example"), now: now)
#expect(judged.level == .unsure)
#expect(judged.reasons == [.running("Example")])
}
+ @Test("What runs is named as Finder names its bundle, else by its identifier, the first of each identifier")
+ func whatRunsIsNamedAsFinderNamesIt() {
+ let old = group(identifier: "com.example.app", written: 400 * 24 * 60 * 60)
+ let processes = [
+ process("com.example.app.helper", bundle: "Example Helper"), process("com.example.app.helper", bundle: "Other"),
+ ]
+ let byIdentifier = RunningCopies.byIdentifier(processes)
+ #expect(OrphanConfidence.judge(old, running: byIdentifier, now: now).reasons == [.running("Example Helper")])
+ #expect(OrphanConfidence.judge(old, running: running("com.example.app.helper"), now: now).reasons == [
+ .running("com.example.app.helper"),
+ ])
+ }
+
+ private func process(_ identifier: String, bundle: String? = nil) -> RunningCopies.Process {
+ RunningCopies.Process(
+ identifier: 1, bundleIdentifier: identifier,
+ bundleURL: bundle.map { URL(filePath: "/Volumes/org.example.none/\($0).app", directoryHint: .isDirectory) }
+ )
+ }
+
+ private func running(_ identifier: String, bundle: String? = nil) -> [String: RunningCopies.Process] {
+ [identifier: process(identifier, bundle: bundle)]
+ }
+
@Test("A folder written this week belongs to something that still runs, even if no app claims it")
func recentWritingOutranksAGoneApp() {
let fresh = group(written: 2 * 24 * 60 * 60, remembered: left("Example"))
@@ -144,31 +168,32 @@ struct OrphanConfidenceTests {
lastPath: "/Applications/Example.app"
)
let itsOwn = group(identifier: "md.example", written: 400 * 24 * 60 * 60, remembered: gone)
- #expect(OrphanConfidence.judge(itsOwn, running: ["md.example.helper": "Running md.example.helper"], now: now).reasons == [.running("Running md.example.helper")])
+ #expect(OrphanConfidence.judge(itsOwn, running: running("md.example.helper"), now: now).reasons == [.running("md.example.helper")])
let nobodysPrefix = group(identifier: "md.example", written: 400 * 24 * 60 * 60)
- #expect(OrphanConfidence.judge(nobodysPrefix, running: ["md.example.helper": "Running md.example.helper"], now: now).level == .certain)
+ #expect(OrphanConfidence.judge(nobodysPrefix, running: running("md.example.helper"), now: now).level == .certain)
}
@Test("What is running is matched the way an app is: a helper runs under its app's identifier and a longer one")
func aRunningHelperOrItsAppBothCount() {
let old = group(identifier: "com.example.app", written: 400 * 24 * 60 * 60)
- #expect(OrphanConfidence.judge(old, running: ["com.example.app.helper": "Running com.example.app.helper"], now: now).reasons == [.running("Running com.example.app.helper")])
- #expect(OrphanConfidence.judge(old, running: ["COM.EXAMPLE.APP": "Running COM.EXAMPLE.APP"], now: now).reasons == [.running("Running COM.EXAMPLE.APP")])
+ #expect(OrphanConfidence.judge(old, running: running("com.example.app.helper"), now: now).reasons == [.running("com.example.app.helper")])
+ #expect(OrphanConfidence.judge(old, running: running("COM.EXAMPLE.APP"), now: now).reasons == [.running("COM.EXAMPLE.APP")])
let helper = group(identifier: "com.example.app.helper", written: 400 * 24 * 60 * 60)
- #expect(OrphanConfidence.judge(helper, running: ["com.example.app": "Running com.example.app"], now: now).reasons == [.running("Running com.example.app")])
+ #expect(OrphanConfidence.judge(helper, running: running("com.example.app"), now: now).reasons == [.running("com.example.app")])
// Two components name a maker, not an app, and `com.example.apple` is not a helper of `com.example.app`.
let other = group(identifier: "com.example.other", written: 400 * 24 * 60 * 60)
- #expect(OrphanConfidence.judge(other, running: ["com.example": "Running com.example"], now: now).level == .certain)
- #expect(OrphanConfidence.judge(old, running: ["com.example.apple": "Running com.example.apple"], now: now).level == .certain)
+ #expect(OrphanConfidence.judge(other, running: running("com.example"), now: now).level == .certain)
+ #expect(OrphanConfidence.judge(old, running: running("com.example.apple"), now: now).level == .certain)
}
@Test("A Safari web app is its own app: Safari running, or another web app, says nothing about it")
func safariRunningSaysNothingOfAWebApp() {
let identifier = "com.apple.Safari.WebApp.0E4F6A2C-1B3D-4E5F-8A9B-0C1D2E3F4A5B"
let webApp = group(identifier: identifier, written: 400 * 24 * 60 * 60)
- #expect(OrphanConfidence.judge(webApp, running: ["com.apple.Safari": "Running com.apple.Safari"], now: now).level == .certain)
- #expect(OrphanConfidence.judge(webApp, running: ["com.apple.Safari.WebApp": "Running com.apple.Safari.WebApp"], now: now).level == .certain)
- #expect(OrphanConfidence.judge(webApp, running: [identifier: "Web App"], now: now).reasons == [.running("Web App")])
+ #expect(OrphanConfidence.judge(webApp, running: running("com.apple.Safari"), now: now).level == .certain)
+ #expect(OrphanConfidence.judge(webApp, running: running("com.apple.Safari.WebApp"), now: now).level == .certain)
+ let named = OrphanConfidence.judge(webApp, running: running(identifier, bundle: "Web App"), now: now)
+ #expect(named.reasons == [.running("Web App")])
}
@Test("An app Peel watched leave is the strongest thing it can say")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
index bb83e7c8..fa34e842 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
@@ -668,7 +668,11 @@ struct OrphanScannerTests {
}
let scan = await scanner(in: directory).scan(
- installedApps: installed, remembered: gone, running: ["com.gone.busy": "Busy"]
+ installedApps: installed, remembered: gone,
+ running: [RunningCopies.Process(
+ identifier: 1, bundleIdentifier: "com.gone.busy",
+ bundleURL: URL(filePath: "/Volumes/org.example.none/Busy.app", directoryHint: .isDirectory)
+ )]
)
let paths = { (urls: [URL]) in Set(urls.map(PathPattern.comparablePath(of:))) }
func group(_ identifier: String) -> OrphanGroup? { scan.groups.first { $0.identifier == identifier } }
diff --git a/Peel/Model/OrphanLibrary.swift b/Peel/Model/OrphanLibrary.swift
index 2b5774bc..582b5260 100644
--- a/Peel/Model/OrphanLibrary.swift
+++ b/Peel/Model/OrphanLibrary.swift
@@ -1,4 +1,3 @@
-import AppKit
import Foundation
import Observation
import PeelCore
@@ -79,12 +78,7 @@ final class OrphanLibrary {
let remembered = await AppMemory().remember(installedApps)
// What is currently running is better evidence than anything worked out from what is installed: a
// helper or an agent can keep a folder that no app bundle claims.
- let running = Dictionary(
- NSWorkspace.shared.runningApplications.compactMap { app in
- app.bundleIdentifier.map { ($0, app.localizedName ?? $0) }
- },
- uniquingKeysWith: { first, _ in first }
- )
+ let running = RunningCopies.current
let scan = await OrphanScanner(exclusions: ExclusionsStore.shared.exclusions).scan(
installedApps: installedApps,
remembered: remembered,