mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-07 21:20:07 +01:00
✨ Fix issue with $PATH, adds boot checks
This commit is contained in:
@@ -11,26 +11,41 @@ import Cocoa
|
||||
@NSApplicationMain
|
||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
|
||||
var timer: Timer?
|
||||
var version: PhpVersionExtractor? = nil
|
||||
var availablePhpVersions : [String] = []
|
||||
var busy: Bool = false
|
||||
|
||||
let statusItem = NSStatusBar.system.statusItem(
|
||||
withLength: 32
|
||||
)
|
||||
|
||||
var timer: Timer?
|
||||
var version: PHPVersion? = nil
|
||||
var availablePhpVersions : [String] = []
|
||||
var busy: Bool = false
|
||||
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
// Start with the ducky
|
||||
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
|
||||
// Perform environment boot checks
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
Environment.performBootChecks()
|
||||
}
|
||||
// Check if the correct stuff is installed
|
||||
self.availablePhpVersions = Services.detectPhpVersions()
|
||||
self.setStatusBarImage(version: "???")
|
||||
self.updatePhpVersionInStatusBar()
|
||||
// Schedule a request to fetch the PHP version every 15 seconds
|
||||
Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(updatePhpVersionInStatusBar), userInfo: nil, repeats: true)
|
||||
Timer.scheduledTimer(
|
||||
timeInterval: 15,
|
||||
target: self,
|
||||
selector: #selector(updatePhpVersionInStatusBar),
|
||||
userInfo: nil,
|
||||
repeats: true
|
||||
)
|
||||
}
|
||||
|
||||
func setStatusBarImage(version: String) {
|
||||
self.setStatusBar(image: ImageGenerator.generateImageForStatusBar(width: 32.0, text: version))
|
||||
}
|
||||
|
||||
func setStatusBar(image: NSImage) {
|
||||
if let button = statusItem.button {
|
||||
let image = ImageGenerator.generateImageForStatusBar(width: 32.0, text: version)
|
||||
image.isTemplate = true
|
||||
button.image = image
|
||||
}
|
||||
@@ -41,41 +56,51 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
|
||||
@objc func updatePhpVersionInStatusBar() {
|
||||
self.version = PhpVersionExtractor()
|
||||
self.setStatusBarImage(version: self.busy ? "🏗" : self.version!.short)
|
||||
self.version = PHPVersion()
|
||||
if (self.busy) {
|
||||
self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!)
|
||||
} else {
|
||||
self.setStatusBarImage(version: self.version!.short)
|
||||
}
|
||||
|
||||
self.updateMenu()
|
||||
}
|
||||
|
||||
func updateMenu() {
|
||||
let menu = NSMenu()
|
||||
var string = "We are not sure what version of PHP you are running."
|
||||
if (version != nil) {
|
||||
string = "You are running PHP \(version!.long)"
|
||||
}
|
||||
menu.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
if (self.availablePhpVersions.count > 0 && !busy) {
|
||||
for index in (0..<self.availablePhpVersions.count) {
|
||||
let version = self.availablePhpVersions[index]
|
||||
let action = #selector(self.switchToPhpVersion(sender:))
|
||||
let menuItem = NSMenuItem(title: "Switch to PHP \(version)", action: (version == self.version?.short) ? nil : action, keyEquivalent: "\(index + 1)")
|
||||
menuItem.tag = index
|
||||
menu.addItem(menuItem)
|
||||
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
||||
let menu = NSMenu()
|
||||
var string = "We are not sure what version of PHP you are running."
|
||||
if (self.version != nil) {
|
||||
string = "You are running PHP \(self.version!.long)"
|
||||
}
|
||||
menu.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
}
|
||||
if (self.busy) {
|
||||
menu.addItem(NSMenuItem(title: "Switching PHP versions...", action: nil, keyEquivalent: ""))
|
||||
if (self.availablePhpVersions.count > 0 && !self.busy) {
|
||||
for index in (0..<self.availablePhpVersions.count) {
|
||||
let version = self.availablePhpVersions[index]
|
||||
let action = #selector(self.switchToPhpVersion(sender:))
|
||||
let menuItem = NSMenuItem(title: "Switch to PHP \(version)", action: (version == self.version?.short) ? nil : action, keyEquivalent: "\(index + 1)")
|
||||
menuItem.tag = index
|
||||
menu.addItem(menuItem)
|
||||
}
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
}
|
||||
if (self.busy) {
|
||||
menu.addItem(NSMenuItem(title: "Switching PHP versions...", action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
}
|
||||
menu.addItem(NSMenuItem(title: Services.mysqlIsRunning() ? "You are running MySQL" : "MySQL is not active", action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem(title: Services.nginxIsRunning() ? "You are running nginx" : "nginx is not active", action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
// menu.addItem(NSMenuItem(title: "About phpmon", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem(title: "Quit phpmon", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
|
||||
DispatchQueue.main.async {
|
||||
self.statusItem.menu = menu
|
||||
}
|
||||
}
|
||||
menu.addItem(NSMenuItem(title: Services.mysqlIsRunning() ? "You are running MySQL" : "MySQL is not active", action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem(title: Services.nginxIsRunning() ? "You are running nginx" : "nginx is not active", action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
menu.addItem(NSMenuItem(title: "Quit phpmon", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
|
||||
statusItem.menu = menu
|
||||
}
|
||||
|
||||
@objc func switchToPhpVersion(sender: AnyObject) {
|
||||
@objc public func switchToPhpVersion(sender: AnyObject) {
|
||||
let index = sender.tag!
|
||||
let version = self.availablePhpVersions[index]
|
||||
self.busy = true
|
||||
|
||||
22
phpmon/Assets.xcassets/StatusBarIcon.imageset/Contents.json
vendored
Normal file
22
phpmon/Assets.xcassets/StatusBarIcon.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"filename" : "phpmon.png",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"filename" : "phpmon@2x.png",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
phpmon/Assets.xcassets/StatusBarIcon.imageset/phpmon.png
vendored
Normal file
BIN
phpmon/Assets.xcassets/StatusBarIcon.imageset/phpmon.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 432 B |
BIN
phpmon/Assets.xcassets/StatusBarIcon.imageset/phpmon@2x.png
vendored
Normal file
BIN
phpmon/Assets.xcassets/StatusBarIcon.imageset/phpmon@2x.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 801 B |
23
phpmon/Helpers/Alert.swift
Normal file
23
phpmon/Helpers/Alert.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Alert.swift
|
||||
// phpmon
|
||||
//
|
||||
// Created by Nico Verbruggen on 12/06/2019.
|
||||
// Copyright © 2019 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class Alert {
|
||||
public static func present(
|
||||
messageText: String,
|
||||
informativeText: String,
|
||||
buttonTitle: String = "OK"
|
||||
) {
|
||||
let alert = NSAlert.init()
|
||||
alert.messageText = messageText
|
||||
alert.informativeText = informativeText
|
||||
alert.addButton(withTitle: buttonTitle)
|
||||
alert.runModal()
|
||||
}
|
||||
}
|
||||
39
phpmon/Helpers/Environment.swift
Normal file
39
phpmon/Helpers/Environment.swift
Normal file
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Environment.swift
|
||||
// phpmon
|
||||
//
|
||||
// Created by Nico Verbruggen on 12/06/2019.
|
||||
// Copyright © 2019 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Environment {
|
||||
public static func performBootChecks()
|
||||
{
|
||||
if (!Shell.execute(command: "which php").contains("/usr/local/bin/php")) {
|
||||
DispatchQueue.main.async {
|
||||
Alert.present(
|
||||
messageText: "PHP is not correctly installed",
|
||||
informativeText: "You must install PHP via brew. Try running `which php` in Terminal, it should return `/usr/local/bin/php`. The app will not work correctly until you resolve this issue."
|
||||
)
|
||||
}
|
||||
}
|
||||
if (!Shell.execute(command: "ls /usr/local/opt | grep php@7.3").contains("php@7.3")) {
|
||||
DispatchQueue.main.async {
|
||||
Alert.present(
|
||||
messageText: "PHP 7.3 is not correctly installed",
|
||||
informativeText: "PHP 7.3 alias was not found in `/usr/local/opt`. The app will not work correctly until you resolve this issue."
|
||||
)
|
||||
}
|
||||
}
|
||||
if (!Shell.execute(command: "which valet").contains("/usr/local/bin/valet")) {
|
||||
DispatchQueue.main.async {
|
||||
Alert.present(
|
||||
messageText: "Laravel Valet is not correctly installed",
|
||||
informativeText: "You must install Valet via brew. Try running `which valet` in Terminal, it should return `/usr/local/bin/valet`. The app will not work correctly until you resolve this issue."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
class PhpVersionExtractor {
|
||||
class PHPVersion {
|
||||
|
||||
var short : String = "???"
|
||||
var long : String = "???"
|
||||
@@ -17,7 +17,7 @@ class PhpVersionExtractor {
|
||||
// Get the info about the PHP installation
|
||||
let output = Shell.execute(command: "php -v")
|
||||
// Get everything before "(cli)" (PHP X.X.X (cli) ...)
|
||||
var version = output!.components(separatedBy: " (cli)")[0]
|
||||
var version = output.components(separatedBy: " (cli)")[0]
|
||||
// Strip away the text before the version number
|
||||
version = version.components(separatedBy: "PHP ")[1]
|
||||
self.long = version
|
||||
@@ -27,7 +27,7 @@ class Services {
|
||||
|
||||
public static func detectPhpVersions() -> [String] {
|
||||
let files = Shell.execute(command: "ls /usr/local/opt | grep php@")
|
||||
var versions = files!.components(separatedBy: "\n")
|
||||
var versions = files.components(separatedBy: "\n")
|
||||
// Remove all empty strings
|
||||
versions.removeAll { (string) -> Bool in
|
||||
return (string == "")
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
import Cocoa
|
||||
|
||||
class Shell {
|
||||
public static func execute(command: String) -> String?
|
||||
public static func execute(command: String) -> String
|
||||
{
|
||||
let task = Process()
|
||||
task.launchPath = "/bin/bash"
|
||||
task.arguments = ["-c", command]
|
||||
task.arguments = ["--login", "-c", command]
|
||||
|
||||
let pipe = Pipe()
|
||||
task.standardOutput = pipe
|
||||
|
||||
@@ -19,16 +19,16 @@
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<string>2</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
|
||||
<key>LSUIElement</key>
|
||||
<true/>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string>Copyright © 2019 Nico Verbruggen. All rights reserved.</string>
|
||||
<key>NSMainStoryboardFile</key>
|
||||
<string>Main</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
<string>NSApplication</string>
|
||||
<key>LSUIElement</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
Reference in New Issue
Block a user