prole/prole-app/Sources/PortMappingsXML.swift
chrisfu 197c72dd7a Refactor prole-app and establish temporary release process
- Moved prole-tools-app to prole-app at the project root to make it self-contained for transition to its own repository.
- Created prole-tools-app/dist/ directory to host build artifacts.
- Generated distribution artifacts (Prole Tools.app and Prole Tools.zip) using prole-app/build.sh package.
- Checked in the generated artifacts to prole-tools-app/dist/ (bypassing .gitignore for temporary release process).
Changes Summary
•
Renamed directory prole-tools-app/ to prole-app/.
•
Populated prole-tools-app/dist/ with the latest build output from prole-app/build.sh.
•
Staged all changes, including the forced addition of ignored artifacts in prole-tools-app/dist/.
2026-01-18 15:04:23 -08:00

78 lines
3.1 KiB
Swift

import Foundation
struct PortMappingXML: Equatable {
var id: String
var namespace: String
var target: String
var address: String
var hostPort: String
var servicePort: String
var proto: String
var description: String
}
enum PortMappingsXMLStore {
private static func configURL() -> URL {
// Determine PROLE_HOME similarly to PFScriptBridge
let env = ProcessInfo.processInfo.environment
if let home = env["PROLE_HOME"], !home.isEmpty {
return URL(fileURLWithPath: home).appendingPathComponent("conf/port-mappings.properties")
}
let defaultHome = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("dev/prole")
return defaultHome.appendingPathComponent("conf/port-mappings.properties")
}
static func load() -> [PortMappingXML] {
let url = configURL()
guard let data = try? Data(contentsOf: url) else { return [] }
let parser = XMLParser(data: data)
let delegate = ParserDelegate()
parser.delegate = delegate
if parser.parse() { return delegate.items }
return []
}
static func save(_ items: [PortMappingXML]) throws {
let url = configURL()
var xml = """
<?xml version="1.0" encoding="UTF-8"?>
<portMappings>
"""
for m in items {
xml += " <mapping id=\"\(escape(m.id))\" namespace=\"\(escape(m.namespace))\" target=\"\(escape(m.target))\" address=\"\(escape(m.address))\" hostPort=\"\(escape(m.hostPort))\" servicePort=\"\(escape(m.servicePort))\" protocol=\"\(escape(m.proto))\" description=\"\(escape(m.description))\"/>\n"
}
xml += """
</portMappings>
"""
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
try xml.data(using: .utf8)?.write(to: url)
}
private static func escape(_ s: String) -> String {
return s.replacingOccurrences(of: "&", with: "&amp;")
.replacingOccurrences(of: "\"", with: "&quot;")
.replacingOccurrences(of: "<", with: "&lt;")
.replacingOccurrences(of: ">", with: "&gt;")
}
private final class ParserDelegate: NSObject, XMLParserDelegate {
var items: [PortMappingXML] = []
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
if elementName == "mapping" {
let m = PortMappingXML(
id: attributeDict["id"] ?? "",
namespace: attributeDict["namespace"] ?? "default",
target: attributeDict["target"] ?? "",
address: attributeDict["address"] ?? "127.0.0.1",
hostPort: attributeDict["hostPort"] ?? "",
servicePort: attributeDict["servicePort"] ?? "",
proto: attributeDict["protocol"] ?? "TCP",
description: attributeDict["description"] ?? ""
)
items.append(m)
}
}
}
}