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

🐛 Correctly parse RC PHP version (#132)

It'll be a while before a new release candidate is available, but this
bug has now been resolved.

A new `PhpVersionNumber.parse` method has been added which can throw.

The `VersionExtractor` class is now capable of extracting version
numbers from all strings now too, and isn't just used to determine
the Valet version number.

New tests have been added to handle these scenarios.

This commit also removes the phpmon-cli component, which wasn't being
updated or maintained (it was an experiment).
This commit is contained in:
2022-02-08 18:50:27 +01:00
parent 0f464f5814
commit f5af33c098
6 changed files with 40 additions and 130 deletions

View File

@@ -0,0 +1,11 @@
//
// VersionParseError.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 08/02/2022.
// Copyright © 2022 Nico Verbruggen. All rights reserved.
//
import Foundation
struct VersionParseError: Error {}

View File

@@ -16,7 +16,7 @@ class VersionExtractor {
public static func from(_ string: String) -> String? {
do {
let regex = try NSRegularExpression(
pattern: #"Laravel Valet (?<version>(\d+)(.)(\d+)((.)(\d+))?)"#,
pattern: #"(?<version>(\d+)(.)(\d+)((.)(\d+))?)"#,
options: []
)

View File

@@ -118,6 +118,14 @@ public struct PhpVersionNumber: Equatable {
*/
}
public static func parse(_ text: String) throws -> Self {
guard let versionText = VersionExtractor.from(text) else {
throw VersionParseError()
}
return Self.make(from: versionText)!
}
public static func make(from versionString: String, type: MatchType = .versionOnly) -> Self? {
let regex = try! NSRegularExpression(pattern: type.rawValue, options: [])
let match = regex.matches(in: versionString, options: [], range: NSMakeRange(0, versionString.count)).first

View File

@@ -27,9 +27,10 @@ class PhpInstallation {
arguments: ["--version"]
).trimmingCharacters(in: .whitespacesAndNewlines)
self.longVersion = PhpVersionNumber.make(
from: String(longVersionString.split(separator: "-")[0])
)!
// The parser should always work, or the string has to be very unusual.
// If so, the app SHOULD crash, so that the users report what's up.
// TODO: Alert the user that the version number could not be parsed.
self.longVersion = try! PhpVersionNumber.parse(longVersionString)
}
}