From 069f622c1ff6ca4a2ea9cc125ea93c169ee071e9 Mon Sep 17 00:00:00 2001 From: Kai Salmen Date: Mon, 8 Jul 2024 10:05:21 +0200 Subject: [PATCH] fix: monarch is only generated and included if web project us selected --- packages/generator-langium/src/index.ts | 48 ++++++++++++++++++- .../packages/language/langium-config.json | 15 ------ .../templates/packages/language/src/index.ts | 6 --- .../test/yeoman-generator.test.ts | 6 ++- 4 files changed, 51 insertions(+), 24 deletions(-) delete mode 100644 packages/generator-langium/templates/packages/language/langium-config.json delete mode 100644 packages/generator-langium/templates/packages/language/src/index.ts diff --git a/packages/generator-langium/src/index.ts b/packages/generator-langium/src/index.ts index 2a882699a..a4500cb40 100644 --- a/packages/generator-langium/src/index.ts +++ b/packages/generator-langium/src/index.ts @@ -47,6 +47,22 @@ export interface PostAnwers { openWith: 'code' | false } +/** + * This is a sub-set of LangiumConfig from langium-cli. + * We copy this to not introduce a dependency to langium-cli itself. + */ +export interface LangiumLanguageConfigSubset { + id: string + grammar: string + fileExtensions?: string[] + textMate?: { + out: string + } + monarch?: { + out: string + } +} + function printLogo(log: (message: string) => void): void { log('\u001b[36m┌─────┐ ─┐'); log('\u001b[36;1m┌───┐ │ ╶─╮ ┌─╮ ╭─╮ \u001b[36m╷ ╷ ╷ ┌─┬─╮'); @@ -200,7 +216,6 @@ export class LangiumGenerator extends Generator { this.sourceRoot(path.join(__dirname, `${BASE_DIR}/${PACKAGE_LANGUAGE}`)); const languageFiles = [ 'package.json', - 'langium-config.json', 'README.md', 'tsconfig.json', 'tsconfig.src.json', @@ -219,6 +234,26 @@ export class LangiumGenerator extends Generator { ); } + const langiumConfigJson = { + projectName: languageName, + languages: [{ + id: languageId, + grammar: `src/${languageId}.langium`, + fileExtensions: [ fileExtensionGlob ], + textMate: { + out: `syntaxes/${languageId}.tmLanguage.json` + } + } as LangiumLanguageConfigSubset], + out: 'src/generated' + }; + + let languageIndex = `export * from './${languageId}-module.js'; +export * from './${languageId}-validator.js'; +export * from './generated/ast.js'; +export * from './generated/grammar.js'; +export * from './generated/module.js'; +`; + if (this.answers.includeTest) { mainPackageJson.scripts.test = 'npm run --workspace packages/language test'; @@ -278,8 +313,19 @@ export class LangiumGenerator extends Generator { } mainPackageJson.workspaces.push('packages/web'); tsConfigBuildJson.references.push({ path: './packages/web/tsconfig.json' }); + + this.sourceRoot(path.join(__dirname, `${BASE_DIR}/${PACKAGE_LANGUAGE}`)); + langiumConfigJson.languages[0].monarch = { + out: `src/syntaxes/${languageId}.monarch.ts` + }; + + languageIndex = languageIndex?.concat(`\nexport { default as monarchSyntax } from './syntaxes/${languageId}.monarch.js';`); } + // Write language index.ts and langium-config.json after possible alteration from web inclusion + this.fs.write(this._extensionPath('packages/language/src/index.ts'), languageIndex); + this.fs.writeJSON(this._extensionPath('packages/language/langium-config.json'), langiumConfigJson, undefined, 4); + if (this.answers.includeVSCode) { this.sourceRoot(path.join(__dirname, `${BASE_DIR}/${PACKAGE_EXTENSION}`)); const extensionFiles = [ diff --git a/packages/generator-langium/templates/packages/language/langium-config.json b/packages/generator-langium/templates/packages/language/langium-config.json deleted file mode 100644 index 8326d9efe..000000000 --- a/packages/generator-langium/templates/packages/language/langium-config.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "projectName": "<%= LanguageName %>", - "languages": [{ - "id": "<%= language-id %>", - "grammar": "src/<%= language-id %>.langium", - "fileExtensions": <%= file-extension %>, - "textMate": { - "out": "syntaxes/<%= language-id %>.tmLanguage.json" - }, - "monarch": { - "out": "src/syntaxes/<%= language-id %>.monarch.ts" - } - }], - "out": "src/generated" -} diff --git a/packages/generator-langium/templates/packages/language/src/index.ts b/packages/generator-langium/templates/packages/language/src/index.ts deleted file mode 100644 index 32a299ef0..000000000 --- a/packages/generator-langium/templates/packages/language/src/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from './<%= language-id %>-module.js'; -export * from './<%= language-id %>-validator.js'; -export * from './generated/ast.js'; -export * from './generated/grammar.js'; -export * from './generated/module.js'; -export { default as monarchSyntax } from './syntaxes/<%= language-id %>.monarch.js'; diff --git a/packages/generator-langium/test/yeoman-generator.test.ts b/packages/generator-langium/test/yeoman-generator.test.ts index 441448417..4e4eb42ac 100644 --- a/packages/generator-langium/test/yeoman-generator.test.ts +++ b/packages/generator-langium/test/yeoman-generator.test.ts @@ -41,11 +41,11 @@ describe('Check yeoman generator works', () => { targetRoot + '/packages/language/src/hello-world-module.ts', targetRoot + '/packages/language/src/hello-world-validator.ts', targetRoot + '/packages/language/src/hello-world.langium', - targetRoot + '/packages/language/src/syntaxes/hello-world.monarch.ts', targetRoot + '/packages/language/src/generated/ast.ts', targetRoot + '/packages/language/src/generated/grammar.ts', targetRoot + '/packages/language/src/generated/module.ts', - targetRoot + '/packages/language/syntaxes/hello-world.tmLanguage.json' + targetRoot + '/packages/language/syntaxes/hello-world.tmLanguage.json', + targetRoot + '/packages/language/langium-config.json' ]; const filesTest = (targetRoot: string) => [ @@ -80,6 +80,8 @@ describe('Check yeoman generator works', () => { targetRoot + '/packages/web/tsconfig.json', targetRoot + '/packages/web/vite.config.ts', targetRoot + '/packages/web/README.md', + // only present when web project was selected + targetRoot + '/packages/language/src/syntaxes/hello-world.monarch.ts' ]; const filesExtension = (targetRoot: string) => [