From 8486997f0167cb23b20b2ae4cebc23086b263024 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Thu, 11 Jul 2019 08:27:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Additional=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon.xcodeproj/project.pbxproj | 84 ++++++++++--------- phpmon/AppDelegate.swift | 62 +++++++------- .../Commands/Actions.swift} | 15 +--- .../Commands/Startup.swift} | 4 +- phpmon/{ => Classes}/Helpers/Alert.swift | 0 .../Helpers/MenuBarImageGenerator.swift} | 7 +- phpmon/Classes/{ => Helpers}/PhpVersion.swift | 0 phpmon/Constants.swift | 2 +- phpmon/Singletons/App.swift | 40 +++++++++ phpmon/{Terminal => Singletons}/Shell.swift | 1 - 10 files changed, 121 insertions(+), 94 deletions(-) rename phpmon/{Terminal/Services.swift => Classes/Commands/Actions.swift} (75%) rename phpmon/{Startup/BootChecks.swift => Classes/Commands/Startup.swift} (97%) rename phpmon/{ => Classes}/Helpers/Alert.swift (100%) rename phpmon/{Helpers/ImageGenerator.swift => Classes/Helpers/MenuBarImageGenerator.swift} (86%) rename phpmon/Classes/{ => Helpers}/PhpVersion.swift (100%) create mode 100644 phpmon/Singletons/App.swift rename phpmon/{Terminal => Singletons}/Shell.swift (98%) diff --git a/phpmon.xcodeproj/project.pbxproj b/phpmon.xcodeproj/project.pbxproj index 5a70d55..626ec9d 100644 --- a/phpmon.xcodeproj/project.pbxproj +++ b/phpmon.xcodeproj/project.pbxproj @@ -12,11 +12,12 @@ C41C1B3B22B0098000E7CF16 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C41C1B3A22B0098000E7CF16 /* Assets.xcassets */; }; C41C1B3E22B0098000E7CF16 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C41C1B3C22B0098000E7CF16 /* Main.storyboard */; }; C41C1B4722B009A400E7CF16 /* Shell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4622B009A400E7CF16 /* Shell.swift */; }; - C41C1B4922B00A9800E7CF16 /* ImageGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4822B00A9800E7CF16 /* ImageGenerator.swift */; }; + C41C1B4922B00A9800E7CF16 /* MenuBarImageGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */; }; C41C1B4B22B019FF00E7CF16 /* PhpVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */; }; - C41C1B4D22B0215A00E7CF16 /* Services.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4C22B0215A00E7CF16 /* Services.swift */; }; + C41C1B4D22B0215A00E7CF16 /* Actions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4C22B0215A00E7CF16 /* Actions.swift */; }; C476FF9822B0DD830098105B /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C476FF9722B0DD830098105B /* Alert.swift */; }; - C4D8016622B1584700C6DA1B /* BootChecks.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D8016522B1584700C6DA1B /* BootChecks.swift */; }; + C4811D2422D70A4700B5F6B3 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4811D2322D70A4700B5F6B3 /* App.swift */; }; + C4D8016622B1584700C6DA1B /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D8016522B1584700C6DA1B /* Startup.swift */; }; C4EE188422D3386B00E126E5 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE188322D3386B00E126E5 /* Constants.swift */; }; C4F8C0A422D4F12C002EFE61 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8C0A322D4F12C002EFE61 /* DateExtension.swift */; }; C4F8C0A622D4FA41002EFE61 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = C4F8C0A522D4FA41002EFE61 /* README.md */; }; @@ -31,11 +32,12 @@ C41C1B3F22B0098000E7CF16 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; C41C1B4022B0098000E7CF16 /* phpmon.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = phpmon.entitlements; sourceTree = ""; }; C41C1B4622B009A400E7CF16 /* Shell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shell.swift; sourceTree = ""; }; - C41C1B4822B00A9800E7CF16 /* ImageGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageGenerator.swift; sourceTree = ""; }; + C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarImageGenerator.swift; sourceTree = ""; }; C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpVersion.swift; sourceTree = ""; }; - C41C1B4C22B0215A00E7CF16 /* Services.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Services.swift; sourceTree = ""; }; + C41C1B4C22B0215A00E7CF16 /* Actions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Actions.swift; sourceTree = ""; }; C476FF9722B0DD830098105B /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = ""; }; - C4D8016522B1584700C6DA1B /* BootChecks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BootChecks.swift; sourceTree = ""; }; + C4811D2322D70A4700B5F6B3 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; + C4D8016522B1584700C6DA1B /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = ""; }; C4EE188322D3386B00E126E5 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; C4F8C0A322D4F12C002EFE61 /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = ""; }; C4F8C0A522D4FA41002EFE61 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; @@ -72,14 +74,12 @@ C41C1B3522B0097F00E7CF16 /* phpmon */ = { isa = PBXGroup; children = ( + C41C1B3622B0097F00E7CF16 /* AppDelegate.swift */, C4EE188322D3386B00E126E5 /* Constants.swift */, + C4811D2622D70CEF00B5F6B3 /* Singletons */, C41E181722CB61EB0072CF09 /* Classes */, - C41E181622CB61890072CF09 /* Startup */, - C41E181522CB614C0072CF09 /* Terminal */, - C41C1B4E22B024F100E7CF16 /* Helpers */, C41E181822CB62200072CF09 /* View Controllers */, C4F8C0A222D4F100002EFE61 /* Extensions */, - C41C1B3622B0097F00E7CF16 /* AppDelegate.swift */, C41C1B3F22B0098000E7CF16 /* Info.plist */, C41C1B4022B0098000E7CF16 /* phpmon.entitlements */, C41C1B3A22B0098000E7CF16 /* Assets.xcassets */, @@ -87,36 +87,11 @@ path = phpmon; sourceTree = ""; }; - C41C1B4E22B024F100E7CF16 /* Helpers */ = { - isa = PBXGroup; - children = ( - C41C1B4822B00A9800E7CF16 /* ImageGenerator.swift */, - C476FF9722B0DD830098105B /* Alert.swift */, - ); - path = Helpers; - sourceTree = ""; - }; - C41E181522CB614C0072CF09 /* Terminal */ = { - isa = PBXGroup; - children = ( - C41C1B4622B009A400E7CF16 /* Shell.swift */, - C41C1B4C22B0215A00E7CF16 /* Services.swift */, - ); - path = Terminal; - sourceTree = ""; - }; - C41E181622CB61890072CF09 /* Startup */ = { - isa = PBXGroup; - children = ( - C4D8016522B1584700C6DA1B /* BootChecks.swift */, - ); - path = Startup; - sourceTree = ""; - }; C41E181722CB61EB0072CF09 /* Classes */ = { isa = PBXGroup; children = ( - C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */, + C4811D2722D70D8E00B5F6B3 /* Commands */, + C4811D2822D70D9C00B5F6B3 /* Helpers */, ); path = Classes; sourceTree = ""; @@ -130,6 +105,34 @@ path = "View Controllers"; sourceTree = ""; }; + C4811D2622D70CEF00B5F6B3 /* Singletons */ = { + isa = PBXGroup; + children = ( + C41C1B4622B009A400E7CF16 /* Shell.swift */, + C4811D2322D70A4700B5F6B3 /* App.swift */, + ); + path = Singletons; + sourceTree = ""; + }; + C4811D2722D70D8E00B5F6B3 /* Commands */ = { + isa = PBXGroup; + children = ( + C41C1B4C22B0215A00E7CF16 /* Actions.swift */, + C4D8016522B1584700C6DA1B /* Startup.swift */, + ); + path = Commands; + sourceTree = ""; + }; + C4811D2822D70D9C00B5F6B3 /* Helpers */ = { + isa = PBXGroup; + children = ( + C476FF9722B0DD830098105B /* Alert.swift */, + C41C1B4A22B019FF00E7CF16 /* PhpVersion.swift */, + C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */, + ); + path = Helpers; + sourceTree = ""; + }; C4F8C0A222D4F100002EFE61 /* Extensions */ = { isa = PBXGroup; children = ( @@ -209,11 +212,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C4D8016622B1584700C6DA1B /* BootChecks.swift in Sources */, + C4D8016622B1584700C6DA1B /* Startup.swift in Sources */, C4F8C0A422D4F12C002EFE61 /* DateExtension.swift in Sources */, C41C1B4722B009A400E7CF16 /* Shell.swift in Sources */, - C41C1B4D22B0215A00E7CF16 /* Services.swift in Sources */, - C41C1B4922B00A9800E7CF16 /* ImageGenerator.swift in Sources */, + C41C1B4D22B0215A00E7CF16 /* Actions.swift in Sources */, + C4811D2422D70A4700B5F6B3 /* App.swift in Sources */, + C41C1B4922B00A9800E7CF16 /* MenuBarImageGenerator.swift in Sources */, C41C1B3922B0097F00E7CF16 /* LogViewController.swift in Sources */, C41C1B3722B0097F00E7CF16 /* AppDelegate.swift in Sources */, C41C1B4B22B019FF00E7CF16 /* PhpVersion.swift in Sources */, diff --git a/phpmon/AppDelegate.swift b/phpmon/AppDelegate.swift index fe561d8..666452c 100644 --- a/phpmon/AppDelegate.swift +++ b/phpmon/AppDelegate.swift @@ -14,16 +14,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { // MARK: - Variables let sharedShell : Shell + let state : App let statusItem = NSStatusBar.system.statusItem(withLength: 32) - var timer: Timer? - var version: PhpVersion? = nil - var availablePhpVersions : [String] = [] - var busy: Bool = false - var log: String = "" - var windowController: NSWindowController? = nil + + // MARK: - Initializer override init() { self.sharedShell = Shell.shared + self.state = App.shared super.init() } @@ -34,12 +32,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) // Perform environment boot checks DispatchQueue.global(qos: .userInitiated).async { [unowned self] in - BootChecks.perform() - self.availablePhpVersions = Services.detectPhpVersions() + Startup.checkEnvironment() + App.shared.availablePhpVersions = Actions.detectPhpVersions() self.updatePhpVersionInStatusBar() // Schedule a request to fetch the PHP version every 60 seconds DispatchQueue.main.async { - self.timer = Timer.scheduledTimer( + App.shared.timer = Timer.scheduledTimer( timeInterval: 60, target: self, selector: #selector(self.updatePhpVersionInStatusBar), @@ -57,7 +55,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { // MARK: - UI related func setStatusBarImage(version: String) { - self.setStatusBar(image: ImageGenerator.generateImageForStatusBar(width: 32.0, text: version)) + self.setStatusBar(image: MenuBarImageGenerator.textToImage(width: 32.0, text: version)) } func setStatusBar(image: NSImage) { @@ -71,29 +69,29 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { 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)" + if (App.shared.currentVersion != nil) { + string = "You are running PHP \(App.shared.currentVersion!.long)" } menu.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: "")) - if (self.version != nil) { + if (App.shared.currentVersion != nil) { // Actions menu.addItem(NSMenuItem.separator()) menu.addItem(NSMenuItem(title: "PHP configuration file (php.ini)", action: #selector(self.openActiveConfigFolder), keyEquivalent: "")) } menu.addItem(NSMenuItem.separator()) - if (self.availablePhpVersions.count > 0 && !self.busy) { + if (App.shared.availablePhpVersions.count > 0 && !App.shared.busy) { var shortcutKey = 1 - for index in (0.. Bool { - let running = Shell.shared.pipe("launchctl list | grep homebrew.mxcl.mysql") - return (running != "") - } - - public static func nginxIsRunning() -> Bool { - let running = Shell.shared.pipe("launchctl list | grep homebrew.mxcl.nginx") - return (running != "") - } +class Actions { public static func detectPhpVersions() -> [String] { let files = Shell.shared.pipe("ls /usr/local/opt | grep php@") @@ -49,10 +40,6 @@ class Services { } } - public static func restartPhp(version: String) { - Shell.shared.run("brew services restart php@\(version)") - } - public static func openPhpConfigFolder(version: String) { let files = [NSURL(fileURLWithPath: "/usr/local/etc/php/\(version)/php.ini")]; NSWorkspace.shared.activateFileViewerSelecting(files as [URL]); diff --git a/phpmon/Startup/BootChecks.swift b/phpmon/Classes/Commands/Startup.swift similarity index 97% rename from phpmon/Startup/BootChecks.swift rename to phpmon/Classes/Commands/Startup.swift index ec30034..86d1b71 100644 --- a/phpmon/Startup/BootChecks.swift +++ b/phpmon/Classes/Commands/Startup.swift @@ -8,9 +8,9 @@ import Foundation -class BootChecks { +class Startup { - public static func perform() + public static func checkEnvironment() { self.presentAlertOnMainThreadIf( !Shell.shared.pipe("which php").contains("/usr/local/bin/php"), diff --git a/phpmon/Helpers/Alert.swift b/phpmon/Classes/Helpers/Alert.swift similarity index 100% rename from phpmon/Helpers/Alert.swift rename to phpmon/Classes/Helpers/Alert.swift diff --git a/phpmon/Helpers/ImageGenerator.swift b/phpmon/Classes/Helpers/MenuBarImageGenerator.swift similarity index 86% rename from phpmon/Helpers/ImageGenerator.swift rename to phpmon/Classes/Helpers/MenuBarImageGenerator.swift index 097377e..d143f7f 100644 --- a/phpmon/Helpers/ImageGenerator.swift +++ b/phpmon/Classes/Helpers/MenuBarImageGenerator.swift @@ -8,9 +8,12 @@ import Cocoa -class ImageGenerator { +class MenuBarImageGenerator { - public static func generateImageForStatusBar(width: CGFloat = 30.0, height: CGFloat = 20.0, text: String) -> NSImage { + /** + Takes a string and converts it to an image that can be displayed in the menu bar. + */ + public static func textToImage(width: CGFloat = 30.0, height: CGFloat = 20.0, text: String) -> NSImage { let image = NSImage(size: NSMakeSize(width, height)) let font = NSFont.systemFont(ofSize: 14) diff --git a/phpmon/Classes/PhpVersion.swift b/phpmon/Classes/Helpers/PhpVersion.swift similarity index 100% rename from phpmon/Classes/PhpVersion.swift rename to phpmon/Classes/Helpers/PhpVersion.swift diff --git a/phpmon/Constants.swift b/phpmon/Constants.swift index 44cb21f..d06c265 100644 --- a/phpmon/Constants.swift +++ b/phpmon/Constants.swift @@ -22,5 +22,5 @@ class Constants { This is usually the latest PHP version. */ static let LatestPhpVersion = "7.3" - + } diff --git a/phpmon/Singletons/App.swift b/phpmon/Singletons/App.swift new file mode 100644 index 0000000..66358b2 --- /dev/null +++ b/phpmon/Singletons/App.swift @@ -0,0 +1,40 @@ +// +// StateManager.swift +// phpmon +// +// Created by Nico Verbruggen on 11/07/2019. +// Copyright © 2019 Nico Verbruggen. All rights reserved. +// + +import Cocoa + +class App { + + static let shared = App() + + /** + Whether the application is busy switching versions. + */ + var busy: Bool = false + + /** + The currently active version of PHP. + */ + var currentVersion: PhpVersion? = nil + + /** + All available versions of PHP. + */ + var availablePhpVersions : [String] = [] + + /** + The timer that will periodically fetch the PHP version that is currently active. + */ + var timer: Timer? + + /** + The window controller that will show the log. + */ + var windowController: NSWindowController? = nil + +} diff --git a/phpmon/Terminal/Shell.swift b/phpmon/Singletons/Shell.swift similarity index 98% rename from phpmon/Terminal/Shell.swift rename to phpmon/Singletons/Shell.swift index b184efe..7fddc7d 100644 --- a/phpmon/Terminal/Shell.swift +++ b/phpmon/Singletons/Shell.swift @@ -62,7 +62,6 @@ class Shell { history.append(historyItem) // Keep the last 100 items history = history.suffix(100) - print(history.count) delegate?.didCompleteCommand(historyItem: historyItem) return output