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 a plug-in as Finder would, and tell its two copies apart

Țugui Dragoș8f2ee38e95f2
diff --git a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
index 0df7671f..05769161 100644
--- a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
@@ -181,15 +181,20 @@ public enum Plugins {
return found
}
- /// The name a plug-in shows: the one its bundle declares, or its file's without the extension that says its kind.
- /// A workflow shows the name it has in the Services menu (`NSServices`, `NSMenuItem`), as the person knows it.
+ /// The name a plug-in shows: the one its bundle declares, in the Mac's language when the bundle has it, or its
+ /// file's without the extension that says its kind. A workflow shows the name it has in the Services menu
+ /// (`NSServices`, `NSMenuItem`), as the person knows it.
static func name(of url: URL, info: [String: Any]?) -> String {
if url.pathExtension.caseInsensitiveCompare("workflow") == .orderedSame,
let item = (info?["NSServices"] as? [[String: Any]])?.first?["NSMenuItem"] as? [String: Any],
let menuName = item["default"] as? String, !menuName.isEmpty {
return menuName
}
- return (info?["CFBundleName"] as? String) ?? url.deletingPathExtension().lastPathComponent
+ // `Bundle` reads a bundle without registering it with Launch Services, unlike asking for its display name.
+ let localized = Bundle(url: url)?.localizedInfoDictionary
+ let declared = [localized?["CFBundleDisplayName"], localized?["CFBundleName"], info?["CFBundleName"]]
+ .lazy.compactMap { ($0 as? String)?.trimmingCharacters(in: .whitespacesAndNewlines) }.first { !$0.isEmpty }
+ return declared ?? url.deletingPathExtension().lastPathComponent
}
/// The file extensions each kind of plug-in uses.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PluginsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PluginsTests.swift
index 92bfed2d..41ecb5e4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PluginsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PluginsTests.swift
@@ -74,6 +74,27 @@ struct PluginsTests {
#expect(Plugins.name(of: workflow, info: ["NSServices": [["NSMenuItem": ["default": ""]]]]) == "Example - ask")
}
+ @Test func namesAPlugInAsItsLanguageDoesOrByItsFile() throws {
+ let directory = try TemporaryDirectory()
+ func bundle(_ name: String, info: [String: Any], strings: String? = nil) throws -> URL {
+ try directory.file(
+ "\(name)/Contents/Info.plist",
+ contents: PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
+ )
+ if let strings {
+ try directory.file("\(name)/Contents/Resources/en.lproj/InfoPlist.strings", contents: Data(strings.utf8))
+ }
+ return directory.url.appending(path: name, directoryHint: .isDirectory)
+ }
+ let unnamed = try bundle("Vendor Product.component", info: ["CFBundleName": " "])
+ let localized = try bundle(
+ "Input.app", info: ["CFBundleName": "Input"], strings: "\"CFBundleName\" = \"Kana Input\";\n"
+ )
+
+ #expect(Plugins.name(of: unnamed, info: ["CFBundleName": " "]) == "Vendor Product")
+ #expect(Plugins.name(of: localized, info: ["CFBundleName": "Input"]) == "Kana Input")
+ }
+
@Test func findsPluginBundlesInUserAndSystemLibraries() async throws {
let directory = try TemporaryDirectory()
let info = try PropertyListSerialization.data(
diff --git a/Peel/Views/Plugins/PluginList.swift b/Peel/Views/Plugins/PluginList.swift
index 83cd362d..ee17a039 100644
--- a/Peel/Views/Plugins/PluginList.swift
+++ b/Peel/Views/Plugins/PluginList.swift
@@ -10,6 +10,7 @@ struct PluginList: View {
@Bindable var plugins = plugins
let filtered = filteredPlugins
let byCategory = Dictionary(grouping: filtered, by: \.category)
+ let forAllUsers = Plugin.sayingForAllUsers(plugins.plugins ?? [])
List(selection: $plugins.selection) {
if plugins.plugins?.isEmpty == false {
@@ -30,8 +31,12 @@ struct PluginList: View {
VStack(alignment: .leading, spacing: 1) {
Text(verbatim: plugin.name)
.lineLimit(1)
- if let version = plugin.version {
- Text(verbatim: version)
+ let details = [
+ plugin.version,
+ forAllUsers.contains(plugin.id) ? String(localized: "All users") : nil,
+ ].compactMap(\.self)
+ if !details.isEmpty {
+ Text(verbatim: details.joined(separator: " · "))
.font(.caption)
.foregroundStyle(.secondary)
}
@@ -116,11 +121,24 @@ struct PluginList: View {
private var filteredPlugins: [Plugin] {
let all = plugins.plugins ?? []
guard !searchText.isEmpty else { return all }
- return all.filter {
- SearchText.matches($0.name, searchText)
- || $0.bundleIdentifier.map { identifier in SearchText.matches(identifier, searchText) } == true
+ return all.filter { $0.matches(searchText) }
+ }
+}
+
+extension Plugin {
+ /// Whether a search for `text` finds it: by its name, by its file's name, which Finder shows, or by its identifier.
+ func matches(_ text: String) -> Bool {
+ [name, url.deletingPathExtension().lastPathComponent, bundleIdentifier].contains {
+ $0.map { SearchText.matches($0, text) } == true
}
}
+
+ /// The copies for all users of the plug-ins installed in both Libraries, whose rows would otherwise read alike.
+ static func sayingForAllUsers(_ plugins: [Plugin]) -> Set<Plugin.ID> {
+ let sameName = Dictionary(grouping: plugins) { "\($0.category.rawValue)/\($0.name)" }.values
+ return Set(sameName.filter { copies in copies.contains { !$0.isInstalledForAllUsers } }
+ .flatMap { copies in copies.filter(\.isInstalledForAllUsers).map(\.id) })
+ }
}
extension Plugin.Category {
diff --git a/PeelTests/PluginTextTests.swift b/PeelTests/PluginTextTests.swift
new file mode 100644
index 00000000..1c519899
--- /dev/null
+++ b/PeelTests/PluginTextTests.swift
@@ -0,0 +1,29 @@
+import Foundation
+@testable import Peel
+@testable import PeelCore
+import Testing
+
+struct PluginTextTests {
+ private func plugin(_ path: String, name: String, forAllUsers: Bool = false) -> Plugin {
+ Plugin(
+ url: URL(filePath: path, directoryHint: .isDirectory), name: name, bundleIdentifier: nil, version: nil,
+ category: .audioUnits, isInstalledForAllUsers: forAllUsers, size: 1, requiresPrivileges: forAllUsers,
+ refusal: nil
+ )
+ }
+
+ @Test func findsAPlugInByTheNameFinderShows() {
+ let product = plugin("/Library/Audio/Plug-Ins/Components/Vendor Product.component", name: "Product")
+
+ #expect(product.matches("Vendor"))
+ #expect(product.matches("Product"))
+ }
+
+ @Test func tellsTheCopyForAllUsersOfAPlugInInBothLibraries() {
+ let mine = plugin("/Users/org.example/Library/Audio/Plug-Ins/Components/Echo.component", name: "Echo")
+ let everyones = plugin("/Library/Audio/Plug-Ins/Components/Echo.component", name: "Echo", forAllUsers: true)
+ let alone = plugin("/Library/Audio/Plug-Ins/Components/Reverb.component", name: "Reverb", forAllUsers: true)
+
+ #expect(Plugin.sayingForAllUsers([mine, everyones, alone]) == [everyones.id])
+ }
+}