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.

Know a maker's folder by the developer its signature names

Țugui Dragoș2a7d499279be
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift b/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
index e7d1163d..7434515c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
@@ -154,6 +154,21 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
}
return names
}
+
+ /// What a folder of the app's or its maker's is called, as `Naming.folderName` writes it: the app's names, its
+ /// identifier, the maker's part of the identifier and that part's last name, and the leading words of the
+ /// developer its signature names (`BraveSoftware` for "Brave Software, Inc.").
+ var ownFolderNames: Set<String> {
+ var folders = Set(matchingNames.filter(Naming.isSignificant).map(Naming.normalized))
+ folders.insert(Naming.normalized(bundleIdentifier))
+ if let vendor = Identifier.vendor(of: bundleIdentifier.lowercased()) {
+ folders.insert(Naming.normalized(vendor))
+ folders.insert(Naming.normalized(String(vendor.split(separator: ".").last ?? "")))
+ }
+ let words = (developer ?? "").split { !$0.isLetter && !$0.isNumber }.map { $0.lowercased() }
+ for end in words.indices { folders.insert(words[...end].joined()) }
+ return folders.filter { !$0.isEmpty }
+ }
}
public enum Architecture: String, Sendable, Hashable, CaseIterable {
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
index 82a8e3be..2d139b7d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
@@ -113,12 +113,9 @@ struct LeftoverMatcher: Sendable {
return PathPattern.comparablePath(of: URL(filePath: program))
}
- /// Whether a folder with this name is the app's or its maker's. A folder named `X.localized` is named `X`
- /// (Apple, "Localizing the Name of a Directory").
+ /// Whether a folder with this name is the app's or its maker's.
func namesTheAppOrItsMaker(folder name: String) -> Bool {
- let suffix = ".localized"
- let name = name.lowercased().hasSuffix(suffix) ? String(name.dropLast(suffix.count)) : name
- return target.ownFolderNames.contains(Naming.normalized(name))
+ target.ownFolderNames.contains(Naming.folderName(name))
}
/// The other installed apps with any claim on `fileName`, by bundle identifier, and the other copies of this app
@@ -324,24 +321,11 @@ extension LeftoverMatcher {
vendorPrefix = vendor.map { $0 + "." }
names = app.matchingNames.filter(Naming.isSignificant).map { $0.lowercased() }
normalizedNames = Set(names.map(Naming.normalized))
- ownFolderNames = Self.folderNames(names: normalizedNames, vendor: vendor, developer: app.developer)
+ ownFolderNames = app.ownFolderNames
isApplesOwn = app.isSystemProtected || teamPrefix == Self.appleTeam
isASafariWebApp = app.isASafariWebApp
}
- /// What a folder of the app's or its maker's is called, normalized: the app's names, the maker's part of its
- /// identifier and that part's last name, and the leading words of the developer its signature names.
- private static func folderNames(names: Set<String>, vendor: String?, developer: String?) -> Set<String> {
- var folders = names
- if let vendor {
- folders.insert(Naming.normalized(vendor))
- folders.insert(Naming.normalized(String(vendor.split(separator: ".").last ?? "")))
- }
- let words = (developer ?? "").split { !$0.isLetter && !$0.isNumber }.map { $0.lowercased() }
- for end in words.indices { folders.insert(words[...end].joined()) }
- return folders.filter { !$0.isEmpty }
- }
-
func isSibling(of other: Profile) -> Bool {
if let vendorPrefix, vendorPrefix == other.vendorPrefix { return true }
if let teamPrefix, teamPrefix == other.teamPrefix { return true }
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/Naming.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/Naming.swift
index 238c8dad..a718e655 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/Naming.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/Naming.swift
@@ -15,6 +15,13 @@ enum Naming {
String(string.lowercased().filter { $0.isLetter || $0.isNumber })
}
+ /// The name a folder is known by: normalized, and without `.localized`, since a folder named `X.localized` is
+ /// named `X` (Apple, "Localizing the Name of a Directory").
+ static func folderName(_ name: String) -> String {
+ let suffix = ".localized"
+ return normalized(name.lowercased().hasSuffix(suffix) ? String(name.dropLast(suffix.count)) : name)
+ }
+
static func isSignificant(_ name: String) -> Bool {
let normalized = normalized(name)
return normalized.count >= 3 && !genericNames.contains(normalized)
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/MakersFolders.swift b/Packages/PeelCore/Sources/PeelCore/Removal/MakersFolders.swift
index b2e088ba..1935db2e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/MakersFolders.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/MakersFolders.swift
@@ -9,17 +9,12 @@ struct MakersFolders {
static let kinds: Set<SearchLocation.Kind> = [.applicationSupport, .caches, .logs, .plugIns, .library]
let environment: SearchEnvironment
- /// Lowercased, since disks ignore case by default.
+ /// As `Naming.folderName` writes them, so case and punctuation never tell two names apart.
let names: Set<String>
- /// Each app's identifier and name, its maker's part of the identifier, and that part's last name. Never Apple's.
+ /// The folder names of each app and its maker (`InstalledApp.ownFolderNames`). Never Apple's.
static func names(of apps: [InstalledApp]) -> Set<String> {
- Set(apps.flatMap { app -> [String] in
- guard !ProtectedData.isApplesName(app.bundleIdentifier) else { return [] }
- let maker = Identifier.vendor(of: app.bundleIdentifier)
- return [app.bundleIdentifier, app.name, maker, maker?.split(separator: ".").last.map(String.init)]
- .compactMap { $0?.lowercased() }
- })
+ Set(apps.filter { !ProtectedData.isApplesName($0.bundleIdentifier) }.flatMap(\.ownFolderNames))
}
/// Deepest first. A folder with another name stops the walk, since it may be any app's.
@@ -32,7 +27,7 @@ struct MakersFolders {
var folder = url.deletingLastPathComponent()
var path = PathPattern.comparablePath(of: folder)
while !ends.contains(path), places.contains(where: { PathComponents.isPath(path, inside: $0) }),
- names.contains(folder.lastPathComponent.lowercased()) {
+ names.contains(Naming.folderName(folder.lastPathComponent)) {
folders[path] = folder
folder = folder.deletingLastPathComponent()
path = PathPattern.comparablePath(of: folder)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
index d688bfa3..b83e758b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
@@ -728,6 +728,30 @@ struct UninstallationTests {
}
}
+ /// A maker's folder can be named for the developer the app's signature names rather than for its identifier,
+ /// as in `BraveSoftware/Brave-Browser`: once the profile has moved, the empty folder around it goes too.
+ @Test func aMakersFolderNamedForTheDeveloperIsTheMakers() throws {
+ let directory = try TemporaryDirectory()
+ let profile = try directory.directory("home/Library/Application Support/Seaside Works/Seaside-Browser")
+ let app = InstalledApp(
+ url: URL(filePath: "/Applications/Seaside Browser.app"),
+ bundleIdentifier: "org.example.browser",
+ name: "Seaside Browser",
+ developer: "Seaside Works, Inc."
+ )
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+
+ let found = MakersFolders(environment: environment, names: MakersFolders.names(of: [app])).above([profile])
+
+ #expect(
+ found.map(PathPattern.comparablePath(of:))
+ == [PathPattern.comparablePath(of: profile.deletingLastPathComponent())]
+ )
+ }
+
/// The receipt is what keeps macOS counting the package as installed, so it goes with the app. A receipt
/// proves an app only up to a separator: `com.example.app2.pkg` is another package's.
@Test func offersTheInstallerReceiptWithTheApp() async throws {