Skip to content

apple/pkl-swift-examples

pkl-swift-example

This is a sample project that demonstrates Pkl <> Swift integration.

This is a basic HTTP server that is configured via Pkl in the pkl/ directory.

Requirements

  • Swift +6.1

  • Pkl +0.30.0;s

Project structure

Directory

Description

pkl/

Pkl configuration sources

Sources/Gen/

Generated Swift sources from Pkl

Sources/App/

Swift files for runtime evaluation example

Sources/BuildTimeEval/

Swift files for build-time evaluation example

Codegen

Generated sources are already checked into git for learning purposes.

To generate new Pkl sources, first install the Swift code generator:

curl -L https://github.com/apple/pkl-swift/releases/download/0.7.0/pkl-gen-swift-macos.bin -o pkl-gen-swift
chmod +x pkl-gen-swift

With that installed, generate Swift sources via:

pkl-gen-swift pkl/**.pkl -o Sources/Gen

Build-time evaluation

The example in Sources/BuildTimeEval/ shows how the pkl-binary encoding can be used to configure applications. This mechanism separates evaluation of Pkl code from application runtime by encoding the configuration data as pkl-binary, storing it in a file, and loading it later during application startup. This example works identically to the runtime evaluation example, but does not require the application to spawn the Pkl CLI as a subprocess.

The configuration module is rendered as pkl-binary by running:

pkl eval pkl/dev/config.pkl -f pkl-binary -o pkl/dev/config.msgpack

Running the server

To run the project call swift run.

About

Examples for using Pkl within Swift applications

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors 6