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

♻️ Add valet command (#34), read Valet configuration (#58)

This commit is contained in:
2021-11-29 01:17:23 +01:00
parent bbb04f7907
commit 0986b97051
12 changed files with 221 additions and 12 deletions

View File

@@ -102,6 +102,19 @@ class Actions {
brew("services stop dnsmasq", sudo: true)
}
/**
Kindly asks Valet to switch to a specific PHP version.
*/
public static func switchToPhpVersionUsingValet(
version: String,
availableVersions: [String],
completed: @escaping () -> Void
) {
print("Switching to \(version) using Valet")
print(valet("use php@\(version)"))
completed()
}
/**
Switching to a new PHP version involves:
- unlinking the current version
@@ -203,6 +216,14 @@ class Actions {
// MARK: Common Shell Commands
/**
Runs a `valet` command.
*/
public static func valet(_ command: String) -> String
{
return Shell.pipe("sudo \(Paths.valet) \(command)", requiresPath: true)
}
/**
Runs a `brew` command. Can run as superuser.
*/

View File

@@ -16,7 +16,8 @@ struct HomebrewPackage: Decodable {
let linked_keg: String?
public var version: String {
return aliases.first!.replacingOccurrences(of: "php@", with: "")
return aliases.first!
.replacingOccurrences(of: "php@", with: "")
}
}

View File

@@ -0,0 +1,26 @@
//
// Valet.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 29/11/2021.
// Copyright © 2021 Nico Verbruggen. All rights reserved.
//
import Foundation
class Valet {
var version: String
var detectedSites: [String]
init() {
// Let's see if we can't discern what the Valet version is
// but in order to do so, we'll need to be able to run Valet
// which has, historically, been kind of a pain in the butt
self.version = Actions.valet("--version")
.replacingOccurrences(of: "Laravel Valet ", with: "")
self.detectedSites = []
}
}

View File

@@ -0,0 +1,17 @@
//
// ValetConfiguration.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 29/11/2021.
// Copyright © 2021 Nico Verbruggen. All rights reserved.
//
import Foundation
class ValetConfiguration: Decodable {
let tld: String
let paths: [String]
let loopback: String
}

View File

@@ -55,9 +55,14 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
updatePhpVersionInStatusBar()
// Attempt to find out if PHP-FPM is broken
let installation = App.phpInstall!
installation.notifyAboutBrokenPhpFpm()
// Attempt to find out more info about Valet
let valet = Valet()
print("PHP Monitor has extracted the version number of Valet: \(valet.version)")
// Schedule a request to fetch the PHP version every 60 seconds
DispatchQueue.main.async { [self] in
App.shared.timer = Timer.scheduledTimer(
@@ -349,8 +354,8 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
}
}
// Switch the PHP version
Actions.switchToPhpVersion(
// TODO: Allow for switcher to vary?
Actions.switchToPhpVersionUsingValet(
version: sender.version,
availableVersions: App.shared.availablePhpVersions,
completed: completion

View File

@@ -39,6 +39,10 @@ class Paths {
// - MARK: Binaries
public static var valet: String {
return "/Users/\(whoami)/.composer/vendor/bin/valet"
}
public static var brew: String {
return "\(binPath)/brew"
}
@@ -53,6 +57,10 @@ class Paths {
// - MARK: Paths
public static var whoami: String {
return String(Shell.pipe("whoami").split(separator: "\n")[0])
}
public static var binPath: String {
return "\(shared.baseDir.rawValue)/bin"
}

View File

@@ -11,12 +11,18 @@ class Shell {
// MARK: - Invoke static functions
public static func run(_ command: String) {
public static func run(
_ command: String,
requiresPath: Bool = false
) {
Shell.user.run(command)
}
public static func pipe(_ command: String) -> String {
return Shell.user.pipe(command)
public static func pipe(
_ command: String,
requiresPath: Bool = false
) -> String {
return Shell.user.pipe(command, requiresPath: requiresPath)
}
// MARK: - Singleton
@@ -49,25 +55,36 @@ class Shell {
Uses the default shell.
- Parameter command: The command to run
- Parameter requiresPath: By default, the PATH is not resolved but some binaries might require this
*/
func run(_ command: String) {
func run(
_ command: String,
requiresPath: Bool = false
) {
// Equivalent of piping to /dev/null; don't do anything with the string
_ = pipe(command)
_ = Shell.pipe(command)
}
/**
Runs a shell command and returns the output.
- Parameter command: The command to run
- Parameter shell: Path to the shell to invoke
- Parameter requiresPath: By default, the PATH is not resolved but some binaries might require this
*/
func pipe(_ command: String) -> String {
func pipe(
_ command: String,
requiresPath: Bool = false
) -> String {
let task = Process()
let outputPipe = Pipe()
let errorPipe = Pipe()
let tailoredCommand = requiresPath
? "export PATH=\(Paths.binPath):$PATH && \(command)"
: command
task.launchPath = self.shell
task.arguments = ["--login", "-c", command]
task.arguments = ["--login", "-c", tailoredCommand]
task.standardOutput = outputPipe
task.standardError = errorPipe
task.launch()