Skip to content

Commit

Permalink
fix: monarch is only generated and included if web project us selected
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Jul 8, 2024
1 parent 1067de6 commit 069f622
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
48 changes: 47 additions & 1 deletion packages/generator-langium/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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╷ ╷ ╷ ┌─┬─╮');
Expand Down Expand Up @@ -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',
Expand All @@ -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';

Expand Down Expand Up @@ -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 = [
Expand Down

This file was deleted.

This file was deleted.

6 changes: 4 additions & 2 deletions packages/generator-langium/test/yeoman-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => [
Expand Down Expand Up @@ -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) => [
Expand Down

0 comments on commit 069f622

Please sign in to comment.