# apple/pkl architecture

> Architecture diagram of the GitHub repository apple/pkl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apple/pkl
- Repository: https://github.com/apple/pkl

## Overview

##### Major components (nodes) in the system
- Core Language Engine (pkl-core): This is the heart of the system which includes the parser (ANTLR and custom parsing code), AST (abstract syntax tree) construction, evaluator and various utility classes. It also handles the runtime aspects such as module resolution, security management, and execution.
- Standard Library (stdlib): These modules provide built-in functionality. They supply the default modules (for JSON, YAML, math, XML, etc.) that the Pkl language can import.
- CLI and Interpreters (pkl-cli, pkl-executor, pkl-server): The command-line interface (pkl-cli) provides an interactive or scriptable way to run Pkl programs. The executor and server subprojects further support remote or embedded execution, with a SPI design that allows for different execution strategies.
- Code Generation Modules (pkl-codegen-java, pkl-codegen-kotlin, pkl-commons-cli): These components generate code in target languages from Pkl configurations. They depend on the core engine to resolve information and then produce artifacts (e.g., Java or Kotlin code).
- Build System Integration (pkl-gradle): This module integrates Pkl into Gradle builds. It contains tasks and extensions (like “CodeGenTask” or “EvalTask”) that enable projects to use Pkl as part of their build pipelines.
- Documentation System (pkl-doc): This module uses the core information and the documentation of Pkl packages to generate human-readable documentation (HTML, Markdown) for language reference and package documentation.
- Additional Tools and Plugins (pkl-tools, pkl-lsp, IDE integrations, etc.): These support ecosystem integrations; they are separate repositories but tie into the overall Pkl ecosystem.

##### Relationships and data flows between components
- The CLI (or Gradle plugin) accepts a Pkl source file from the user and hands it off to the Core Engine.
- The Core Engine (pkl-core) parses the source, constructs an AST, applies validations, resolves modules, and executes or interprets the configuration.
- The Standard Library modules are imported by the Core Engine as built-in modules.
- Code generation modules take the processed AST or resolved module information from the Core and emit source code for target languages.
- The Documentation module reads package definitions (often packaged as Pkl packages) and uses code and comments to generate docs.
- The Executor/Server modules enable the Pkl process to be run in a dedicated process or container (e.g., remote execution or tests).

##### Architectural patterns/design principles
- Modular design with clear layer separations: core language processing is separated from tooling and integrations.
- Dependency inversion: many modules (e.g., code generators, documentation generators, Gradle plugin) depend on abstractions provided by pkl-core.
- Plugin architecture: The presence of SPI interfaces (e.g., in pkl-executor/spi) makes it easy to extend or swap out components.
- Build system integration: Gradle is used as the build orchestration tool with custom tasks defined in modules such as pkl-gradle and buildSrc.
- Extensive testing and benchmarking: Directories like “bench” and “pkl-commons-test” indicate that performance and correctness are continuously monitored.

##### Additional details
- Testing and benchmarking as peripheral components that validate all layers.
- Mention that each repository (CLI, core, docs, etc.) is part of the same ecosystem and shares common utilities (e.g., those in pkl-commons and pkl-commons-cli).

– Surrounding that, show the interfaces like the CLI, Executor/Server, and Build Integration components.
– Off to the side, list auxiliary tooling (code generators and documentation generators) that hook into the core.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Nodes
    U["User Input"]:::external
    Gs["Gradle Build (buildSrc & Config)"]:::external

    %% Core Language Processing Layer
    subgraph "Core Language Processing"
        A["Core Engine (Parser,AST,Evaluator,ModuleResolver)"]:::core
        B["Standard Library (JSON,YAML,XML,etc.)"]:::core
    end

    %% Interfaces Layer
    subgraph "Interfaces"
        C["CLI (Interactive/Scriptable)"]:::interface
        D["Executor (Remote/Embedded)"]:::interface
        E["Server (Execution Service)"]:::interface
        F["Build Integration (Gradle Plugin)"]:::interface
    end

    %% Tooling Layer
    subgraph "Tooling"
        G["Java Code Generation"]:::tooling
        H["Kotlin Code Generation"]:::tooling
        I["Commons CLI Support"]:::tooling
        J["Documentation Generator"]:::tooling
        K["Additional Tools & Plugins"]:::tooling
    end

    %% Data Flows & Relationships
    U -->|"triggers"| C
    U -->|"triggers"| F

    Gs -->|"configures"| A
    Gs -->|"configures"| C
    Gs -->|"configures"| F
    Gs -->|"configures"| G
    Gs -->|"configures"| H
    Gs -->|"configures"| I
    Gs -->|"configures"| J

    C -->|"invokes"| A
    F -->|"invokes"| A

    A -->|"imports"| B

    A -->|"executes"| D
    A -->|"executes"| E

    A -->|"generates"| G
    A -->|"generates"| H
    A -->|"supports"| I
    A -->|"providesInfo"| J

    A -->|"integratesWith"| K

    %% Click Events
    click A "https://github.com/apple/pkl/tree/main/pkl-core"
    click B "https://github.com/apple/pkl/tree/main/stdlib"
    click C "https://github.com/apple/pkl/tree/main/pkl-cli"
    click D "https://github.com/apple/pkl/tree/main/pkl-executor"
    click E "https://github.com/apple/pkl/tree/main/pkl-server"
    click F "https://github.com/apple/pkl/tree/main/pkl-gradle"
    click G "https://github.com/apple/pkl/tree/main/pkl-codegen-java"
    click H "https://github.com/apple/pkl/tree/main/pkl-codegen-kotlin"
    click I "https://github.com/apple/pkl/tree/main/pkl-commons-cli"
    click J "https://github.com/apple/pkl/tree/main/pkl-doc"
    click K "https://github.com/apple/pkl/tree/main/pkl-tools"

    %% Styles
    classDef core fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef interface fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef tooling fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;
```
