From 74bb544f3c25669c57565867c6947504a7042f99 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 11 Feb 2022 23:37:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20(part=202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Domain/App/Startup.swift | 61 ++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/phpmon/Domain/App/Startup.swift b/phpmon/Domain/App/Startup.swift index 3254cf3..45d849c 100644 --- a/phpmon/Domain/App/Startup.swift +++ b/phpmon/Domain/App/Startup.swift @@ -13,6 +13,7 @@ class Startup { public var checks: [EnvironmentCheck] = [ EnvironmentCheck( command: { return !FileManager.default.fileExists(atPath: Paths.brew) }, + name: "Homebrew Location Check", titleText: "alert.homebrew_missing.title".localized, descriptionText: "alert.homebrew_missing.info".localized( App.architecture @@ -25,6 +26,7 @@ class Startup { ), EnvironmentCheck( command: { return !Shell.fileExists(Paths.php) }, + name: "PHP Binary Check", titleText: "startup.errors.php_binary.title".localized, descriptionText: "startup.errors.php_binary.desc".localized( Paths.php @@ -32,6 +34,7 @@ class Startup { ), EnvironmentCheck( command: { return !Shell.pipe("ls \(Paths.optPath) | grep php").contains("php") }, + name: "PHP Versions Check", titleText: "startup.errors.php_opt.title".localized, descriptionText: "startup.errors.php_opt.desc".localized( Paths.optPath @@ -42,6 +45,7 @@ class Startup { return !(Shell.fileExists(Paths.valet) || Shell.fileExists("~/.composer/vendor/bin/valet")) }, + name: "Valet Check", titleText: "startup.errors.valet_executable.title".localized, descriptionText: "startup.errors.valet_executable.desc".localized( Paths.valet @@ -49,16 +53,19 @@ class Startup { ), EnvironmentCheck( command: { return HomebrewDiagnostics.cannotLoadService() }, + name: "Homebrew Services Check", titleText: "startup.errors.services_json_error.title".localized, descriptionText: "startup.errors.services_json_error.desc".localized ), EnvironmentCheck( command: { return !Shell.pipe("cat /private/etc/sudoers.d/brew").contains(Paths.brew) }, + name: "Sudo Check (Homebrew)", titleText: "startup.errors.sudoers_brew.title".localized, descriptionText: "startup.errors.sudoers_brew.desc".localized ), EnvironmentCheck( command: { return !Shell.pipe("cat /private/etc/sudoers.d/valet").contains(Paths.valet) }, + name: "Sudo Check (Valet)", titleText: "startup.errors.sudoers_valet.title".localized, descriptionText: "startup.errors.sudoers_valet.desc".localized ), @@ -69,13 +76,12 @@ class Startup { Valet.shared.version = VersionExtractor.from(valet("--version", sudo: false)) return Valet.shared.version == nil }, + name: "Valet Version Check", titleText: "startup.errors.valet_version_unknown.title".localized, descriptionText: "startup.errors.valet_version_unknown.desc".localized ) ] - public var failed: Bool = false - /** Checks the user's environment and checks if PHP Monitor can be used properly. This checks if PHP is installed, Valet is running, the appropriate permissions are set, and more. @@ -89,33 +95,17 @@ class Startup { Log.info("The user is running PHP Monitor with the architecture: \(App.architecture)") for check in self.checks { - let failureCondition = check.command() - - if !failureCondition { + if check.succeeds() { + Log.info("— \(check.name): PASSED") continue } - failed = true + Log.info("— \(check.name): FAILED") - if check.requiresAppRestart { - Alert.notify( - message: check.titleText, - info: check.descriptionText, - button: check.buttonText, - style: .critical - ) - exit(1) - } + showAlert(for: check) - Alert.notify( - message: check.titleText, - info: check.descriptionText, - style: .critical - ) - } - - if failed { failure() + return } @@ -124,6 +114,24 @@ class Startup { success() } + private func showAlert(for check: EnvironmentCheck) { + if check.requiresAppRestart { + Alert.notify( + message: check.titleText, + info: check.descriptionText, + button: check.buttonText, + style: .critical + ) + exit(1) + } + + Alert.notify( + message: check.titleText, + info: check.descriptionText, + style: .critical + ) + } + /** Because the Switcher requires various environment guarantees, the switcher is only initialized when it is done working. @@ -143,6 +151,7 @@ class Startup { */ struct EnvironmentCheck { let command: () -> Bool + let name: String let titleText: String let descriptionText: String let buttonText: String @@ -150,16 +159,22 @@ class Startup { init( command: @escaping () -> Bool, + name: String, titleText: String, descriptionText: String, buttonText: String = "OK", requiresAppRestart: Bool = false ) { self.command = command + self.name = name self.titleText = titleText self.descriptionText = descriptionText self.buttonText = buttonText self.requiresAppRestart = requiresAppRestart } + + public func succeeds() -> Bool { + return !self.command() + } } }