### Install SwiftFormat using Homebrew Source: https://github.com/apple/pkl-swift/blob/main/DEVELOPMENT.adoc Use this command to install SwiftFormat via Homebrew. Ensure Homebrew is installed first. ```shell brew install swiftformat ``` -------------------------------- ### Get Help for Pkl Code Generation CLI Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc Display the help message for the `pkl gen.pkl` command to understand available options and configurations. ```bash pkl run @pkl.swift/gen.pkl --help ``` -------------------------------- ### Implement External Reader Client in Swift Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/external-readers.adoc This example demonstrates how to set up and run an external reader client in Swift. It includes a custom `MyResourceReader` that reads environment variables. ```swift import Foundation import PklSwift @main struct Main { static func main() async throws { let client = ExternalReaderClient( options: ExternalReaderClientOptions( resourceReaders: [MyResourceReader()] )) try await client.run() } } struct MyResourceReader: ResourceReader { var scheme: String { "env2" } var isGlobbable: Bool { false } var hasHierarchicalUris: Bool { false } func listElements(uri: URL) async throws -> [PathElement] { throw PklError("not implemented") } func read(url: URL) async throws -> [UInt8] { let key = url.absoluteString.dropFirst(scheme.count + 1) return Array((ProcessInfo.processInfo.environment[String(key)] ?? "").utf8) } } ``` -------------------------------- ### Evaluate Pkl Configuration in Swift Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc Load and evaluate Pkl configuration data directly into Swift objects using the generated code. This example loads configuration from a file path. ```swift func main() async throws { let config = try await AppConfig.loadFrom(source: .path("pkl/local/appConfig.pkl")) print("I\'m running on host \(config.host) ") } ``` -------------------------------- ### Define Pkl Configuration Data Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc Create a Pkl file that amends a schema and provides specific values for configuration parameters. This example defines local environment settings. ```pkl amends "../AppConfig.pkl" host = "localhost" port = 5939 ``` -------------------------------- ### Install Hawkeye using Cargo Source: https://github.com/apple/pkl-swift/blob/main/DEVELOPMENT.adoc Install the hawkeye formatter locally using Cargo. Requires the Rust toolchain to be installed. ```shell cargo install hawkeye ``` -------------------------------- ### Run Project Formatters Source: https://github.com/apple/pkl-swift/blob/main/DEVELOPMENT.adoc Execute the 'make format' command to run all configured project formatters. Alternatively, run SwiftFormat and hawkeye individually. ```shell make format ``` ```shell swiftformat . ``` ```shell hawkeye format ``` -------------------------------- ### Run Pkl Code Generation Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/codegen.adoc Execute the Pkl code generation tool to convert Pkl configuration files into Swift source files. Ensure the `@pkl.swift/gen.pkl` tool and the input Pkl file are correctly specified. ```bash pkl run @pkl.swift/gen.pkl pkl/AppConfig.pkl -o Sources/MyProject/Generated/ ``` -------------------------------- ### Load Configuration in Swift Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/index.adoc Load configuration from a Pkl file into a Swift object. Ensure the Pkl runtime is available and the configuration class (MyConfig) is defined. ```swift @main class MyApp { static func main() async throws { let config = try await MyConfig.loadFrom(source: .path("config.pkl")) print("I'm running on host \(config.host)") } } ``` -------------------------------- ### Create and Tag Release Source: https://github.com/apple/pkl-swift/wiki/Release-Process Create annotated tags for the release version and the Pkl Swift package. Ensure tags do not have a 'v' prefix. ```bash git tag -a x.y.z -m "Release version x.y.z" git tag -a pkl.swift@x.y.z -m "Release of x.y.z Pkl package" ``` -------------------------------- ### Define Pkl Schema Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc Create a Pkl file to define the structure and types of your application's configuration. ```pkl module AppConfig /// The hostname of this application. host: String /// The port to listen on. port: UInt16 ``` -------------------------------- ### Generate Swift Code from Pkl Schema (CLI) Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc Use the `pkl gen.pkl` CLI tool to generate Swift source code from your Pkl schema file. This command assumes the `pkl.swift` package is available via a URI. ```bash pkl run package://pkg.pkl-lang.org/pkl-swift/pkl.swift@{version}#/gen.pkl pkl/AppConfig.pkl -o Sources/MyApplication/Generated/ ``` -------------------------------- ### Generate Swift Code from Pkl Schema (PklProject) Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc An alternative method to generate Swift code using the `pkl.swift` dependency managed by a `PklProject`. This simplifies the CLI command. ```bash pkl run @pkl.swift/gen.pkl pkl/AppConfig.pkl -o Sources/MyApplication/Generated/ ``` -------------------------------- ### Create Release Branch Source: https://github.com/apple/pkl-swift/wiki/Release-Process Create a new release branch from the main branch for minor version releases. ```bash git checkout -b release/x.x ``` -------------------------------- ### Push Tags to Upstream Source: https://github.com/apple/pkl-swift/wiki/Release-Process Push the created release and package tags to the 'upstream' remote repository. This assumes 'upstream' is the alias for the apple/pkl-swift remote. ```bash git push upstream refs/tags/x.y.z git push upstream refs/tags/pkl.swift@x.y.z ``` -------------------------------- ### Evaluate Pkl module into generated Swift struct Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/evaluation.adoc Use the generated `loadFrom` method on Swift structs that correspond to Pkl modules to evaluate them directly. ```swift @main struct Main { static func main() async throws { let config = try await AppConfig.loadFrom(source: .path("myconfig.pkl")) print("Got config: \(config)") } } ``` -------------------------------- ### Regenerate Test Fixtures Source: https://github.com/apple/pkl-swift/blob/main/DEVELOPMENT.adoc Run this command to regenerate all test fixtures after making changes to the codegen. ```shell make generate-fixtures ``` -------------------------------- ### Evaluate Pkl module's text output Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/evaluation.adoc Use `evaluateOutputText` to retrieve the textual output of a Pkl module, similar to the `output.text` property in Pkl. ```pkl foo = "foo" bar = "bar" ``` ```swift @main struct Main { static func main() async throws { let textOutput = try await withEvaluator { evaluator in try await evaluator.evaluateOutputText(source: .path("my/module.pkl")) } print(textOutput) } } ``` -------------------------------- ### Evaluate arbitrary Pkl expressions Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/evaluation.adoc Evaluate any Pkl expression within a module context using `evaluateExpression`. This method allows specifying the source of the module, the expression to evaluate, and the expected result type. ```swift @main struct Main { static func main() async throws { let res = try await withEvaluator { evaluator in try await evaluator.evaluateExpression( source: .text("foo = 5"), // <1> expression: "foo + 10", // <2> as: Int.self // <3> ) } print("foo is \(res)") // prints "foo is 15" } } ``` -------------------------------- ### Add pkl-swift Dependency to Package.swift Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/quickstart.adoc Include the pkl-swift library as a dependency in your Swift Package Manager project. ```swift let package = Package( dependencies: [ .package(url: "https://github.com/apple/pkl-swift", from: "{version}") ], targets: [ .executableTarget( name: "my-application", dependencies: [.product(name: "PklSwift", package: "pkl-swift")] ), ] ) ``` -------------------------------- ### Resolve Swift Name Conflicts with @swift.Name Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/codegen.adoc Illustrates how to use the `@swift.Name` annotation to prevent naming collisions when Pkl names are converted to Swift names. This is necessary when different Pkl declarations might result in the same Swift identifier. ```pkl import "package://pkg.pkl-lang.org/pkl-swift/pkl.swift@{version}#/swift.pkl" @swift.Name { value = "MyCoolApplication" } class My_Application class MyApplication ``` -------------------------------- ### Evaluate Pkl module into Decodable Swift struct Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/evaluation.adoc Evaluate a Pkl module directly into a Swift struct that conforms to `Decodable`. Ensure the Swift struct's property names match the Pkl module's or use a `CodingKeys` enum. ```pkl foo: String = "hello" bar: Int = 5 ``` ```swift struct Foo: Decodable { let foo: String let bar: Int } ``` ```swift let result = try await withEvaluator { evaluator in try await evaluator.evaluateModule(source: .path("foo.pkl"), as: Foo.self) } ``` ```swift struct Foo: Decodable { let myFoo: String let myBar: Innt enum CodingKeys: String, CodingKey { case myFoo = "foo" case myBar = "bar" } } ``` -------------------------------- ### Pkl Union to Swift Enum Conversion Source: https://github.com/apple/pkl-swift/blob/main/docs/modules/ROOT/pages/codegen.adoc Demonstrates how a Pkl typealias defined as a union of string literals is converted into a Swift enum. This Swift enum conforms to String, CaseIterable, Decodable, and Hashable protocols. ```pkl typealias City = "San Francisco"|"Cupertino"|"London" ``` ```swift enum City: String, CaseIterable, Decodable, Hashable { case sanFrancisco = "San Francisco" case cupertino = "Cupertino" case london = "London" } ``` -------------------------------- ### Merge Changes into Release Branch Source: https://github.com/apple/pkl-swift/wiki/Release-Process Merge changes from the main branch into the existing release branch using a fast-forward merge. ```bash git checkout release/x.x git merge --ff-only main ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.