template_codegen

Code generator for precompiled MoonBit templates.

template
codegen
generator
precompile
cli
moon add justjavac/template_codegen@0.1.3
Download zip
Author
Version
0.1.3
License
MIT
Last updated
last month
Downloads
48

Dependencies

README

#justjavac/template_codegen

Scans #tpl.path("...") bindings and generates Render implementations.

Install the CLI with moon install justjavac/template_codegen.

#
CodegenError

pub(all) suberror CodegenError {
InvalidArguments(String)
InvalidBinding(String, Int, String)
MissingTemplate(String)
UnexpandedInclude(String)
} derive(Eq,
Debug
)

Errors raised by the code generator.

InvalidArguments reports CLI misuse, InvalidBinding reports malformed #tpl.path source bindings, MissingTemplate reports files that cannot be found, and UnexpandedInclude protects in-memory generation from emitting a renderer that still contains static include nodes.

#
TemplateInput

pub(all) struct TemplateInput {
source_path : String
template_path : String
struct_name : String
field_names : Array[String]
content : String
} derive(Eq, ToJson,
Debug
)

A source binding plus its loaded template content.

source_path, template_path, and struct_name identify the source binding. field_names is used to rewrite bare template field references to self.<field>, and content stores the template text after any static includes have been expanded.

#
generate

fn generate(inputs : Array[TemplateInput]) -> String raise

Generates a complete MoonBit source file from loaded template inputs.

The returned source contains one Render implementation per input. Package imports are not emitted; callers should import justjavac/template in the target package's moon.pkg.

#
generate_from_files

fn generate_from_files(source_files : Array[String], output_path : String) -> Unit raise

Generates output_path by scanning source files and reading their templates.

Only .mbt inputs are scanned, and the output path is ignored if it appears in the input list. Template paths are resolved relative to the source file that owns the #tpl.path attribute, and static includes are expanded before source generation.

#
generate_one

fn generate_one(input : TemplateInput) -> String raise

Generates one Render implementation from an in-memory template.

This function is useful for tests and tools that already loaded template text. Static include nodes must be expanded before calling it; otherwise it raises CodegenError::UnexpandedInclude.

#
qualify_filters

fn qualify_filters(expr : String) -> String

Qualifies short pipeline filters as calls to @template.

Already-qualified calls such as value |> @custom.filter are preserved. Bare identifiers after |> are treated as runtime filters so generated packages only need an explicit dependency on justjavac/template.

#
qualify_template_code

fn qualify_template_code(expr : String, field_names : Array[String]) -> String

Prefixes template struct fields with self. and qualifies short filters.

The transformation is intentionally small and template-oriented: it skips quoted strings, leaves already-qualified values alone, avoids field labels, and then rewrites short pipeline filters such as title |> trim to title |> @template.trim.

#
run_cli

fn run_cli(args : Array[String]) -> Unit raise

Parses command line arguments and runs file-based generation.

The supported CLI shape is template_codegen --scan <inputs...> -o <output>. Extra non-scan arguments are ignored so the first executable path from @env.args() can be passed directly.

#
scan_source

fn scan_source(source_path : String, source : String) -> Array[
TemplateBinding
] raise CodegenError

Scans MoonBit source for #tpl.path("...") bindings.

Each binding records the template path and the following struct name. This scanner intentionally handles only the small attribute/struct shape needed by codegen; MoonBit syntax and type checking remain the compiler's job. Field names from the struct body are collected so generated template code can add self. automatically where needed.

#
template_input

fn template_input(source_path~ : String, template_path~ : String, struct_name~ : String, field_names? : Array[String], content~ : String) -> TemplateInput

Creates an in-memory generation input.

Use this constructor when tests or custom tools already have template text available. Pass field_names when template expressions omit self. for fields declared on the bound struct.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io