@hydranium/cli 1.0.0-next.19 → 1.0.0-next.192
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/README.md +14 -0
- package/bin/hydranium-cli.js +13 -0
- package/lib/cli-version.js.map +1 -1
- package/lib/cli.js +0 -0
- package/lib/commands/analyze-heap-args.js.map +1 -1
- package/lib/commands/analyze-heap.d.ts +3 -0
- package/lib/commands/analyze-heap.d.ts.map +1 -1
- package/lib/commands/analyze-heap.js +2 -1
- package/lib/commands/analyze-heap.js.map +1 -1
- package/lib/commands/ast-ground-truth-args.js +5 -5
- package/lib/commands/ast-ground-truth-args.js.map +1 -1
- package/lib/commands/ast-ground-truth.js +1 -1
- package/lib/commands/ast-ground-truth.js.map +1 -1
- package/lib/commands/generate-ast-builder.d.ts +49 -0
- package/lib/commands/generate-ast-builder.d.ts.map +1 -0
- package/lib/commands/generate-ast-builder.js +200 -0
- package/lib/commands/generate-ast-builder.js.map +1 -0
- package/lib/commands/generate-transfer-model-args.d.ts.map +1 -1
- package/lib/commands/generate-transfer-model-args.js +11 -1
- package/lib/commands/generate-transfer-model-args.js.map +1 -1
- package/lib/commands/generate-transfer-model-config.d.ts.map +1 -1
- package/lib/commands/generate-transfer-model-config.js +3 -1
- package/lib/commands/generate-transfer-model-config.js.map +1 -1
- package/lib/commands/generate-transfer-model.d.ts +9 -0
- package/lib/commands/generate-transfer-model.d.ts.map +1 -1
- package/lib/commands/generate-transfer-model.js +16 -3
- package/lib/commands/generate-transfer-model.js.map +1 -1
- package/lib/commands/harness-args.d.ts +19 -35
- package/lib/commands/harness-args.d.ts.map +1 -1
- package/lib/commands/harness-args.js +38 -16
- package/lib/commands/harness-args.js.map +1 -1
- package/lib/commands/headless-harness.d.ts +32 -18
- package/lib/commands/headless-harness.d.ts.map +1 -1
- package/lib/commands/headless-harness.js +19 -11
- package/lib/commands/headless-harness.js.map +1 -1
- package/lib/commands/init-args.js.map +1 -1
- package/lib/commands/init-prompt.d.ts.map +1 -1
- package/lib/commands/init-templates.d.ts +0 -9
- package/lib/commands/init-templates.d.ts.map +1 -1
- package/lib/commands/init-templates.js +78 -55
- package/lib/commands/init-templates.js.map +1 -1
- package/lib/commands/init-wizard.js.map +1 -1
- package/lib/commands/init-workspace.d.ts +7 -3
- package/lib/commands/init-workspace.d.ts.map +1 -1
- package/lib/commands/init-workspace.js +11 -6
- package/lib/commands/init-workspace.js.map +1 -1
- package/lib/commands/init.d.ts +4 -2
- package/lib/commands/init.d.ts.map +1 -1
- package/lib/commands/init.js +7 -10
- package/lib/commands/init.js.map +1 -1
- package/lib/commands/lint-grammar-args.js +5 -5
- package/lib/commands/lint-grammar-args.js.map +1 -1
- package/lib/commands/lint-grammar-report.js.map +1 -1
- package/lib/commands/lint-grammar.js +1 -1
- package/lib/commands/lint-grammar.js.map +1 -1
- package/lib/commands/measure-memory-args.d.ts.map +1 -1
- package/lib/commands/measure-memory-args.js +11 -4
- package/lib/commands/measure-memory-args.js.map +1 -1
- package/lib/commands/measure-memory-driver.js +3 -2
- package/lib/commands/measure-memory-driver.js.map +1 -1
- package/lib/commands/measure-memory.js +1 -1
- package/lib/commands/measure-memory.js.map +1 -1
- package/lib/commands/model-docs-args.js +5 -5
- package/lib/commands/model-docs-args.js.map +1 -1
- package/lib/commands/model-docs.js +1 -1
- package/lib/commands/model-docs.js.map +1 -1
- package/lib/commands/projects-args.d.ts.map +1 -1
- package/lib/commands/projects-args.js +5 -5
- package/lib/commands/projects-args.js.map +1 -1
- package/lib/commands/projects.d.ts +3 -4
- package/lib/commands/projects.d.ts.map +1 -1
- package/lib/commands/projects.js +2 -2
- package/lib/commands/projects.js.map +1 -1
- package/lib/commands/query-args.d.ts.map +1 -1
- package/lib/commands/query-args.js +5 -5
- package/lib/commands/query-args.js.map +1 -1
- package/lib/commands/query.d.ts +3 -4
- package/lib/commands/query.d.ts.map +1 -1
- package/lib/commands/query.js +2 -2
- package/lib/commands/query.js.map +1 -1
- package/lib/commands/reflect-args.js +5 -5
- package/lib/commands/reflect-args.js.map +1 -1
- package/lib/commands/reflect-report.js.map +1 -1
- package/lib/commands/reflect.js +1 -1
- package/lib/commands/reflect.js.map +1 -1
- package/lib/commands/save-args.d.ts.map +1 -1
- package/lib/commands/save-args.js +6 -4
- package/lib/commands/save-args.js.map +1 -1
- package/lib/commands/save.d.ts +8 -4
- package/lib/commands/save.d.ts.map +1 -1
- package/lib/commands/save.js +31 -6
- package/lib/commands/save.js.map +1 -1
- package/lib/commands/validate-args.js +5 -5
- package/lib/commands/validate-args.js.map +1 -1
- package/lib/commands/validate-report.js.map +1 -1
- package/lib/commands/validate.js +1 -1
- package/lib/commands/validate.js.map +1 -1
- package/lib/commands/watch-args.d.ts.map +1 -1
- package/lib/commands/watch-args.js +6 -4
- package/lib/commands/watch-args.js.map +1 -1
- package/lib/commands/watch.d.ts +3 -4
- package/lib/commands/watch.d.ts.map +1 -1
- package/lib/commands/watch.js +20 -2
- package/lib/commands/watch.js.map +1 -1
- package/lib/driver-heap.d.ts +52 -0
- package/lib/driver-heap.d.ts.map +1 -0
- package/lib/driver-heap.js +51 -0
- package/lib/driver-heap.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/log-level.d.ts +21 -11
- package/lib/log-level.d.ts.map +1 -1
- package/lib/log-level.js +25 -14
- package/lib/log-level.js.map +1 -1
- package/package.json +35 -34
- package/src/commands/analyze-heap.ts +4 -1
- package/src/commands/ast-ground-truth-args.ts +7 -7
- package/src/commands/ast-ground-truth.ts +1 -1
- package/src/commands/generate-ast-builder.ts +246 -0
- package/src/commands/generate-transfer-model-args.ts +11 -1
- package/src/commands/generate-transfer-model-config.ts +3 -1
- package/src/commands/generate-transfer-model.ts +25 -3
- package/src/commands/harness-args.ts +42 -19
- package/src/commands/headless-harness.ts +33 -20
- package/src/commands/init-templates.ts +81 -56
- package/src/commands/init-workspace.ts +17 -7
- package/src/commands/init.ts +12 -13
- package/src/commands/lint-grammar-args.ts +7 -7
- package/src/commands/lint-grammar.ts +1 -1
- package/src/commands/measure-memory-args.ts +14 -6
- package/src/commands/measure-memory-driver.ts +3 -2
- package/src/commands/measure-memory.ts +1 -1
- package/src/commands/model-docs-args.ts +7 -7
- package/src/commands/model-docs.ts +1 -1
- package/src/commands/projects-args.ts +5 -4
- package/src/commands/projects.ts +4 -6
- package/src/commands/query-args.ts +5 -4
- package/src/commands/query.ts +4 -6
- package/src/commands/reflect-args.ts +7 -7
- package/src/commands/reflect.ts +1 -1
- package/src/commands/save-args.ts +6 -3
- package/src/commands/save.ts +49 -10
- package/src/commands/validate-args.ts +7 -7
- package/src/commands/validate.ts +1 -1
- package/src/commands/watch-args.ts +6 -3
- package/src/commands/watch.ts +28 -7
- package/src/driver-heap.ts +65 -0
- package/src/index.ts +1 -0
- package/src/log-level.ts +42 -14
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
import * as fs from 'fs';
|
|
11
|
+
import * as path from 'path';
|
|
12
|
+
import type { SourceFile } from 'ts-morph';
|
|
13
|
+
|
|
14
|
+
/** Inputs for {@link emitAstBuilder}, resolved by the transfer-model command. */
|
|
15
|
+
export interface EmitAstBuilderOptions {
|
|
16
|
+
/** Destination path for the generated builder module. */
|
|
17
|
+
outFile: string;
|
|
18
|
+
/** Path to the Langium-generated AST file the builder imports from. */
|
|
19
|
+
astFile: string;
|
|
20
|
+
/** Language id derived from the `<LanguageId>AstReflection` class name. */
|
|
21
|
+
languageId: string;
|
|
22
|
+
/** Header line naming the command that regenerates the file. */
|
|
23
|
+
regenCommand: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A single `makeAstNodeBuilder` binding the generator emits.
|
|
28
|
+
*
|
|
29
|
+
* `typeMap` is the type expression handed to the generic — the whole-project
|
|
30
|
+
* alias for the merged binding, `<Namespace>.AstType` for a narrowed one.
|
|
31
|
+
*/
|
|
32
|
+
interface BuilderBinding {
|
|
33
|
+
exportName: string;
|
|
34
|
+
typeMap: string;
|
|
35
|
+
/** Grammar this binding is narrowed to; absent on the merged binding. */
|
|
36
|
+
namespace?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Langium declares this type alias inside each language's namespace in a
|
|
41
|
+
* multi-language project. Its presence is what marks a namespace as a grammar's
|
|
42
|
+
* type registry rather than an incidental one.
|
|
43
|
+
*/
|
|
44
|
+
const NAMESPACED_AST_TYPE_NAME = 'AstType';
|
|
45
|
+
|
|
46
|
+
/** Export name of the merged, whole-project binding. */
|
|
47
|
+
const MERGED_EXPORT_NAME = 'astNode';
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Emit a module binding {@link makeAstNodeBuilder} to a project's reflection,
|
|
51
|
+
* once per grammar plus once across all of them.
|
|
52
|
+
*
|
|
53
|
+
* **The binding is boilerplate with exactly one degree of freedom** — the type
|
|
54
|
+
* map — so hand-writing it buys nothing and goes stale the moment a grammar is
|
|
55
|
+
* added. Generating it is also the only way a narrowed binding can exist at all:
|
|
56
|
+
* the per-grammar type maps live in namespaces Langium emits only on its
|
|
57
|
+
* multi-language path, so a single-grammar project gets the merged binding alone
|
|
58
|
+
* and there is nothing to narrow.
|
|
59
|
+
*
|
|
60
|
+
* **A narrowed binding is only as narrow as its grammar's import closure.**
|
|
61
|
+
* Langium's per-grammar `AstType` lists everything REACHABLE from that grammar,
|
|
62
|
+
* so where one grammar imports all the others its map equals the project's and
|
|
63
|
+
* its binding checks nothing the merged one does not. The emitted doc says so at
|
|
64
|
+
* each declaration rather than promising a guarantee the type map cannot give.
|
|
65
|
+
*
|
|
66
|
+
* Answers `false` when the AST source declares no whole-project type alias,
|
|
67
|
+
* which means the file did not come from `langium generate` and the caller
|
|
68
|
+
* should report rather than write a module that cannot compile.
|
|
69
|
+
*/
|
|
70
|
+
export function emitAstBuilder(astSource: SourceFile, options: EmitAstBuilderOptions): boolean {
|
|
71
|
+
const content = buildAstBuilderSource(astSource, options);
|
|
72
|
+
if (content === undefined) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fs.mkdirSync(path.dirname(options.outFile), { recursive: true });
|
|
77
|
+
if (fs.existsSync(options.outFile) && fs.readFileSync(options.outFile, 'utf-8') === content) {
|
|
78
|
+
console.log('AST builder is up to date.');
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
fs.writeFileSync(options.outFile, content, 'utf-8');
|
|
82
|
+
console.log(`Generated: ${options.outFile}`);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The module text {@link emitAstBuilder} would write, or `undefined` when the
|
|
88
|
+
* AST source declares no whole-project type alias. Pure over its inputs, so the
|
|
89
|
+
* emitted shape is testable without touching a filesystem.
|
|
90
|
+
*/
|
|
91
|
+
export function buildAstBuilderSource(astSource: SourceFile, options: EmitAstBuilderOptions): string | undefined {
|
|
92
|
+
const mergedTypeMap = `${options.languageId}AstType`;
|
|
93
|
+
if (!astSource.getTypeAlias(mergedTypeMap)) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const bindings: BuilderBinding[] = [{ exportName: MERGED_EXPORT_NAME, typeMap: mergedTypeMap }];
|
|
98
|
+
for (const namespace of findGrammarNamespaces(astSource)) {
|
|
99
|
+
bindings.push({
|
|
100
|
+
exportName: builderExportName(namespace),
|
|
101
|
+
typeMap: `${namespace}.${NAMESPACED_AST_TYPE_NAME}`,
|
|
102
|
+
namespace
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
assertDistinctExportNames(bindings);
|
|
106
|
+
|
|
107
|
+
return renderAstBuilder(bindings, {
|
|
108
|
+
reflectionName: findReflectionConstName(astSource, options.languageId),
|
|
109
|
+
importPath: relativeImportPath(options.outFile, options.astFile),
|
|
110
|
+
regenCommand: options.regenCommand
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Namespaces that carry a grammar's type registry, in declaration order.
|
|
116
|
+
*
|
|
117
|
+
* Keyed on the nested `AstType` alias rather than on the namespace being
|
|
118
|
+
* exported, because an adopter's augmentation can add namespaces to the same
|
|
119
|
+
* file for unrelated reasons and only the ones Langium emits per grammar have
|
|
120
|
+
* a type map to bind.
|
|
121
|
+
*/
|
|
122
|
+
function findGrammarNamespaces(astSource: SourceFile): string[] {
|
|
123
|
+
const names: string[] = [];
|
|
124
|
+
for (const module of astSource.getModules()) {
|
|
125
|
+
const name = module.getName();
|
|
126
|
+
if (name && module.getTypeAlias(NAMESPACED_AST_TYPE_NAME)) {
|
|
127
|
+
names.push(name);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return names;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* `Domain` → `domainNode`. Lower-cases the leading character only, so a grammar
|
|
135
|
+
* named `OrderFlow` reads as `orderFlowNode` rather than losing its word break.
|
|
136
|
+
*/
|
|
137
|
+
function builderExportName(namespace: string): string {
|
|
138
|
+
return `${namespace.charAt(0).toLowerCase()}${namespace.slice(1)}Node`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Two grammars whose names differ only in their leading character collapse to
|
|
143
|
+
* one export. Emitting anyway would silently drop a binding, so fail with both
|
|
144
|
+
* names rather than leave the caller to discover it at compile time.
|
|
145
|
+
*/
|
|
146
|
+
function assertDistinctExportNames(bindings: readonly BuilderBinding[]): void {
|
|
147
|
+
const seen = new Map<string, string>();
|
|
148
|
+
for (const binding of bindings) {
|
|
149
|
+
const previous = seen.get(binding.exportName);
|
|
150
|
+
if (previous !== undefined) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Cannot emit the AST builder: grammars '${previous}' and '${binding.namespace}' both map to the export ` +
|
|
153
|
+
`'${binding.exportName}'. Rename one grammar so the generated bindings stay distinct.`
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
seen.set(binding.exportName, binding.namespace ?? MERGED_EXPORT_NAME);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Name of the const holding the project's reflection instance.
|
|
162
|
+
*
|
|
163
|
+
* Langium emits `export const reflection = new <LanguageId>AstReflection()`, but
|
|
164
|
+
* the const is located by its initializer rather than by that name so a renamed
|
|
165
|
+
* export still resolves. Falls back to the emitted default when no declaration
|
|
166
|
+
* matches, which keeps the output compiling against a stock Langium file.
|
|
167
|
+
*/
|
|
168
|
+
function findReflectionConstName(astSource: SourceFile, languageId: string): string {
|
|
169
|
+
const reflectionClass = `${languageId}AstReflection`;
|
|
170
|
+
for (const declaration of astSource.getVariableDeclarations()) {
|
|
171
|
+
if (declaration.getInitializer()?.getText().replace(/\s/g, '') === `new${reflectionClass}()`) {
|
|
172
|
+
return declaration.getName();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return 'reflection';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Module specifier for the AST file as seen from the generated module's own
|
|
180
|
+
* directory. Emitted with a `.js` extension and posix separators because the
|
|
181
|
+
* output is ESM TypeScript, where the specifier names the compiled sibling.
|
|
182
|
+
*/
|
|
183
|
+
function relativeImportPath(outFile: string, astFile: string): string {
|
|
184
|
+
const relative = path.relative(path.dirname(path.resolve(outFile)), path.resolve(astFile));
|
|
185
|
+
const posix = relative.split(path.sep).join('/').replace(/\.ts$/, '.js');
|
|
186
|
+
return posix.startsWith('.') ? posix : `./${posix}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function renderAstBuilder(
|
|
190
|
+
bindings: readonly BuilderBinding[],
|
|
191
|
+
context: { reflectionName: string; importPath: string; regenCommand: string }
|
|
192
|
+
): string {
|
|
193
|
+
const imported = [context.reflectionName, ...bindings.map(binding => `type ${binding.namespace ?? binding.typeMap}`)];
|
|
194
|
+
const out: string[] = [];
|
|
195
|
+
|
|
196
|
+
out.push('/******************************************************************************');
|
|
197
|
+
out.push(' * Generated from the Langium AST — DO NOT EDIT MANUALLY!');
|
|
198
|
+
out.push(` * ${context.regenCommand}`);
|
|
199
|
+
out.push(' ******************************************************************************/');
|
|
200
|
+
out.push('');
|
|
201
|
+
out.push('/* eslint-disable */');
|
|
202
|
+
out.push('');
|
|
203
|
+
out.push("import { makeAstNodeBuilder } from '@hydranium/core';");
|
|
204
|
+
out.push(`import { ${imported.join(', ')} } from '${context.importPath}';`);
|
|
205
|
+
out.push('');
|
|
206
|
+
|
|
207
|
+
// A single-grammar project has nothing to narrow to, so the merged binding is
|
|
208
|
+
// simply THE binding there and must not point at a sibling that was not emitted.
|
|
209
|
+
const narrowed = bindings.some(binding => binding.namespace !== undefined);
|
|
210
|
+
const guarantee = [
|
|
211
|
+
' * Mandatory fields are a type error at the call site, and grammar-declared',
|
|
212
|
+
' * containment arrays are materialised from reflection metadata, so a built',
|
|
213
|
+
' * node carries `[]` where a cast literal would leave `undefined`.'
|
|
214
|
+
];
|
|
215
|
+
|
|
216
|
+
for (const binding of bindings) {
|
|
217
|
+
out.push('/**');
|
|
218
|
+
if (binding.namespace !== undefined) {
|
|
219
|
+
out.push(` * AST-node factory narrowed to the '${binding.namespace}' grammar.`);
|
|
220
|
+
out.push(' *');
|
|
221
|
+
out.push(' * Narrowed by grammar REACHABILITY, not by ownership: a grammar that');
|
|
222
|
+
out.push(" * imports another sees that one's types too, so this binding is only as");
|
|
223
|
+
out.push(` * narrow as '${binding.namespace}'s import closure. Where that closure is the`);
|
|
224
|
+
out.push(' * whole project, it accepts exactly what the merged binding does and the');
|
|
225
|
+
out.push(' * choice is documentation rather than a check.');
|
|
226
|
+
out.push(' *');
|
|
227
|
+
out.push(...guarantee);
|
|
228
|
+
} else if (narrowed) {
|
|
229
|
+
out.push(' * AST-node factory spanning every grammar in this project.');
|
|
230
|
+
out.push(' *');
|
|
231
|
+
out.push(' * Prefer the narrowed binding matching the grammar the call site works in:');
|
|
232
|
+
out.push(" * it rejects a type outside that grammar's import closure, which this one");
|
|
233
|
+
out.push(' * accepts from anywhere. Reach for this one where a call site genuinely');
|
|
234
|
+
out.push(' * spans grammars.');
|
|
235
|
+
} else {
|
|
236
|
+
out.push(" * AST-node factory for this project's grammar.");
|
|
237
|
+
out.push(' *');
|
|
238
|
+
out.push(...guarantee);
|
|
239
|
+
}
|
|
240
|
+
out.push(' */');
|
|
241
|
+
out.push(`export const ${binding.exportName} = makeAstNodeBuilder<${binding.typeMap}>(${context.reflectionName});`);
|
|
242
|
+
out.push('');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return out.join('\n');
|
|
246
|
+
}
|
|
@@ -29,6 +29,7 @@ export const GENERATE_TRANSFER_MODEL_FLAGS: readonly string[] = [
|
|
|
29
29
|
'--ast-file',
|
|
30
30
|
'--augmentation-file',
|
|
31
31
|
'--out-file',
|
|
32
|
+
'--ast-builder-file',
|
|
32
33
|
'--element-type-name',
|
|
33
34
|
'--terminals-name',
|
|
34
35
|
'--terminals-source-name',
|
|
@@ -48,7 +49,8 @@ export const GENERATE_TRANSFER_MODEL_VALUE_FLAGS: readonly string[] = GENERATE_T
|
|
|
48
49
|
export const GENERATE_TRANSFER_MODEL_HELP: readonly string[] = [
|
|
49
50
|
'Usage: hydranium-cli generate-transfer-model [options]',
|
|
50
51
|
'',
|
|
51
|
-
'Generate a serializable transfer-model TypeScript file from a Langium AST
|
|
52
|
+
'Generate a serializable transfer-model TypeScript file from a Langium AST, and',
|
|
53
|
+
'optionally an AST-node builder module bound to the same reflection. The',
|
|
52
54
|
'required inputs (--ast-file / --augmentation-file / --out-file) may instead come',
|
|
53
55
|
'from a --config JSON file, and --ast-file can be auto-discovered from a Langium',
|
|
54
56
|
"config's `out` directory. Precedence, highest first: explicit flags, then the",
|
|
@@ -67,6 +69,11 @@ export const GENERATE_TRANSFER_MODEL_HELP: readonly string[] = [
|
|
|
67
69
|
' directory: it treats that directory as exclusively',
|
|
68
70
|
' its own, so every `langium generate` reports this',
|
|
69
71
|
' file as unexpected and offers to delete it.',
|
|
72
|
+
' --ast-builder-file <path> Also emit an AST-node builder module here: one',
|
|
73
|
+
' makeAstNodeBuilder binding per grammar, plus one',
|
|
74
|
+
' spanning all of them. Omit if nothing constructs',
|
|
75
|
+
' AST nodes. Same restriction as --out-file: keep it',
|
|
76
|
+
" out of langium-cli's own `out` directory.",
|
|
70
77
|
' --element-type-name <name> Base element type name in output. Default: TransferElement.',
|
|
71
78
|
' --terminals-name <name> Terminals const name in output. Default: ModelTerminals.',
|
|
72
79
|
' --terminals-source-name <n> Source-side terminals variable name. Default: <LanguageId>Terminals.',
|
|
@@ -120,6 +127,9 @@ export function parseGenerateOptions(args: string[], onError: UsageError = exitW
|
|
|
120
127
|
case '--out-file':
|
|
121
128
|
flags.outFile = next();
|
|
122
129
|
break;
|
|
130
|
+
case '--ast-builder-file':
|
|
131
|
+
flags.astBuilderFile = next();
|
|
132
|
+
break;
|
|
123
133
|
case '--element-type-name':
|
|
124
134
|
flags.elementTypeName = next();
|
|
125
135
|
break;
|
|
@@ -12,7 +12,7 @@ import * as path from 'node:path';
|
|
|
12
12
|
import type { GenerateTransferModelOptions } from './generate-transfer-model.js';
|
|
13
13
|
|
|
14
14
|
/** Path-valued option keys — resolved relative to a config/langium-config file's directory. */
|
|
15
|
-
const PATH_KEYS = ['astFile', 'augmentationFile', 'outFile'] as const;
|
|
15
|
+
const PATH_KEYS = ['astFile', 'augmentationFile', 'outFile', 'astBuilderFile'] as const;
|
|
16
16
|
|
|
17
17
|
/** Resolve `value` against `baseDir` when it is relative; absolute paths pass through. */
|
|
18
18
|
function resolveAgainst(baseDir: string, value: string): string {
|
|
@@ -43,6 +43,7 @@ export function loadTransferModelConfig(configPath: string): Partial<GenerateTra
|
|
|
43
43
|
'astFile',
|
|
44
44
|
'augmentationFile',
|
|
45
45
|
'outFile',
|
|
46
|
+
'astBuilderFile',
|
|
46
47
|
'elementTypeName',
|
|
47
48
|
'terminalsName',
|
|
48
49
|
'terminalsSourceName',
|
|
@@ -118,6 +119,7 @@ export function mergeTransferModelOptions(...sources: Array<Partial<GenerateTran
|
|
|
118
119
|
astFile: astFile!,
|
|
119
120
|
augmentationFile: augmentationFile!,
|
|
120
121
|
outFile: outFile!,
|
|
122
|
+
astBuilderFile: pick('astBuilderFile'),
|
|
121
123
|
elementTypeName: pick('elementTypeName'),
|
|
122
124
|
terminalsName: pick('terminalsName'),
|
|
123
125
|
terminalsSourceName: pick('terminalsSourceName'),
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import * as fs from 'fs';
|
|
11
11
|
import * as path from 'path';
|
|
12
|
+
import { emitAstBuilder } from './generate-ast-builder.js';
|
|
12
13
|
import {
|
|
13
14
|
type Node,
|
|
14
15
|
type ObjectLiteralExpression,
|
|
@@ -35,6 +36,15 @@ export interface GenerateTransferModelOptions {
|
|
|
35
36
|
augmentationFile: string;
|
|
36
37
|
/** Destination path for the generated transfer model. */
|
|
37
38
|
outFile: string;
|
|
39
|
+
/**
|
|
40
|
+
* Destination for a generated AST-node builder module. Omitted by consumers
|
|
41
|
+
* that construct no AST nodes; the transfer model is emitted either way.
|
|
42
|
+
*
|
|
43
|
+
* Emitted from this command rather than its own because the AST source is
|
|
44
|
+
* already parsed here, and because a consumer that regenerates one artefact
|
|
45
|
+
* and not the other has the two disagreeing about the same grammar.
|
|
46
|
+
*/
|
|
47
|
+
astBuilderFile?: string;
|
|
38
48
|
/** Name used for the base element type in the output. Defaults to `TransferElement`. */
|
|
39
49
|
elementTypeName?: string;
|
|
40
50
|
/** Name used for the terminal-patterns const in the output. Defaults to `ModelTerminals`. */
|
|
@@ -396,10 +406,22 @@ export function generateTransferModel(options: GenerateTransferModelOptions): vo
|
|
|
396
406
|
fs.mkdirSync(path.dirname(options.outFile), { recursive: true });
|
|
397
407
|
if (fs.existsSync(options.outFile) && fs.readFileSync(options.outFile, 'utf-8') === content) {
|
|
398
408
|
console.log('Transfer model is up to date.');
|
|
399
|
-
|
|
409
|
+
} else {
|
|
410
|
+
fs.writeFileSync(options.outFile, content, 'utf-8');
|
|
411
|
+
console.log(`Generated: ${options.outFile}`);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (options.astBuilderFile !== undefined) {
|
|
415
|
+
const emitted = emitAstBuilder(astSource, {
|
|
416
|
+
outFile: options.astBuilderFile,
|
|
417
|
+
astFile: options.astFile,
|
|
418
|
+
languageId,
|
|
419
|
+
regenCommand
|
|
420
|
+
});
|
|
421
|
+
if (!emitted) {
|
|
422
|
+
console.warn(`Warning: no '${languageId}AstType' alias in ${options.astFile}; skipping the AST builder.`);
|
|
423
|
+
}
|
|
400
424
|
}
|
|
401
|
-
fs.writeFileSync(options.outFile, content, 'utf-8');
|
|
402
|
-
console.log(`Generated: ${options.outFile}`);
|
|
403
425
|
}
|
|
404
426
|
|
|
405
427
|
// ---------------------------------------------------------------------------
|
|
@@ -35,7 +35,7 @@ import type { LogThreshold } from '@hydranium/protocol';
|
|
|
35
35
|
import { statSync } from 'node:fs';
|
|
36
36
|
import * as path from 'node:path';
|
|
37
37
|
import { fileURLToPath } from 'node:url';
|
|
38
|
-
import { parseLogLevelOption } from '../log-level.js';
|
|
38
|
+
import { type LogOptions, parseLogLevelOption } from '../log-level.js';
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* How a parser reports a usage problem. It never returns, so a parser can treat
|
|
@@ -68,33 +68,57 @@ export const SERVICES_FLAG = '--services';
|
|
|
68
68
|
* on which command a caller happened to reach for.
|
|
69
69
|
*/
|
|
70
70
|
export const LOG_LEVEL_FLAG = '--log-level';
|
|
71
|
+
export const LOG_FILE_FLAG = '--log-file';
|
|
72
|
+
export const LOG_FILE_LEVEL_FLAG = '--log-file-level';
|
|
73
|
+
|
|
74
|
+
/** The log flags every subcommand takes, read by {@link logOptions}. */
|
|
75
|
+
export const LOG_VALUE_FLAGS = [LOG_LEVEL_FLAG, LOG_FILE_FLAG, LOG_FILE_LEVEL_FLAG] as const;
|
|
71
76
|
|
|
72
77
|
/** The value flags {@link parseServerSpawnOptions} consumes itself. */
|
|
73
|
-
export const SERVER_SPAWN_VALUE_FLAGS: readonly string[] = ['--server', '--cwd',
|
|
78
|
+
export const SERVER_SPAWN_VALUE_FLAGS: readonly string[] = ['--server', '--cwd', ...LOG_VALUE_FLAGS];
|
|
74
79
|
|
|
75
80
|
/**
|
|
76
|
-
* The `--help`
|
|
81
|
+
* The `--help` lines every subcommand describes {@link LOG_VALUE_FLAGS} with.
|
|
77
82
|
*
|
|
78
83
|
* Shared text, per-command padding: `command-args.test.ts` compares the flags a
|
|
79
84
|
* help block DESCRIBES against the flags the parser accepts, and it recognises a
|
|
80
|
-
* description by the two-space indent — so
|
|
85
|
+
* description by the two-space indent — so each line has to be a real option entry
|
|
81
86
|
* in each block, and the column each block aligns its descriptions at is the
|
|
82
|
-
* block's own.
|
|
87
|
+
* block's own. A flag too wide for that column takes its description on the
|
|
88
|
+
* next line, at the column.
|
|
83
89
|
*/
|
|
84
|
-
export function
|
|
85
|
-
|
|
90
|
+
export function logHelpLines(padTo: number, head = 'the head this boots'): string[] {
|
|
91
|
+
const line = (flag: string, text: string): string[] =>
|
|
92
|
+
flag.length < padTo - 1 ? [` ${flag.padEnd(padTo)}${text}`] : [` ${flag}`, `${' '.repeat(padTo + 2)}${text}`];
|
|
93
|
+
return [
|
|
94
|
+
line(`${LOG_LEVEL_FLAG} <lvl>`, `Log threshold for ${head} (off|error|warn|info|debug|trace).`),
|
|
95
|
+
line(`${LOG_FILE_FLAG} <file>`, `Also write ${head}'s log to <file>; {workspace} expands to the workspace.`),
|
|
96
|
+
line(`${LOG_FILE_LEVEL_FLAG} <lvl>`, `Threshold for that file, which the log-level setting does not change.`)
|
|
97
|
+
].flat();
|
|
86
98
|
}
|
|
87
99
|
|
|
88
100
|
/**
|
|
89
|
-
* Narrow
|
|
90
|
-
*
|
|
101
|
+
* Narrow the raw {@link LOG_VALUE_FLAGS} values a parser collected, leaving
|
|
102
|
+
* absent flags absent.
|
|
103
|
+
*
|
|
104
|
+
* `--log-file` resolves against the caller's directory: the child may run
|
|
105
|
+
* elsewhere (`--cwd`), and a relative path would land the log there.
|
|
91
106
|
*
|
|
92
107
|
* Throws rather than reaching `onError`, matching {@link parseServerSpawnOptions}:
|
|
93
108
|
* the level vocabulary belongs to the protocol package, which reports it, and the
|
|
94
109
|
* entry point's catch turns that into the same message-and-exit.
|
|
95
110
|
*/
|
|
96
|
-
export function
|
|
97
|
-
|
|
111
|
+
export function logOptions(options: Readonly<Record<string, string | undefined>>): LogOptions {
|
|
112
|
+
const level = (flag: string): LogThreshold | undefined => {
|
|
113
|
+
const value = options[flag];
|
|
114
|
+
return value === undefined ? undefined : parseLogLevelOption(value, flag);
|
|
115
|
+
};
|
|
116
|
+
const logFile = options[LOG_FILE_FLAG];
|
|
117
|
+
return {
|
|
118
|
+
logLevel: level(LOG_LEVEL_FLAG),
|
|
119
|
+
logFile: logFile === undefined ? undefined : path.resolve(logFile),
|
|
120
|
+
logFileLevel: level(LOG_FILE_LEVEL_FLAG)
|
|
121
|
+
};
|
|
98
122
|
}
|
|
99
123
|
|
|
100
124
|
/**
|
|
@@ -336,11 +360,10 @@ function kebabToCamel(name: string): string {
|
|
|
336
360
|
}
|
|
337
361
|
|
|
338
362
|
/** What {@link parseServerSpawnOptions} recovered from an argv. */
|
|
339
|
-
export interface SpawnOptions {
|
|
363
|
+
export interface SpawnOptions extends LogOptions {
|
|
340
364
|
serverCommand: string;
|
|
341
365
|
serverArgs: string[];
|
|
342
366
|
cwd?: string;
|
|
343
|
-
logLevel?: LogThreshold;
|
|
344
367
|
/** The tokens this parser did not claim, for the subcommand's own options. */
|
|
345
368
|
extra: string[];
|
|
346
369
|
}
|
|
@@ -363,10 +386,10 @@ function isRelativePathToken(token: string, isCommand: boolean): boolean {
|
|
|
363
386
|
|
|
364
387
|
/**
|
|
365
388
|
* Shared option parser for subcommands that spawn a data-server child. Consumes
|
|
366
|
-
* `--server` / `--cwd` /
|
|
389
|
+
* `--server` / `--cwd` / the {@link LOG_VALUE_FLAGS} and returns the surviving tokens as
|
|
367
390
|
* `extra` for {@link parseFlagOptions} to finish.
|
|
368
391
|
*
|
|
369
|
-
* An unrecognised
|
|
392
|
+
* An unrecognised log level value throws rather than reaching `onError`: the
|
|
370
393
|
* level vocabulary belongs to the protocol package, which reports it, and the
|
|
371
394
|
* entry point's catch turns that into the same message-and-exit.
|
|
372
395
|
*
|
|
@@ -383,7 +406,7 @@ function isRelativePathToken(token: string, isCommand: boolean): boolean {
|
|
|
383
406
|
export function parseServerSpawnOptions(args: string[], commandName: string, onError: UsageError = exitWithUsage): SpawnOptions {
|
|
384
407
|
let serverSpec: string | undefined;
|
|
385
408
|
let cwd: string | undefined;
|
|
386
|
-
|
|
409
|
+
const logFlags: Record<string, string> = {};
|
|
387
410
|
const extra: string[] = [];
|
|
388
411
|
for (let index = 0; index < args.length; index += 1) {
|
|
389
412
|
const flag = args[index];
|
|
@@ -399,8 +422,8 @@ export function parseServerSpawnOptions(args: string[], commandName: string, onE
|
|
|
399
422
|
serverSpec = claim();
|
|
400
423
|
} else if (flag === '--cwd') {
|
|
401
424
|
cwd = claim();
|
|
402
|
-
} else if (
|
|
403
|
-
|
|
425
|
+
} else if ((LOG_VALUE_FLAGS as readonly string[]).includes(flag)) {
|
|
426
|
+
logFlags[flag] = claim();
|
|
404
427
|
} else {
|
|
405
428
|
extra.push(flag);
|
|
406
429
|
}
|
|
@@ -425,5 +448,5 @@ export function parseServerSpawnOptions(args: string[], commandName: string, onE
|
|
|
425
448
|
);
|
|
426
449
|
}
|
|
427
450
|
}
|
|
428
|
-
return { serverCommand, serverArgs, cwd,
|
|
451
|
+
return { serverCommand, serverArgs, cwd, ...logOptions(logFlags), extra };
|
|
429
452
|
}
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
10
|
import type * as HydraniumCoreNode from '@hydranium/core/node';
|
|
11
|
-
import type { LogThreshold } from '@hydranium/protocol';
|
|
12
11
|
import { spawn } from 'node:child_process';
|
|
13
12
|
import { statSync, writeFileSync } from 'node:fs';
|
|
14
13
|
import { createRequire } from 'node:module';
|
|
15
14
|
import * as path from 'node:path';
|
|
16
15
|
import { pathToFileURL } from 'node:url';
|
|
17
|
-
import {
|
|
16
|
+
import { driverHeapArgs, type HeapReading } from '../driver-heap.js';
|
|
17
|
+
import { logEnv, type LogOptions } from '../log-level.js';
|
|
18
18
|
import { SERVICES_FLAG } from './harness-args.js';
|
|
19
19
|
|
|
20
20
|
/** The flag every report-producing subcommand names its destination file with. */
|
|
@@ -47,9 +47,10 @@ export interface HeadlessContext {
|
|
|
47
47
|
* ZERO-ARG `createServices(): { shared }`. `hydranium-cli` is language-agnostic
|
|
48
48
|
* and cannot statically import a head's `create<Lang>Services`, so the dynamic
|
|
49
49
|
* import is the seam that keeps the binary head-neutral. Resolving the harness
|
|
50
|
-
* from the head's graph (via `createRequire` rooted at the head module)
|
|
51
|
-
*
|
|
52
|
-
*
|
|
50
|
+
* from the head's graph (via `createRequire` rooted at the head module) rather
|
|
51
|
+
* than from the CLI's guarantees it runs against the SAME core copy as
|
|
52
|
+
* `createServices`, which a second copy of the module would break even where
|
|
53
|
+
* both resolve.
|
|
53
54
|
*
|
|
54
55
|
* Throws a clear error when the path names no file, and when the export is
|
|
55
56
|
* missing or not a function.
|
|
@@ -129,36 +130,48 @@ export function spawnNodeChild(execArgs: string[], env?: Record<string, string>)
|
|
|
129
130
|
/** How a subcommand parent reaches its driver child; the seam `__spawnForTest` replaces. */
|
|
130
131
|
export type SpawnDriverChild = (execArgs: string[], env?: Record<string, string>) => Promise<number>;
|
|
131
132
|
|
|
132
|
-
/**
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
readonly logLevel?: LogThreshold;
|
|
133
|
+
/**
|
|
134
|
+
* What every `--services` subcommand carries for the child it spawns, on top of
|
|
135
|
+
* its own options. The log options apply to the head the driver boots; an
|
|
136
|
+
* absent one leaves the child's inherited environment alone, so an ambient
|
|
137
|
+
* variable still wins where a caller set one.
|
|
138
|
+
*/
|
|
139
|
+
export interface DriverSpawnOptions extends LogOptions {
|
|
140
140
|
/** Test-only: capture the node argv and env instead of spawning the real child. */
|
|
141
141
|
readonly __spawnForTest?: SpawnDriverChild;
|
|
142
|
+
/**
|
|
143
|
+
* Test-only: decide the heap ceiling from a stated cgroup reading rather
|
|
144
|
+
* than the machine's. Without it an argv assertion means one thing on a
|
|
145
|
+
* workstation and nothing at all under a memory limit, so a suite that
|
|
146
|
+
* asserts the ceiling is present passes vacuously in a container.
|
|
147
|
+
*/
|
|
148
|
+
readonly __heapReadingForTest?: HeapReading;
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
/**
|
|
145
152
|
* Parent-side: run a subcommand's driver child and propagate its exit code, so a
|
|
146
153
|
* shell or CI step sees the gate.
|
|
147
154
|
*
|
|
148
|
-
* **The log
|
|
149
|
-
* head's logger reads `
|
|
150
|
-
* which is inside the driver's dynamic import — a flag the driver
|
|
151
|
-
* arrive after the only moment it can be read. Setting
|
|
152
|
-
* exporting
|
|
153
|
-
*
|
|
155
|
+
* **The log options travel in the child's ENVIRONMENT, not its argv.** The
|
|
156
|
+
* head's logger reads its `HYDRANIUM_LOG_*` variables while `createServices`
|
|
157
|
+
* constructs it, which is inside the driver's dynamic import — a flag the driver
|
|
158
|
+
* parsed would arrive after the only moment it can be read. Setting them here
|
|
159
|
+
* rather than exporting them also leaves the CLI's own output alone, unlike
|
|
160
|
+
* ambient variables the parent would carry too.
|
|
154
161
|
*
|
|
155
162
|
* A head that binds a logger of its own decides for itself whether the flag means
|
|
156
163
|
* anything, which is the intended seam: the CLI is language-agnostic and cannot
|
|
157
164
|
* reach past `createServices`.
|
|
165
|
+
*
|
|
166
|
+
* **The heap ceiling is prepended HERE rather than passed by each subcommand.**
|
|
167
|
+
* Every caller wants the same answer to the same question, and the answer is not
|
|
168
|
+
* a constant — see {@link driverHeapArgs}. A literal at the call site is a place
|
|
169
|
+
* the container case can be missed, and the caller that misses it takes the whole
|
|
170
|
+
* cgroup down with it rather than failing on its own.
|
|
158
171
|
*/
|
|
159
172
|
export async function runDriverChild(execArgs: string[], options: DriverSpawnOptions): Promise<void> {
|
|
160
173
|
const spawnChild = options.__spawnForTest ?? spawnNodeChild;
|
|
161
|
-
const code = await spawnChild(
|
|
174
|
+
const code = await spawnChild([...driverHeapArgs(options.__heapReadingForTest), ...execArgs], logEnv(options));
|
|
162
175
|
if (code) {
|
|
163
176
|
process.exitCode = code;
|
|
164
177
|
}
|