@intellectual-frontiers/libraries 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/affected.d.ts +28 -0
- package/dist/affected.js +250 -0
- package/dist/changes.d.ts +27 -0
- package/dist/changes.js +470 -0
- package/dist/check.d.ts +15 -0
- package/dist/check.js +198 -0
- package/dist/generate-ontology.d.ts +37 -0
- package/dist/generate-ontology.js +293 -0
- package/dist/generate-workflows.d.ts +12 -0
- package/dist/generate-workflows.js +102 -0
- package/dist/imports/nist-sp-800-53.import.d.ts +33 -0
- package/dist/imports/nist-sp-800-53.import.js +34 -0
- package/dist/imports/schema-org.import.d.ts +33 -0
- package/dist/imports/schema-org.import.js +31 -0
- package/dist/install-bun.d.ts +1 -0
- package/dist/install-bun.js +58 -0
- package/dist/libraries.d.ts +79 -0
- package/dist/libraries.js +386 -0
- package/dist/mod.d.ts +16 -0
- package/dist/mod.js +16 -0
- package/dist/newer.d.ts +27 -0
- package/dist/newer.js +147 -0
- package/dist/oscal-register.d.ts +65 -0
- package/dist/oscal-register.js +344 -0
- package/dist/pack-library.d.ts +47 -0
- package/dist/pack-library.js +498 -0
- package/dist/pins.d.ts +15 -0
- package/dist/pins.js +29 -0
- package/dist/publish-ontology.d.ts +160 -0
- package/dist/publish-ontology.js +227 -0
- package/dist/versions.d.ts +26 -0
- package/dist/versions.js +556 -0
- package/dist/workflows/definitions.d.ts +65 -0
- package/dist/workflows/definitions.js +619 -0
- package/dist/workflows/mod.d.ts +10 -0
- package/dist/workflows/mod.js +10 -0
- package/dist/workflows/model.d.ts +143 -0
- package/dist/workflows/model.js +81 -0
- package/dist/workflows/render.d.ts +9 -0
- package/dist/workflows/render.js +179 -0
- package/package.json +129 -4
- package/src/affected.ts +265 -0
- package/src/changes.ts +546 -0
- package/src/check.ts +223 -0
- package/src/generate-ontology.ts +350 -0
- package/src/generate-workflows.ts +103 -0
- package/src/imports/nist-sp-800-53.import.ts +36 -0
- package/src/imports/schema-org.import.ts +31 -0
- package/src/install-bun.ts +53 -0
- package/src/libraries.ts +385 -0
- package/src/mod.ts +16 -0
- package/src/newer.ts +154 -0
- package/src/oscal-register.ts +443 -0
- package/src/pack-library.ts +522 -0
- package/src/pins.ts +30 -0
- package/src/publish-ontology.ts +354 -0
- package/src/versions.ts +594 -0
- package/src/workflows/definitions.ts +855 -0
- package/src/workflows/mod.ts +30 -0
- package/src/workflows/model.ts +187 -0
- package/src/workflows/render.ts +176 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed model of GitHub Actions workflows and the writer of their files (commons 0030-generated-workflows): the values a repository writes its workflows as, and the YAML they are made into. A repository
|
|
3
|
+
* that imports this writes its own workflows as TypeScript and makes `.github/workflows/<name>.auto.yml` from them, as this repository does with `commons/libraries/src/generate-workflows.ts`.
|
|
4
|
+
*
|
|
5
|
+
* Spec: 0001-libraries FR-004
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
type CompositeAction,
|
|
11
|
+
expr,
|
|
12
|
+
type Expr,
|
|
13
|
+
type Filter,
|
|
14
|
+
job,
|
|
15
|
+
type Job,
|
|
16
|
+
type Permissions,
|
|
17
|
+
type Runner,
|
|
18
|
+
run,
|
|
19
|
+
type RunStep,
|
|
20
|
+
type Scalar,
|
|
21
|
+
type Step,
|
|
22
|
+
type Strategy,
|
|
23
|
+
type Triggers,
|
|
24
|
+
use,
|
|
25
|
+
type UseStep,
|
|
26
|
+
validateAction,
|
|
27
|
+
validateWorkflow,
|
|
28
|
+
type Workflow,
|
|
29
|
+
} from "./model.ts";
|
|
30
|
+
export { GENERATOR, renderAction, renderWorkflow } from "./render.ts";
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed model of a GitHub Actions workflow (commons 0030-generated-workflows FR-002, FR-005): the triggers, the jobs, the steps, the strategy, the permissions and the
|
|
3
|
+
* concurrency of a workflow as values the compiler checks, so that a step that is neither `uses` nor `run`, a job with no step, a permission that is neither `read` nor `write` or an
|
|
4
|
+
* expression written as a plain string is a type error, and a mistake the types cannot say (an action with no version, a job that needs a job that is not there) is a finding of `validateWorkflow`.
|
|
5
|
+
* The files GitHub reads are made from these values by `render.ts`; nothing here reads or writes a file.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* An expression is made with `expr("matrix.os")`, which gives `${{ matrix.os }}` and a type of its own, so that a string that is meant to be read by the runner is told from one that is not. A step is a
|
|
9
|
+
* `uses` step or a `run` step (`use(...)` and `run(...)`), a job has its steps as a non-empty tuple, and a workflow has its jobs by id, in the order they are written. The condition of a job or a
|
|
10
|
+
* step (`if`) is the text of an expression without its `${{ }}`, as GitHub reads it.
|
|
11
|
+
*
|
|
12
|
+
* @limits
|
|
13
|
+
* It models what this repository's workflows use: push, pull request, manual and reusable triggers, a job's matrix, permissions, environment, concurrency and the steps of a composite action. It
|
|
14
|
+
* does not model every key GitHub knows; the schema GitHub's tools publish is the check of what is written (`tests/commons-workflows-schema.test.ts`), and a key the model lacks is added here,
|
|
15
|
+
* where the compiler then asks every workflow about it.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { expr, job, run, use, validateWorkflow } from "./model.ts";
|
|
20
|
+
* const w = { file: "x", name: "x", on: { push: {} }, jobs: { test: job({ runsOn: "ubuntu-latest", timeoutMinutes: 5, steps: [use("actions/checkout@v4"), run("bun test")] }) } };
|
|
21
|
+
* validateWorkflow(w); // []
|
|
22
|
+
* expr("matrix.os"); // "${{ matrix.os }}"
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Spec: 0001-libraries FR-004
|
|
26
|
+
*
|
|
27
|
+
* @module
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
declare const expression: unique symbol;
|
|
31
|
+
/** A string the runner reads as an expression: `${{ ... }}`. */
|
|
32
|
+
export type Expr = string & { readonly [expression]: true };
|
|
33
|
+
/** The expression with the text `e` (`expr("matrix.os")` is `${{ matrix.os }}`). */
|
|
34
|
+
export const expr = (e: string): Expr => `\${{ ${e} }}` as Expr;
|
|
35
|
+
|
|
36
|
+
/** The runners the workflows of this repository ask for, or an expression that gives one (`expr("matrix.os")`). */
|
|
37
|
+
export type Runner = "ubuntu-latest" | "ubuntu-24.04" | "macos-latest" | "windows-latest" | Expr;
|
|
38
|
+
|
|
39
|
+
/** A value of a `with` or an `env`: a string, a number, a boolean or an expression. */
|
|
40
|
+
export type Scalar = string | number | boolean;
|
|
41
|
+
|
|
42
|
+
type StepCommon = {
|
|
43
|
+
readonly name?: string;
|
|
44
|
+
readonly id?: string;
|
|
45
|
+
/** The text of the condition, without `${{ }}`. */
|
|
46
|
+
readonly when?: string;
|
|
47
|
+
readonly env?: Readonly<Record<string, Scalar>>;
|
|
48
|
+
readonly workingDirectory?: string;
|
|
49
|
+
readonly timeoutMinutes?: number;
|
|
50
|
+
/** A comment written above the step. */
|
|
51
|
+
readonly comment?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** A step that uses an action: `owner/name@version`, or `./path` for one of this repository's. */
|
|
55
|
+
export type UseStep = StepCommon & {
|
|
56
|
+
readonly kind: "uses";
|
|
57
|
+
readonly uses: string;
|
|
58
|
+
readonly with?: Readonly<Record<string, Scalar>>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** A step that runs a script. */
|
|
62
|
+
export type RunStep = StepCommon & {
|
|
63
|
+
readonly kind: "run";
|
|
64
|
+
readonly run: string;
|
|
65
|
+
readonly shell?: "bash" | "pwsh" | "sh";
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type Step = UseStep | RunStep;
|
|
69
|
+
|
|
70
|
+
/** A step that uses an action. */
|
|
71
|
+
export const use = (uses: string, o: Omit<UseStep, "kind" | "uses"> = {}): UseStep => ({ kind: "uses", uses, ...o });
|
|
72
|
+
/** A step that runs a script; a script of more than one line ends with a newline. */
|
|
73
|
+
export const run = (script: string, o: Omit<RunStep, "kind" | "run"> = {}): RunStep => ({ kind: "run", run: script, ...o });
|
|
74
|
+
|
|
75
|
+
/** The permissions of the token: each scope `read` or `write`. */
|
|
76
|
+
export type Permissions = Readonly<
|
|
77
|
+
Partial<
|
|
78
|
+
Record<
|
|
79
|
+
"contents" | "pages" | "id-token" | "actions" | "packages" | "pull-requests" | "issues" | "checks" | "statuses",
|
|
80
|
+
"read" | "write" | "none"
|
|
81
|
+
>
|
|
82
|
+
>
|
|
83
|
+
>;
|
|
84
|
+
|
|
85
|
+
/** The branches, paths and tags a push or a pull request is run for; none is every one. */
|
|
86
|
+
export type Filter = { readonly branches?: readonly string[]; readonly paths?: readonly string[]; readonly tags?: readonly string[] };
|
|
87
|
+
|
|
88
|
+
export type Triggers = {
|
|
89
|
+
readonly push?: Filter;
|
|
90
|
+
readonly pullRequest?: Filter;
|
|
91
|
+
/** Run by hand from the Actions page. */
|
|
92
|
+
readonly workflowDispatch?: true;
|
|
93
|
+
/** Called by another workflow. */
|
|
94
|
+
readonly workflowCall?: true;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type Strategy = {
|
|
98
|
+
readonly failFast?: boolean;
|
|
99
|
+
readonly matrix: Readonly<Record<string, readonly (string | number)[]>>;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export type Job = {
|
|
103
|
+
readonly name?: string;
|
|
104
|
+
readonly runsOn: Runner;
|
|
105
|
+
readonly timeoutMinutes: number;
|
|
106
|
+
readonly needs?: readonly string[];
|
|
107
|
+
/** The text of the condition, without `${{ }}`. */
|
|
108
|
+
readonly when?: string;
|
|
109
|
+
readonly permissions?: Permissions;
|
|
110
|
+
readonly environment?: { readonly name: string; readonly url?: string };
|
|
111
|
+
readonly env?: Readonly<Record<string, Scalar>>;
|
|
112
|
+
readonly strategy?: Strategy;
|
|
113
|
+
readonly steps: readonly [Step, ...Step[]];
|
|
114
|
+
/** A comment written above the job. */
|
|
115
|
+
readonly comment?: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** A job, written as a function so that its steps are read as a non-empty tuple. */
|
|
119
|
+
export const job = (j: Job): Job => j;
|
|
120
|
+
|
|
121
|
+
export type Workflow = {
|
|
122
|
+
/** The file's name without its directory and extension: `data` is `.github/workflows/data.auto.yml`. */
|
|
123
|
+
readonly file: string;
|
|
124
|
+
/** The workflow's name, which a check's name begins with. */
|
|
125
|
+
readonly name: string;
|
|
126
|
+
/** A comment written under the name. */
|
|
127
|
+
readonly comment?: string;
|
|
128
|
+
readonly on: Triggers;
|
|
129
|
+
readonly permissions?: Permissions;
|
|
130
|
+
readonly env?: Readonly<Record<string, Scalar>>;
|
|
131
|
+
readonly concurrency?: { readonly group: string; readonly cancelInProgress: boolean | Expr };
|
|
132
|
+
/** The jobs by id, in the order they are written. */
|
|
133
|
+
readonly jobs: Readonly<Record<string, Job>>;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** A composite action, the file `action.yml` of a folder under `.github/actions/` (its name is fixed by GitHub, so it cannot carry `.auto.`). */
|
|
137
|
+
export type CompositeAction = {
|
|
138
|
+
/** The folder of the action under `.github/actions/`. */
|
|
139
|
+
readonly folder: string;
|
|
140
|
+
readonly name: string;
|
|
141
|
+
readonly description: string;
|
|
142
|
+
readonly inputs: Readonly<Record<string, { readonly description: string; readonly default?: string }>>;
|
|
143
|
+
readonly steps: readonly [Step, ...Step[]];
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** An action's reference is `owner/name@version` or `./path`; a version is a tag (`v4`, `v1.2.3`) or a full commit (FR-005). */
|
|
147
|
+
const REFERENCE = /^(?:\.\/[\w./-]+|[\w.-]+\/[\w./-]+@(?:v?\d[\w.-]*|[0-9a-f]{40}))$/;
|
|
148
|
+
|
|
149
|
+
/** What is wrong with a step, one sentence each: an action with no version, an empty script, a working directory on a step that uses an action. */
|
|
150
|
+
export function stepFindings(s: Step, where: string): readonly string[] {
|
|
151
|
+
const found: string[] = [];
|
|
152
|
+
if (s.kind === "uses") {
|
|
153
|
+
if (!REFERENCE.test(s.uses))
|
|
154
|
+
found.push(
|
|
155
|
+
`${where}: the action ${JSON.stringify(s.uses)} is not ./path or owner/name@version, a version of a tag or a commit (FR-005)`,
|
|
156
|
+
);
|
|
157
|
+
if (s.workingDirectory !== undefined) found.push(`${where}: a step that uses an action has no working directory`);
|
|
158
|
+
} else if (s.run.trim() === "") found.push(`${where}: the script is empty`);
|
|
159
|
+
return found;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** What is wrong with a workflow, one sentence each: no trigger, a job that needs a job that is not there or itself, a step that is wrong, and a name that is not a file's. */
|
|
163
|
+
export function validateWorkflow(w: Workflow): readonly string[] {
|
|
164
|
+
const found: string[] = [];
|
|
165
|
+
if (!/^[a-z][a-z0-9-]*$/.test(w.file)) found.push(`${w.file}: the file's name is lower-case letters, digits and hyphens`);
|
|
166
|
+
if (Object.keys(w.on).length === 0) found.push(`${w.file}: no trigger`);
|
|
167
|
+
const ids = Object.keys(w.jobs);
|
|
168
|
+
if (ids.length === 0) found.push(`${w.file}: no job`);
|
|
169
|
+
for (const [id, j] of Object.entries(w.jobs)) {
|
|
170
|
+
for (const n of j.needs ?? []) {
|
|
171
|
+
if (n === id) found.push(`${w.file}: job ${id} needs itself`);
|
|
172
|
+
else if (!ids.includes(n)) found.push(`${w.file}: job ${id} needs ${n}, which is no job`);
|
|
173
|
+
}
|
|
174
|
+
j.steps.forEach((s, i) => found.push(...stepFindings(s, `${w.file}: job ${id}, step ${i + 1}`)));
|
|
175
|
+
}
|
|
176
|
+
return found;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** What is wrong with a composite action, as `validateWorkflow` says of a workflow. */
|
|
180
|
+
export function validateAction(a: CompositeAction): readonly string[] {
|
|
181
|
+
return a.steps.flatMap((s, i) => [
|
|
182
|
+
...stepFindings(s, `.github/actions/${a.folder}: step ${i + 1}`),
|
|
183
|
+
...(s.kind === "run" && s.shell === undefined
|
|
184
|
+
? [`.github/actions/${a.folder}: step ${i + 1} runs a script and names no shell, which a composite action asks`]
|
|
185
|
+
: []),
|
|
186
|
+
]);
|
|
187
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The files GitHub reads, made from the typed model (commons 0030-generated-workflows FR-001, FR-004): `renderWorkflow` and `renderAction` give the YAML of a workflow and of a composite action as text, the
|
|
3
|
+
* same bytes for the same values, each beginning with a line that says it is generated and by what.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* The YAML written is the small part of it that GitHub's files need: mappings in the order the model gives, lists, scalars, comments and literal blocks for a script of more than one line. A scalar is written as it is where
|
|
7
|
+
* a YAML reader would read it as the same string, and in double quotes (JSON's) where it would read it as a number, a boolean, a null or a mapping. A script of one line is a scalar; one of several lines is a literal block that
|
|
8
|
+
* keeps its last newline (`|`) or drops it (`|-`) as the script has it.
|
|
9
|
+
*
|
|
10
|
+
* @limits
|
|
11
|
+
* It writes and never reads; the parity of a rendering with a file that was written by hand is a comparison of what a YAML reader makes of the two (`tests/commons-workflows-schema.test.ts`).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { renderWorkflow } from "./render.ts";
|
|
16
|
+
* import { job, run, use } from "./model.ts";
|
|
17
|
+
* renderWorkflow({ file: "x", name: "x", on: { push: {} }, jobs: { t: job({ runsOn: "ubuntu-latest", timeoutMinutes: 5, steps: [use("actions/checkout@v4"), run("bun test")] }) } }).includes("runs-on: ubuntu-latest"); // true
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Spec: 0001-libraries FR-004
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { absurd } from "@intellectual-frontiers/core";
|
|
25
|
+
import type { CompositeAction, Filter, Job, Permissions, Scalar, Step, Workflow } from "./model.ts";
|
|
26
|
+
|
|
27
|
+
/** The command that writes the files, named in the first line of each. */
|
|
28
|
+
export const GENERATOR = "bun commons/libraries/src/generate-workflows.ts";
|
|
29
|
+
|
|
30
|
+
const RESERVED = /^(?:true|false|null|yes|no|on|off|y|n|~)$/i;
|
|
31
|
+
const PLAIN = /^[A-Za-z_./][A-Za-z0-9_ ./@-]*$/;
|
|
32
|
+
|
|
33
|
+
/** A scalar as YAML: plain where it is read back as the same string, otherwise in double quotes. */
|
|
34
|
+
export function scalar(v: Scalar): string {
|
|
35
|
+
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
36
|
+
if (PLAIN.test(v) && !RESERVED.test(v) && !/^[-+]?(?:\d|\.\d)/.test(v) && !v.endsWith(" ") && !v.startsWith(" ")) return v;
|
|
37
|
+
// an expression, which GitHub's own examples write as it is: plain where nothing in the text would end a plain scalar
|
|
38
|
+
if (v.includes("${{") && !v.startsWith(" ") && !v.endsWith(" ") && !/: | #|"|\n|^[[{!&*|>%@`'-]/.test(v)) return v;
|
|
39
|
+
return JSON.stringify(v);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A key as YAML: the names GitHub uses are plain, anything else is quoted. */
|
|
43
|
+
function key(k: string): string {
|
|
44
|
+
return /^[A-Za-z_][A-Za-z0-9_-]*$/.test(k) ? k : JSON.stringify(k);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const pad = (n: number): string => " ".repeat(n);
|
|
48
|
+
|
|
49
|
+
function comment(text: string | undefined, n: number): string[] {
|
|
50
|
+
return text === undefined ? [] : text.split("\n").map((l) => (l === "" ? `${pad(n)}#` : `${pad(n)}# ${l}`));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `name: value` at an indent, a literal block where the value is a script of several lines. */
|
|
54
|
+
function entry(k: string, v: Scalar, n: number): string[] {
|
|
55
|
+
if (typeof v === "string" && v.includes("\n")) {
|
|
56
|
+
const keep = v.endsWith("\n") ? "|" : "|-";
|
|
57
|
+
const body = (v.endsWith("\n") ? v.slice(0, -1) : v).split("\n");
|
|
58
|
+
return [`${pad(n)}${key(k)}: ${keep}`, ...body.map((l) => (l === "" ? "" : `${pad(n + 2)}${l}`))];
|
|
59
|
+
}
|
|
60
|
+
return [`${pad(n)}${key(k)}: ${scalar(v)}`];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function mapping(k: string, m: Readonly<Record<string, Scalar>> | undefined, n: number): string[] {
|
|
64
|
+
if (m === undefined || Object.keys(m).length === 0) return [];
|
|
65
|
+
return [`${pad(n)}${key(k)}:`, ...Object.entries(m).flatMap(([a, b]) => entry(a, b, n + 2))];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function list(k: string, items: readonly (string | number)[], n: number): string[] {
|
|
69
|
+
return [`${pad(n)}${key(k)}:`, ...items.map((i) => `${pad(n + 2)}- ${scalar(i)}`)];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function flowList(k: string, items: readonly (string | number)[], n: number): string[] {
|
|
73
|
+
return [`${pad(n)}${key(k)}: [${items.map((i) => scalar(i)).join(", ")}]`];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function permissions(p: Permissions | undefined, n: number): string[] {
|
|
77
|
+
return mapping("permissions", p, n);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function filter(k: string, f: Filter | undefined, n: number): string[] {
|
|
81
|
+
if (f === undefined) return [];
|
|
82
|
+
const inner = [
|
|
83
|
+
...(f.branches === undefined ? [] : flowList("branches", f.branches, n + 2)),
|
|
84
|
+
...(f.paths === undefined ? [] : list("paths", f.paths, n + 2)),
|
|
85
|
+
...(f.tags === undefined ? [] : list("tags", f.tags, n + 2)),
|
|
86
|
+
];
|
|
87
|
+
return inner.length === 0 ? [`${pad(n)}${key(k)}:`] : [`${pad(n)}${key(k)}:`, ...inner];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function step(s: Step, n: number): string[] {
|
|
91
|
+
const out: string[] = [...comment(s.comment, n)];
|
|
92
|
+
const props: string[][] = [];
|
|
93
|
+
if (s.name !== undefined) props.push(entry("name", s.name, 0));
|
|
94
|
+
if (s.id !== undefined) props.push(entry("id", s.id, 0));
|
|
95
|
+
if (s.when !== undefined) props.push(entry("if", s.when, 0));
|
|
96
|
+
switch (s.kind) {
|
|
97
|
+
case "uses":
|
|
98
|
+
props.push(entry("uses", s.uses, 0));
|
|
99
|
+
if (s.with !== undefined) props.push(mapping("with", s.with, 0));
|
|
100
|
+
break;
|
|
101
|
+
case "run":
|
|
102
|
+
if (s.shell !== undefined) props.push(entry("shell", s.shell, 0));
|
|
103
|
+
props.push(entry("run", s.run, 0));
|
|
104
|
+
break;
|
|
105
|
+
default:
|
|
106
|
+
return absurd(s);
|
|
107
|
+
}
|
|
108
|
+
if (s.env !== undefined) props.push(mapping("env", s.env, 0));
|
|
109
|
+
if (s.workingDirectory !== undefined) props.push(entry("working-directory", s.workingDirectory, 0));
|
|
110
|
+
if (s.timeoutMinutes !== undefined) props.push(entry("timeout-minutes", s.timeoutMinutes, 0));
|
|
111
|
+
const lines = props.flat();
|
|
112
|
+
lines.forEach((l, i) => out.push(i === 0 ? `${pad(n)}- ${l}` : l === "" ? "" : `${pad(n + 2)}${l}`));
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function jobLines(id: string, j: Job): string[] {
|
|
117
|
+
const out: string[] = [...comment(j.comment, 2), ` ${key(id)}:`];
|
|
118
|
+
if (j.name !== undefined) out.push(...entry("name", j.name, 4));
|
|
119
|
+
out.push(...entry("runs-on", j.runsOn, 4));
|
|
120
|
+
out.push(...entry("timeout-minutes", j.timeoutMinutes, 4));
|
|
121
|
+
if (j.needs !== undefined) out.push(...flowList("needs", j.needs, 4));
|
|
122
|
+
if (j.when !== undefined) out.push(...entry("if", j.when, 4));
|
|
123
|
+
out.push(...permissions(j.permissions, 4));
|
|
124
|
+
if (j.environment !== undefined) {
|
|
125
|
+
out.push(" environment:", ...entry("name", j.environment.name, 6));
|
|
126
|
+
if (j.environment.url !== undefined) out.push(...entry("url", j.environment.url, 6));
|
|
127
|
+
}
|
|
128
|
+
out.push(...mapping("env", j.env, 4));
|
|
129
|
+
if (j.strategy !== undefined) {
|
|
130
|
+
out.push(" strategy:");
|
|
131
|
+
if (j.strategy.failFast !== undefined) out.push(...entry("fail-fast", j.strategy.failFast, 6));
|
|
132
|
+
out.push(" matrix:", ...Object.entries(j.strategy.matrix).flatMap(([k, v]) => flowList(k, v, 8)));
|
|
133
|
+
}
|
|
134
|
+
out.push(" steps:", ...j.steps.flatMap((s) => step(s, 6)));
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The text of a workflow's file. */
|
|
139
|
+
export function renderWorkflow(w: Workflow): string {
|
|
140
|
+
const out: string[] = [`# Generated by ${GENERATOR}; do not edit (commons 0030-generated-workflows).`, `name: ${scalar(w.name)}`, ""];
|
|
141
|
+
if (w.comment !== undefined) out.push(...comment(w.comment, 0));
|
|
142
|
+
out.push("on:");
|
|
143
|
+
out.push(...filter("push", w.on.push, 2), ...filter("pull_request", w.on.pullRequest, 2));
|
|
144
|
+
if (w.on.workflowDispatch === true) out.push(" workflow_dispatch:");
|
|
145
|
+
if (w.on.workflowCall === true) out.push(" workflow_call:");
|
|
146
|
+
out.push("");
|
|
147
|
+
if (w.permissions !== undefined) out.push(...permissions(w.permissions, 0), "");
|
|
148
|
+
if (w.env !== undefined) out.push(...mapping("env", w.env, 0), "");
|
|
149
|
+
if (w.concurrency !== undefined) {
|
|
150
|
+
out.push(
|
|
151
|
+
"concurrency:",
|
|
152
|
+
...entry("group", w.concurrency.group, 2),
|
|
153
|
+
...entry("cancel-in-progress", w.concurrency.cancelInProgress, 2),
|
|
154
|
+
"",
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
out.push("jobs:");
|
|
158
|
+
Object.entries(w.jobs).forEach(([id, j], i) => {
|
|
159
|
+
if (i > 0) out.push("");
|
|
160
|
+
out.push(...jobLines(id, j));
|
|
161
|
+
});
|
|
162
|
+
return `${out.join("\n")}\n`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The text of a composite action's `action.yml`. */
|
|
166
|
+
export function renderAction(a: CompositeAction): string {
|
|
167
|
+
const out: string[] = [`# Generated by ${GENERATOR}; do not edit (commons 0030-generated-workflows FR-008).`, `name: ${scalar(a.name)}`];
|
|
168
|
+
out.push(...entry("description", a.description, 0));
|
|
169
|
+
out.push("inputs:");
|
|
170
|
+
for (const [k, v] of Object.entries(a.inputs)) {
|
|
171
|
+
out.push(` ${key(k)}:`, ...entry("description", v.description, 4));
|
|
172
|
+
if (v.default !== undefined) out.push(...entry("default", v.default, 4));
|
|
173
|
+
}
|
|
174
|
+
out.push("runs:", " using: composite", " steps:", ...a.steps.flatMap((s) => step(s, 4)));
|
|
175
|
+
return `${out.join("\n")}\n`;
|
|
176
|
+
}
|