Skip to content

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.

Start in an empty folder:

Terminal window
git init
builder init

builder init writes project-builder.json and a schematics/ folder, and installs @pbuilder/sdk as a dev dependency, creating a package.json first.

Terminal window
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:

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:

Terminal window
npx pbuilder-codegen schematics/service
pbuilder-codegen: wrote schema.generated.ts

The 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.

The factory creates one file. Both its path and its content are templates, rendered at run time with the options you pass:

schematics/service/factory.ts
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 },
});
};

runFactoryForTest runs the factory in memory, with no CLI and no files written. Save this as schematics/service/factory.test.ts:

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 =};");
});
Terminal window
bun test
1 pass
0 fail
3 expect() calls

The 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.

--dry-run prints the plan and writes nothing:

Terminal window
builder execute --output=pretty --dry-run default:service --name=orderHistory
dry run: nothing is written to the workspace
create (1)
create src/services/order-history.ts
✓ done — 1 create

The path is already rendered, and src/ does not exist yet. The port input was not passed, so the plan uses its default, 3000.

Terminal window
builder execute --output=pretty default:service --name=orderHistory --port=4000
create (1)
create src/services/order-history.ts
✓ done — 1 create
src/services/order-history.ts
export 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.

A generator makes code once. An evaluation schematic keeps checking it afterwards. Scaffold one:

Terminal window
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:

project-builder.json
"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:

schematics/check-service/factory.ts
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:

Terminal window
builder validate --output=pretty
dry run: nothing is written to the workspace
✓ done — no changes
passed: 0 finding(s), none with the severity error

The 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_findings

The 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.

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:

Terminal window
npm ci
bun test
builder validate

It 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.

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.