-
Notifications
You must be signed in to change notification settings - Fork 0
Code Generation
lexicon-generate turns a composed lexicon into source or interchange artifacts.
swift run lexicon-generate commerce.lexicon \
--type swift,kotlin,go,rust,ts,json,json-ldThe --type option accepts a comma-separated list.
| Command | Output |
|---|---|
swift |
Swift source that imports SwiftLexicon. |
swift-standalone |
Stand-alone Swift source. |
kotlin |
Stand-alone Kotlin source. |
go |
Stand-alone Go source. |
rust |
Stand-alone Rust source. |
ts |
Stand-alone TypeScript source. |
json |
JSON classes and mixins snapshot. |
json-ld |
SKOS JSON-LD. |
swift run lexicon-generate commerce.lexicon --type go -o Generated/commerce-o/--output is a path without the final extension. The generator appends the extension for the selected type.
Examples:
| Type | Output with -o Generated/commerce
|
|---|---|
swift |
Generated/commerce.swift |
go |
Generated/commerce.go |
rust |
Generated/commerce.rs |
ts |
Generated/commerce.ts |
json |
Generated/commerce.json |
json-ld |
Generated/commerce.jsonld |
Do not generate two targets with the same extension to the same output stem in one command. For example, generate swift and swift-standalone separately because both write .swift.
Generation composes imports before emitting code:
@ ./shared-commerce.lexicon
commerce:
api:
@ ./storefront-api.lexicon
If composition conflicts are reported, generation fails instead of choosing a partial output.
Use swift when generated Swift should depend on the SwiftLexicon runtime and event APIs.
Use swift-standalone, kotlin, go, rust, or ts when you want a single generated file with no Lexicon package runtime dependency in the target language.
Use json when another tool wants the resolved class/mixin graph.
Use json-ld when ontology, knowledge-graph, or documentation tooling wants SKOS-compatible JSON-LD.
For Swift packages, prefer the build-tool plugins:
.target(
name: "CommerceVocabulary",
dependencies: [
.product(name: "SwiftLexicon", package: "Lexicon")
],
plugins: [
.plugin(name: "SwiftLibraryGeneratorPlugin", package: "Lexicon")
]
)The plugin scans the target directory for .lexicon files and generates Swift into SwiftPM's plugin work directory.
Use SwiftStandAloneGeneratorPlugin when the generated source should not depend on SwiftLexicon:
.target(
name: "CommerceVocabulary",
plugins: [
.plugin(name: "SwiftStandAloneGeneratorPlugin", package: "Lexicon")
]
)For non-Swift projects, run lexicon-generate from the native build tool:
-
go generatefor Go. -
build.rsor a Makefile target for Rust. - Gradle
Exectask for Kotlin. - npm script for TypeScript.
Keep generated files in a deterministic directory and commit them if the target build should not need Swift installed.
Generated code preserves exact lemma IDs while adapting selectors to target-language rules:
- Swift and Kotlin use backticks for reserved words.
- Go exports TitleCase selectors and keeps exact lowercase IDs through
ID(). - Rust uses raw identifiers such as
r#typeand also provides an exact-path macro. - TypeScript uses class fields and
__for the exact ID.
See the individual language pages for examples.