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.

The peel://open link is built and read in one place, with tests

Țugui Dragoș7fea429adb13
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index cb5e328c..168c4f92 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -18,6 +18,8 @@ Peel is four programs built from one Xcode project, and a Swift package that hol
```mermaid
flowchart LR
Finder["Finder extension<br>sandboxed"] -- "peel://open" --> App["Peel.app<br>runs as you"]
+ Finder --> Link["PeelLink<br>what a link says"]
+ App --> Link
CLI["peel command<br>runs as you"] --> Core["PeelCore<br>scans, matches, moves"]
App --> Core
App -- "XPC, administrators only" --> Helper["Helper<br>runs as root"]
@@ -25,7 +27,7 @@ flowchart LR
Helper --> Privileged
```
-The package, `Packages/PeelCore`, has three libraries:
+The package, `Packages/PeelCore`, has four libraries:
- **PeelCore** decides and acts: it finds apps and their leftovers, measures folders, matches files to apps,
and moves things to the Trash. It has no interface and speaks plain English; the app words everything a
@@ -33,6 +35,8 @@ The package, `Packages/PeelCore`, has three libraries:
- **PeelPrivileged** is the part the helper shares: which paths may be touched, what is protected, how a tool
is run, and how a path is split into names. The helper links nothing else, so it stays small.
- **PeelCommandLine** holds the `peel` commands. `PeelCLI` is only their entry point.
+- **PeelLink** says what a `peel://open?path=` link means, for the Finder extension that sends it, the
+ notifications that send it too, and the app that reads it. The extension links nothing else.
Keeping the logic in the package is what makes it testable: `swift test --package-path Packages/PeelCore` runs
more than 900 tests without building the app.
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6915dffa..74f44b05 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -87,7 +87,8 @@ xcodebuild test -project Peel.xcodeproj -scheme PeelUITests -derivedDataPath bui
- `PeelCLI/`: the `peel` command line tool, a thin entry point.
- `PeelUITests/`: the accessibility audit, run by hand before a release.
- `Packages/PeelCore/`: everything that decides and acts, with the tests. `PeelCore` scans, matches, and removes;
- `PeelPrivileged` is the part the helper shares; `PeelCommandLine` holds the `peel` commands.
+ `PeelPrivileged` is the part the helper shares; `PeelCommandLine` holds the `peel` commands; `PeelLink` is the
+ `peel://open` link the Finder extension sends and the app reads.
- `Localization/`: every translation, and nothing else: the string catalogs of the app and the Finder extension.
- `Support/`: the Info.plists, the helper's launchd property list, and the manual page of `peel`.
- `Logo/`: the only source of the logo: the app icon (`Peel.icon`), the menu bar glyph, and `export.sh`, which
diff --git a/Packages/PeelCore/Package.swift b/Packages/PeelCore/Package.swift
index 9796880a..a628e993 100644
--- a/Packages/PeelCore/Package.swift
+++ b/Packages/PeelCore/Package.swift
@@ -17,18 +17,24 @@ let package = Package(
.library(name: "PeelCore", targets: ["PeelCore"]),
.library(name: "PeelPrivileged", targets: ["PeelPrivileged"]),
.library(name: "PeelCommandLine", targets: ["PeelCommandLine"]),
+ .library(name: "PeelLink", targets: ["PeelLink"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.8.2"),
],
targets: [
.target(name: "PeelPrivileged", swiftSettings: swiftSettings),
+ .target(name: "PeelLink", swiftSettings: swiftSettings),
.target(name: "PeelCore", dependencies: ["PeelPrivileged"], swiftSettings: swiftSettings),
.target(
name: "PeelCommandLine",
dependencies: ["PeelCore", .product(name: "ArgumentParser", package: "swift-argument-parser")],
swiftSettings: swiftSettings
),
- .testTarget(name: "PeelCoreTests", dependencies: ["PeelCore", "PeelPrivileged", "PeelCommandLine"], swiftSettings: swiftSettings),
+ .testTarget(
+ name: "PeelCoreTests",
+ dependencies: ["PeelCore", "PeelPrivileged", "PeelCommandLine", "PeelLink"],
+ swiftSettings: swiftSettings
+ ),
]
)
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/OpenRequest.swift b/Packages/PeelCore/Sources/PeelLink/OpenRequest.swift
similarity index 57%
rename from Packages/PeelCore/Sources/PeelCore/Apps/OpenRequest.swift
rename to Packages/PeelCore/Sources/PeelLink/OpenRequest.swift
index 16395261..230f4523 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/OpenRequest.swift
+++ b/Packages/PeelCore/Sources/PeelLink/OpenRequest.swift
@@ -1,6 +1,22 @@
public import Foundation
public enum OpenRequest {
+ public static func application(amongSelected items: [URL]) -> URL? {
+ guard items.count == 1, items[0].pathExtension == "app" else { return nil }
+ return items[0]
+ }
+
+ public static func link(toApplicationAt path: String) -> URL? {
+ var components = URLComponents(string: "peel://open")
+ components?.queryItems = [URLQueryItem(name: "path", value: path)]
+ return components?.url
+ }
+
+ public static func hostApplication(ofExtensionAt bundle: URL) -> URL? {
+ let host = bundle.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
+ return host.pathExtension == "app" ? host : nil
+ }
+
/// The app bundle to show for a dropped or opened file, or a `peel://open?path=…` link.
public static func applicationURL(from url: URL) -> URL? {
if url.isFileURL {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenRequestTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenRequestTests.swift
index 7308f5e2..b58eca92 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenRequestTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenRequestTests.swift
@@ -1,14 +1,11 @@
import Foundation
-import PeelCore
+import PeelLink
import Testing
/// What Peel's own notifications and the Finder menu send: `peel://open?path=` and the path of a bundle.
struct OpenRequestTests {
- /// Built the way `PeelNotifications` and the Finder extension build it.
private func link(for app: URL) throws -> URL {
- var components = try #require(URLComponents(string: "peel://open"))
- components.queryItems = [URLQueryItem(name: "path", value: app.path(percentEncoded: false))]
- return try #require(components.url)
+ try #require(OpenRequest.link(toApplicationAt: app.path(percentEncoded: false)))
}
/// A bundle is a folder, and a folder URL's path ends in a slash, which is the form every sender uses.
@@ -25,6 +22,14 @@ struct OpenRequestTests {
}
}
+ @Test func opensTheAppItsLinkNamesWhateverItsName() throws {
+ for name in ["Tom & Jerry.app", "a=b.app", "C++ #1 100%.app", "Ünïcode ?.app", "plus+sign.app"] {
+ let app = URL(filePath: "/Applications/\(name)", directoryHint: .isDirectory)
+ let opened = try #require(OpenRequest.applicationURL(from: try link(for: app)), "\(name) was refused")
+ #expect(opened.path(percentEncoded: false) == app.path(percentEncoded: false))
+ }
+ }
+
@Test func refusesWhatIsNotABundleOrNotALinkOfPeels() throws {
#expect(OpenRequest.applicationURL(from: try link(for: URL(filePath: "/Users/x/Documents/notes.txt"))) == nil)
#expect(OpenRequest.applicationURL(from: try link(for: URL(filePath: "/Users/x/Foo.app/Contents"))) == nil)
@@ -33,4 +38,24 @@ struct OpenRequestTests {
#expect(OpenRequest.applicationURL(from: try #require(URL(string: "https://example.com/Foo.app"))) == nil)
#expect(OpenRequest.applicationURL(from: URL(filePath: "/Users/x/Documents/notes.txt")) == nil)
}
+
+ @Test func theFinderMenuIsForExactlyOneApp() {
+ let app = URL(filePath: "/Applications/Foo.app", directoryHint: .isDirectory)
+ let other = URL(filePath: "/Applications/Bar.app", directoryHint: .isDirectory)
+ let document = URL(filePath: "/Users/x/Documents/notes.txt")
+
+ #expect(OpenRequest.application(amongSelected: [app]) == app)
+ #expect(OpenRequest.application(amongSelected: []) == nil)
+ #expect(OpenRequest.application(amongSelected: [document]) == nil)
+ #expect(OpenRequest.application(amongSelected: [app, other]) == nil)
+ #expect(OpenRequest.application(amongSelected: [app, document]) == nil)
+ }
+
+ @Test func theLinkGoesToTheCopyThatHoldsTheExtension() {
+ let inside = URL(filePath: "/Applications/Peel.app/Contents/PlugIns/PeelFinder.appex")
+ let alone = URL(filePath: "/Users/x/Build/Products/Debug/PeelFinder.appex")
+
+ #expect(OpenRequest.hostApplication(ofExtensionAt: inside) == URL(filePath: "/Applications/Peel.app/"))
+ #expect(OpenRequest.hostApplication(ofExtensionAt: alone) == nil)
+ }
}
diff --git a/Peel.xcodeproj/project.pbxproj b/Peel.xcodeproj/project.pbxproj
index 1562c424..e8feedb3 100644
--- a/Peel.xcodeproj/project.pbxproj
+++ b/Peel.xcodeproj/project.pbxproj
@@ -18,6 +18,8 @@
AA0000000000000000000029 /* PeelPrivileged in Frameworks */ = {isa = PBXBuildFile; productRef = AA0000000000000000000028 /* PeelPrivileged */; };
AA0000000000000000000030 /* com.tuguidragos.Peel.Helper.plist in Embed Launch Daemon */ = {isa = PBXBuildFile; fileRef = AA000000000000000000002F /* com.tuguidragos.Peel.Helper.plist */; };
AA0000000000000000000012 /* PeelCore in Frameworks */ = {isa = PBXBuildFile; productRef = AA0000000000000000000011 /* PeelCore */; };
+ AA0000000000000000000066 /* PeelLink in Frameworks */ = {isa = PBXBuildFile; productRef = AA0000000000000000000065 /* PeelLink */; };
+ AA0000000000000000000068 /* PeelLink in Frameworks */ = {isa = PBXBuildFile; productRef = AA0000000000000000000067 /* PeelLink */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -163,6 +165,7 @@
buildActionMask = 2147483647;
files = (
AA0000000000000000000012 /* PeelCore in Frameworks */,
+ AA0000000000000000000068 /* PeelLink in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -170,6 +173,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ AA0000000000000000000066 /* PeelLink in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -288,6 +292,9 @@
AA0000000000000000000058 /* PeelFinder */,
);
name = PeelFinder;
+ packageProductDependencies = (
+ AA0000000000000000000065 /* PeelLink */,
+ );
productName = PeelFinder;
productReference = AA0000000000000000000035 /* PeelFinder.appex */;
productType = "com.apple.product-type.app-extension";
@@ -341,6 +348,7 @@
name = Peel;
packageProductDependencies = (
AA0000000000000000000011 /* PeelCore */,
+ AA0000000000000000000067 /* PeelLink */,
);
productName = Peel;
productReference = AA0000000000000000000004 /* Peel.app */;
@@ -1011,6 +1019,14 @@
isa = XCSwiftPackageProductDependency;
productName = PeelCore;
};
+ AA0000000000000000000065 /* PeelLink */ = {
+ isa = XCSwiftPackageProductDependency;
+ productName = PeelLink;
+ };
+ AA0000000000000000000067 /* PeelLink */ = {
+ isa = XCSwiftPackageProductDependency;
+ productName = PeelLink;
+ };
/* End XCSwiftPackageProductDependency section */
};
rootObject = AA0000000000000000000001 /* Project object */;
diff --git a/Peel/Model/PeelNotifications.swift b/Peel/Model/PeelNotifications.swift
index c7b9bc31..8e2657e0 100644
--- a/Peel/Model/PeelNotifications.swift
+++ b/Peel/Model/PeelNotifications.swift
@@ -1,6 +1,7 @@
import AppKit
import Foundation
import PeelCore
+import PeelLink
import SwiftUI
import UserNotifications
@@ -69,10 +70,8 @@ final class PeelNotifications: NSObject, UNUserNotificationCenterDelegate {
nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
guard
let path = response.notification.request.content.userInfo[Self.pathKey] as? String,
- var components = URLComponents(string: "peel://open")
+ let url = OpenRequest.link(toApplicationAt: path)
else { return }
- components.queryItems = [URLQueryItem(name: "path", value: path)]
- guard let url = components.url else { return }
await MainActor.run {
_ = NSWorkspace.shared.open(url)
}
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index 8032c8e9..6b936c4c 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -1,4 +1,5 @@
import PeelCore
+import PeelLink
import SwiftUI
struct AppList: View {
diff --git a/Peel/Views/ContentView.swift b/Peel/Views/ContentView.swift
index 349335b7..27d43b0f 100644
--- a/Peel/Views/ContentView.swift
+++ b/Peel/Views/ContentView.swift
@@ -1,4 +1,5 @@
import PeelCore
+import PeelLink
import SwiftUI
struct ContentView: View {
diff --git a/PeelFinder/FinderExtension.swift b/PeelFinder/FinderExtension.swift
index 2f04c01a..827da4e9 100644
--- a/PeelFinder/FinderExtension.swift
+++ b/PeelFinder/FinderExtension.swift
@@ -1,5 +1,6 @@
import AppKit
import FinderSync
+import PeelLink
final class FinderExtension: FIFinderSync {
override init() {
@@ -22,32 +23,20 @@ final class FinderExtension: FIFinderSync {
}
@objc private func openInPeel(_ sender: NSMenuItem) {
- guard let application = selectedApplication, var components = URLComponents(string: "peel://open") else { return }
- components.queryItems = [URLQueryItem(name: "path", value: application.path(percentEncoded: false))]
- guard let url = components.url else { return }
+ guard
+ let application = selectedApplication,
+ let url = OpenRequest.link(toApplicationAt: application.path(percentEncoded: false))
+ else { return }
// Opens the URL in the copy of Peel that contains this extension. Launch Services might send `peel://`
// URLs to a different copy of Peel.
- guard let host = hostApplication else {
+ guard let host = OpenRequest.hostApplication(ofExtensionAt: Bundle.main.bundleURL) else {
NSWorkspace.shared.open(url)
return
}
NSWorkspace.shared.open([url], withApplicationAt: host, configuration: NSWorkspace.OpenConfiguration())
}
- /// The app that contains this extension, or `nil` when the extension is not inside an app.
- /// It is three levels up: `Peel.app/Contents/PlugIns/PeelFinder.appex`.
- private var hostApplication: URL? {
- let host = Bundle.main.bundleURL
- .deletingLastPathComponent()
- .deletingLastPathComponent()
- .deletingLastPathComponent()
- return host.pathExtension == "app" ? host : nil
- }
-
private var selectedApplication: URL? {
- guard let items = FIFinderSyncController.default().selectedItemURLs(), items.count == 1, items[0].pathExtension == "app" else {
- return nil
- }
- return items[0]
+ FIFinderSyncController.default().selectedItemURLs().flatMap(OpenRequest.application(amongSelected:))
}
}