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

♻️ Refactor provisioning

This commit is contained in:
2025-08-01 18:41:23 +02:00
parent 38268858f3
commit 1419767013
5 changed files with 88 additions and 67 deletions

View File

@@ -0,0 +1,25 @@
//
// InstallHomebrew.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 01/08/2025.
// Copyright © 2025 Nico Verbruggen. All rights reserved.
//
class InstallHomebrew {
public func run() async throws {
let script = """
NONINTERACTIVE=1 /bin/bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
"""
_ = try await Shell.attach(script, didReceiveOutput: { (string: String, _: ShellStream) in
print(string)
}, withTimeout: 60 * 10)
}
public func verify() async {
// Make sure the Homebrew directory exists
// Make sure the `brew` binary exists
}
}

View File

@@ -1,54 +0,0 @@
//
// Provision.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 01/08/2025.
// Copyright © 2025 Nico Verbruggen. All rights reserved.
//
class Provision {
/**
Adds a given line to .zshrc, which may be needed to adjust the PATH.
*/
private func addToShell(_ text: String) async -> Bool {
let outcome = await Shell.pipe("""
touch ~/.zshrc && \
grep -qxF '\(text)' ~/.zshrc \
|| echo '\n\n\(text)\n' >> ~/.zshrc
""")
if outcome.hasError {
return false
}
return true
}
/**
Installs Homebrew. Requires elevated permission.
*/
public func installHomebrew() async throws {
let script = """
NONINTERACTIVE=1 /bin/bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
"""
_ = try await Shell.attach(script, didReceiveOutput: { (string: String, _: ShellStream) in
print(string)
}, withTimeout: 60 * 10)
}
/**
Adds Homebrew binaries to the PATH.
*/
public func addHomebrewPath() async {
_ = await addToShell("export PATH=$HOME/bin:/opt/homebrew/bin:$PATH")
}
/**
Adds Homebrew binaries to the PATH.
*/
public func addPhpMonitorPath() async {
_ = await addToShell("export PATH=$HOME/bin:~/.config/phpmon/bin:$PATH")
}
}

View File

@@ -0,0 +1,40 @@
//
// Provision+zshrc.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 01/08/2025.
// Copyright © 2025 Nico Verbruggen. All rights reserved.
//
class ZshRunCommand {
/**
Adds a given line to .zshrc, which may be needed to adjust the PATH.
*/
private func add(_ text: String) async -> Bool {
let outcome = await Shell.pipe("""
touch ~/.zshrc && \
grep -qxF '\(text)' ~/.zshrc \
|| echo '\n\n\(text)\n' >> ~/.zshrc
""")
if outcome.hasError {
return false
}
return true
}
/**
Adds Homebrew binaries to the PATH.
*/
public func addHomebrewPath() async {
_ = await add("export PATH=$HOME/bin:/opt/homebrew/bin:$PATH")
}
/**
Adds PHP Monitor binaries to the PATH.
*/
public func addPhpMonitorPath() async {
_ = await add("export PATH=$HOME/bin:~/.config/phpmon/bin:$PATH")
}
}

View File

@@ -54,9 +54,7 @@ class WarningManager: ObservableObject {
url: "https://github.com/nicoverbruggen/phpmon/wiki/PHP-Monitor-helper-binaries",
fix: {
// Add to PATH
await Provision().addPhpMonitorPath()
// Reload the PATH (via new Shell instance)
ActiveShell.reload()
await ZshRunCommand().addPhpMonitorPath()
// Finally, perform environment checks again
await WarningManager.shared.checkEnvironment()
}
@@ -98,6 +96,8 @@ class WarningManager: ObservableObject {
Checks the user's environment and checks if any special warnings apply.
*/
func checkEnvironment() async {
ActiveShell.reload()
if ProcessInfo.processInfo.environment["EXTREME_DOCTOR_MODE"] != nil {
self.temporaryWarnings = self.evaluations
await self.broadcastWarnings()