1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

🏗 WIP: Async formulae

This commit is contained in:
2023-03-19 21:54:49 +01:00
parent e1eb61859e
commit 4f11f3d8d3
3 changed files with 41 additions and 29 deletions

View File

@ -8,12 +8,25 @@
import Foundation
class Brew {
class Brew: ObservableObject {
static let shared = Brew()
init() {
Task {
// Asynchronously load available updates
let items = await loadPhpVersions(loadOutdated: false)
Task { @MainActor in
self.phpVersions = items
}
}
}
@Published var phpVersions: [BrewFormula] = []
/// The version of Homebrew that was detected.
var version: VersionNumber?
/// Determine which version of Homebrew is installed.
public func determineVersion() async {
let output = await Shell.pipe("\(Paths.brew) --version")
self.version = try? VersionNumber.parse(output.out)
@ -29,6 +42,7 @@ class Brew {
}
}
/// Each formula for each PHP version that can be installed.
public static var phpVersionFormulae = [
"8.2": "php@8.2",
"8.1": "php@8.1",
@ -38,28 +52,33 @@ class Brew {
"7.2": "shivammathur/php/php@7.2",
"7.1": "shivammathur/php/php@7.1",
"7.0": "shivammathur/php/php@7.0",
"5.6": "shivammathur/php/php@5.6",
"5.6": "shivammathur/php/php@5.6"
]
public func getPhpVersions() async -> [BrewFormula] {
let command = """
\(Paths.brew) update >/dev/null && \
\(Paths.brew) outdated --json --formulae
"""
public func loadPhpVersions(loadOutdated: Bool) async -> [BrewFormula] {
var outdated: [OutdatedFormula]?
let rawJsonText = await Shell.pipe(command).out
.data(using: .utf8)!
if loadOutdated {
let command = """
\(Paths.brew) update >/dev/null && \
\(Paths.brew) outdated --json --formulae
"""
let outdated = try? JSONDecoder().decode(
OutdatedFormulae.self,
from: rawJsonText
).formulae.filter({ formula in
formula.name.starts(with: "php")
})
let rawJsonText = await Shell.pipe(command).out
.data(using: .utf8)!
outdated = try? JSONDecoder().decode(
OutdatedFormulae.self,
from: rawJsonText
).formulae.filter({ formula in
formula.name.starts(with: "php")
})
}
print(PhpEnv.shared.cachedPhpInstallations)
return Self.phpVersionFormulae.map { (version, formula) in
let fullVersion = PhpEnv.shared.cachedPhpInstallations[version]?.versionNumber.text
var upgradeVersion: String? = nil
var upgradeVersion: String?
if let version = fullVersion {
upgradeVersion = outdated?.first(where: { formula in
@ -73,8 +92,6 @@ class Brew {
installedVersion: fullVersion,
upgradeVersion: upgradeVersion
)
}.sorted { a, b in
a.displayName > b.displayName
}
}.sorted { $0.displayName > $1.displayName }
}
}

View File

@ -10,27 +10,21 @@ import Foundation
import SwiftUI
struct PhpFormulaeView: View {
@State var formulae: [BrewFormula]
@ObservedObject var brew: Brew = Brew.shared
@State var busy: Bool = true
@State var title: String = "Doing a thing"
@State var description: String = "Preparing..."
init(formulae: [BrewFormula], busy: Bool = true, title: String = "", description: String = "") {
self.formulae = formulae
init(busy: Bool = true, title: String = "", description: String = "") {
self.busy = busy
self.title = title
self.description = description
Task { @MainActor in
let items = await Brew.shared.getPhpVersions()
print(items)
}
}
var body: some View {
BlockingOverlayView(busy: busy, title: title, text: description) {
VStack {
List(Array(formulae.enumerated()), id: \.1.name) { (index, formula) in
List(Array(brew.phpVersions.enumerated()), id: \.1.name) { (index, formula) in
HStack {
Image(systemName: formula.icon)
.resizable()
@ -81,6 +75,7 @@ struct PhpFormulaeView: View {
}
}
/*
struct PhpFormulaeView_Previews: PreviewProvider {
static var previews: some View {
PhpFormulaeView(formulae: [
@ -129,6 +124,7 @@ struct PhpFormulaeView_Previews: PreviewProvider {
]).frame(width: 600, height: 500)
}
}
*/
extension BrewFormula {
var icon: String {

View File

@ -24,7 +24,6 @@ class PhpVersionManagerWindowController: PMWindowController {
let windowController = Self()
windowController.window = NSWindow()
windowController.view = PhpFormulaeView(
formulae: [],
busy: true,
title: "Loading PHP versions",
description: "Loading available PHP versions..."