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.

peel asks which app is meant when Foo.app is also in the current folder

Țugui Dragoș8110279c28cb
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
index 8b2aff64..0ee27674 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
@@ -94,7 +94,7 @@ struct LeftoversCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "leftovers",
abstract: "Show the files an app leaves behind.",
- discussion: "APP can be the path to the app, its bundle identifier, or its name."
+ discussion: "APP can be the path to the app, its bundle identifier, or its name. A bundle in the current folder is written ./Name.app."
)
@Argument(help: "The app to inspect.")
@@ -224,7 +224,7 @@ struct UninstallCommand: AsyncParsableCommand {
"""
)
- @Argument(help: "The app to remove: its path, bundle identifier, or name.")
+ @Argument(help: "The app to remove: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.")
var app: String
@Flag(help: "Move only the app, not its leftovers.")
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
index 59157e7b..f4aab1c4 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/ExclusionCommands.swift
@@ -84,7 +84,7 @@ struct ExclusionsCommand: AsyncParsableCommand {
@Argument(help: "Files or folders to leave alone.", completion: .file())
var paths: [String] = []
- @Option(help: "An app to leave alone: its path, bundle identifier, or name.")
+ @Option(help: "An app to leave alone: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.")
var app: String?
func validate() throws {
@@ -123,7 +123,7 @@ struct ExclusionsCommand: AsyncParsableCommand {
@Argument(help: "Files or folders to stop leaving alone.", completion: .file())
var paths: [String] = []
- @Option(help: "An app to stop leaving alone: its path, bundle identifier, or name.")
+ @Option(help: "An app to stop leaving alone: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.")
var app: String?
func validate() throws {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/AppLookup.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/AppLookup.swift
index 89307a0d..2f818b77 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/AppLookup.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/AppLookup.swift
@@ -37,11 +37,15 @@ enum AppLookup {
/// the folder it runs from.
typealias Inspect = (URL) -> InstalledApp?
- /// Finds an app by path, bundle identifier, or name, tried in that order. A name may end in `.app`, like
- /// the bundle's file name.
- static func app(matching query: String, in apps: [InstalledApp], inspect: Inspect = AppInspector.inspect) throws(Failure) -> InstalledApp {
+ /// Finds an app by path, bundle identifier, or name, tried in that order, a path being read from `folder`. A name
+ /// may end in `.app`, like the bundle's file name; when a bundle of that name also sits in `folder` and is not
+ /// the app the name finds, the answer is ambiguous, since either may be meant (`./Foo.app` names the one there).
+ static func app(
+ matching query: String, in apps: [InstalledApp], from folder: URL = .currentFolder,
+ inspect: Inspect = AppInspector.inspect
+ ) throws(Failure) -> InstalledApp {
if query.contains("/") {
- return try app(at: query, in: apps, inspect: inspect)
+ return try app(at: query, in: apps, from: folder, inspect: inspect)
}
// Two bundles can share an identifier: a copy in `~/Applications` beside one in `/Applications`, or a
// beta beside the release. Taking the first match would remove whichever sorted first, so this throws.
@@ -53,21 +57,37 @@ enum AppLookup {
}
// Compared without the user's locale: under Turkish rules, `iina` wouldn't match `IINA`.
let named = apps.filter { names(of: $0).contains { $0.compare(bareName(query), options: [.caseInsensitive, .widthInsensitive]) == .orderedSame } }
+ let local = localBundle(named: query, in: folder).map(normalizedPath)
+ let paths = named.map { normalizedPath($0.url) }
+ if let local, !paths.isEmpty, !paths.contains(local) {
+ throw .ambiguous(query, paths + [local])
+ }
switch named.count {
case 0: break
case 1: return named[0]
- default: throw .ambiguous(query, named.map { normalizedPath($0.url) })
+ default: throw .ambiguous(query, paths)
}
// A name ending in `.app` may still be a bundle sitting in the current folder.
- guard query.lowercased().hasSuffix(".app"), let app = try? app(at: query, in: apps, inspect: inspect) else {
+ guard local != nil, let app = try? app(at: query, in: apps, from: folder, inspect: inspect) else {
throw .notFound(query)
}
return app
}
+ /// The bundle named `query` in `folder`, when `query` ends in `.app` and one is there.
+ private static func localBundle(named query: String, in folder: URL) -> URL? {
+ guard query.lowercased().hasSuffix(".app") else { return nil }
+ let url = URL(argument: query, relativeTo: folder)
+ var isFolder: ObjCBool = false
+ let isThere = FileManager.default.fileExists(atPath: url.path(percentEncoded: false), isDirectory: &isFolder)
+ return isThere && isFolder.boolValue ? url : nil
+ }
+
/// Finds the app at a path, which may be a bundle that isn't installed, such as one in the current folder.
- private static func app(at query: String, in apps: [InstalledApp], inspect: Inspect) throws(Failure) -> InstalledApp {
- let path = normalizedPath(URL(argument: query))
+ private static func app(
+ at query: String, in apps: [InstalledApp], from folder: URL, inspect: Inspect
+ ) throws(Failure) -> InstalledApp {
+ let path = normalizedPath(URL(argument: query, relativeTo: folder))
if let app = apps.first(where: { normalizedPath($0.url) == path }) {
return app
}
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift
index a9cf37f4..da499ed2 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Arguments.swift
@@ -122,8 +122,16 @@ extension URL {
/// A file, folder, or app given on the command line, relative to the current directory. What is there is
/// named as a file or as a folder, as the disk says, so it compares equal to the same item named anywhere else.
init(argument: String) {
- let currentDirectory = URL(filePath: FileManager.default.currentDirectoryPath, directoryHint: .isDirectory)
+ self.init(argument: argument, relativeTo: .currentFolder)
+ }
+
+ init(argument: String, relativeTo folder: URL) {
let path = (argument as NSString).expandingTildeInPath
- self = URL(filePath: path, directoryHint: .checkFileSystem, relativeTo: currentDirectory).standardizedFileURL
+ self = URL(filePath: path, directoryHint: .checkFileSystem, relativeTo: folder).standardizedFileURL
+ }
+
+ /// The folder the command was run from.
+ static var currentFolder: URL {
+ URL(filePath: FileManager.default.currentDirectoryPath, directoryHint: .isDirectory)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
index 45730fb9..05b79e7a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
@@ -117,6 +117,26 @@ struct CommandLineTests {
}
}
+ /// `cd ~/Downloads && peel uninstall Editor.app` could mean the installed Editor or the bundle in the current
+ /// folder, so neither is picked for the person; `./Editor.app` names the one there.
+ @Test func aBundleInTheCurrentFolderBesideAnInstalledOneIsNamedByItsPath() throws {
+ let directory = try TemporaryDirectory()
+ let local = try directory.directory("Downloads/Editor.app")
+ let downloads = directory.url.appending(path: "Downloads", directoryHint: .isDirectory)
+ let editor = InstalledApp(
+ url: URL(filePath: "/Applications/Editor.app", directoryHint: .isDirectory),
+ bundleIdentifier: "org.example.editor", name: "Editor"
+ )
+ let copy = InstalledApp(url: local, bundleIdentifier: "org.example.editor", name: "Editor")
+ let paths = ["/Applications/Editor.app", PathPattern.comparablePath(of: local)]
+
+ #expect(throws: AppLookup.Failure.ambiguous("Editor.app", paths)) {
+ try AppLookup.app(matching: "Editor.app", in: [editor], from: downloads)
+ }
+ #expect(try AppLookup.app(matching: "Editor", in: [editor], from: downloads) == editor)
+ #expect(try AppLookup.app(matching: "./Editor.app", in: [editor], from: downloads) { _ in copy } == copy)
+ }
+
@Test func refusesAnAppExcludedInSettings() throws {
let editor = InstalledApp(url: URL(filePath: "/Applications/Editor.app", directoryHint: .isDirectory), bundleIdentifier: "com.example.editor", name: "Editor")
diff --git a/Support/peel.1 b/Support/peel.1
index bd908783..3e658981 100644
--- a/Support/peel.1
+++ b/Support/peel.1
@@ -53,7 +53,7 @@ Show help information.
.It Em leftovers
Show the files an app leaves behind.
.Pp
-APP can be the path to the app, its bundle identifier, or its name.
+APP can be the path to the app, its bundle identifier, or its name. A bundle in the current folder is written ./Name.app.
.Bl -tag -width 6n
.It Ar app
The app to inspect.
@@ -73,7 +73,7 @@ Only leftovers Peel suggests are included. Items that need administrator access
The app itself moves first, so nothing of it goes to the Trash while it stays installed. Answering no exits with code 2; a removal that couldn't finish exits 1, and so does a privacy reset that didn't happen.
.Bl -tag -width 6n
.It Ar app
-The app to remove: its path, bundle identifier, or name.
+The app to remove: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.
.It Fl -keep-leftovers
Move only the app, not its leftovers.
.It Fl -reset-privacy
@@ -171,7 +171,7 @@ A whole disk, a home folder, or a Library is refused: excluding one would leave
.It Ar paths...
Files or folders to leave alone.
.It Fl -app Ar app
-An app to leave alone: its path, bundle identifier, or name.
+An app to leave alone: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.
.It Fl -version
Show the version.
.It Fl h , -help
@@ -183,7 +183,7 @@ Stop leaving something alone.
.It Ar paths...
Files or folders to stop leaving alone.
.It Fl -app Ar app
-An app to stop leaving alone: its path, bundle identifier, or name.
+An app to stop leaving alone: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.
.It Fl -version
Show the version.
.It Fl h , -help