1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-07 05:10:06 +01:00

♻️ Refactor (part 2)

This commit is contained in:
2022-02-11 23:37:20 +01:00
parent dae47e3779
commit 74bb544f3c

View File

@@ -13,6 +13,7 @@ class Startup {
public var checks: [EnvironmentCheck] = [ public var checks: [EnvironmentCheck] = [
EnvironmentCheck( EnvironmentCheck(
command: { return !FileManager.default.fileExists(atPath: Paths.brew) }, command: { return !FileManager.default.fileExists(atPath: Paths.brew) },
name: "Homebrew Location Check",
titleText: "alert.homebrew_missing.title".localized, titleText: "alert.homebrew_missing.title".localized,
descriptionText: "alert.homebrew_missing.info".localized( descriptionText: "alert.homebrew_missing.info".localized(
App.architecture App.architecture
@@ -25,6 +26,7 @@ class Startup {
), ),
EnvironmentCheck( EnvironmentCheck(
command: { return !Shell.fileExists(Paths.php) }, command: { return !Shell.fileExists(Paths.php) },
name: "PHP Binary Check",
titleText: "startup.errors.php_binary.title".localized, titleText: "startup.errors.php_binary.title".localized,
descriptionText: "startup.errors.php_binary.desc".localized( descriptionText: "startup.errors.php_binary.desc".localized(
Paths.php Paths.php
@@ -32,6 +34,7 @@ class Startup {
), ),
EnvironmentCheck( EnvironmentCheck(
command: { return !Shell.pipe("ls \(Paths.optPath) | grep php").contains("php") }, command: { return !Shell.pipe("ls \(Paths.optPath) | grep php").contains("php") },
name: "PHP Versions Check",
titleText: "startup.errors.php_opt.title".localized, titleText: "startup.errors.php_opt.title".localized,
descriptionText: "startup.errors.php_opt.desc".localized( descriptionText: "startup.errors.php_opt.desc".localized(
Paths.optPath Paths.optPath
@@ -42,6 +45,7 @@ class Startup {
return !(Shell.fileExists(Paths.valet) return !(Shell.fileExists(Paths.valet)
|| Shell.fileExists("~/.composer/vendor/bin/valet")) || Shell.fileExists("~/.composer/vendor/bin/valet"))
}, },
name: "Valet Check",
titleText: "startup.errors.valet_executable.title".localized, titleText: "startup.errors.valet_executable.title".localized,
descriptionText: "startup.errors.valet_executable.desc".localized( descriptionText: "startup.errors.valet_executable.desc".localized(
Paths.valet Paths.valet
@@ -49,16 +53,19 @@ class Startup {
), ),
EnvironmentCheck( EnvironmentCheck(
command: { return HomebrewDiagnostics.cannotLoadService() }, command: { return HomebrewDiagnostics.cannotLoadService() },
name: "Homebrew Services Check",
titleText: "startup.errors.services_json_error.title".localized, titleText: "startup.errors.services_json_error.title".localized,
descriptionText: "startup.errors.services_json_error.desc".localized descriptionText: "startup.errors.services_json_error.desc".localized
), ),
EnvironmentCheck( EnvironmentCheck(
command: { return !Shell.pipe("cat /private/etc/sudoers.d/brew").contains(Paths.brew) }, command: { return !Shell.pipe("cat /private/etc/sudoers.d/brew").contains(Paths.brew) },
name: "Sudo Check (Homebrew)",
titleText: "startup.errors.sudoers_brew.title".localized, titleText: "startup.errors.sudoers_brew.title".localized,
descriptionText: "startup.errors.sudoers_brew.desc".localized descriptionText: "startup.errors.sudoers_brew.desc".localized
), ),
EnvironmentCheck( EnvironmentCheck(
command: { return !Shell.pipe("cat /private/etc/sudoers.d/valet").contains(Paths.valet) }, command: { return !Shell.pipe("cat /private/etc/sudoers.d/valet").contains(Paths.valet) },
name: "Sudo Check (Valet)",
titleText: "startup.errors.sudoers_valet.title".localized, titleText: "startup.errors.sudoers_valet.title".localized,
descriptionText: "startup.errors.sudoers_valet.desc".localized descriptionText: "startup.errors.sudoers_valet.desc".localized
), ),
@@ -69,13 +76,12 @@ class Startup {
Valet.shared.version = VersionExtractor.from(valet("--version", sudo: false)) Valet.shared.version = VersionExtractor.from(valet("--version", sudo: false))
return Valet.shared.version == nil return Valet.shared.version == nil
}, },
name: "Valet Version Check",
titleText: "startup.errors.valet_version_unknown.title".localized, titleText: "startup.errors.valet_version_unknown.title".localized,
descriptionText: "startup.errors.valet_version_unknown.desc".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. 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. 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)") Log.info("The user is running PHP Monitor with the architecture: \(App.architecture)")
for check in self.checks { for check in self.checks {
let failureCondition = check.command() if check.succeeds() {
Log.info("\(check.name): PASSED")
if !failureCondition {
continue continue
} }
failed = true Log.info("\(check.name): FAILED")
if check.requiresAppRestart { showAlert(for: check)
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
)
}
if failed {
failure() failure()
return return
} }
@@ -124,6 +114,24 @@ class Startup {
success() 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 Because the Switcher requires various environment guarantees, the switcher is only
initialized when it is done working. initialized when it is done working.
@@ -143,6 +151,7 @@ class Startup {
*/ */
struct EnvironmentCheck { struct EnvironmentCheck {
let command: () -> Bool let command: () -> Bool
let name: String
let titleText: String let titleText: String
let descriptionText: String let descriptionText: String
let buttonText: String let buttonText: String
@@ -150,16 +159,22 @@ class Startup {
init( init(
command: @escaping () -> Bool, command: @escaping () -> Bool,
name: String,
titleText: String, titleText: String,
descriptionText: String, descriptionText: String,
buttonText: String = "OK", buttonText: String = "OK",
requiresAppRestart: Bool = false requiresAppRestart: Bool = false
) { ) {
self.command = command self.command = command
self.name = name
self.titleText = titleText self.titleText = titleText
self.descriptionText = descriptionText self.descriptionText = descriptionText
self.buttonText = buttonText self.buttonText = buttonText
self.requiresAppRestart = requiresAppRestart self.requiresAppRestart = requiresAppRestart
} }
public func succeeds() -> Bool {
return !self.command()
}
} }
} }