Buckets:
| // Copyright (c) 2026 the Zuke contributors | |
| // SPDX-License-Identifier: MIT | |
| /** | |
| * Renders the build's dependency graph as a committed Markdown page | |
| * (`docs/graph.md`) whose Mermaid diagram GitHub draws natively — the same | |
| * nodes and edges `./zuke graph` prints as text, kept current by the same | |
| * generate-then-verify pattern as the workflows and the plugin skills: | |
| * `graphDoc` regenerates the page, `graphDocCheck` fails the gate on drift. | |
| * | |
| * Everything except the two file operations is pure: {@link graphDocTargets} | |
| * extracts the rows from the discovered targets and {@link renderGraphDoc} | |
| * renders the whole page as a string, so the output is unit-testable without | |
| * touching disk. | |
| * | |
| * @module | |
| */ | |
| import { FileTasks, type TargetBuilder } from "@zuke/core"; | |
| import { formatText } from "./fmt_text.ts"; | |
| /** Where the generated graph page is committed. */ | |
| export const GRAPH_DOC_PATH = "docs/graph.md"; | |
| /** One target extracted from the discovered build. */ | |
| export interface GraphDocTarget { | |
| /** The target's name (nested fields are dotted, e.g. `workflows.ci`). */ | |
| name: string; | |
| /** The target's description, or `""` if none. */ | |
| description: string; | |
| /** Names of the hard `dependsOn` references, in declaration order. */ | |
| deps: string[]; | |
| } | |
| /** | |
| * Extract the graph rows from discovered targets: one row per target, with | |
| * edges for hard `dependsOn` references between known targets only — the same | |
| * data `zuke graph` prints. Declaration order is preserved so the rendered | |
| * page is deterministic. | |
| */ | |
| export function graphDocTargets( | |
| targets: Map<string, TargetBuilder>, | |
| ): GraphDocTarget[] { | |
| const rows: GraphDocTarget[] = []; | |
| for (const [name, t] of targets) { | |
| const deps: string[] = []; | |
| for (const dep of t.dependsOn_) { | |
| const depName = dep?.name_; | |
| if (depName !== undefined && targets.has(depName)) deps.push(depName); | |
| } | |
| rows.push({ name, description: t.description_ ?? "", deps }); | |
| } | |
| return rows; | |
| } | |
| /** | |
| * Escape a target name for use inside a quoted Mermaid node label. Names are | |
| * TS identifiers (or dotted paths for nested fields), so this is defensive: | |
| * Mermaid reads `"` as the label delimiter and `#…;` as an entity. | |
| */ | |
| function mermaidLabel(name: string): string { | |
| return name.replaceAll("#", "#35;").replaceAll('"', "#quot;"); | |
| } | |
| /** Escape a string for use inside a Markdown table cell. */ | |
| function tableCell(text: string): string { | |
| return text.replaceAll("|", "\\|").replaceAll("\n", " "); | |
| } | |
| /** | |
| * Render the Mermaid `flowchart` for the graph: every target as a node with a | |
| * synthetic id (`t0`, `t1`, … — so a name like `end`, a Mermaid keyword, can | |
| * never break the diagram) and an arrow from each dependency to its dependent. | |
| */ | |
| export function renderMermaid(rows: GraphDocTarget[]): string { | |
| const ids = new Map(rows.map((row, i) => [row.name, `t${i}`])); | |
| const lines = ["flowchart TD"]; | |
| for (const row of rows) { | |
| lines.push(` ${ids.get(row.name)}["${mermaidLabel(row.name)}"]`); | |
| } | |
| for (const row of rows) { | |
| for (const dep of row.deps) { | |
| const from = ids.get(dep); | |
| // A dep naming no row would render a broken `undefined -->` edge; | |
| // graphDocTargets never produces one, but callers of this exported | |
| // function could. | |
| if (from !== undefined) lines.push(` ${from} --> ${ids.get(row.name)}`); | |
| } | |
| } | |
| return lines.join("\n"); | |
| } | |
| /** | |
| * Render the whole committed page: title, provenance note, the Mermaid | |
| * diagram, and a target reference table. Pure — the input fully determines | |
| * the output, so the drift check is a string comparison. | |
| */ | |
| export function renderGraphDoc(targets: Map<string, TargetBuilder>): string { | |
| const rows = graphDocTargets(targets); | |
| const edgeCount = rows.reduce((n, row) => n + row.deps.length, 0); | |
| const header = [ | |
| "# Zuke's build graph", | |
| "", | |
| "<!-- Generated by `./zuke graphDoc` — do not edit by hand. -->", | |
| "", | |
| "The dependency graph of [`zuke.ts`](../zuke.ts) — Zuke building itself. " + | |
| "An arrow points from a dependency to the target that depends on it, so " + | |
| "a target runs after everything that points at it. This is the same " + | |
| "graph `./zuke graph` prints as text and `./zuke graph --output=html` " + | |
| "renders interactively.", | |
| "", | |
| `${rows.length} target(s), ${edgeCount} dependency edge(s). Regenerate ` + | |
| "with `./zuke graphDoc`; the `graphDocCheck` target in the CI gate " + | |
| "fails when this page drifts from the build.", | |
| "", | |
| ]; | |
| if (rows.length === 0) { | |
| return [...header, "No targets defined.", ""].join("\n"); | |
| } | |
| const table = [ | |
| "## Targets", | |
| "", | |
| "| Target | Description | Depends on |", | |
| "| --- | --- | --- |", | |
| ...rows.map((row) => { | |
| const deps = row.deps.length === 0 | |
| ? "—" | |
| : row.deps.map((dep) => `\`${dep}\``).join(", "); | |
| return `| \`${row.name}\` | ${tableCell(row.description)} | ${ | |
| tableCell(deps) | |
| } |`; | |
| }), | |
| ]; | |
| return [ | |
| ...header, | |
| "```mermaid", | |
| renderMermaid(rows), | |
| "```", | |
| "", | |
| ...table, | |
| "", | |
| ].join("\n"); | |
| } | |
| /** | |
| * The exact bytes the page should hold: {@link renderGraphDoc}'s output run | |
| * through `deno fmt`. | |
| * | |
| * The page lives under `docs/`, which the formatter covers, so the rendered | |
| * text is not the committed text: `deno fmt` rewraps the prose paragraphs to | |
| * the margin and pads the target table's columns into alignment. Comparing | |
| * against the raw render would make `format` and `graphDocCheck` contradict | |
| * each other — each one's fix breaks the other. Formatting here, at the one | |
| * place that reads and writes the file, keeps {@link renderGraphDoc} pure and | |
| * leaves the formatter the sole authority on layout. | |
| */ | |
| export async function canonicalGraphDoc( | |
| targets: Map<string, TargetBuilder>, | |
| ): Promise<string> { | |
| return await formatText(renderGraphDoc(targets), "md"); | |
| } | |
| /** | |
| * Render and write the graph page. Returns `true` when the file changed (or | |
| * was created), `false` when it was already current — so the target can say | |
| * which happened. | |
| */ | |
| export async function writeGraphDoc( | |
| targets: Map<string, TargetBuilder>, | |
| path: string = GRAPH_DOC_PATH, | |
| ): Promise<boolean> { | |
| const content = await canonicalGraphDoc(targets); | |
| if (await FileTasks.exists(path)) { | |
| if (await FileTasks.readText(path) === content) return false; | |
| } | |
| await FileTasks.writeText(path, content); | |
| return true; | |
| } | |
| /** | |
| * The ways the committed page has drifted from the build: missing, or its | |
| * content no longer matches what {@link canonicalGraphDoc} produces. Empty | |
| * means the page is current. | |
| */ | |
| export async function checkGraphDoc( | |
| targets: Map<string, TargetBuilder>, | |
| path: string = GRAPH_DOC_PATH, | |
| ): Promise<string[]> { | |
| if (!await FileTasks.exists(path)) return [`${path} (missing)`]; | |
| const committed = await FileTasks.readText(path); | |
| if (committed !== await canonicalGraphDoc(targets)) { | |
| return [`${path} (stale)`]; | |
| } | |
| return []; | |
| } | |
Xet Storage Details
- Size:
- 7 kB
- Xet hash:
- 292ed885ae5ebdc0f764f65bbdf18d03386c15a008726f5c313882523582850e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.