From 4b8ad911f13c63988624248f5db968d425d9c9e9 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 21 Nov 2022 17:42:32 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20`nginx-full`?= =?UTF-8?q?=20(#211)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 14 ++++++--- phpmon/Common/Core/Actions.swift | 30 +++++++++---------- phpmon/Common/Core/Homebrew.swift | 25 ++++++++++++++++ .../PHP/Switcher/InternalSwitcher.swift | 2 +- phpmon/Domain/App/ServicesManager.swift | 6 ++-- phpmon/Domain/App/Startup.swift | 2 +- .../Homebrew/HomebrewDiagnostics.swift | 19 ++++++++++-- phpmon/Domain/Menu/MainMenu+Startup.swift | 6 +++- phpmon/Domain/SwiftUI/Menu/ServicesView.swift | 6 ++-- 9 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 phpmon/Common/Core/Homebrew.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 84b7a57..26df602 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -244,6 +244,8 @@ C4CE3BBA27B31F670086CA49 /* ComposerWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CE3BB927B31F670086CA49 /* ComposerWindow.swift */; }; C4CE3BBB27B324230086CA49 /* MainMenu+Switcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CE3BB727B31F2E0086CA49 /* MainMenu+Switcher.swift */; }; C4CE3BBC27B324250086CA49 /* ComposerWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CE3BB927B31F670086CA49 /* ComposerWindow.swift */; }; + C4D27F24292BDF630081B38F /* Homebrew.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D27F23292BDF630081B38F /* Homebrew.swift */; }; + C4D27F25292BDF630081B38F /* Homebrew.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D27F23292BDF630081B38F /* Homebrew.swift */; }; C4D5CFCA27E0F9CD00035329 /* NginxConfigurationFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D5CFC927E0F9CD00035329 /* NginxConfigurationFile.swift */; }; C4D5CFCB27E0F9CD00035329 /* NginxConfigurationFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D5CFC927E0F9CD00035329 /* NginxConfigurationFile.swift */; }; C4D8016622B1584700C6DA1B /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D8016522B1584700C6DA1B /* Startup.swift */; }; @@ -462,6 +464,7 @@ C4CDA892288F1A71007CE25F /* Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keys.swift; sourceTree = ""; }; C4CE3BB727B31F2E0086CA49 /* MainMenu+Switcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MainMenu+Switcher.swift"; sourceTree = ""; }; C4CE3BB927B31F670086CA49 /* ComposerWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposerWindow.swift; sourceTree = ""; }; + C4D27F23292BDF630081B38F /* Homebrew.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Homebrew.swift; sourceTree = ""; }; C4D5CFC927E0F9CD00035329 /* NginxConfigurationFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NginxConfigurationFile.swift; sourceTree = ""; }; C4D8016522B1584700C6DA1B /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = ""; }; C4D89BC52783C99400A02B68 /* ComposerJson.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposerJson.swift; sourceTree = ""; }; @@ -644,6 +647,7 @@ C4C1019A27C65C6F001FACC2 /* Process.swift */, C40C7F2F27722E8D00DDDCDC /* Logger.swift */, C417DC73277614690015E6EE /* Helpers.swift */, + C4D27F23292BDF630081B38F /* Homebrew.swift */, ); path = Core; sourceTree = ""; @@ -1413,6 +1417,7 @@ C476FF9822B0DD830098105B /* Alert.swift in Sources */, C474B00624C0E98C00066A22 /* LocalNotification.swift in Sources */, C4D5CFCA27E0F9CD00035329 /* NginxConfigurationFile.swift in Sources */, + C4D27F24292BDF630081B38F /* Homebrew.swift in Sources */, C485707028BF452300539B36 /* WarningsWindowController.swift in Sources */, C4CE3BBA27B31F670086CA49 /* ComposerWindow.swift in Sources */, C4D9ADC8277611A0007277F4 /* InternalSwitcher.swift in Sources */, @@ -1555,6 +1560,7 @@ C4F30B0B278E203C00755FCE /* MainMenu+Startup.swift in Sources */, C485707C28BF459500539B36 /* NoWarningsView.swift in Sources */, C4F5FBCD28218CB8001065C5 /* Xdebug.swift in Sources */, + C4D27F25292BDF630081B38F /* Homebrew.swift in Sources */, C40B24F227A310770018C7D2 /* Events.swift in Sources */, C4F30B0A278E1A1A00755FCE /* ComposerJson.swift in Sources */, C4C0E8E027F88AEB002D32A9 /* FakeSiteScanner.swift in Sources */, @@ -1741,7 +1747,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 985; + CURRENT_PROJECT_VERSION = 990; DEAD_CODE_STRIPPING = YES; DEBUG = YES; DEVELOPMENT_TEAM = 8M54J5J787; @@ -1752,7 +1758,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 5.6.4; + MARKETING_VERSION = 5.6.5; PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1769,7 +1775,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 985; + CURRENT_PROJECT_VERSION = 990; DEAD_CODE_STRIPPING = YES; DEBUG = NO; DEVELOPMENT_TEAM = 8M54J5J787; @@ -1780,7 +1786,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 5.6.4; + MARKETING_VERSION = 5.6.5; PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/phpmon/Common/Core/Actions.swift b/phpmon/Common/Core/Actions.swift index 37107c0..589880c 100644 --- a/phpmon/Common/Core/Actions.swift +++ b/phpmon/Common/Core/Actions.swift @@ -13,37 +13,35 @@ class Actions { // MARK: - Services public static func restartPhpFpm() { - brew("services restart \(PhpEnv.phpInstall.formula)", sudo: true) + brew("services restart \(Homebrew.Formulae.php)", sudo: true) } public static func restartNginx() { - brew("services restart nginx", sudo: true) + brew("services restart \(Homebrew.Formulae.nginx)", sudo: true) } public static func restartDnsMasq() { - brew("services restart dnsmasq", sudo: true) + brew("services restart \(Homebrew.Formulae.dnsmasq)", sudo: true) } public static func stopValetServices() { - brew("services stop \(PhpEnv.phpInstall.formula)", sudo: true) - brew("services stop nginx", sudo: true) - brew("services stop dnsmasq", sudo: true) + brew("services stop \(Homebrew.Formulae.php)", sudo: true) + brew("services stop \(Homebrew.Formulae.nginx)", sudo: true) + brew("services stop \(Homebrew.Formulae.dnsmasq)", sudo: true) } public static func fixHomebrewPermissions() throws { var servicesCommands = [ - "\(Paths.brew) services stop nginx", - "\(Paths.brew) services stop dnsmasq" + "\(Paths.brew) services stop \(Homebrew.Formulae.nginx)", + "\(Paths.brew) services stop \(Homebrew.Formulae.dnsmasq)" ] var cellarCommands = [ - "chown -R \(Paths.whoami):admin \(Paths.cellarPath)/nginx", - "chown -R \(Paths.whoami):admin \(Paths.cellarPath)/dnsmasq" + "chown -R \(Paths.whoami):admin \(Paths.cellarPath)/\(Homebrew.Formulae.nginx)", + "chown -R \(Paths.whoami):admin \(Paths.cellarPath)/\(Homebrew.Formulae.dnsmasq)" ] PhpEnv.shared.availablePhpVersions.forEach { version in - let formula = version == PhpEnv.brewPhpVersion - ? "php" - : "php@\(version)" + let formula = version == PhpEnv.brewPhpVersion ? "php" : "php@\(version)" servicesCommands.append("\(Paths.brew) services stop \(formula)") cellarCommands.append("chown -R \(Paths.whoami):admin \(Paths.cellarPath)/\(formula)") } @@ -145,9 +143,9 @@ class Actions { */ public static func fixMyValet(completed: @escaping () -> Void) { InternalSwitcher().performSwitch(to: PhpEnv.brewPhpVersion, completion: { - brew("services restart dnsmasq", sudo: true) - brew("services restart php", sudo: true) - brew("services restart nginx", sudo: true) + brew("services restart \(Homebrew.Formulae.dnsmasq)", sudo: true) + brew("services restart \(Homebrew.Formulae.php)", sudo: true) + brew("services restart \(Homebrew.Formulae.nginx)", sudo: true) completed() }) } diff --git a/phpmon/Common/Core/Homebrew.swift b/phpmon/Common/Core/Homebrew.swift new file mode 100644 index 0000000..8c3d86a --- /dev/null +++ b/phpmon/Common/Core/Homebrew.swift @@ -0,0 +1,25 @@ +// +// Homebrew.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 21/11/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation + +class Homebrew { + struct Formulae { + static var php: String { + return PhpEnv.phpInstall.formula + } + + static var nginx: String { + return HomebrewDiagnostics.usesNginxFullFormula ? "nginx-full" : "nginx" + } + + static var dnsmasq: String { + return "dnsmasq" + } + } +} diff --git a/phpmon/Common/PHP/Switcher/InternalSwitcher.swift b/phpmon/Common/PHP/Switcher/InternalSwitcher.swift index ee96726..97a1173 100644 --- a/phpmon/Common/PHP/Switcher/InternalSwitcher.swift +++ b/phpmon/Common/PHP/Switcher/InternalSwitcher.swift @@ -46,7 +46,7 @@ class InternalSwitcher: PhpSwitcher { } Log.info("Restarting nginx, just to be sure!") - brew("services restart nginx", sudo: true) + brew("services restart \(Homebrew.Formulae.nginx)", sudo: true) Log.info("The new version(s) have been linked!") completion() diff --git a/phpmon/Domain/App/ServicesManager.swift b/phpmon/Domain/App/ServicesManager.swift index 7372a8d..5b5ad9a 100644 --- a/phpmon/Domain/App/ServicesManager.swift +++ b/phpmon/Domain/App/ServicesManager.swift @@ -18,9 +18,9 @@ class ServicesManager: ObservableObject { public static func loadHomebrewServices(completed: (() -> Void)? = nil) { let rootServiceNames = [ - PhpEnv.phpInstall.formula, - "nginx", - "dnsmasq" + Homebrew.Formulae.php, + Homebrew.Formulae.nginx, + Homebrew.Formulae.dnsmasq ] DispatchQueue.global(qos: .background).async { diff --git a/phpmon/Domain/App/Startup.swift b/phpmon/Domain/App/Startup.swift index 8d80154..f003db8 100644 --- a/phpmon/Domain/App/Startup.swift +++ b/phpmon/Domain/App/Startup.swift @@ -160,7 +160,7 @@ class Startup { // Verify if the Homebrew services are running (as root). // ================================================================================= EnvironmentCheck( - command: { return HomebrewDiagnostics.cannotLoadService() }, + command: { return HomebrewDiagnostics.cannotLoadService("dnsmasq") }, name: "`sudo \(Paths.brew) services info` JSON loaded", titleText: "startup.errors.services_json_error.title".localized, subtitleText: "startup.errors.services_json_error.subtitle".localized, diff --git a/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift b/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift index d6a8eec..10d491e 100644 --- a/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift +++ b/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift @@ -29,6 +29,21 @@ class HomebrewDiagnostics { return installedTaps.contains("nicoverbruggen/cask") }() + /** + Determines whether to use the regular `nginx` or `nginx-full` formula. + */ + public static var usesNginxFullFormula: Bool = { + guard let destination = try? FileManager.default + .destinationOfSymbolicLink(atPath: "\(Paths.binPath)/nginx") else { return false } + + return + // Verify that the `nginx` binary is symlinked to a directory that includes `nginx-full`. + destination.contains("/nginx-full/") + // Verify that the formula exists. + && !Shell.pipe("\(Paths.brew) info nginx-full --json") + .contains("Error: No available formula") + }() + /** It is possible to have the `shivammathur/php` tap installed, and for the core homebrew information to be outdated. This will then result in two different aliases claiming to point to the same formula (`php`). @@ -128,10 +143,10 @@ class HomebrewDiagnostics { } /** - In order to see if we support the --json syntax, we'll query nginx. + In order to see if we support the --json syntax, we'll query dnsmasq. If the JSON response cannot be parsed, Homebrew is probably out of date. */ - public static func cannotLoadService(_ name: String = "nginx") -> Bool { + public static func cannotLoadService(_ name: String) -> Bool { let serviceInfo = try? JSONDecoder().decode( [HomebrewService].self, from: Shell.pipe( diff --git a/phpmon/Domain/Menu/MainMenu+Startup.swift b/phpmon/Domain/Menu/MainMenu+Startup.swift index 08072a8..4fd52fa 100644 --- a/phpmon/Domain/Menu/MainMenu+Startup.swift +++ b/phpmon/Domain/Menu/MainMenu+Startup.swift @@ -29,12 +29,16 @@ extension MainMenu { When the environment is all clear and the app can run, let's go. */ private func onEnvironmentPass() { - // Determine install method Log.info(HomebrewDiagnostics.customCaskInstalled ? "The app has probably been installed via Homebrew Cask." : "The app has probably been installed directly." ) + Log.info(HomebrewDiagnostics.usesNginxFullFormula + ? "The app will be using the `nginx-full` formula." + : "The app will be using the `nginx` formula." + ) + // Attempt to find out more info about Valet if Valet.shared.version != nil { Log.info("PHP Monitor has extracted the version number of Valet: \(Valet.shared.version!)") diff --git a/phpmon/Domain/SwiftUI/Menu/ServicesView.swift b/phpmon/Domain/SwiftUI/Menu/ServicesView.swift index db87a04..98e0a19 100644 --- a/phpmon/Domain/SwiftUI/Menu/ServicesView.swift +++ b/phpmon/Domain/SwiftUI/Menu/ServicesView.swift @@ -17,9 +17,9 @@ struct ServicesView: View { static func asMenuItem(perRow: Int = 3) -> NSMenuItem { let item = NSMenuItem() var services = [ - PhpEnv.phpInstall.formula, - "nginx", - "dnsmasq" + Homebrew.Formulae.php, + Homebrew.Formulae.nginx, + Homebrew.Formulae.dnsmasq ] if Preferences.custom.hasServices() {