mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 12:00:09 +02:00
In order to make this possible, I've added a new `sync()` method to the Shellable protocol, which now should allow us to run shell commands synchronously. Back to basics, as this was how *all* commands were run in legacy versions of PHP Monitor. The advantage here is that there is now a choice. Previously, you'd have to use the `system()` helper that I added. Usage of that helper is now discouraged, as is the synchronous shell method, but it may be useful for some methods where waiting for the outcome of the output is required. (Important: the `system()` helper is still required when determining what the preferred terminal is during the initialization of the `Paths` class.)
77 lines
2.3 KiB
Swift
77 lines
2.3 KiB
Swift
//
|
|
// RemovePhpVersionCommand.swift
|
|
// PHP Monitor
|
|
//
|
|
// Created by Nico Verbruggen on 21/03/2023.
|
|
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class RemovePhpVersionCommand: BrewCommand {
|
|
let formula: String
|
|
let version: String
|
|
let phpGuard: PhpGuard
|
|
|
|
init(formula: String) {
|
|
self.version = formula
|
|
.replacingOccurrences(of: "php@", with: "")
|
|
.replacingOccurrences(of: "shivammathur/php/", with: "")
|
|
self.formula = formula
|
|
self.phpGuard = PhpGuard()
|
|
}
|
|
|
|
func getCommandTitle() -> String {
|
|
return "Removing PHP \(version)..."
|
|
}
|
|
|
|
func execute(onProgress: @escaping (BrewCommandProgress) -> Void) async throws {
|
|
onProgress(.create(
|
|
value: 0.2,
|
|
title: getCommandTitle(),
|
|
description: "Please wait while Homebrew removes PHP \(version)..."
|
|
))
|
|
|
|
let command = """
|
|
export HOMEBREW_NO_INSTALL_UPGRADE=true; \
|
|
export HOMEBREW_NO_INSTALL_CLEANUP=true; \
|
|
\(Paths.brew) remove \(formula) --force --ignore-dependencies
|
|
"""
|
|
|
|
do {
|
|
try await BrewPermissionFixer().fixPermissions()
|
|
} catch {
|
|
return
|
|
}
|
|
|
|
var loggedMessages: [String] = []
|
|
|
|
let (process, _) = try! await Shell.attach(
|
|
command,
|
|
didReceiveOutput: { text, _ in
|
|
if !text.isEmpty {
|
|
Log.perf(text)
|
|
loggedMessages.append(text)
|
|
}
|
|
},
|
|
withTimeout: .minutes(5)
|
|
)
|
|
|
|
if process.terminationStatus <= 0 {
|
|
onProgress(.create(value: 0.95, title: getCommandTitle(), description: "Reloading PHP versions..."))
|
|
|
|
await PhpEnvironments.detectPhpVersions()
|
|
|
|
await MainMenu.shared.refreshActiveInstallation()
|
|
|
|
if let version = phpGuard.currentVersion {
|
|
await MainMenu.shared.switchToPhpVersionAndWait(version, silently: true)
|
|
}
|
|
|
|
onProgress(.create(value: 1, title: getCommandTitle(), description: "The operation has succeeded."))
|
|
} else {
|
|
throw BrewCommandError(error: "The command failed to run correctly.", log: loggedMessages)
|
|
}
|
|
}
|
|
}
|