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.

An app of its own is never quit as another app's helper

Țugui Dragoș05fa3c878a02
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift b/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift
index c1d23efe..54918c65 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift
@@ -26,8 +26,9 @@ public enum RunningCopies {
/// Returns the processes in `running` that belong to `app`. The app itself is known by where it runs from:
/// a copy elsewhere is another app, and quitting it would close the one in use. The app's helpers run from
/// inside its bundle, or carry an identifier the bundle embeds or one that extends the app's, unless another
- /// installed app owns that identifier (Chrome Canary beside Chrome). A process whose location is unknown is
- /// judged by its identifier.
+ /// installed app owns that identifier (Chrome Canary beside Chrome) or it runs as an app of its own outside a
+ /// Library, as Canary does on another disk. A process whose location is unknown is judged by its identifier.
+ /// Identifiers are compared without case, as bundle identifiers are.
///
/// `sharingItsSettings` is for a reset: every copy of the app writes the same preference domain, wherever
/// it runs from, so all of them count.
@@ -35,24 +36,28 @@ public enum RunningCopies {
to app: InstalledApp,
among running: [Process],
installedApps: [InstalledApp],
- sharingItsSettings: Bool = false
+ sharingItsSettings: Bool = false,
+ environment: SearchEnvironment = .current
) -> [Process] {
let bundle = PathPattern.comparablePath(of: app.url)
- let embedded = Set(app.embeddedBundleIdentifiers)
+ let identifier = app.bundleIdentifier.lowercased()
+ let embedded = Set(app.embeddedBundleIdentifiers.map { $0.lowercased() })
let others = installedApps
- .filter { PathPattern.comparablePath(of: $0.url) != bundle && $0.bundleIdentifier != app.bundleIdentifier }
- .map(\.bundleIdentifier)
+ .filter { PathPattern.comparablePath(of: $0.url) != bundle }
+ .filter { $0.bundleIdentifier.lowercased() != identifier }
+ .map { $0.bundleIdentifier.lowercased() }
return running.filter { process in
let place = process.bundleURL.map(PathPattern.comparablePath)
if let place, PathComponents.isPath(place, inside: bundle) { return true }
- if process.bundleIdentifier == app.bundleIdentifier { return sharingItsSettings || place == nil || place == bundle }
- guard embedded.contains(process.bundleIdentifier) || process.bundleIdentifier.hasPrefix(app.bundleIdentifier + ".") else { return false }
+ let name = process.bundleIdentifier.lowercased()
+ if name == identifier { return sharingItsSettings || place == nil || place == bundle }
+ guard embedded.contains(name) || name.hasPrefix(identifier + ".") else { return false }
+ if let place, environment.keepsOnItsOwn(appAt: place) { return false }
// Leaves out what another installed app owns: its own identifier, or one that extends an identifier
// longer than this app's. So Canary's helpers belong to Canary, never to Chrome.
return !others.contains { other in
- process.bundleIdentifier == other
- || (other.count > app.bundleIdentifier.count && process.bundleIdentifier.hasPrefix(other + "."))
+ name == other || (other.count > identifier.count && name.hasPrefix(other + "."))
}
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
index 95326cd8..b017b970 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
@@ -65,18 +65,11 @@ public struct LeftoverScanner: Sendable {
let listed = Set(known.filter { wanted.contains($0.bundleIdentifier.lowercased()) }.map { PathPattern.comparablePath(of: PathPattern.canonical($0.url)) })
return identifiers.flatMap(AppInspector.applicationURLs).filter { url in
let path = PathPattern.comparablePath(of: PathPattern.canonical(url))
- return !listed.contains(path) && !PathComponents.isPath(path, atOrInside: bundle) && Self.standsOnItsOwn(path, in: environment)
+ return !listed.contains(path) && !PathComponents.isPath(path, atOrInside: bundle)
+ && environment.keepsOnItsOwn(appAt: path)
}.compactMap(AppInspector.inspect)
}
- /// True for an app at `path` that a person keeps on its own. One inside the home's Library or the Mac's is part of
- /// another app (a helper, an agent, an update being prepared, a build) and goes with that app's folder.
- static func standsOnItsOwn(_ path: String, in environment: SearchEnvironment) -> Bool {
- ![environment.homeDirectory, environment.rootDirectory].contains { folder in
- PathComponents.isPath(path, inside: PathPattern.comparablePath(of: folder.appending(path: "Library", directoryHint: .isDirectory)))
- }
- }
-
@concurrent
func scan(_ app: InstalledApp, matcher: LeftoverMatcher) async -> LeftoverScan {
let results = await withTaskGroup(of: (location: SearchLocation, result: LocationResult).self) { group in
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
index da0b67f7..b974ffa0 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
@@ -128,6 +128,15 @@ public struct SearchEnvironment: Sendable {
)
}
+ /// True for an app at `path` that a person keeps on its own. One inside the home's Library or the Mac's is part
+ /// of another app (a helper, an agent, an update being prepared, a build) and goes with that app.
+ func keepsOnItsOwn(appAt path: String) -> Bool {
+ ![homeDirectory, rootDirectory].contains { folder in
+ let library = folder.appending(path: "Library", directoryHint: .isDirectory)
+ return PathComponents.isPath(path, inside: PathPattern.comparablePath(of: library))
+ }
+ }
+
static let userEntries = LibraryFolder.user.map { (SearchLocation.Kind($0), $0.rawValue) }
static let localEntries = LibraryFolder.local.map { (SearchLocation.Kind($0), $0.rawValue) }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
index b90bc041..992738ef 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
@@ -116,12 +116,12 @@ struct LeftoverScannerTests {
@Test func anAppInALibraryFolderIsPartOfAnotherApp() {
let mac = SearchEnvironment(homeDirectory: URL(filePath: "/Users/x"), rootDirectory: URL(filePath: "/"))
- #expect(!LeftoverScanner.standsOnItsOwn("/Users/x/Library/Application Support/Sketchpad/SketchpadAgent.app", in: mac))
- #expect(!LeftoverScanner.standsOnItsOwn("/Library/Application Support/Sketchpad/Updater.app", in: mac))
- #expect(LeftoverScanner.standsOnItsOwn("/Users/x/Downloads/Sketchpad Nightly.app", in: mac))
- #expect(LeftoverScanner.standsOnItsOwn("/Volumes/Disk/Sketchpad.app", in: mac))
- #expect(LeftoverScanner.standsOnItsOwn("/System/Library/Input Methods/CharacterPalette.app", in: mac))
- #expect(LeftoverScanner.standsOnItsOwn("/Users/x/Libraryish/Sketchpad.app", in: mac))
+ #expect(!mac.keepsOnItsOwn(appAt: "/Users/x/Library/Application Support/Sketchpad/SketchpadAgent.app"))
+ #expect(!mac.keepsOnItsOwn(appAt: "/Library/Application Support/Sketchpad/Updater.app"))
+ #expect(mac.keepsOnItsOwn(appAt: "/Users/x/Downloads/Sketchpad Nightly.app"))
+ #expect(mac.keepsOnItsOwn(appAt: "/Volumes/Disk/Sketchpad.app"))
+ #expect(mac.keepsOnItsOwn(appAt: "/System/Library/Input Methods/CharacterPalette.app"))
+ #expect(mac.keepsOnItsOwn(appAt: "/Users/x/Libraryish/Sketchpad.app"))
}
/// The guard, which reads every spelling of a path from the disk, is asked only about what the scan would take
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RunningCopiesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RunningCopiesTests.swift
index 9439f30f..9aaf34f3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RunningCopiesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RunningCopiesTests.swift
@@ -77,6 +77,33 @@ struct RunningCopiesTests {
#expect(RunningCopies.belonging(to: old, among: running, installedApps: [chrome, old], sharingItsSettings: true).count == 1)
}
+ /// An app that continues the identifier and runs from somewhere Peel does not list apps, such as Chrome Canary
+ /// on another disk, is an app of its own. What runs from inside a Library, as an agent does, is the app's.
+ @Test func leavesAnAppOfItsOwnThatContinuesTheIdentifierWhereverItRuns() {
+ let running = [
+ process("com.google.Chrome.canary", at: "/Volumes/Disk/Google Chrome Canary.app"),
+ process("com.google.Chrome.agent", at: "/Users/x/Library/Application Support/Google/Agent.app"),
+ ]
+
+ let mac = SearchEnvironment(
+ homeDirectory: URL(filePath: "/Users/x", directoryHint: .isDirectory),
+ rootDirectory: URL(filePath: "/", directoryHint: .isDirectory)
+ )
+
+ let belonging = RunningCopies.belonging(to: chrome, among: running, installedApps: [chrome], environment: mac)
+
+ #expect(belonging.map(\.bundleIdentifier) == ["com.google.Chrome.agent"])
+ }
+
+ /// Bundle identifiers are case insensitive, so a process written in other capitals is still the app's.
+ @Test func readsIdentifiersWithoutCase() {
+ let chromeInCapitals = process("COM.GOOGLE.CHROME", at: "/Applications/Google Chrome.app")
+ #expect(belonging([chromeInCapitals]) == ["COM.GOOGLE.CHROME"])
+ #expect(belonging([process("com.google.chrome.helper", at: nil)]) == ["com.google.chrome.helper"])
+ #expect(belonging([process("com.google.keystone.agent", at: nil)]) == ["com.google.keystone.agent"])
+ #expect(belonging([process("COM.GOOGLE.CHROME.CANARY", at: nil)]).isEmpty)
+ }
+
/// If it is not known where a process runs from, a matching identifier counts it as the app, to be safe.
@Test func countsTheAppWhenWhereItRunsFromIsNotKnown() {
#expect(belonging([process("com.google.Chrome", at: nil)]) == ["com.google.Chrome"])