1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-08 04:20:07 +02:00

♻️ Add static methods to Shell

This commit is contained in:
2021-01-29 20:36:33 +01:00
parent 3a2cd3bff6
commit 2958378411
4 changed files with 24 additions and 12 deletions

View File

@ -14,7 +14,7 @@ class Actions {
public static func detectPhpVersions() -> [String] 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") var versions = files.components(separatedBy: "\n")
// Remove all empty strings // Remove all empty strings
@ -167,7 +167,7 @@ class Actions {
*/ */
private static func brew(_ command: String, sudo: Bool = false) 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) private static func sed(file: String, original: String, replacement: String)
{ {
Shell.user.run(""" Shell.run("""
sed -i '' 's/\(original)/\(replacement)/g' \(file) sed -i '' 's/\(original)/\(replacement)/g' \(file)
""") """)
} }
@ -185,7 +185,7 @@ class Actions {
*/ */
private static func grepContains(file: String, query: String) -> Bool 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" grep -q '\(query)' \(file); [ $? -eq 0 ] && echo "YES" || echo "NO"
""") """)
.trimmingCharacters(in: .whitespacesAndNewlines) .trimmingCharacters(in: .whitespacesAndNewlines)

View File

@ -31,34 +31,34 @@ class Startup {
) )
self.performEnvironmentCheck( 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, messageText: "startup.errors.php_opt.title".localized,
informativeText: "startup.errors.php_opt.desc".localized, informativeText: "startup.errors.php_opt.desc".localized,
breaking: true breaking: true
) )
self.performEnvironmentCheck( 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, messageText: "startup.errors.valet_executable.title".localized,
informativeText: "startup.errors.valet_executable.desc".localized, informativeText: "startup.errors.valet_executable.desc".localized,
breaking: true breaking: true
) )
self.performEnvironmentCheck( 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, messageText: "startup.errors.sudoers_brew.title".localized,
informativeText: "startup.errors.sudoers_brew.desc".localized, informativeText: "startup.errors.sudoers_brew.desc".localized,
breaking: true breaking: true
) )
self.performEnvironmentCheck( 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, messageText: "startup.errors.sudoers_valet.title".localized,
informativeText: "startup.errors.sudoers_valet.desc".localized, informativeText: "startup.errors.sudoers_valet.desc".localized,
breaking: true 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( self.performEnvironmentCheck(
(services.countInstances(of: "started") > 1), (services.countInstances(of: "started") > 1),
messageText: "startup.errors.services.title".localized, 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("PHP Monitor has determined the application has successfully passed all checks.")
print("Determining which version of PHP is aliased to `php` via Homebrew...") 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( App.shared.brewPhpPackage = try! JSONDecoder().decode(
[HomebrewPackage].self, [HomebrewPackage].self,

View File

@ -213,7 +213,7 @@ class MainMenu: NSObject, NSWindowDelegate {
@objc public func openPhpInfo() { @objc public func openPhpInfo() {
self.waitAndExecute({ self.waitAndExecute({
try! "<?php phpinfo();".write(toFile: "/tmp/phpmon_phpinfo.php", atomically: true, encoding: .utf8) try! "<?php phpinfo();".write(toFile: "/tmp/phpmon_phpinfo.php", atomically: true, encoding: .utf8)
Shell.user.run("\(Paths.binPath())/php-cgi -q /tmp/phpmon_phpinfo.php > /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")!) NSWorkspace.shared.open(URL(string: "file:///private/tmp/phpmon_phpinfo.html")!)
}) })

View File

@ -9,6 +9,18 @@ import Cocoa
class Shell { 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) Singleton to access a user shell (with --login)
*/ */
@ -50,7 +62,7 @@ class Shell {
Checks if a file exists at the provided path. Checks if a file exists at the provided path.
*/ */
public static func fileExists(_ path: String) -> Bool { public static func fileExists(_ path: String) -> Bool {
return Shell.user.pipe( return Shell.pipe(
"if [ -f \(path) ]; then echo \"PHP_Y_FE\"; fi" "if [ -f \(path) ]; then echo \"PHP_Y_FE\"; fi"
).contains("PHP_Y_FE") ).contains("PHP_Y_FE")
} }