From 0d4a144524402fb3f5f4f26e879d276b9722ecba Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 28 Jan 2022 16:42:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Cleanup=20`HomebrewDiagnostics`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PHP/Homebrew/HomebrewService.swift | 13 -------- phpmon/Domain/Core/Startup.swift | 2 +- .../Integrations/Composer/PhpFrameworks.swift | 6 ++-- .../Homebrew/HomebrewDiagnostics.swift | 32 +++++++++++-------- phpmon/Domain/Menu/MainMenu+Startup.swift | 2 +- phpmon/Localizable.strings | 2 +- 6 files changed, 25 insertions(+), 32 deletions(-) diff --git a/phpmon-common/PHP/Homebrew/HomebrewService.swift b/phpmon-common/PHP/Homebrew/HomebrewService.swift index 2cc9b07..6c21af4 100644 --- a/phpmon-common/PHP/Homebrew/HomebrewService.swift +++ b/phpmon-common/PHP/Homebrew/HomebrewService.swift @@ -18,17 +18,4 @@ struct HomebrewService: Decodable, Equatable { let status: String? let log_path: String? let error_log_path: String? - - public static let serviceToCheck = "nginx" - public static func servicesCanBeLoaded() -> Bool { - let serviceInfo = try? JSONDecoder().decode( - [HomebrewService].self, - from: Shell.pipe( - "sudo \(Paths.brew) services info \(self.serviceToCheck) --json", - requiresPath: true - ).data(using: .utf8)! - ) - - return serviceInfo != nil - } } diff --git a/phpmon/Domain/Core/Startup.swift b/phpmon/Domain/Core/Startup.swift index fed4915..ef2f542 100644 --- a/phpmon/Domain/Core/Startup.swift +++ b/phpmon/Domain/Core/Startup.swift @@ -50,7 +50,7 @@ class Startup { ) performEnvironmentCheck( - !HomebrewService.servicesCanBeLoaded(), + HomebrewDiagnostics.cannotLoadService(), messageText: "startup.errors.services_json_error.title".localized, informativeText: "startup.errors.services_json_error.desc".localized, breaking: true diff --git a/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift b/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift index ba94373..e06c07d 100644 --- a/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift +++ b/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift @@ -10,8 +10,10 @@ import Foundation struct PhpFrameworks { - /// This list should probably be reversed when checked, because some of these - /// will also require either laravel/framework or symfony/symfony. + /** + This list should probably be reversed when checked, because some of these + will also require either `laravel/framework` or `symfony/symfony`. + */ public static let DependencyList = [ // COMMON FRAMEWORKS diff --git a/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift b/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift index 0f8dec2..0ec9eb6 100644 --- a/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift +++ b/phpmon/Domain/Integrations/Homebrew/HomebrewDiagnostics.swift @@ -10,19 +10,6 @@ import Foundation class HomebrewDiagnostics { - enum Errors: String { - case aliasConflict = "alias_conflict" - } - - static let shared = HomebrewDiagnostics() - var errors: [HomebrewDiagnostics.Errors] = [] - - init() { - if determineAliasConflicts() { - errors.append(.aliasConflict) - } - } - /** 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`). @@ -30,7 +17,7 @@ class HomebrewDiagnostics { This check only needs to be performed if the `shivammathur/php` tap is active. */ - public func determineAliasConflicts() -> Bool + public static func hasAliasConflict() -> Bool { let tapAlias = Shell.pipe("\(Paths.brew) info shivammathur/php/php --json") @@ -67,4 +54,21 @@ class HomebrewDiagnostics { return false } } + + /** + In order to see if we support the --json syntax, we'll query nginx. + If the JSON response cannot be parsed, Homebrew is probably out of date. + */ + public static func cannotLoadService(_ name: String = "nginx") -> Bool + { + let serviceInfo = try? JSONDecoder().decode( + [HomebrewService].self, + from: Shell.pipe( + "sudo \(Paths.brew) services info \(name) --json", + requiresPath: true + ).data(using: .utf8)! + ) + + return serviceInfo == nil + } } diff --git a/phpmon/Domain/Menu/MainMenu+Startup.swift b/phpmon/Domain/Menu/MainMenu+Startup.swift index 35434ba..bea4da2 100644 --- a/phpmon/Domain/Menu/MainMenu+Startup.swift +++ b/phpmon/Domain/Menu/MainMenu+Startup.swift @@ -30,7 +30,7 @@ extension MainMenu { private func onEnvironmentPass() { PhpEnv.detectPhpVersions() - if HomebrewDiagnostics.shared.errors.contains(.aliasConflict) { + if HomebrewDiagnostics.hasAliasConflict() { DispatchQueue.main.async { Alert.notify( message: "alert.php_alias_conflict.title".localized, diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index 2593cd7..5763c5a 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -241,7 +241,7 @@ You can do this by running `composer global update` in your terminal. After that /// 6. Cannot retrieve services "startup.errors.services_json_error.title" = "Cannot determine services status"; -"startup.errors.services_json_error.desc" = "PHP Monitor was unable to check if some of the services are up and running.\n\nPHP Monitor usually queries `brew` using the following command to test if the services can be retrieved: `sudo brew services info nginx --json`. PHP Monitor received an invalid response (no JSON)."; +"startup.errors.services_json_error.desc" = "PHP Monitor usually queries `brew` using the following command to test if the services can be retrieved: `sudo brew services info nginx --json`.\n\nPHP Monitor could not interpret this response. This can happen if your Homebrew installation is out of date, in which case Homebrew won't return JSON yet.\n\nYou can usually fix this by running `brew update`."; /// 7. Multiple services active "startup.errors.services.title" = "Multiple PHP services are active";