From 5af1f09ee11ae74252093e339450af32ecfa9ad6 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Thu, 23 Dec 2021 13:27:19 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Ensure=20app=20can=20handle=20in?= =?UTF-8?q?teractions=20with=20path=20w/=20spaces=20(#74)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Domain/Helpers/Application.swift | 2 +- phpmon/Domain/Integrations/Valet/Valet.swift | 5 +++-- phpmon/Domain/SiteList/SiteListVC.swift | 21 +++++++++++++++----- phpmon/Localizable.strings | 3 +++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/phpmon/Domain/Helpers/Application.swift b/phpmon/Domain/Helpers/Application.swift index 6bd64f1..6632a9c 100644 --- a/phpmon/Domain/Helpers/Application.swift +++ b/phpmon/Domain/Helpers/Application.swift @@ -34,7 +34,7 @@ class Application { (This will open the app if it isn't open yet.) */ @objc public func openDirectory(file: String) { - return Shell.run("/usr/bin/open -a \"\(name)\" \(file)") + return Shell.run("/usr/bin/open -a \"\(name)\" \"\(file)\"") } /** Checks if the app is installed. */ diff --git a/phpmon/Domain/Integrations/Valet/Valet.swift b/phpmon/Domain/Integrations/Valet/Valet.swift index d8b88c8..128371d 100644 --- a/phpmon/Domain/Integrations/Valet/Valet.swift +++ b/phpmon/Domain/Integrations/Valet/Valet.swift @@ -167,11 +167,12 @@ class Valet { } public func determineSecured(_ tld: String) { - secured = Shell.fileExists("~/.config/valet/Certificates/\(self.name!).\(tld).key") + let name = self.name!.replacingOccurrences(of: " ", with: "\\ ") + secured = Shell.fileExists("~/.config/valet/Certificates/\(name).\(tld).key") } public func determineDriver() { - let driver = Shell.pipe("cd \(absolutePath!) && valet which", requiresPath: true) + let driver = Shell.pipe("cd \"\(absolutePath!)\" && valet which", requiresPath: true) if driver.contains("This site is served by") { self.driver = driver // TODO: Use a regular expression to retrieve the driver instead? diff --git a/phpmon/Domain/SiteList/SiteListVC.swift b/phpmon/Domain/SiteList/SiteListVC.swift index b03fad1..77815ab 100644 --- a/phpmon/Domain/SiteList/SiteListVC.swift +++ b/phpmon/Domain/SiteList/SiteListVC.swift @@ -171,7 +171,7 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { let originalSecureStatus = selectedSite!.secured let action = selectedSite!.secured ? "unsecure" : "secure" let selectedSite = selectedSite! - let command = "cd \(selectedSite.absolutePath!) && sudo \(Paths.valet) \(action) && exit;" + let command = "cd '\(selectedSite.absolutePath!)' && sudo \(Paths.valet) \(action) && exit;" waitAndExecute { Shell.run(command, requiresPath: true) @@ -204,16 +204,20 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { @objc public func openInBrowser() { let prefix = selectedSite!.secured ? "https://" : "http://" - let url = "\(prefix)\(selectedSite!.name!).\(Valet.shared.config.tld)" - NSWorkspace.shared.open(URL(string: url)!) + let url = URL(string: "\(prefix)\(selectedSite!.name!).\(Valet.shared.config.tld)") + if url != nil { + NSWorkspace.shared.open(url!) + } else { + warnAboutInvalidFolderAction() + } } @objc public func openInFinder() { - Shell.run("open \(selectedSite!.absolutePath!)") + Shell.run("open '\(selectedSite!.absolutePath!)'") } @objc public func openInTerminal() { - Shell.run("open -b com.apple.terminal \(selectedSite!.absolutePath!)") + Shell.run("open -b com.apple.terminal '\(selectedSite!.absolutePath!)'") } @objc public func unlinkSite() { @@ -239,6 +243,13 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { ) } + private func warnAboutInvalidFolderAction() { + _ = Alert.present( + messageText: "site_list.alert.invalid_folder_name".localized, + informativeText: "site_list.alert.invalid_folder_name_desc".localized + ) + } + // MARK: - (Search) Text Field Delegate func searchedFor(text: String) { diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index 42050fc..cf9ccda 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -74,6 +74,9 @@ "site_list.detected_apps" = "Detected Applications"; "site_list.system_apps" = "System Applications"; +"site_list.alert.invalid_folder_name" = "Invalid folder name"; +"site_list.alert.invalid_folder_name_desc" = "This folder could not be resolved to a valid URL. This is usually because there’s a space in the folder name. Please rename the folder, reload the list of sites, and try again."; + // EDITORS "editors.alert.try_again" = "Try Again";