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

🏗 Parse CaskFile using regex

This commit is contained in:
2023-02-05 13:16:04 +01:00
parent 5c92d47ff0
commit 208a430066
4 changed files with 54 additions and 19 deletions

View File

@ -48,4 +48,8 @@ class AppUpdater {
public func presentCouldNotRetrieveUpdate() { public func presentCouldNotRetrieveUpdate() {
} }
private func prepareForDownload() {
}
} }

View File

@ -37,7 +37,8 @@ class DomainListPhpCell: NSTableCellView, DomainListCellProtocol {
imageViewPhpVersionOK.image = NSImage(named: "Isolated") imageViewPhpVersionOK.image = NSImage(named: "Isolated")
imageViewPhpVersionOK.toolTip = "domain_list.tooltips.isolated".localized(site.servingPhpVersion) imageViewPhpVersionOK.toolTip = "domain_list.tooltips.isolated".localized(site.servingPhpVersion)
} else { } else {
imageViewPhpVersionOK.isHidden = (site.preferredPhpVersion == "???" || !site.isCompatibleWithPreferredPhpVersion) imageViewPhpVersionOK.isHidden = (site.preferredPhpVersion == "???"
|| !site.isCompatibleWithPreferredPhpVersion)
imageViewPhpVersionOK.image = NSImage(named: "Checkmark") imageViewPhpVersionOK.image = NSImage(named: "Checkmark")
imageViewPhpVersionOK.toolTip = "domain_list.tooltips.checkmark".localized(site.preferredPhpVersion) imageViewPhpVersionOK.toolTip = "domain_list.tooltips.checkmark".localized(site.preferredPhpVersion)

View File

@ -11,10 +11,24 @@ import Foundation
struct CaskFile { struct CaskFile {
var properties: [String: String] var properties: [String: String]
var name: String {
return self.properties["name"]!
}
var url: String {
return self.properties["url"]!
}
var sha256: String {
return self.properties["sha256"]!
}
var version: String {
return self.properties["version"]!
}
public static func from(url: URL) -> CaskFile? { public static func from(url: URL) -> CaskFile? {
let string = try? String(contentsOf: url) let string = try? String(contentsOf: url)
guard let string else { guard let string else {
Log.err("The content of the URL for the CaskFile could not be retrieved")
return nil return nil
} }
@ -22,25 +36,37 @@ struct CaskFile {
.filter { $0 != "" } .filter { $0 != "" }
if lines.count < 4 { if lines.count < 4 {
Log.err("The CaskFile is <4 lines long, which is too short")
return nil return nil
} }
if !lines.first!.starts(with: "cask") || !lines.last!.starts(with: "end") { if !lines.first!.starts(with: "cask") || !lines.last!.starts(with: "end") {
Log.err("The CaskFile does not start with 'cask' or does not end with 'end'")
return nil return nil
} }
var props: [String: String] = [:] var props: [String: String] = [:]
lines.forEach { line in let regex = try! NSRegularExpression(pattern: "(\\w+)\\s+'([^']+)'")
let text = line.trimmingCharacters(in: .whitespacesAndNewlines)
let parts = text.split(separator: " ")
if parts.count == 2 { for line in lines {
props[String(parts[0])] = String(parts[1]) if let match = regex.firstMatch(
.replacingOccurrences(of: "\'", with: "") in: String(line),
range: NSRange(location: 0, length: line.utf16.count)
) {
let keyRange = match.range(at: 1)
let valueRange = match.range(at: 2)
let key = (line as NSString).substring(with: keyRange)
let value = (line as NSString).substring(with: valueRange)
props[key] = value
} }
} }
for required in ["version", "sha256", "url", "name"] where !props.keys.contains(required) {
Log.err("Property '\(required)' expected on CaskFile, assuming CaskFile is invalid")
return nil
}
return CaskFile(properties: props) return CaskFile(properties: props)
} }

View File

@ -17,32 +17,36 @@ class CaskFileParserTest: XCTestCase {
} }
func test_can_extract_fields_from_cask_file() throws { func test_can_extract_fields_from_cask_file() throws {
let caskFile = CaskFile.from(url: CaskFileParserTest.exampleFilePath) guard let caskFile = CaskFile.from(url: CaskFileParserTest.exampleFilePath) else {
return XCTFail("The CaskFile could not be parsed, check the log for more info")
}
XCTAssertEqual( XCTAssertEqual(
caskFile!.properties["version"], caskFile.version,
"5.7.2_1035" "5.7.2_1035"
) )
XCTAssertEqual( XCTAssertEqual(
caskFile!.properties["homepage"], caskFile.sha256,
"https://phpmon.app" "1cb147bd1b1fbd52971d90dff577465b644aee7c878f15ede57f46e8f217067a"
) )
XCTAssertEqual( XCTAssertEqual(
caskFile!.properties["appcast"], caskFile.name,
"https://github.com/nicoverbruggen/phpmon/releases.atom" "PHP Monitor DEV"
) )
XCTAssertEqual( XCTAssertEqual(
caskFile!.properties["url"], caskFile.url,
"https://github.com/nicoverbruggen/phpmon/releases/download/v5.7.2/phpmon-dev.zip" "https://github.com/nicoverbruggen/phpmon/releases/download/v5.7.2/phpmon-dev.zip"
) )
} }
func test_can_extract_fields_from_remote_cask_file() throws { func test_can_extract_fields_from_remote_cask_file() throws {
let caskFile = CaskFile.from(url: Constants.Urls.StableBuildCaskFile) guard let caskFile = CaskFile.from(url: Constants.Urls.StableBuildCaskFile) else {
return XCTFail("The remote CaskFile could not be parsed, check the log for more info")
}
XCTAssertTrue(caskFile!.properties.keys.contains("version")) XCTAssertTrue(caskFile.properties.keys.contains("version"))
XCTAssertTrue(caskFile!.properties.keys.contains("homepage")) XCTAssertTrue(caskFile.properties.keys.contains("homepage"))
XCTAssertTrue(caskFile!.properties.keys.contains("url")) XCTAssertTrue(caskFile.properties.keys.contains("url"))
XCTAssertTrue(caskFile!.properties.keys.contains("appcast")) XCTAssertTrue(caskFile.properties.keys.contains("appcast"))
} }
} }