From first schematic to CI
This tutorial takes one schematic from an empty folder to a CI check. You build a generator
that writes a service class, test it, preview and run it, then add an evaluation schematic
that builder validate runs as a gate. It continues from
Your first schematic with a new example, a generator
that uses a template and a numeric input, so steps 1 and 2 are short. Each step links the page
that covers the topic in depth.
Every command and output below comes from a run with the builder CLI v0.19.17 and
@pbuilder/sdk 0.23.0, on Node 26 and Bun 1.4. Install the CLI
first if you haven’t.
1. Create the workspace
Section titled “1. Create the workspace”Start in an empty folder:
git initbuilder initbuilder init writes project-builder.json and a schematics/ folder, and installs
@pbuilder/sdk as a dev dependency, creating a package.json first.
2. Scaffold a schematic
Section titled “2. Scaffold a schematic”builder new schematic service --title "Add a service" --job "create a service module"This creates schematics/service/ with a schema.json, a factory.ts stub and a
schema.generated.ts, and registers it in project-builder.json as default:service. From CLI v0.21.0, --title and
at least one --job are required: they go in the schematic’s entry and are what a person or a
coding agent picks it by.
3. Declare the inputs and generate their types
Section titled “3. Declare the inputs and generate their types”Replace the contents of schematics/service/schema.json:
{ "properties": { "name": { "type": "string", "label": "Service name", "required": true }, "port": { "type": "number", "label": "Port", "default": 3000 } }}Then regenerate schema.generated.ts from it:
npx pbuilder-codegen schematics/servicepbuilder-codegen: wrote schema.generated.tsThe generated Input type now has name: string and port: number. After you change several
schemas, run npx pbuilder-codegen with no argument to regenerate every registered schematic.
See Type generation.
4. Write the factory
Section titled “4. Write the factory”The factory creates one file. Both its path and its content are templates, rendered at run time with the options you pass:
import { create } from "@pbuilder/sdk/commons";import type { Input } from "./schema.generated.ts";
export default (input: Input) => { create("src/services/{= .name | dasherize =}.ts", { template: [ "export class {= .name | classify =}Service {", " readonly port = {= .port =};", "}", "", ].join("\n"), options: { name: input.name, port: input.port }, });};5. Test the factory
Section titled “5. Test the factory”runFactoryForTest runs the factory in memory, with no CLI and no files written. Save this as
schematics/service/factory.test.ts:
import { expect, test } from "bun:test";import { runFactoryForTest } from "@pbuilder/sdk/testing";import factory from "./factory.ts";
test("writes one service file with the port option", async () => { const result = await runFactoryForTest(factory, { name: "orderHistory", port: 4000 }); expect(result.error).toBeUndefined(); expect(result.tree.size).toBe(1); const [content] = [...result.tree.values()]; expect(content).toContain("readonly port = {= .port =};");});bun test 1 pass 0 fail 3 expect() callsThe assertion checks the raw {= .port =} text because the test harness does not render
templates. The engine renders them when you run the schematic, which the next two steps show.
The testing guide covers seed files and running the harness on Node with
Vitest, Jest or Rstest instead of Bun.
6. Preview with a dry run
Section titled “6. Preview with a dry run”--dry-run prints the plan and writes nothing:
builder execute --output=pretty --dry-run default:service --name=orderHistorydry run: nothing is written to the workspacecreate (1) create src/services/order-history.ts✓ done — 1 createThe path is already rendered, and src/ does not exist yet. The port input was not passed, so
the plan uses its default, 3000.
7. Run it
Section titled “7. Run it”builder execute --output=pretty default:service --name=orderHistory --port=4000create (1) create src/services/order-history.ts✓ done — 1 createexport class OrderHistoryService { readonly port = 4000;}create does not overwrite. Running the same command again fails with a path-collision error
and exit code 5, and writes nothing. Leaving out --name fails before the factory runs, with
execute_missing_required_inputs and exit code 2.
8. Check the result with builder validate
Section titled “8. Check the result with builder validate”A generator makes code once. An evaluation schematic keeps checking it afterwards. Scaffold one:
builder new schematic check-service --title "Check the service" --job "check a service module"builder new schematic always creates a generator and has no --role flag yet (cli#269). Mark
it as an evaluation by adding "role": "evaluation" to its entry in project-builder.json. The
CLI ignores the title and jobs of an evaluation:
"check-service": { "path": "./schematics/check-service", "title": "Check the service", "jobs": ["check a service module"], "role": "evaluation"}The factory reads the workspace and returns findings. This one reports an error when the service file is missing and a warning when it still uses the default port:
import { find } from "@pbuilder/sdk";import type { Input } from "./schema.generated.ts";
export default async (input: Input) => { const path = "src/services/order-history.ts"; const source = await find(path).read();
if (source === undefined) { return { items: [{ rule: "service-exists", message: `${path} is missing`, severity: "error" }], }; } if (source.includes("port = 3000")) { return { items: [{ rule: "service-port", message: "the service uses the default port", severity: "warning", path }], }; } return { items: [] };};Run it:
builder validate --output=prettydry run: nothing is written to the workspace✓ done — no changespassed: 0 finding(s), none with the severity errorThe exit code is 0. A warning is reported but does not fail the check. Delete
src/services/order-history.ts and run it again:
error service-exists — src/services/order-history.ts is missing [default:check-service]✗ [CLI] the evaluation reported 1 finding(s) with the severity error validate_findingsThe exit code is 6. Running the evaluation with builder execute is refused with
execute_schematic_not_executable (exit code 2); evaluations only run through
builder validate. Check code with builder validate covers finding
fields, --include and --exclude, and checking a generator for drift.
9. Run the checks in CI
Section titled “9. Run the checks in CI”The exit code of builder validate carries the verdict, so a CI job needs only a few commands.
This sequence ran in the workspace above after deleting node_modules:
npm cibun testbuilder validateIt ended with 1 pass, then {"result":{"role":"evaluation","findings":[]},…} and exit code 0.
Piped output is JSON, so a job that runs builder validate without --output prints the
envelope. The builder CLI must be installed on the CI machine; see
Installation. This tutorial did not run the sequence on a CI
provider.
builder validate exits with 6 on an error finding and with 2 or 5 when the check itself
cannot run, so a failing job tells you which one happened.
10. Run it from an AI agent
Section titled “10. Run it from an AI agent”An agent uses the same commands: builder info to find the schematic and its inputs,
builder execute --dry-run to preview, and builder validate to check. See
Use Project Builder with AI coding agents.
Next steps
Section titled “Next steps”- Template language: the pipes and conditionals available in
create(). - Mutation verbs: everything a factory can do to the tree.
- Error handling: what a rejected run reports and how to write failure paths.