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

Correctly parse .valetrc files

This commit is contained in:
2023-01-24 19:47:28 +01:00
parent b5d2fef184
commit 66393094b0
7 changed files with 127 additions and 26 deletions

View File

@@ -0,0 +1,48 @@
//
// RCFile.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 24/01/2023.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
struct RCFile {
let path: String?
let fields: [String: String]
static func fromPath(_ path: String) -> RCFile? {
do {
let text = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8)
return RCFile(path: path, contents: text)
} catch {
return nil
}
}
init(path: String? = nil, contents: String) {
var fields: [String: String] = [:]
contents
.split(separator: "\n")
.forEach({ line in
if line.contains("=") {
let content = line.split(separator: "=")
let key = String(content[0])
.trimmingCharacters(in: .whitespaces)
.replacingOccurrences(of: "\"", with: "")
if key.starts(with: "#") {
return
}
let value = String(content[1])
.trimmingCharacters(in: .whitespaces)
.replacingOccurrences(of: "\"", with: "")
fields[key] = value
}
})
self.path = path
self.fields = fields
}
}

View File

@@ -207,19 +207,20 @@ class ValetSite: ValetListable {
}
/**
Checks the contents of the .valetphprc file and determine the version, if possible.
Checks the contents of the .valetphprc file and determine the version.
The first file found takes precendence over all others.
*/
private func determineValetPhpFileInfo() {
let files = [
(".valetphprc", VersionSource.valetphprc),
(".valetrc", VersionSource.valetrc)
(".valetrc", VersionSource.valetrc),
(".valetphprc", VersionSource.valetphprc)
]
for (suffix, source) in files {
do {
let path = "\(absolutePath)/\(suffix)"
if FileSystem.fileExists(path) {
try self.handleValetFile(path, source)
return try self.handleValetFile(path, source)
}
} catch {
Log.err("Something went wrong parsing the '\(suffix)' file")
@@ -239,7 +240,7 @@ class ValetSite: ValetListable {
self.composerPhpSource = source
}
case .valetrc:
self.parseValetRcFile(contents)
self.parseValetRcFile(path, contents)
default:
return
}
@@ -248,9 +249,20 @@ class ValetSite: ValetListable {
/**
Specifically extract PHP information from a .valetrc file.
*/
private func parseValetRcFile(_ text: String) {
// TODO: Implement this
fatalError("A .valetrc file was found, needs to be parsed!")
private func parseValetRcFile(_ path: String, _ text: String) {
let valetRc = RCFile(path: path, contents: text)
guard let versionString = valetRc.fields["PHP"] else {
if valetRc.path != nil {
Log.perf("\(self.name)'s .valetrc file at '\(valetRc.path!)' lacks a 'PHP' entry.")
}
return
}
if let version = VersionExtractor.from(versionString) {
self.composerPhp = version
self.composerPhpSource = .valetrc
}
}
// MARK: - File Parsing