xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / PackLib / PackSchema.swift
PackSchema.swift111 lines · 3.4 KiBRaw
| 1 | import Foundation |
| 2 | import Yams |
| 3 | import XUtils |
| 4 | |
| 5 | public struct PackSchemaBase: Codable, Sendable { |
| 6 | public enum Version: Int, Codable, Sendable { |
| 7 | case v1 = 1 |
| 8 | } |
| 9 | |
| 10 | public var version: Version |
| 11 | |
| 12 | public var orgID: String? |
| 13 | public var bundleID: String? |
| 14 | |
| 15 | public var product: String? |
| 16 | |
| 17 | public var infoPath: String? |
| 18 | public var entitlementsPath: String? |
| 19 | |
| 20 | public var iconPath: String? |
| 21 | public var resources: [String]? |
| 22 | |
| 23 | /// Pre-compiled asset catalog (fork extension for Linux, where `actool` |
| 24 | /// is unavailable). `compiledDir` points at a directory produced on a Mac |
| 25 | /// by `actool` (e.g. `Assets.car`, loose icon PNGs and |
| 26 | /// `assets-partial-Info.plist`). Every file in the directory except |
| 27 | /// `*.sha256` sidecars and the partial plist is copied verbatim into the |
| 28 | /// `.app`; the partial plist is merged into the app's `Info.plist`. |
| 29 | public var assetCatalog: AssetCatalog? |
| 30 | |
| 31 | public struct AssetCatalog: Codable, Sendable { |
| 32 | public var compiledDir: String? |
| 33 | } |
| 34 | |
| 35 | public var extensions: [Extension]? |
| 36 | |
| 37 | /// Skip auto-creating the LSP support file if it's missing. |
| 38 | public var skipLSP: Bool? |
| 39 | |
| 40 | public struct Extension: Codable, Sendable { |
| 41 | public var product: String |
| 42 | public var bundleID: String? |
| 43 | public var infoPath: String |
| 44 | public var resources: [String]? |
| 45 | public var entitlementsPath: String? |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | @dynamicMemberLookup |
| 50 | public struct PackSchema: Sendable { |
| 51 | public typealias Extension = PackSchemaBase.Extension |
| 52 | |
| 53 | public enum IDSpecifier: Sendable { |
| 54 | case orgID(String) |
| 55 | case bundleID(String) |
| 56 | |
| 57 | func formBundleID(product: String) -> String { |
| 58 | switch self { |
| 59 | case .orgID(let orgID): "\(orgID).\(product)" |
| 60 | case .bundleID(let bundleID): bundleID |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public let base: PackSchemaBase |
| 66 | public let idSpecifier: IDSpecifier |
| 67 | |
| 68 | public init(validating base: PackSchemaBase) throws { |
| 69 | self.base = base |
| 70 | |
| 71 | if base.version != .v1 { |
| 72 | throw StringError("xtool.yml: Unsupported schema version: \(base.version.rawValue)") |
| 73 | } |
| 74 | |
| 75 | switch (base.bundleID, base.orgID) { |
| 76 | case (let bundleID?, _): |
| 77 | idSpecifier = .bundleID(bundleID) |
| 78 | case (nil, let orgID?): |
| 79 | idSpecifier = .orgID(orgID) |
| 80 | case (nil, nil): |
| 81 | throw StringError("xtool.yml: Must specify either orgID or bundleID") |
| 82 | } |
| 83 | |
| 84 | if let iconPath = base.iconPath { |
| 85 | let ext = URL(fileURLWithPath: iconPath).pathExtension |
| 86 | guard ext == "png" else { |
| 87 | throw StringError("xtool.yml: iconPath should have a 'png' path extension. Got '\(ext)'.") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if let compiledDir = base.assetCatalog?.compiledDir, compiledDir.isEmpty { |
| 92 | throw StringError("xtool.yml: assetCatalog.compiledDir must not be empty when present.") |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // swiftlint:disable:next force_try |
| 97 | public static let `default` = try! PackSchema(validating: .init( |
| 98 | version: .v1, |
| 99 | orgID: "com.example" |
| 100 | )) |
| 101 | |
| 102 | public init(url: URL) async throws { |
| 103 | let data = try await Data(reading: url) |
| 104 | let base = try YAMLDecoder().decode(PackSchemaBase.self, from: data) |
| 105 | try self.init(validating: base) |
| 106 | } |
| 107 | |
| 108 | public subscript<Subject>(dynamicMember keyPath: KeyPath<PackSchemaBase, Subject>) -> Subject { |
| 109 | self.base[keyPath: keyPath] |
| 110 | } |
| 111 | } |