Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
The peel link command never replaces what may not be Peel's
Țugui Dragoș6a86ebf442da
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstringsindex fac3f971..84e0bf7d 100644--- a/Localization/Peel/Localizable.xcstrings+++ b/Localization/Peel/Localizable.xcstrings@@ -115579,6 +115579,112 @@}}},+ "Something else is at %@, and Peel offers no command that would replace it." : {+ "localizations" : {+ "cs" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "V umístění %@ je něco jiného a Peel nenabízí žádný příkaz, který by to nahradil."+ }+ },+ "de" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Unter %@ liegt etwas anderes, und Peel bietet keinen Befehl an, der es ersetzen würde."+ }+ },+ "es" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Hay otra cosa en %@ y Peel no ofrece ningún comando que la sustituya."+ }+ },+ "fr" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Autre chose se trouve à l’emplacement %@, et Peel ne propose aucune commande qui le remplacerait."+ }+ },+ "it" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "In %@ c’è qualcos’altro e Peel non offre alcun comando che lo sostituisca."+ }+ },+ "ja" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@には別のものがあります。Peelはそれを置き換えるコマンドを提示しません。"+ }+ },+ "ko" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@에 다른 항목이 있으며, Peel은 이를 대체하는 명령을 제공하지 않습니다."+ }+ },+ "nl" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Op %@ staat iets anders, en Peel biedt geen commando aan dat het zou vervangen."+ }+ },+ "pl" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "W %@ jest coś innego, a Peel nie oferuje polecenia, które by to zastąpiło."+ }+ },+ "pt" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Há outra coisa em %@, e o Peel não oferece nenhum comando que a substitua."+ }+ },+ "ro" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "La %@ se află altceva, iar Peel nu oferă nicio comandă care l-ar înlocui."+ }+ },+ "ru" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "По пути %@ уже находится что-то другое, и Peel не предлагает команду, которая бы это заменила."+ }+ },+ "sv" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Något annat finns på %@, och Peel erbjuder inget kommando som skulle ersätta det."+ }+ },+ "tr" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@ konumunda başka bir şey var ve Peel onu değiştirecek bir komut sunmuyor."+ }+ },+ "uk" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "За шляхом %@ уже є щось інше, і Peel не пропонує команди, яка б це замінила."+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@位置已有其他内容,Peel不提供会替换它的命令。"+ }+ },+ "zh-Hant" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@已有其他項目,Peel不提供會取代它的指令。"+ }+ }+ }+ },"Something else is there" : {"localizations" : {"cs" : {diff --git a/Packages/PeelCore/Sources/PeelCore/Home/Access.swift b/Packages/PeelCore/Sources/PeelCore/Home/Access.swiftindex 41f28f80..84724b99 100644--- a/Packages/PeelCore/Sources/PeelCore/Home/Access.swift+++ b/Packages/PeelCore/Sources/PeelCore/Home/Access.swift@@ -57,9 +57,12 @@ public enum CommandLineTool {public enum Standing: Sendable, Hashable {case installedcase missing- /// Something else is at the path: a link to another place or to a Peel that is gone, or a file that is- /// not a link. It may not be Peel's, so Peel never offers a command that removes it.- case pointsElsewhere+ /// A link to the tool inside another copy of Peel, moved or gone, or to this copy's tool when it does not run.+ /// Replacing it takes nothing but the link.+ case otherPeel+ /// A file that is not a link, or a link to anything but a Peel's tool. It may not be Peel's, so Peel never+ /// offers a command that replaces it.+ case somethingElse}/// Where Peel is running from. It decides whether a link to the tool inside Peel keeps working.@@ -80,11 +83,18 @@ public enum CommandLineTool {return applications.contains { PathComponents.isPath(path, inside: $0) } ? .applications : .elsewhere}- /// The shell command that links the tool, built from where Peel really is rather than a fixed path. A single- /// quote in the path is closed, escaped, and reopened, so the shell reads the path as written.- public static func installCommand(embedded: URL) -> String {+ /// The shell command that links the tool, built from where Peel really is rather than a fixed path, or nil+ /// where something that may not be Peel's is in the link's place. `ln -f` deletes what is there, so only a link+ /// to another Peel's tool is replaced. A single quote in the path is closed, escaped, and reopened, so the shell+ /// reads the path as written.+ public static func installCommand(embedded: URL, standing: Standing) -> String? {let path = embedded.path(percentEncoded: false).replacingOccurrences(of: "'", with: "'\\''")- return "sudo mkdir -p /usr/local/bin && sudo ln -sf '\(path)' \(CommandLineTool.path)"+ let link = switch standing {+ case .installed, .missing: "ln -s"+ case .otherPeel: "ln -sf"+ case .somethingElse: nil as String?+ }+ return link.map { "sudo mkdir -p /usr/local/bin && sudo \($0) '\(path)' \(CommandLineTool.path)" }}public static func standing(at path: String = CommandLineTool.path, embedded: URL) -> Standing {@@ -93,13 +103,19 @@ public enum CommandLineTool {var info = stat()guard lstat(path, &info) == 0 else { return .missing }// A file that is not a link: something else named `peel`, not Peel's tool.- return .pointsElsewhere+ return .somethingElse}let target = URL(filePath: destination, relativeTo: URL(filePath: path).deletingLastPathComponent()).standardizedFileURLguard target.path(percentEncoded: false) == embedded.standardizedFileURL.path(percentEncoded: false) else {- return .pointsElsewhere+ return isAPeelsTool(target) ? .otherPeel : .somethingElse}- return manager.isExecutableFile(atPath: target.path(percentEncoded: false)) ? .installed : .pointsElsewhere+ return manager.isExecutableFile(atPath: target.path(percentEncoded: false)) ? .installed : .otherPeel+ }++ /// Whether `url` is where a copy of Peel keeps its tool: `<name>.app/Contents/Helpers/peel`.+ private static func isAPeelsTool(_ url: URL) -> Bool {+ let names = url.pathComponents+ return names.count >= 4 && names.suffix(3) == ["Contents", "Helpers", "peel"] && names[names.count - 4].hasSuffix(".app")}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomeTests.swiftindex 12fe691e..fa239539 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HomeTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomeTests.swift@@ -227,13 +227,27 @@ struct PrivacyDatabaseTests {try FileManager.default.removeItem(atPath: link)try FileManager.default.createSymbolicLink(atPath: link, withDestinationPath: directory.url.appending(path: "Gone.app/Contents/Helpers/peel").path(percentEncoded: false))- #expect(standing() == .pointsElsewhere, "a link to a Peel that is gone read as installed")+ #expect(standing() == .otherPeel, "a link to a Peel that is gone read as installed")++ try FileManager.default.removeItem(atPath: link)+ try FileManager.default.createSymbolicLink(atPath: link, withDestinationPath: "/opt/other/bin/peel")+ #expect(standing() == .somethingElse, "a link to another program read as Peel's")try FileManager.default.removeItem(atPath: link)let stranger = try directory.file("stranger", bytes: 16)try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: stranger.path(percentEncoded: false))try FileManager.default.copyItem(atPath: stranger.path(percentEncoded: false), toPath: link)- #expect(standing() == .pointsElsewhere, "somebody else's executable read as Peel's tool")+ #expect(standing() == .somethingElse, "somebody else's executable read as Peel's tool")+ }++ /// `ln -f` deletes what is in the link's place, for good, so it replaces only a link to another Peel's tool,+ /// and Peel offers no command at all where something else is.+ @Test func neverOffersACommandThatReplacesWhatIsNotPeels() throws {+ let embedded = URL(filePath: "/Applications/Peel.app/Contents/Helpers/peel")++ #expect(CommandLineTool.installCommand(embedded: embedded, standing: .missing) == "sudo mkdir -p /usr/local/bin && sudo ln -s '/Applications/Peel.app/Contents/Helpers/peel' /usr/local/bin/peel")+ #expect(CommandLineTool.installCommand(embedded: embedded, standing: .otherPeel) == "sudo mkdir -p /usr/local/bin && sudo ln -sf '/Applications/Peel.app/Contents/Helpers/peel' /usr/local/bin/peel")+ #expect(CommandLineTool.installCommand(embedded: embedded, standing: .somethingElse) == nil)}/// Installed with Homebrew on a Mac with Apple silicon, the `peel` command is Homebrew's link in@@ -270,8 +284,8 @@ struct PrivacyDatabaseTests {#expect(place("/Volumes/Peel 1.0/Peel.app") == .elsewhere)#expect(place("/private/var/folders/x2/abc/d/AppTranslocation/41B9A/d/Peel.app") == .temporaryCopy)- let command = CommandLineTool.installCommand(embedded: URL(filePath: "/Users/me/Sam's Apps/Peel.app/Contents/Helpers/peel"))- #expect(command.hasSuffix("sudo ln -sf '/Users/me/Sam'\\''s Apps/Peel.app/Contents/Helpers/peel' /usr/local/bin/peel"))+ let command = CommandLineTool.installCommand(embedded: URL(filePath: "/Users/me/Sam's Apps/Peel.app/Contents/Helpers/peel"), standing: .missing)+ #expect(command?.hasSuffix("sudo ln -s '/Users/me/Sam'\\''s Apps/Peel.app/Contents/Helpers/peel' /usr/local/bin/peel") == true)}}/// The engine that turns "^[1 item](inflect: true)" into "1 item". It corrects a plural noun as well, so bothdiff --git a/Peel/Views/Home/HomePermissionsView.swift b/Peel/Views/Home/HomePermissionsView.swiftindex ed8b6c2c..696175a5 100644--- a/Peel/Views/Home/HomePermissionsView.swift+++ b/Peel/Views/Home/HomePermissionsView.swift@@ -165,8 +165,8 @@ struct HomePermissionsContent: View {private func action(_ permission: HomeModel.Permission, state: HomeModel.State) -> some View {if let action = home.action(for: permission) {// The command-line row uses `CopyButton`, which says "Copied" once the command is on the pasteboard.- if permission == .commandLine {- CopyButton(text: SettingsKey.commandLineInstallCommand, title: action)+ if permission == .commandLine, let command = SettingsKey.commandLineInstallCommand {+ CopyButton(text: command, title: action).buttonStyle(.stickerQuiet).accessibilityLabel(Text("\(String(localized: action)): \(String(localized: permission.title))", comment: "What VoiceOver reads for a button. The first %@ is the action, such as Install. The second is what it acts on, such as Helper."))} else {@@ -320,7 +320,7 @@ fileprivate extension HomeModel {case (.appManagement, .pending): "Checked at first removal"case (.commandLine, .on): "Installed"case (.commandLine, _):- HomeModel.commandLineStanding == .pointsElsewhere ? "Something else is there" : "Not installed"+ HomeModel.commandLineStanding == .somethingElse ? "Something else is there" : "Not installed"case (_, .on): "On"case (_, .pending): LocalizedStringResource("Unknown (permission state)", defaultValue: "Unknown")case (_, .off), (_, .missing): "Off"@@ -345,7 +345,7 @@ fileprivate extension HomeModel {case .finderExtension, .openAtLogin: return nil// Short on purpose: every row shares the grid's button column, so a wide label here would narrow// every row's name. The row's name says what is copied, and the note beside it explains.- case .commandLine: return state == .on ? nil : "Copy"+ case .commandLine: return state == .on || SettingsKey.commandLineInstallCommand == nil ? nil : "Copy"}}}diff --git a/Peel/Views/Settings/SettingsView.swift b/Peel/Views/Settings/SettingsView.swiftindex 376ead6d..bf87877f 100644--- a/Peel/Views/Settings/SettingsView.swift+++ b/Peel/Views/Settings/SettingsView.swift@@ -81,8 +81,9 @@ enum SettingsKey {static let skippedUpdateVersions = UpdatePreferences.Key.skippedVersionsstatic let updateMemory = "updateMemory"- static var commandLineInstallCommand: String {- CommandLineTool.installCommand(embedded: Bundle.main.bundleURL.appending(path: "Contents/Helpers/peel"))+ /// Nil while something that may not be Peel's is where the link goes.+ static var commandLineInstallCommand: String? {+ CommandLineTool.installCommand(embedded: Bundle.main.bundleURL.appending(path: "Contents/Helpers/peel"), standing: HomeModel.commandLineStanding)}}@@ -102,7 +103,7 @@ private func state(_ title: LocalizedStringResource, _ symbol: String, _ tint: C}private struct GeneralSettingsView: View {- private static var installCommand: String { SettingsKey.commandLineInstallCommand }+ private var installCommand: String? { SettingsKey.commandLineInstallCommand }private static let place = CommandLineTool.place()@AppStorage(SettingsKey.checksForAppUpdates) private var checksForAppUpdates = true@@ -262,8 +263,8 @@ private struct GeneralSettingsView: View {}LabeledContent {// Not offered from a temporary copy: a link into it would break as soon as Peel quits.- if Self.place != .temporaryCopy {- CopyButton(text: Self.installCommand)+ if Self.place != .temporaryCopy, let installCommand {+ CopyButton(text: installCommand)}} label: {titled("Command-line tool",@@ -282,10 +283,16 @@ private struct GeneralSettingsView: View {.foregroundStyle(.secondary)}if Self.place != .temporaryCopy {- Text(verbatim: Self.installCommand)- .font(.caption.monospaced())- .foregroundStyle(.secondary)- .textSelection(.enabled)+ if let installCommand {+ Text(verbatim: installCommand)+ .font(.caption.monospaced())+ .foregroundStyle(.secondary)+ .textSelection(.enabled)+ } else {+ Text("Something else is at \(Text(verbatim: CommandLineTool.path)), and Peel offers no command that would replace it.")+ .font(.caption)+ .foregroundStyle(.secondary)+ }}}