From 2958378411090e887101efb058e6787b95a6a8bf Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 29 Jan 2021 20:36:33 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Add=20static=20methods=20t?= =?UTF-8?q?o=20Shell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Classes/Commands/Actions.swift | 8 ++++---- phpmon/Classes/Commands/Startup.swift | 12 ++++++------ phpmon/Singletons/MainMenu.swift | 2 +- phpmon/Singletons/Shell.swift | 14 +++++++++++++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/phpmon/Classes/Commands/Actions.swift b/phpmon/Classes/Commands/Actions.swift index 255acba..6a5aa9e 100644 --- a/phpmon/Classes/Commands/Actions.swift +++ b/phpmon/Classes/Commands/Actions.swift @@ -14,7 +14,7 @@ class Actions { public static func detectPhpVersions() -> [String] { - let files = Shell.user.pipe("ls \(Paths.optPath()) | grep php@") + let files = Shell.pipe("ls \(Paths.optPath()) | grep php@") var versions = files.components(separatedBy: "\n") // Remove all empty strings @@ -167,7 +167,7 @@ class Actions { */ private static func brew(_ command: String, sudo: Bool = false) { - Shell.user.run("\(sudo ? "sudo " : "")" + "\(Paths.brew()) \(command)") + Shell.run("\(sudo ? "sudo " : "")" + "\(Paths.brew()) \(command)") } /** @@ -175,7 +175,7 @@ class Actions { */ private static func sed(file: String, original: String, replacement: String) { - Shell.user.run(""" + Shell.run(""" sed -i '' 's/\(original)/\(replacement)/g' \(file) """) } @@ -185,7 +185,7 @@ class Actions { */ private static func grepContains(file: String, query: String) -> Bool { - return Shell.user.pipe(""" + return Shell.pipe(""" grep -q '\(query)' \(file); [ $? -eq 0 ] && echo "YES" || echo "NO" """) .trimmingCharacters(in: .whitespacesAndNewlines) diff --git a/phpmon/Classes/Commands/Startup.swift b/phpmon/Classes/Commands/Startup.swift index c556c6a..f4a4312 100644 --- a/phpmon/Classes/Commands/Startup.swift +++ b/phpmon/Classes/Commands/Startup.swift @@ -31,34 +31,34 @@ class Startup { ) self.performEnvironmentCheck( - !Shell.user.pipe("ls \(Paths.optPath()) | grep php").contains("php"), + !Shell.pipe("ls \(Paths.optPath()) | grep php").contains("php"), messageText: "startup.errors.php_opt.title".localized, informativeText: "startup.errors.php_opt.desc".localized, breaking: true ) self.performEnvironmentCheck( - !Shell.user.pipe("which valet").contains("/usr/local/bin/valet"), + !Shell.pipe("which valet").contains("/usr/local/bin/valet"), messageText: "startup.errors.valet_executable.title".localized, informativeText: "startup.errors.valet_executable.desc".localized, breaking: true ) self.performEnvironmentCheck( - !Shell.user.pipe("cat /private/etc/sudoers.d/brew").contains("\(Paths.binPath())/brew"), + !Shell.pipe("cat /private/etc/sudoers.d/brew").contains("\(Paths.binPath())/brew"), messageText: "startup.errors.sudoers_brew.title".localized, informativeText: "startup.errors.sudoers_brew.desc".localized, breaking: true ) self.performEnvironmentCheck( - !Shell.user.pipe("cat /private/etc/sudoers.d/valet").contains("/usr/local/bin/valet"), + !Shell.pipe("cat /private/etc/sudoers.d/valet").contains("/usr/local/bin/valet"), messageText: "startup.errors.sudoers_valet.title".localized, informativeText: "startup.errors.sudoers_valet.desc".localized, breaking: true ) - let services = Shell.user.pipe("\(Paths.brew()) services list | grep php") + let services = Shell.pipe("\(Paths.brew()) services list | grep php") self.performEnvironmentCheck( (services.countInstances(of: "started") > 1), messageText: "startup.errors.services.title".localized, @@ -81,7 +81,7 @@ class Startup { print("PHP Monitor has determined the application has successfully passed all checks.") print("Determining which version of PHP is aliased to `php` via Homebrew...") - let brewPhpAlias = Shell.user.pipe("\(Paths.brew()) info php --json"); + let brewPhpAlias = Shell.pipe("\(Paths.brew()) info php --json"); App.shared.brewPhpPackage = try! JSONDecoder().decode( [HomebrewPackage].self, diff --git a/phpmon/Singletons/MainMenu.swift b/phpmon/Singletons/MainMenu.swift index 4823673..87b7358 100644 --- a/phpmon/Singletons/MainMenu.swift +++ b/phpmon/Singletons/MainMenu.swift @@ -213,7 +213,7 @@ class MainMenu: NSObject, NSWindowDelegate { @objc public func openPhpInfo() { self.waitAndExecute({ try! " /tmp/phpmon_phpinfo.html") + Shell.run("\(Paths.binPath())/php-cgi -q /tmp/phpmon_phpinfo.php > /tmp/phpmon_phpinfo.html") }, { NSWorkspace.shared.open(URL(string: "file:///private/tmp/phpmon_phpinfo.html")!) }) diff --git a/phpmon/Singletons/Shell.swift b/phpmon/Singletons/Shell.swift index bca6bf2..a71ce70 100644 --- a/phpmon/Singletons/Shell.swift +++ b/phpmon/Singletons/Shell.swift @@ -9,6 +9,18 @@ import Cocoa class Shell { + // MARK: - Invoke static functions + + public static func run(_ command: String) { + Shell.user.run(command) + } + + public static func pipe(_ command: String, shell: String = "/bin/sh") -> String { + Shell.user.pipe(command, shell: shell) + } + + // MARK: - Singleton + /** Singleton to access a user shell (with --login) */ @@ -50,7 +62,7 @@ class Shell { Checks if a file exists at the provided path. */ public static func fileExists(_ path: String) -> Bool { - return Shell.user.pipe( + return Shell.pipe( "if [ -f \(path) ]; then echo \"PHP_Y_FE\"; fi" ).contains("PHP_Y_FE") }