1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-04-02 17:40: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 import Foundation
class Brew { class Brew: ObservableObject {
static let shared = Brew() 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. /// The version of Homebrew that was detected.
var version: VersionNumber? var version: VersionNumber?
/// Determine which version of Homebrew is installed.
public func determineVersion() async { public func determineVersion() async {
let output = await Shell.pipe("\(Paths.brew) --version") let output = await Shell.pipe("\(Paths.brew) --version")
self.version = try? VersionNumber.parse(output.out) 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 = [ public static var phpVersionFormulae = [
"8.2": "php@8.2", "8.2": "php@8.2",
"8.1": "php@8.1", "8.1": "php@8.1",
@@ -38,28 +52,33 @@ class Brew {
"7.2": "shivammathur/php/php@7.2", "7.2": "shivammathur/php/php@7.2",
"7.1": "shivammathur/php/php@7.1", "7.1": "shivammathur/php/php@7.1",
"7.0": "shivammathur/php/php@7.0", "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] { public func loadPhpVersions(loadOutdated: Bool) async -> [BrewFormula] {
let command = """ var outdated: [OutdatedFormula]?
\(Paths.brew) update >/dev/null && \
\(Paths.brew) outdated --json --formulae
"""
let rawJsonText = await Shell.pipe(command).out if loadOutdated {
.data(using: .utf8)! let command = """
\(Paths.brew) update >/dev/null && \
\(Paths.brew) outdated --json --formulae
"""
let outdated = try? JSONDecoder().decode( let rawJsonText = await Shell.pipe(command).out
OutdatedFormulae.self, .data(using: .utf8)!
from: rawJsonText outdated = try? JSONDecoder().decode(
).formulae.filter({ formula in OutdatedFormulae.self,
formula.name.starts(with: "php") from: rawJsonText
}) ).formulae.filter({ formula in
formula.name.starts(with: "php")
})
}
print(PhpEnv.shared.cachedPhpInstallations)
return Self.phpVersionFormulae.map { (version, formula) in return Self.phpVersionFormulae.map { (version, formula) in
let fullVersion = PhpEnv.shared.cachedPhpInstallations[version]?.versionNumber.text let fullVersion = PhpEnv.shared.cachedPhpInstallations[version]?.versionNumber.text
var upgradeVersion: String? = nil var upgradeVersion: String?
if let version = fullVersion { if let version = fullVersion {
upgradeVersion = outdated?.first(where: { formula in upgradeVersion = outdated?.first(where: { formula in
@@ -73,8 +92,6 @@ class Brew {
installedVersion: fullVersion, installedVersion: fullVersion,
upgradeVersion: upgradeVersion upgradeVersion: upgradeVersion
) )
}.sorted { a, b in }.sorted { $0.displayName > $1.displayName }
a.displayName > b.displayName
}
} }
} }

View File

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

View File

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