@hydranium/cli 1.0.0-next.4 → 1.0.0-next.400
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 +102 -204
- package/bin/hydranium-cli.js +13 -0
- package/heap-analysis/heap/core/load.mjs +2 -2
- package/heap-analysis/heap/langium/langium.classifier.mjs +22 -11
- package/lib/cli-version.js.map +1 -1
- package/lib/cli.js +7 -2
- package/lib/cli.js.map +1 -1
- package/lib/commands/analyze-heap-args.d.ts.map +1 -1
- package/lib/commands/analyze-heap-args.js +1 -2
- 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.d.ts +3 -2
- package/lib/commands/ast-ground-truth-args.d.ts.map +1 -1
- package/lib/commands/ast-ground-truth-args.js +11 -9
- package/lib/commands/ast-ground-truth-args.js.map +1 -1
- package/lib/commands/ast-ground-truth-driver.js +4 -2
- package/lib/commands/ast-ground-truth-driver.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/client-session.d.ts +17 -0
- package/lib/commands/client-session.d.ts.map +1 -0
- package/lib/commands/client-session.js +27 -0
- package/lib/commands/client-session.js.map +1 -0
- package/lib/commands/create-args.d.ts +17 -0
- package/lib/commands/create-args.d.ts.map +1 -0
- package/lib/commands/create-args.js +53 -0
- package/lib/commands/create-args.js.map +1 -0
- package/lib/commands/create.d.ts +38 -0
- package/lib/commands/create.d.ts.map +1 -0
- package/lib/commands/create.js +43 -0
- package/lib/commands/create.js.map +1 -0
- package/lib/commands/delete-args.d.ts +17 -0
- package/lib/commands/delete-args.d.ts.map +1 -0
- package/lib/commands/delete-args.js +49 -0
- package/lib/commands/delete-args.js.map +1 -0
- package/lib/commands/delete.d.ts +30 -0
- package/lib/commands/delete.d.ts.map +1 -0
- package/lib/commands/delete.js +32 -0
- package/lib/commands/delete.js.map +1 -0
- 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 +191 -0
- package/lib/commands/generate-ast-builder.js.map +1 -0
- package/lib/commands/generate-languages.d.ts +35 -0
- package/lib/commands/generate-languages.d.ts.map +1 -0
- package/lib/commands/generate-languages.js +105 -0
- package/lib/commands/generate-languages.js.map +1 -0
- package/lib/commands/generate-transfer-builder.d.ts +33 -0
- package/lib/commands/generate-transfer-builder.d.ts.map +1 -0
- package/lib/commands/generate-transfer-builder.js +86 -0
- package/lib/commands/generate-transfer-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 +30 -5
- 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 +10 -3
- package/lib/commands/generate-transfer-model-config.js.map +1 -1
- package/lib/commands/generate-transfer-model.d.ts +49 -9
- package/lib/commands/generate-transfer-model.d.ts.map +1 -1
- package/lib/commands/generate-transfer-model.js +235 -70
- package/lib/commands/generate-transfer-model.js.map +1 -1
- package/lib/commands/generated-module-path.d.ts +16 -0
- package/lib/commands/generated-module-path.d.ts.map +1 -0
- package/lib/commands/generated-module-path.js +21 -0
- package/lib/commands/generated-module-path.js.map +1 -0
- package/lib/commands/harness-args.d.ts +44 -42
- package/lib/commands/harness-args.d.ts.map +1 -1
- package/lib/commands/harness-args.js +109 -27
- package/lib/commands/harness-args.js.map +1 -1
- package/lib/commands/headless-harness.d.ts +37 -20
- package/lib/commands/headless-harness.d.ts.map +1 -1
- package/lib/commands/headless-harness.js +71 -19
- package/lib/commands/headless-harness.js.map +1 -1
- package/lib/commands/init-args.d.ts +2 -0
- package/lib/commands/init-args.d.ts.map +1 -1
- package/lib/commands/init-args.js +74 -31
- package/lib/commands/init-args.js.map +1 -1
- package/lib/commands/init-hosts.d.ts +24 -0
- package/lib/commands/init-hosts.d.ts.map +1 -0
- package/lib/commands/init-hosts.js +37 -0
- package/lib/commands/init-hosts.js.map +1 -0
- package/lib/commands/init-prompt.d.ts +3 -3
- package/lib/commands/init-prompt.d.ts.map +1 -1
- package/lib/commands/init-prompt.js +4 -5
- package/lib/commands/init-prompt.js.map +1 -1
- package/lib/commands/init-templates.d.ts +20 -14
- package/lib/commands/init-templates.d.ts.map +1 -1
- package/lib/commands/init-templates.js +3015 -750
- package/lib/commands/init-templates.js.map +1 -1
- package/lib/commands/init-wizard.d.ts +3 -0
- package/lib/commands/init-wizard.d.ts.map +1 -1
- package/lib/commands/init-wizard.js +46 -31
- package/lib/commands/init-wizard.js.map +1 -1
- package/lib/commands/init-workspace.d.ts +13 -11
- package/lib/commands/init-workspace.d.ts.map +1 -1
- package/lib/commands/init-workspace.js +21 -7
- package/lib/commands/init-workspace.js.map +1 -1
- package/lib/commands/init.d.ts +58 -22
- package/lib/commands/init.d.ts.map +1 -1
- package/lib/commands/init.js +203 -48
- package/lib/commands/init.js.map +1 -1
- package/lib/commands/kebab.d.ts +15 -0
- package/lib/commands/kebab.d.ts.map +1 -0
- package/lib/commands/kebab.js +17 -0
- package/lib/commands/kebab.js.map +1 -0
- package/lib/commands/lint-grammar-args.d.ts +3 -2
- package/lib/commands/lint-grammar-args.d.ts.map +1 -1
- package/lib/commands/lint-grammar-args.js +13 -10
- package/lib/commands/lint-grammar-args.js.map +1 -1
- package/lib/commands/lint-grammar-driver.js +2 -1
- package/lib/commands/lint-grammar-driver.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 +3 -2
- package/lib/commands/measure-memory-args.d.ts.map +1 -1
- package/lib/commands/measure-memory-args.js +22 -11
- package/lib/commands/measure-memory-args.js.map +1 -1
- package/lib/commands/measure-memory-driver.js +5 -3
- 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.d.ts +4 -3
- package/lib/commands/model-docs-args.d.ts.map +1 -1
- package/lib/commands/model-docs-args.js +12 -10
- package/lib/commands/model-docs-args.js.map +1 -1
- package/lib/commands/model-docs-driver.js +2 -1
- package/lib/commands/model-docs-driver.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 +6 -5
- package/lib/commands/query-args.js.map +1 -1
- package/lib/commands/query.d.ts +6 -5
- package/lib/commands/query.d.ts.map +1 -1
- package/lib/commands/query.js +5 -4
- package/lib/commands/query.js.map +1 -1
- package/lib/commands/reflect-args.d.ts +3 -2
- package/lib/commands/reflect-args.d.ts.map +1 -1
- package/lib/commands/reflect-args.js +13 -10
- package/lib/commands/reflect-args.js.map +1 -1
- package/lib/commands/reflect-driver.js +2 -1
- package/lib/commands/reflect-driver.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 +7 -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 +12 -23
- package/lib/commands/save.js.map +1 -1
- package/lib/commands/validate-args.d.ts +3 -2
- package/lib/commands/validate-args.d.ts.map +1 -1
- package/lib/commands/validate-args.js +13 -10
- package/lib/commands/validate-args.js.map +1 -1
- package/lib/commands/validate-driver.js +2 -1
- package/lib/commands/validate-driver.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 +3 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -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/lib/spawn-data-server.d.ts +9 -4
- package/lib/spawn-data-server.d.ts.map +1 -1
- package/lib/spawn-data-server.js +5 -4
- package/lib/spawn-data-server.js.map +1 -1
- package/lib/stderr-connection-logger.d.ts +12 -0
- package/lib/stderr-connection-logger.d.ts.map +1 -0
- package/lib/stderr-connection-logger.js +16 -0
- package/lib/stderr-connection-logger.js.map +1 -0
- package/lib/testing/echo-server.js +9 -1
- package/lib/testing/echo-server.js.map +1 -1
- package/package.json +90 -38
- package/src/cli.ts +7 -2
- package/src/commands/analyze-heap-args.ts +1 -2
- package/src/commands/analyze-heap.ts +4 -1
- package/src/commands/ast-ground-truth-args.ts +15 -12
- package/src/commands/ast-ground-truth-driver.ts +4 -2
- package/src/commands/ast-ground-truth.ts +1 -1
- package/src/commands/client-session.ts +33 -0
- package/src/commands/create-args.ts +68 -0
- package/src/commands/create.ts +83 -0
- package/src/commands/delete-args.ts +64 -0
- package/src/commands/delete.ts +61 -0
- package/src/commands/generate-ast-builder.ts +236 -0
- package/src/commands/generate-languages.ts +132 -0
- package/src/commands/generate-transfer-builder.ts +101 -0
- package/src/commands/generate-transfer-model-args.ts +30 -5
- package/src/commands/generate-transfer-model-config.ts +10 -3
- package/src/commands/generate-transfer-model.ts +316 -68
- package/src/commands/generated-module-path.ts +22 -0
- package/src/commands/harness-args.ts +99 -31
- package/src/commands/headless-harness.ts +89 -28
- package/src/commands/init-args.ts +76 -31
- package/src/commands/init-hosts.ts +51 -0
- package/src/commands/init-prompt.ts +5 -6
- package/src/commands/init-templates.ts +3183 -760
- package/src/commands/init-wizard.ts +55 -31
- package/src/commands/init-workspace.ts +31 -16
- package/src/commands/init.ts +261 -63
- package/src/commands/kebab.ts +17 -0
- package/src/commands/lint-grammar-args.ts +17 -13
- package/src/commands/lint-grammar-driver.ts +2 -1
- package/src/commands/lint-grammar.ts +1 -1
- package/src/commands/measure-memory-args.ts +27 -14
- package/src/commands/measure-memory-driver.ts +5 -3
- package/src/commands/measure-memory.ts +1 -1
- package/src/commands/model-docs-args.ts +16 -13
- package/src/commands/model-docs-driver.ts +2 -1
- 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 +6 -4
- package/src/commands/query.ts +9 -9
- package/src/commands/reflect-args.ts +17 -13
- package/src/commands/reflect-driver.ts +2 -1
- package/src/commands/reflect.ts +1 -1
- package/src/commands/save-args.ts +7 -3
- package/src/commands/save.ts +24 -27
- package/src/commands/validate-args.ts +17 -13
- package/src/commands/validate-driver.ts +2 -1
- 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 +3 -0
- package/src/log-level.ts +42 -14
- package/src/spawn-data-server.ts +17 -5
- package/src/stderr-connection-logger.ts +18 -0
- package/src/testing/echo-server.ts +11 -2
- package/templates/client/gitignore +3 -0
- package/templates/client/src/__GRAMMAR_ID__-diagram-module.ts +82 -0
- package/templates/editorconfig +15 -0
- package/templates/gitignore +1 -0
- package/templates/host-browser/esbuild.mjs +82 -0
- package/templates/host-browser/gitignore +4 -0
- package/templates/host-browser/hydranium-mark.svg +40 -0
- package/templates/host-browser/index.html +287 -0
- package/templates/host-browser/playwright.config.mts +24 -0
- package/templates/host-browser/src/data-status.ts +15 -0
- package/templates/host-browser/src/diagram-save.ts +60 -0
- package/templates/host-browser/src/diagram.ts +121 -0
- package/templates/host-browser/src/editor.ts +102 -0
- package/templates/host-browser/src/log.ts +14 -0
- package/templates/host-browser/src/monaco-editor-core-worker.d.ts +7 -0
- package/templates/host-browser/src/monaco-editor-worker.ts +10 -0
- package/templates/host-browser/src/posix-path-shim.ts +23 -0
- package/templates/host-browser/src/shown-file.ts +61 -0
- package/templates/host-browser/src/workspace.ts +5 -0
- package/templates/host-browser/tsconfig.e2e.json +16 -0
- package/templates/host-browser/tsconfig.page.json +16 -0
- package/templates/host-browser/tsconfig.worker.json +16 -0
- package/templates/host-theia-browser/esbuild.mjs +24 -0
- package/templates/host-theia-browser/gitignore +9 -0
- package/templates/host-theia-browser/playwright.config.mts +42 -0
- package/templates/host-theia-browser/start.mjs +50 -0
- package/templates/host-theia-browser/tsconfig.e2e.json +16 -0
- package/templates/host-theia-electron/esbuild.mjs +27 -0
- package/templates/host-theia-electron/gitignore +8 -0
- package/templates/host-theia-electron/playwright.config.mts +12 -0
- package/templates/host-theia-electron/start.mjs +47 -0
- package/templates/host-theia-electron/tsconfig.e2e.json +16 -0
- package/templates/host-vscode/esbuild.mjs +38 -0
- package/templates/host-vscode/gitignore +7 -0
- package/templates/host-vscode/playwright.config.mts +14 -0
- package/templates/host-vscode/src/__GRAMMAR_ID__-diagram-editor.ts +19 -0
- package/templates/host-vscode/src/show-model-command.ts +24 -0
- package/templates/host-vscode/src/webview/__GRAMMAR_ID__-diagram.ts +18 -0
- package/templates/host-vscode/src/webview/diagram.css +40 -0
- package/templates/host-vscode/start.mjs +22 -0
- package/templates/host-vscode/test/install-vscode.mts +21 -0
- package/templates/host-vscode/test/vscode-version.mts +10 -0
- package/templates/host-vscode/tsconfig.e2e.json +17 -0
- package/templates/host-vscode/tsconfig.json +19 -0
- package/templates/host-vscode/tsconfig.webview.json +17 -0
- package/templates/oxlintrc.json +4 -0
- package/templates/protocol/gitignore +3 -0
- package/templates/protocol/src/__GRAMMAR_ID__-diagram-types.ts +47 -0
- package/templates/server/gitignore +6 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/create-node-operation-handler.ts +62 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/delete-operation-handler.ts +63 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/gmodel-factory.ts +144 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/state.ts +18 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/storage.ts +16 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/submission-handler.ts +24 -0
- package/templates/server/src/grammar/common.langium +12 -0
- package/templates/server/src/language-server/__GRAMMAR_ID__-serializer.ts +53 -0
- package/templates/server/src/language-server/__GRAMMAR_ID__-validation.ts +30 -0
- package/templates/server/src/language-server/ast.ts +23 -0
- package/templates/server/src/node/services.ts +16 -0
- package/templates/server/test/__GRAMMAR_ID__-diagram.test.ts +403 -0
- package/templates/server/test/tsconfig.json +4 -0
- package/templates/server/tsconfig.test.json +10 -0
- package/templates/server/vitest.config.ts +14 -0
- package/templates/theia/gitignore +3 -0
- package/templates/theia/src/browser/__GRAMMAR_ID__-diagram-frontend-module.ts +65 -0
- package/templates/theia/src/browser/__PROJECT_ID__-connection-preload-module.ts +15 -0
- package/templates/theia/src/browser/__PROJECT_ID__-data-connection.ts +15 -0
- package/templates/theia/src/browser/__PROJECT_ID__-data-port.ts +11 -0
- package/templates/theia/src/browser/__PROJECT_ID__-glsp-client-contribution.ts +13 -0
- package/templates/theia/src/browser/__PROJECT_ID__-show-model-command.ts +51 -0
- package/templates/theia/src/common/__PROJECT_ID__-glsp-contribution.ts +3 -0
- package/templates/theia/src/node/__PROJECT_ID__-connection-backend-module.ts +12 -0
- package/templates/theia/src/node/__PROJECT_ID__-data-backend-module.ts +15 -0
- package/templates/theia/src/node/__PROJECT_ID__-glsp-backend-module.ts +16 -0
- package/templates/theia/style/diagram.css +20 -0
- package/templates/theia/tsconfig.json +24 -0
- package/templates/vscode-servers/gitignore +4 -0
- package/templates/vscode-servers/tsconfig.json +19 -0
- package/templates/workspace/nodes.__EXTENSION__ +5 -0
- package/templates/workspace/references.__EXTENSION__ +2 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 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 type { TransferElement } from '@hydranium/protocol';
|
|
11
|
+
import type { DataServerProtocol } from '@hydranium/protocol/data';
|
|
12
|
+
import { logEnv, type LogOptions } from '../log-level.js';
|
|
13
|
+
import { withDataServer } from '../spawn-data-server.js';
|
|
14
|
+
import { withClientSession } from './client-session.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Options for the {@link runDelete} subcommand. Deletes the document at `uri`,
|
|
18
|
+
* its file and then the document, via the data-server's `deleteModelDocument`
|
|
19
|
+
* RPC, whatever text it holds. Prints nothing; a failed delete rejects.
|
|
20
|
+
*
|
|
21
|
+
* `clientId` defaults to `'hydranium-cli'`. The command deletes as a client
|
|
22
|
+
* session under it: it registers the session, opens the document, deletes it,
|
|
23
|
+
* and ends the session.
|
|
24
|
+
*/
|
|
25
|
+
export interface DeleteCommandOptions extends LogOptions {
|
|
26
|
+
readonly serverCommand: string;
|
|
27
|
+
readonly serverArgs?: readonly string[];
|
|
28
|
+
readonly cwd?: string;
|
|
29
|
+
readonly uri: string;
|
|
30
|
+
readonly clientId?: string;
|
|
31
|
+
readonly __proxyForTest?: DataServerProtocol<TransferElement>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function runDelete(options: DeleteCommandOptions): Promise<void> {
|
|
35
|
+
const clientId = options.clientId ?? 'hydranium-cli';
|
|
36
|
+
if (options.__proxyForTest) {
|
|
37
|
+
await deleteOpened(options.__proxyForTest, options.uri, clientId);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
await withDataServer(
|
|
41
|
+
{
|
|
42
|
+
command: options.serverCommand,
|
|
43
|
+
args: options.serverArgs,
|
|
44
|
+
cwd: options.cwd,
|
|
45
|
+
env: logEnv(options)
|
|
46
|
+
},
|
|
47
|
+
server => deleteOpened(server, options.uri, clientId)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Open `uri` as the session `clientId` and delete it, then end the session. */
|
|
52
|
+
function deleteOpened(
|
|
53
|
+
server: Pick<DataServerProtocol<TransferElement>, 'createSession' | 'openModelDocument' | 'deleteModelDocument' | 'closeSession'>,
|
|
54
|
+
uri: string,
|
|
55
|
+
clientId: string
|
|
56
|
+
): Promise<void> {
|
|
57
|
+
return withClientSession(server, clientId, async () => {
|
|
58
|
+
await server.openModelDocument({ uri, clientId });
|
|
59
|
+
await server.deleteModelDocument({ uri, clientId, baseVersion: 'any' });
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
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
|
+
import { relativeImportPath } from './generated-module-path.js';
|
|
14
|
+
|
|
15
|
+
/** Inputs for {@link emitAstBuilder}, resolved by the transfer-model command. */
|
|
16
|
+
export interface EmitAstBuilderOptions {
|
|
17
|
+
/** Destination path for the generated builder module. */
|
|
18
|
+
outFile: string;
|
|
19
|
+
/** Path to the Langium-generated AST file the builder imports from. */
|
|
20
|
+
astFile: string;
|
|
21
|
+
/** Language id derived from the `<LanguageId>AstReflection` class name. */
|
|
22
|
+
languageId: string;
|
|
23
|
+
/** Header line naming the command that regenerates the file. */
|
|
24
|
+
regenCommand: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A single `makeAstNodeBuilder` binding the generator emits.
|
|
29
|
+
*
|
|
30
|
+
* `typeMap` is the type expression handed to the generic — the whole-project
|
|
31
|
+
* alias for the merged binding, `<Namespace>.AstType` for a narrowed one.
|
|
32
|
+
*/
|
|
33
|
+
interface BuilderBinding {
|
|
34
|
+
exportName: string;
|
|
35
|
+
typeMap: string;
|
|
36
|
+
/** Grammar this binding is narrowed to; absent on the merged binding. */
|
|
37
|
+
namespace?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Langium declares this type alias inside each language's namespace in a
|
|
42
|
+
* multi-language project. Its presence is what marks a namespace as a grammar's
|
|
43
|
+
* type registry rather than an incidental one.
|
|
44
|
+
*/
|
|
45
|
+
const NAMESPACED_AST_TYPE_NAME = 'AstType';
|
|
46
|
+
|
|
47
|
+
/** Export name of the merged, whole-project binding. */
|
|
48
|
+
const MERGED_EXPORT_NAME = 'astNode';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Emit a module binding {@link makeAstNodeBuilder} to a project's reflection,
|
|
52
|
+
* once per grammar plus once across all of them.
|
|
53
|
+
*
|
|
54
|
+
* **The binding is boilerplate with exactly one degree of freedom** — the type
|
|
55
|
+
* map — so hand-writing it buys nothing and goes stale the moment a grammar is
|
|
56
|
+
* added. Generating it is also the only way a narrowed binding can exist at all:
|
|
57
|
+
* the per-grammar type maps live in namespaces Langium emits only on its
|
|
58
|
+
* multi-language path, so a single-grammar project gets the merged binding alone
|
|
59
|
+
* and there is nothing to narrow.
|
|
60
|
+
*
|
|
61
|
+
* **A narrowed binding is only as narrow as its grammar's import closure.**
|
|
62
|
+
* Langium's per-grammar `AstType` lists everything REACHABLE from that grammar,
|
|
63
|
+
* so where one grammar imports all the others its map equals the project's and
|
|
64
|
+
* its binding checks nothing the merged one does not. The emitted doc says so at
|
|
65
|
+
* each declaration rather than promising a guarantee the type map cannot give.
|
|
66
|
+
*
|
|
67
|
+
* Answers `false` when the AST source declares no whole-project type alias,
|
|
68
|
+
* which means the file did not come from `langium generate` and the caller
|
|
69
|
+
* should report rather than write a module that cannot compile.
|
|
70
|
+
*/
|
|
71
|
+
export function emitAstBuilder(astSource: SourceFile, options: EmitAstBuilderOptions): boolean {
|
|
72
|
+
const content = buildAstBuilderSource(astSource, options);
|
|
73
|
+
if (content === undefined) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fs.mkdirSync(path.dirname(options.outFile), { recursive: true });
|
|
78
|
+
if (fs.existsSync(options.outFile) && fs.readFileSync(options.outFile, 'utf-8') === content) {
|
|
79
|
+
console.log('AST builder is up to date.');
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
fs.writeFileSync(options.outFile, content, 'utf-8');
|
|
83
|
+
console.log(`Generated: ${options.outFile}`);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The module text {@link emitAstBuilder} would write, or `undefined` when the
|
|
89
|
+
* AST source declares no whole-project type alias. Pure over its inputs, so the
|
|
90
|
+
* emitted shape is testable without touching a filesystem.
|
|
91
|
+
*/
|
|
92
|
+
export function buildAstBuilderSource(astSource: SourceFile, options: EmitAstBuilderOptions): string | undefined {
|
|
93
|
+
const mergedTypeMap = `${options.languageId}AstType`;
|
|
94
|
+
if (!astSource.getTypeAlias(mergedTypeMap)) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const bindings: BuilderBinding[] = [{ exportName: MERGED_EXPORT_NAME, typeMap: mergedTypeMap }];
|
|
99
|
+
for (const namespace of findGrammarNamespaces(astSource)) {
|
|
100
|
+
bindings.push({
|
|
101
|
+
exportName: builderExportName(namespace),
|
|
102
|
+
typeMap: `${namespace}.${NAMESPACED_AST_TYPE_NAME}`,
|
|
103
|
+
namespace
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
assertDistinctExportNames(bindings);
|
|
107
|
+
|
|
108
|
+
return renderAstBuilder(bindings, {
|
|
109
|
+
reflectionName: findReflectionConstName(astSource, options.languageId),
|
|
110
|
+
importPath: relativeImportPath(options.outFile, options.astFile),
|
|
111
|
+
regenCommand: options.regenCommand
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Namespaces that carry a grammar's type registry, in declaration order.
|
|
117
|
+
*
|
|
118
|
+
* Keyed on the nested `AstType` alias rather than on the namespace being
|
|
119
|
+
* exported, because an adopter's augmentation can add namespaces to the same
|
|
120
|
+
* file for unrelated reasons and only the ones Langium emits per grammar have
|
|
121
|
+
* a type map to bind.
|
|
122
|
+
*/
|
|
123
|
+
function findGrammarNamespaces(astSource: SourceFile): string[] {
|
|
124
|
+
const names: string[] = [];
|
|
125
|
+
for (const module of astSource.getModules()) {
|
|
126
|
+
const name = module.getName();
|
|
127
|
+
if (name && module.getTypeAlias(NAMESPACED_AST_TYPE_NAME)) {
|
|
128
|
+
names.push(name);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return names;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* `Domain` → `domainNode`. Lower-cases the leading character only, so a grammar
|
|
136
|
+
* named `OrderFlow` reads as `orderFlowNode` rather than losing its word break.
|
|
137
|
+
*/
|
|
138
|
+
function builderExportName(namespace: string): string {
|
|
139
|
+
return `${namespace.charAt(0).toLowerCase()}${namespace.slice(1)}Node`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Two grammars whose names differ only in their leading character collapse to
|
|
144
|
+
* one export. Emitting anyway would silently drop a binding, so fail with both
|
|
145
|
+
* names rather than leave the caller to discover it at compile time.
|
|
146
|
+
*/
|
|
147
|
+
function assertDistinctExportNames(bindings: readonly BuilderBinding[]): void {
|
|
148
|
+
const seen = new Map<string, string>();
|
|
149
|
+
for (const binding of bindings) {
|
|
150
|
+
const previous = seen.get(binding.exportName);
|
|
151
|
+
if (previous !== undefined) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Cannot emit the AST builder: grammars '${previous}' and '${binding.namespace}' both map to the export ` +
|
|
154
|
+
`'${binding.exportName}'. Rename one grammar so the generated bindings stay distinct.`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
seen.set(binding.exportName, binding.namespace ?? MERGED_EXPORT_NAME);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Name of the const holding the project's reflection instance.
|
|
163
|
+
*
|
|
164
|
+
* Langium emits `export const reflection = new <LanguageId>AstReflection()`, but
|
|
165
|
+
* the const is located by its initializer rather than by that name so a renamed
|
|
166
|
+
* export still resolves. Falls back to the emitted default when no declaration
|
|
167
|
+
* matches, which keeps the output compiling against a stock Langium file.
|
|
168
|
+
*/
|
|
169
|
+
function findReflectionConstName(astSource: SourceFile, languageId: string): string {
|
|
170
|
+
const reflectionClass = `${languageId}AstReflection`;
|
|
171
|
+
for (const declaration of astSource.getVariableDeclarations()) {
|
|
172
|
+
if (declaration.getInitializer()?.getText().replace(/\s/g, '') === `new${reflectionClass}()`) {
|
|
173
|
+
return declaration.getName();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return 'reflection';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function renderAstBuilder(
|
|
180
|
+
bindings: readonly BuilderBinding[],
|
|
181
|
+
context: { reflectionName: string; importPath: string; regenCommand: string }
|
|
182
|
+
): string {
|
|
183
|
+
const imported = [context.reflectionName, ...bindings.map(binding => `type ${binding.namespace ?? binding.typeMap}`)];
|
|
184
|
+
const out: string[] = [];
|
|
185
|
+
|
|
186
|
+
out.push('/******************************************************************************');
|
|
187
|
+
out.push(' * Generated from the Langium AST — DO NOT EDIT MANUALLY!');
|
|
188
|
+
out.push(` * ${context.regenCommand}`);
|
|
189
|
+
out.push(' ******************************************************************************/');
|
|
190
|
+
out.push('');
|
|
191
|
+
out.push('/* eslint-disable */');
|
|
192
|
+
out.push('');
|
|
193
|
+
out.push("import { makeAstNodeBuilder } from '@hydranium/core';");
|
|
194
|
+
out.push(`import { ${imported.join(', ')} } from '${context.importPath}';`);
|
|
195
|
+
out.push('');
|
|
196
|
+
|
|
197
|
+
// A single-grammar project has nothing to narrow to, so the merged binding is
|
|
198
|
+
// simply THE binding there and must not point at a sibling that was not emitted.
|
|
199
|
+
const narrowed = bindings.some(binding => binding.namespace !== undefined);
|
|
200
|
+
const guarantee = [
|
|
201
|
+
' * Mandatory fields are a type error at the call site, and grammar-declared',
|
|
202
|
+
' * containment arrays are materialised from reflection metadata, so a built',
|
|
203
|
+
' * node carries `[]` where a cast literal would leave `undefined`.'
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
for (const binding of bindings) {
|
|
207
|
+
out.push('/**');
|
|
208
|
+
if (binding.namespace !== undefined) {
|
|
209
|
+
out.push(` * AST-node factory narrowed to the '${binding.namespace}' grammar.`);
|
|
210
|
+
out.push(' *');
|
|
211
|
+
out.push(' * Narrowed by grammar REACHABILITY, not by ownership: a grammar that');
|
|
212
|
+
out.push(" * imports another sees that one's types too, so this binding is only as");
|
|
213
|
+
out.push(` * narrow as '${binding.namespace}'s import closure. Where that closure is the`);
|
|
214
|
+
out.push(' * whole project, it accepts exactly what the merged binding does and the');
|
|
215
|
+
out.push(' * choice is documentation rather than a check.');
|
|
216
|
+
out.push(' *');
|
|
217
|
+
out.push(...guarantee);
|
|
218
|
+
} else if (narrowed) {
|
|
219
|
+
out.push(' * AST-node factory spanning every grammar in this project.');
|
|
220
|
+
out.push(' *');
|
|
221
|
+
out.push(' * Prefer the narrowed binding matching the grammar the call site works in:');
|
|
222
|
+
out.push(" * it rejects a type outside that grammar's import closure, which this one");
|
|
223
|
+
out.push(' * accepts from anywhere. Reach for this one where a call site genuinely');
|
|
224
|
+
out.push(' * spans grammars.');
|
|
225
|
+
} else {
|
|
226
|
+
out.push(" * AST-node factory for this project's grammar.");
|
|
227
|
+
out.push(' *');
|
|
228
|
+
out.push(...guarantee);
|
|
229
|
+
}
|
|
230
|
+
out.push(' */');
|
|
231
|
+
out.push(`export const ${binding.exportName} = makeAstNodeBuilder<${binding.typeMap}>(${context.reflectionName});`);
|
|
232
|
+
out.push('');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return out.join('\n');
|
|
236
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 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 { kebab } from './kebab.js';
|
|
13
|
+
import { type Expression, type ObjectLiteralExpression, type SourceFile, SyntaxKind } from 'ts-morph';
|
|
14
|
+
|
|
15
|
+
/** Inputs for {@link emitLanguages}, resolved by the transfer-model command. */
|
|
16
|
+
export interface EmitLanguagesOptions {
|
|
17
|
+
/** Destination path for the generated languages module. */
|
|
18
|
+
outFile: string;
|
|
19
|
+
/** Header line naming the command that regenerates the file. */
|
|
20
|
+
regenCommand: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface LanguageEntry {
|
|
24
|
+
constName: string;
|
|
25
|
+
languageId: string;
|
|
26
|
+
fileExtensions: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Suffix langium-cli gives each grammar's metadata const in `module.ts`. */
|
|
30
|
+
const METADATA_SUFFIX = 'LanguageMetaData';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Emit a module stating each grammar's language id and file extensions for a
|
|
34
|
+
* package a client can depend on.
|
|
35
|
+
*
|
|
36
|
+
* Read from the `<Grammar>LanguageMetaData` consts langium-cli writes into
|
|
37
|
+
* `module.ts`, which is the only place the ids exist outside `langium-config.json`
|
|
38
|
+
* and which a client cannot import, since it pulls in Langium. Generating the
|
|
39
|
+
* copy instead of writing it by hand is what keeps every host's id list in step
|
|
40
|
+
* with the grammars.
|
|
41
|
+
*/
|
|
42
|
+
export function emitLanguages(moduleSource: SourceFile, options: EmitLanguagesOptions): void {
|
|
43
|
+
const content = buildLanguagesSource(moduleSource, options);
|
|
44
|
+
fs.mkdirSync(path.dirname(options.outFile), { recursive: true });
|
|
45
|
+
if (fs.existsSync(options.outFile) && fs.readFileSync(options.outFile, 'utf-8') === content) {
|
|
46
|
+
console.log('Languages module is up to date.');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
fs.writeFileSync(options.outFile, content, 'utf-8');
|
|
50
|
+
console.log(`Generated: ${options.outFile}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The module text {@link emitLanguages} would write. Pure over its inputs, so
|
|
55
|
+
* the emitted shape is testable without touching a filesystem. Throws when the
|
|
56
|
+
* source declares no metadata, since an empty list would make every host
|
|
57
|
+
* register nothing without an error.
|
|
58
|
+
*/
|
|
59
|
+
export function buildLanguagesSource(moduleSource: SourceFile, options: Pick<EmitLanguagesOptions, 'regenCommand'>): string {
|
|
60
|
+
const entries: LanguageEntry[] = [];
|
|
61
|
+
const grammarsByConst = new Map<string, string>();
|
|
62
|
+
for (const declaration of moduleSource.getVariableDeclarations()) {
|
|
63
|
+
const name = declaration.getName();
|
|
64
|
+
if (!name.endsWith(METADATA_SUFFIX)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const literal = objectLiteralOf(declaration.getInitializer());
|
|
68
|
+
const grammar = name.slice(0, -METADATA_SUFFIX.length);
|
|
69
|
+
const constName = `${kebab(grammar).replace(/-/g, '_').toUpperCase()}_LANGUAGE`;
|
|
70
|
+
const clash = grammarsByConst.get(constName);
|
|
71
|
+
if (clash !== undefined) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Cannot emit the languages module: grammars '${clash}' and '${grammar}' both map to '${constName}'. ` +
|
|
74
|
+
'Rename one grammar so the generated constants stay distinct.'
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
grammarsByConst.set(constName, grammar);
|
|
78
|
+
entries.push({
|
|
79
|
+
constName,
|
|
80
|
+
languageId: stringProperty(literal, name, 'languageId'),
|
|
81
|
+
fileExtensions: stringArrayProperty(literal, name, 'fileExtensions')
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (entries.length === 0) {
|
|
85
|
+
throw new Error(`Cannot emit the languages module: ${moduleSource.getFilePath()} declares no '<Grammar>${METADATA_SUFFIX}'.`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const out: string[] = [];
|
|
89
|
+
out.push('/******************************************************************************');
|
|
90
|
+
out.push(' * Generated from the Langium language metadata — DO NOT EDIT MANUALLY!');
|
|
91
|
+
out.push(` * ${options.regenCommand}`);
|
|
92
|
+
out.push(' ******************************************************************************/');
|
|
93
|
+
out.push('');
|
|
94
|
+
for (const entry of entries) {
|
|
95
|
+
const extensions = entry.fileExtensions.map(extension => JSON.stringify(extension)).join(', ');
|
|
96
|
+
out.push(
|
|
97
|
+
`export const ${entry.constName} = { languageId: ${JSON.stringify(entry.languageId)}, fileExtensions: [${extensions}] } as const;`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
out.push('');
|
|
101
|
+
out.push('/** Every language of the project, in the order langium-cli declares them. */');
|
|
102
|
+
out.push(`export const ALL_LANGUAGES = [${entries.map(entry => entry.constName).join(', ')}] as const;`);
|
|
103
|
+
return out.join('\n') + '\n';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The object literal under `as const` / `satisfies` wrappers, which langium-cli adds. */
|
|
107
|
+
function objectLiteralOf(initializer: Expression | undefined): ObjectLiteralExpression | undefined {
|
|
108
|
+
return (
|
|
109
|
+
initializer?.asKind(SyntaxKind.ObjectLiteralExpression) ?? initializer?.getFirstDescendantByKind(SyntaxKind.ObjectLiteralExpression)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function stringProperty(literal: ObjectLiteralExpression | undefined, owner: string, key: string): string {
|
|
114
|
+
const value = literal?.getProperty(key)?.asKind(SyntaxKind.PropertyAssignment)?.getInitializer()?.asKind(SyntaxKind.StringLiteral);
|
|
115
|
+
if (value === undefined) {
|
|
116
|
+
throw new Error(`Cannot emit the languages module: ${owner} has no string '${key}'.`);
|
|
117
|
+
}
|
|
118
|
+
return value.getLiteralValue();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function stringArrayProperty(literal: ObjectLiteralExpression | undefined, owner: string, key: string): string[] {
|
|
122
|
+
const array = literal
|
|
123
|
+
?.getProperty(key)
|
|
124
|
+
?.asKind(SyntaxKind.PropertyAssignment)
|
|
125
|
+
?.getInitializer()
|
|
126
|
+
?.asKind(SyntaxKind.ArrayLiteralExpression);
|
|
127
|
+
const values = array?.getElements().map(element => element.asKind(SyntaxKind.StringLiteral)?.getLiteralValue());
|
|
128
|
+
if (values === undefined || !values.every((value): value is string => value !== undefined)) {
|
|
129
|
+
throw new Error(`Cannot emit the languages module: ${owner} has no string array '${key}'.`);
|
|
130
|
+
}
|
|
131
|
+
return values;
|
|
132
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 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 { describeError } from '@hydranium/protocol';
|
|
13
|
+
import { relativeImportPath } from './generated-module-path.js';
|
|
14
|
+
|
|
15
|
+
/** Inputs for {@link emitTransferBuilder}, resolved by the transfer-model command. */
|
|
16
|
+
export interface EmitTransferBuilderOptions {
|
|
17
|
+
/** Destination path for the generated builder module. */
|
|
18
|
+
outFile: string;
|
|
19
|
+
/** Path of the generated transfer model the builder binds to. */
|
|
20
|
+
transferModelFile: string;
|
|
21
|
+
/** Language id derived from the `<LanguageId>AstReflection` class name. */
|
|
22
|
+
languageId: string;
|
|
23
|
+
/** Header line naming the command that regenerates the file. */
|
|
24
|
+
regenCommand: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Emit a test module binding `makeTransferNodeBuilder` to the transfer model's
|
|
29
|
+
* reflection and type map, as `transferNode`.
|
|
30
|
+
*
|
|
31
|
+
* The module usually sits in a test tree of the server package while the
|
|
32
|
+
* transfer model sits in a protocol package of its own, and a relative import
|
|
33
|
+
* would reach into that package's sources. Across packages it therefore imports
|
|
34
|
+
* the model by the package's name, which relies on the package's main entry
|
|
35
|
+
* re-exporting the generated model; within one package it imports by path.
|
|
36
|
+
*/
|
|
37
|
+
export function emitTransferBuilder(options: EmitTransferBuilderOptions): void {
|
|
38
|
+
const content = buildTransferBuilderSource(options);
|
|
39
|
+
fs.mkdirSync(path.dirname(options.outFile), { recursive: true });
|
|
40
|
+
if (fs.existsSync(options.outFile) && fs.readFileSync(options.outFile, 'utf-8') === content) {
|
|
41
|
+
console.log('Transfer builder is up to date.');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
fs.writeFileSync(options.outFile, content, 'utf-8');
|
|
45
|
+
console.log(`Generated: ${options.outFile}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The module text {@link emitTransferBuilder} would write. */
|
|
49
|
+
export function buildTransferBuilderSource(options: EmitTransferBuilderOptions): string {
|
|
50
|
+
const modelPackage = owningPackage(options.transferModelFile);
|
|
51
|
+
const specifier =
|
|
52
|
+
modelPackage !== undefined && modelPackage.dir !== owningPackage(options.outFile)?.dir
|
|
53
|
+
? modelPackage.name
|
|
54
|
+
: relativeImportPath(options.outFile, options.transferModelFile);
|
|
55
|
+
const reflection = `${options.languageId}TransferReflection`;
|
|
56
|
+
const typeMap = `${options.languageId}TransferTypes`;
|
|
57
|
+
return [
|
|
58
|
+
'/******************************************************************************',
|
|
59
|
+
' * Generated from the Langium AST — DO NOT EDIT MANUALLY!',
|
|
60
|
+
` * ${options.regenCommand}`,
|
|
61
|
+
' ******************************************************************************/',
|
|
62
|
+
'',
|
|
63
|
+
'/* eslint-disable */',
|
|
64
|
+
'',
|
|
65
|
+
"import { makeTransferNodeBuilder } from '@hydranium/protocol/testing';",
|
|
66
|
+
`import { ${reflection}, type ${typeMap} } from '${specifier}';`,
|
|
67
|
+
'',
|
|
68
|
+
'/**',
|
|
69
|
+
" * Transfer-node factory for this project's grammar, for tests. It fills the",
|
|
70
|
+
' * grammar defaults, so a test names only the fields it cares about;',
|
|
71
|
+
' * `transferNode.partial` fills none and types the node as incomplete.',
|
|
72
|
+
' */',
|
|
73
|
+
`export const transferNode = makeTransferNodeBuilder<${typeMap}, typeof ${reflection}>(${reflection});`,
|
|
74
|
+
''
|
|
75
|
+
].join('\n');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The nearest `package.json` at or above `file` that names a package. */
|
|
79
|
+
function owningPackage(file: string): { dir: string; name: string } | undefined {
|
|
80
|
+
let dir = path.dirname(path.resolve(file));
|
|
81
|
+
for (;;) {
|
|
82
|
+
const manifest = path.join(dir, 'package.json');
|
|
83
|
+
if (fs.existsSync(manifest)) {
|
|
84
|
+
let parsed: unknown;
|
|
85
|
+
try {
|
|
86
|
+
parsed = JSON.parse(fs.readFileSync(manifest, 'utf-8'));
|
|
87
|
+
} catch (error: unknown) {
|
|
88
|
+
throw new Error(`Cannot read '${manifest}' to name the transfer model's package: ${describeError(error)}`);
|
|
89
|
+
}
|
|
90
|
+
const name = typeof parsed === 'object' && parsed !== null && 'name' in parsed ? parsed.name : undefined;
|
|
91
|
+
if (typeof name === 'string') {
|
|
92
|
+
return { dir, name };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const parent = path.dirname(dir);
|
|
96
|
+
if (parent === dir) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
dir = parent;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -29,6 +29,9 @@ export const GENERATE_TRANSFER_MODEL_FLAGS: readonly string[] = [
|
|
|
29
29
|
'--ast-file',
|
|
30
30
|
'--augmentation-file',
|
|
31
31
|
'--out-file',
|
|
32
|
+
'--ast-builder-file',
|
|
33
|
+
'--transfer-builder-file',
|
|
34
|
+
'--languages-file',
|
|
32
35
|
'--element-type-name',
|
|
33
36
|
'--terminals-name',
|
|
34
37
|
'--terminals-source-name',
|
|
@@ -48,11 +51,13 @@ export const GENERATE_TRANSFER_MODEL_VALUE_FLAGS: readonly string[] = GENERATE_T
|
|
|
48
51
|
export const GENERATE_TRANSFER_MODEL_HELP: readonly string[] = [
|
|
49
52
|
'Usage: hydranium-cli generate-transfer-model [options]',
|
|
50
53
|
'',
|
|
51
|
-
'Generate a serializable transfer-model TypeScript file from a Langium AST
|
|
52
|
-
'
|
|
53
|
-
'
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
'Generate a serializable transfer-model TypeScript file from a Langium AST, and',
|
|
55
|
+
'optionally an AST-node builder bound to the same reflection and a transfer-node',
|
|
56
|
+
'builder for tests. The required inputs (--ast-file / --augmentation-file /',
|
|
57
|
+
'--out-file) may instead come from a --config JSON file, and --ast-file can be',
|
|
58
|
+
"auto-discovered from a Langium config's `out` directory. Precedence, highest",
|
|
59
|
+
'first: explicit flags, then the --config file, then the langium-config-derived',
|
|
60
|
+
'--ast-file.',
|
|
56
61
|
'',
|
|
57
62
|
'Options:',
|
|
58
63
|
' --config <path> JSON config supplying any of the options below.',
|
|
@@ -67,6 +72,17 @@ export const GENERATE_TRANSFER_MODEL_HELP: readonly string[] = [
|
|
|
67
72
|
' directory: it treats that directory as exclusively',
|
|
68
73
|
' its own, so every `langium generate` reports this',
|
|
69
74
|
' file as unexpected and offers to delete it.',
|
|
75
|
+
' --ast-builder-file <path> Also emit an AST-node builder module here: one',
|
|
76
|
+
' makeAstNodeBuilder binding per grammar, plus one',
|
|
77
|
+
' spanning all of them. Omit if nothing constructs',
|
|
78
|
+
' AST nodes. Same restriction as --out-file: keep it',
|
|
79
|
+
" out of langium-cli's own `out` directory.",
|
|
80
|
+
' --transfer-builder-file <p> Also emit a test module here binding',
|
|
81
|
+
' makeTransferNodeBuilder to the transfer model as',
|
|
82
|
+
' transferNode. Import it from tests only.',
|
|
83
|
+
" --languages-file <path> Also emit each grammar's language id and file",
|
|
84
|
+
' extensions here, read from the module.ts beside',
|
|
85
|
+
' --ast-file, for a package without Langium.',
|
|
70
86
|
' --element-type-name <name> Base element type name in output. Default: TransferElement.',
|
|
71
87
|
' --terminals-name <name> Terminals const name in output. Default: ModelTerminals.',
|
|
72
88
|
' --terminals-source-name <n> Source-side terminals variable name. Default: <LanguageId>Terminals.',
|
|
@@ -120,6 +136,15 @@ export function parseGenerateOptions(args: string[], onError: UsageError = exitW
|
|
|
120
136
|
case '--out-file':
|
|
121
137
|
flags.outFile = next();
|
|
122
138
|
break;
|
|
139
|
+
case '--ast-builder-file':
|
|
140
|
+
flags.astBuilderFile = next();
|
|
141
|
+
break;
|
|
142
|
+
case '--transfer-builder-file':
|
|
143
|
+
flags.transferBuilderFile = next();
|
|
144
|
+
break;
|
|
145
|
+
case '--languages-file':
|
|
146
|
+
flags.languagesFile = next();
|
|
147
|
+
break;
|
|
123
148
|
case '--element-type-name':
|
|
124
149
|
flags.elementTypeName = next();
|
|
125
150
|
break;
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
|
|
10
10
|
import * as fs from 'node:fs';
|
|
11
11
|
import * as path from 'node:path';
|
|
12
|
+
import { describeError } from '@hydranium/protocol';
|
|
12
13
|
import type { GenerateTransferModelOptions } from './generate-transfer-model.js';
|
|
13
14
|
|
|
14
15
|
/** Path-valued option keys — resolved relative to a config/langium-config file's directory. */
|
|
15
|
-
const PATH_KEYS = ['astFile', 'augmentationFile', 'outFile'] as const;
|
|
16
|
+
const PATH_KEYS = ['astFile', 'augmentationFile', 'outFile', 'astBuilderFile', 'transferBuilderFile', 'languagesFile'] as const;
|
|
16
17
|
|
|
17
18
|
/** Resolve `value` against `baseDir` when it is relative; absolute paths pass through. */
|
|
18
19
|
function resolveAgainst(baseDir: string, value: string): string {
|
|
@@ -31,7 +32,7 @@ export function loadTransferModelConfig(configPath: string): Partial<GenerateTra
|
|
|
31
32
|
try {
|
|
32
33
|
raw = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
33
34
|
} catch (error: unknown) {
|
|
34
|
-
throw new Error(`Cannot read transfer-model config '${configPath}': ${
|
|
35
|
+
throw new Error(`Cannot read transfer-model config '${configPath}': ${describeError(error)}`);
|
|
35
36
|
}
|
|
36
37
|
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
37
38
|
throw new Error(`Transfer-model config '${configPath}' must be a JSON object.`);
|
|
@@ -43,6 +44,9 @@ export function loadTransferModelConfig(configPath: string): Partial<GenerateTra
|
|
|
43
44
|
'astFile',
|
|
44
45
|
'augmentationFile',
|
|
45
46
|
'outFile',
|
|
47
|
+
'astBuilderFile',
|
|
48
|
+
'transferBuilderFile',
|
|
49
|
+
'languagesFile',
|
|
46
50
|
'elementTypeName',
|
|
47
51
|
'terminalsName',
|
|
48
52
|
'terminalsSourceName',
|
|
@@ -74,7 +78,7 @@ export function deriveAstFileFromLangiumConfig(langiumConfigPath: string): strin
|
|
|
74
78
|
try {
|
|
75
79
|
raw = JSON.parse(fs.readFileSync(langiumConfigPath, 'utf-8'));
|
|
76
80
|
} catch (error: unknown) {
|
|
77
|
-
throw new Error(`Cannot read langium-config '${langiumConfigPath}': ${
|
|
81
|
+
throw new Error(`Cannot read langium-config '${langiumConfigPath}': ${describeError(error)}`);
|
|
78
82
|
}
|
|
79
83
|
const out = (raw as { out?: unknown })?.out;
|
|
80
84
|
if (typeof out !== 'string') {
|
|
@@ -118,6 +122,9 @@ export function mergeTransferModelOptions(...sources: Array<Partial<GenerateTran
|
|
|
118
122
|
astFile: astFile!,
|
|
119
123
|
augmentationFile: augmentationFile!,
|
|
120
124
|
outFile: outFile!,
|
|
125
|
+
astBuilderFile: pick('astBuilderFile'),
|
|
126
|
+
transferBuilderFile: pick('transferBuilderFile'),
|
|
127
|
+
languagesFile: pick('languagesFile'),
|
|
121
128
|
elementTypeName: pick('elementTypeName'),
|
|
122
129
|
terminalsName: pick('terminalsName'),
|
|
123
130
|
terminalsSourceName: pick('terminalsSourceName'),
|