From 108ae05c1d991b7001fd22166b178bae8d088ce0 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Wed, 5 Oct 2022 22:38:54 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Use=20new=20shell=20when=20parsi?= =?UTF-8?q?ng=20apps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Helpers/Application.swift | 37 +++++++++++++------ phpmon/Common/PHP/PHP Version/PhpHelper.swift | 4 +- .../DomainList/DomainListVC+Actions.swift | 4 +- phpmon/Domain/Menu/MainMenu+Startup.swift | 31 ++++++++++------ phpmon/Next/Testables.swift | 14 ++++++- 5 files changed, 62 insertions(+), 28 deletions(-) diff --git a/phpmon/Common/Helpers/Application.swift b/phpmon/Common/Helpers/Application.swift index ae50d34..0f1b1fe 100644 --- a/phpmon/Common/Helpers/Application.swift +++ b/phpmon/Common/Helpers/Application.swift @@ -33,31 +33,46 @@ class Application { Attempt to open a specific directory in the app of choice. (This will open the app if it isn't open yet.) */ - @objc public func openDirectory(file: String) { - return LegacyShell.run("/usr/bin/open -a \"\(name)\" \"\(file)\"") + @objc public func openDirectory(file: String) async { + return await Shell.quiet("/usr/bin/open -a \"\(name)\" \"\(file)\"") } /** Checks if the app is installed. */ - func isInstalled() -> Bool { - // If this script does not complain, the app exists! - return LegacyShell.user.executeSynchronously( + func isInstalled() async -> Bool { + + let (process, output) = try! await Shell.attach( "/usr/bin/open -Ra \"\(name)\"", - requiresPath: false - ).task.terminationStatus == 0 + didReceiveOutput: { _, _ in }, + withTimeout: 2.0 + ) + + if Shell is TestableShell { + // When testing, check the error output (must not be empty) + return !output.hasError + } else { + // If this script does not complain, the app exists! + return process.terminationStatus == 0 + } } /** Detect which apps are available to open a specific directory. */ - static public func detectPresetApplications() -> [Application] { - return [ + static public func detectPresetApplications() async -> [Application] { + var detected: [Application] = [] + + let detectable = [ Application("PhpStorm", .editor), Application("Visual Studio Code", .editor), Application("Sublime Text", .editor), Application("Sublime Merge", .git_gui), Application("iTerm", .terminal) - ].filter { - return $0.isInstalled() + ] + + for app in detectable where await app.isInstalled() { + detected.append(app) } + + return detected } } diff --git a/phpmon/Common/PHP/PHP Version/PhpHelper.swift b/phpmon/Common/PHP/PHP Version/PhpHelper.swift index d40736a..1165fa1 100644 --- a/phpmon/Common/PHP/PHP Version/PhpHelper.swift +++ b/phpmon/Common/PHP/PHP Version/PhpHelper.swift @@ -26,9 +26,9 @@ class PhpHelper { let canWriteSymlinks = FileManager.default.isWritableFile(atPath: "/usr/local/bin/") do { - LegacyShell.run("mkdir -p ~/.config/phpmon/bin") + Task { await Shell.quiet("mkdir -p ~/.config/phpmon/bin") } - if FileManager.default.fileExists(atPath: destination) { + if Filesystem.fileExists(destination) { let contents = try String(contentsOfFile: destination) if !contents.contains(keyPhrase) { Log.info("The file at '\(destination)' already exists and was not generated by PHP Monitor " diff --git a/phpmon/Domain/DomainList/DomainListVC+Actions.swift b/phpmon/Domain/DomainList/DomainListVC+Actions.swift index d90a491..23f17ed 100644 --- a/phpmon/Domain/DomainList/DomainListVC+Actions.swift +++ b/phpmon/Domain/DomainList/DomainListVC+Actions.swift @@ -107,9 +107,9 @@ extension DomainListVC { LegacyShell.run("open -b com.apple.terminal '\(selectedSite!.absolutePath)'") } - @objc func openWithEditor(sender: EditorMenuItem) { + @objc func openWithEditor(sender: EditorMenuItem) async { guard let editor = sender.editor else { return } - editor.openDirectory(file: selectedSite!.absolutePath) + await editor.openDirectory(file: selectedSite!.absolutePath) } @objc func isolateSite(sender: PhpMenuItem) { diff --git a/phpmon/Domain/Menu/MainMenu+Startup.swift b/phpmon/Domain/Menu/MainMenu+Startup.swift index efb89a2..901f14e 100644 --- a/phpmon/Domain/Menu/MainMenu+Startup.swift +++ b/phpmon/Domain/Menu/MainMenu+Startup.swift @@ -161,22 +161,29 @@ extension MainMenu { Detect which applications are installed that can be used to open a domain's source directory. */ private func detectApplications() { - Log.info("Detecting applications...") + Task { + Log.info("Detecting applications...") - App.shared.detectedApplications = Application.detectPresetApplications() + App.shared.detectedApplications = await Application.detectPresetApplications() - let customApps = Preferences.custom.scanApps?.map { appName in - return Application(appName, .user_supplied) - }.filter { app in - return app.isInstalled() - } ?? [] + let customApps = Preferences.custom.scanApps?.map { appName in + return Application(appName, .user_supplied) + } ?? [] - App.shared.detectedApplications.append(contentsOf: customApps) + var detectedCustomApps: [Application] = [] - let appNames = App.shared.detectedApplications.map { app in - return app.name + for app in customApps where await app.isInstalled() { + detectedCustomApps.append(app) + } + + App.shared.detectedApplications + .append(contentsOf: detectedCustomApps) + + let appNames = App.shared.detectedApplications.map { app in + return app.name + } + + Log.info("Detected applications: \(appNames)") } - - Log.info("Detected applications: \(appNames)") } } diff --git a/phpmon/Next/Testables.swift b/phpmon/Next/Testables.swift index 982ca7d..efa800e 100644 --- a/phpmon/Next/Testables.swift +++ b/phpmon/Next/Testables.swift @@ -77,10 +77,22 @@ class Testables { nicoverbruggen/cask shivammathur/php """), + "mkdir -p ~/.config/phpmon" + : .instant(""), "/opt/homebrew/bin/brew info php --json" : .instant(ShellStrings.brewJson), "brew info shivammathur/php/php --json" - : .instant("Error: No available formula with the name \"shivammathur/php/php\".") + : .instant("Error: No available formula with the name \"shivammathur/php/php\"."), + "/usr/bin/open -Ra \"PhpStorm\"" + : .instant("Unable to find application named 'PhpStorm'", .stdErr), + "/usr/bin/open -Ra \"Visual Studio Code\"" + : .instant("Unable to find application named 'Visual Studio Code'", .stdErr), + "/usr/bin/open -Ra \"Sublime Text\"" + : .instant("Unable to find application named 'Sublime Text'", .stdErr), + "/usr/bin/open -Ra \"Sublime Merge\"" + : .instant("Unable to find application named 'Sublime Merge'", .stdErr), + "/usr/bin/open -Ra \"iTerm\"" + : .instant("Unable to find application named 'iTerm'", .stdErr) ] ) }