@imp/rules/imp/codegen

Module: @imp/rules/imp/codegen.

assert_generated_src_path(rule, index, actual, expected)

The run-time half of the staging contract: a generated source artifact must be at exactly the workspace path that its entry declared, because a compiler finds only the files that are in the package directory. A rule calls this function in its task run callback, when it knows the sandbox path (exec.path(handle)).

Source: rules/imp/codegen/index.luau:156.

codegen(options)

Declare generated sources that stay in the graph.

local codegen = require("@imp/rules/imp/codegen").codegen

local bindings = codegen({ display = "generate Odin bindings", tools = { python3 = core.native_tool("python3") }, inputs = { schema = core.files({ include = { "schema.json" } }) }, output_paths = { "app/generated/bindings.odin" }, argv = function(_exec, _resolved) return { "python3", "tools/schema_to_odin.py", "schema.json", "app/generated/bindings.odin" } end, })

output_paths are workspace-relative, and they are the contract: a package stages each artifact at exactly that path, so a path has one owner. Exclude a generated path from each glob that could also include it.

A nested output path needs no mkdir. The executor creates the parent directory of each declared file output before it starts the program.

Options are as for generated_files(): display, tools, inputs, output_paths, and argv.

Returns a frozen table { paths, files, BUILD, CODEGEN }. files[path] is the artifact handle of that output path. BUILD builds the generator and writes nothing to the workspace. CODEGEN holds the same generator task and makes it selectable with imp codegen, which exports the files below dist/codegen/.

Source: rules/imp/codegen/index.luau:61.

dedupe_generated_srcs(list, options)

Remove generated sources that repeat one workspace path, and reject a conflict: two different artifacts at the same path. In the sandbox, one would overwrite the other, and the input order would select the result.

Options: rule (the consuming rule name, for error messages).

Source: rules/imp/codegen/index.luau:131.

normalize_generated_srcs(entries, options)

Normalize the generated_srcs list of a rule into the shape of the staging contract: an array of { artifact, expected_path }, where expected_path is the workspace-relative path of the artifact.

An entry is a codegen() result, which gives one pair for each output path, or a { artifact, path } table. resolve_path changes the rule-relative path of such a table to a workspace-relative path. Its default returns the path unchanged.

This function is internal to the language rules that consume generated sources. It is not part of the public API.

Options: rule (the consuming rule name, for error messages) and optional resolve_path.

Source: rules/imp/codegen/index.luau:97.

stamp_file(options)

Declare a graph artifact that contains fixed text.

This is the simplest codegen(): the content is a literal, so there is no generator command and no declared tool. argv[0] is a bare sh, the shell that the executor finds.

Options: output (the workspace-relative output path) and text.

Returns a frozen { file, BUILD } with the file artifact and its build root.

Source: rules/imp/codegen/index.luau:177.