Repository Public License
1import AppKit2import Foundation3import PrismDataRuntime4import PrismRenderer5import SwiftUI6import PrismComponentCatalog7import PrismComponentSDK8import PrismCore9import PrismIconKit10import PrismObjectModel11import PrismSatellitePublisher1213/// Developer command-line tool. Subcommands are added as the modules they exercise land.14func fail(_ message: String) -> Never {15 FileHandle.standardError.write(Data((message + "\n").utf8))16 exit(1)17}1819let args = Array(CommandLine.arguments.dropFirst())20switch args.first {21case "catalog-lock":22 // Prints the Swift source of CatalogLock for the current manifests.23 var out = "// Generated by `scripts/update-catalog-lock.sh` — do not edit by hand.\n"24 out += "// Recorded content hashes of the catalog manifests; the registry rejects any manifest that does not match.\n"25 out += "public enum CatalogLock {\n public static let hashes: [String: String] = [\n"26 for m in Catalog.manifests.sorted(by: { $0.type < $1.type }) { out += " \"\(m.type)\": \"\(m.contentHash)\",\n" }27 out += " ]\n}\n"28 print(out, terminator: "")29case "catalog":30 for r in Catalog.registry.rejections { print("REJECTED \(r.type): \(r.reason)") }31 for m in Catalog.registry.all { print("\(m.category)\t\(m.type)\t\(m.version)\t\(m.name)") }32 print("\(Catalog.registry.count) components loaded, \(Catalog.registry.rejections.count) rejected")33case "validate":34 guard args.count == 2, let data = FileManager.default.contents(atPath: args[1]) else { fail("usage: prism-cli validate <blueprint.json>") }35 do {36 let bp = try AppBlueprint.load(data)37 let issues = BlueprintValidator.validate(bp, registry: Catalog.registry)38 issues.forEach { print($0) }39 print("\(bp.name): \(bp.objects.count) objects, \(issues.errors.count) errors, \(issues.warnings.count) warnings, coverage \(Int(Catalog.registry.coverage(of: bp) * 100))%")40 exit(issues.hasErrors ? 2 : 0)41 } catch { fail(error.localizedDescription) }42case "render":43 // Renders a blueprint off-screen with its sample rows (and an optional CSV for the first table) to a PNG.44 guard args.count >= 3, let data = FileManager.default.contents(atPath: args[1]) else { fail("usage: prism-cli render <blueprint.json> <out.png> [data.csv]") }45 MainActor.assumeIsolated {46 do {47 let bp = try AppBlueprint.load(data)48 let runtime = try AppRuntime(blueprint: bp, store: AppDataStore(url: nil), registry: Catalog.registry)49 runtime.locale = Locale(identifier: "en_US"); runtime.currencyCode = "USD"50 if args.count > 3, let table = bp.tables.first?.id { _ = runtime.importCSV(from: URL(fileURLWithPath: args[3]), into: table) }51 let size = CGSize(width: 1180, height: 760)52 let host = NSHostingView(rootView: AppView(runtime: runtime).frame(width: size.width, height: size.height).background(Color(nsColor: .windowBackgroundColor)))53 host.frame = CGRect(origin: .zero, size: size)54 let window = NSWindow(contentRect: host.frame, styleMask: [.borderless], backing: .buffered, defer: false)55 window.contentView = host56 host.layoutSubtreeIfNeeded()57 RunLoop.main.run(until: Date().addingTimeInterval(0.5))58 guard let rep = host.bitmapImageRepForCachingDisplay(in: host.bounds) else { fail("could not render") }59 host.cacheDisplay(in: host.bounds, to: rep)60 try rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: args[2]))61 for line in runtime.logs where line.level != .info { print("\(line.level.rawValue): \(line.message)") }62 print("rendered \(bp.name): \(bp.components.count) components")63 } catch { fail(error.localizedDescription) }64 }65case "icon":66 guard args.count == 3 else { fail("usage: prism-cli icon <name> <out.icns>") }67 do { try SatelliteIcon.write(name: args[1], seed: args[1], to: URL(fileURLWithPath: args[2])) } catch { fail(error.localizedDescription) }68case "icon-spec":69 // Draws a designed icon (an IconSpec JSON file) as an .icns.70 guard args.count == 3, let data = FileManager.default.contents(atPath: args[1]) else { fail("usage: prism-cli icon-spec <icon.json> <out.icns>") }71 do { try SatelliteIcon.write(spec: try JSONValue.parse(data), to: URL(fileURLWithPath: args[2])) } catch { fail(error.localizedDescription) }72case "icon-png":73 // Draws an icon spec (JSON file) to a PNG, to look at what a design produces.74 guard args.count >= 3, let data = FileManager.default.contents(atPath: args[1]) else { fail("usage: prism-cli icon-png <icon.json> <out.png> [pixels]") }75 do {76 let spec = try JSONValue.parse(data)77 let problems = IconSpec.problems(spec)78 guard problems.isEmpty else { fail(problems.joined(separator: "\n")) }79 try IconRenderer.png(spec, pixels: args.count > 3 ? Int(args[3]) ?? 512 : 512).write(to: URL(fileURLWithPath: args[2]))80 } catch { fail(error.localizedDescription) }81default:82 fail("usage: prism-cli <catalog|catalog-lock|validate <file>|render <blueprint> <out.png>|icon <name> <out.icns>|icon-spec <icon.json> <out.icns>|icon-png <icon.json> <out.png> [pixels]>")83}84