-
Notifications
You must be signed in to change notification settings - Fork 81
Add an option to generate a minimal project with Yeoman #1967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JMazat
wants to merge
21
commits into
eclipse-langium:main
Choose a base branch
from
JMazat:add-example-prompt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ff13df2
Add prompt for example code
JMazat 4ecc222
Make test prompt conditional
JMazat 958054f
Don't generate sources when asked
JMazat 6120d35
Fix generation & build
JMazat 1f5ca9a
Add minimal template
JMazat 9c4937f
Add TODOs in minimal template
JMazat 571b894
Add minimal CLI template
JMazat b4f3d67
Add missing files
JMazat 2c164e5
Fix minimal template compilation
JMazat 34c0cee
changement grammaire + lien vers les docs dans le validator
NicolasChusseau 030784e
Merge branch with stash
JMazat 6a2e5c4
Add question for Entry name
JMazat d652154
Add setups & teardown in minimal template
JMazat 1a3ce69
Restore CLI generation in minimal project
JMazat e574f0a
Restore validator in minimal project
JMazat da7f1c9
fix generator
NicolasChusseau 724303d
Implement feedback from client
JMazat a0f5e59
Clean up comments
JMazat 6fab026
Remove JS from the generator
JMazat 8b0b193
Change description of
JMazat 46e0d5c
Input entry name for example project
JMazat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
packages/generator-langium/templates/packages/cli-minimal/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Command-line interface (CLI) | ||
|
||
Check [this part](https://langium.org/docs/learn/minilogo/customizing_cli/) of the Langium Minilogo Tutorial as a useful guide to the CLI. | ||
|
||
## What's in the folder? | ||
|
||
- [package.json](./package.json) - The manifest file of your cli package. | ||
- [tsconfig.src.json](./tsconfig.src.json) - The package specific TypeScript compiler configuration extending the [base config](../../tsconfig.json). | ||
- [tsconfig.json](./tsconfig.json) - TypeScript compiler configuration options required for proper functionality of VSCode. | ||
- [bin/cli.js](bin/cli/cli.js) - Script referenced in the [package.json](./package.json) and used to execute the command-line interface. | ||
- [src/cli/main.ts](src/cli/main.ts) - The entry point of the command line interface (CLI) of your language. | ||
- [src/cli/generator.ts](src/cli/generator.ts) - The code generator used by the CLI to write output files from DSL documents. | ||
- [src/cli/util.ts](src/cli/util.ts) - Utility code for the CLI. | ||
|
||
## Instructions | ||
|
||
Run `node ./bin/cli` to see options for the CLI; `node ./bin/cli generate <file>` generates code for a given DSL file. |
4 changes: 4 additions & 0 deletions
4
packages/generator-langium/templates/packages/cli-minimal/bin/cli.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env node | ||
|
||
import main from '../out/main.js'; | ||
main(); |
32 changes: 32 additions & 0 deletions
32
packages/generator-langium/templates/packages/cli-minimal/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "<%= language-id %>-cli", | ||
"description": "The cli specific package", | ||
"version": "0.0.1", | ||
"type": "module", | ||
"engines": { | ||
"node": ">=20.10.0", | ||
"npm": ">=10.2.3" | ||
}, | ||
"files": [ | ||
"bin", | ||
"out", | ||
"src" | ||
], | ||
"bin": { | ||
"<%= language-id %>-cli": "./bin/cli.js" | ||
}, | ||
"scripts": { | ||
"clean": "shx rm -fr *.tsbuildinfo out", | ||
"build": "echo 'No build step'", | ||
"build:clean": "npm run clean && npm run build" | ||
}, | ||
"dependencies": { | ||
"<%= language-id %>-language": "0.0.1", | ||
"chalk": "~5.3.0", | ||
"commander": "~11.1.0" | ||
}, | ||
"volta": { | ||
"node": "20.19.2", | ||
"npm": "10.8.2" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/generator-langium/templates/packages/cli-minimal/src/generator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { <%= EntryName %> } from '<%= language-id %>-language'; | ||
import { expandToNode, toString } from 'langium/generate'; | ||
import * as fs from 'node:fs'; | ||
import { extractDestinationAndName } from './util.js'; | ||
|
||
export function generateOutput(model: <%= EntryName %>, source: string, destination: string): string { | ||
const data = extractDestinationAndName(destination); | ||
|
||
const fileNode = expandToNode` | ||
// TODO : place here generated code | ||
`.appendNewLineIfNotEmpty(); | ||
|
||
if (!fs.existsSync(data.destination)) { | ||
fs.mkdirSync(data.destination, { recursive: true }); | ||
} | ||
fs.writeFileSync(destination, toString(fileNode)); | ||
return destination; | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/generator-langium/templates/packages/cli-minimal/src/main.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { <%= EntryName %> } from '<%= language-id %>-language'; | ||
import { create<%= LanguageName %>Services, <%= LanguageName %>LanguageMetaData } from '<%= language-id %>-language'; | ||
import chalk from 'chalk'; | ||
import { Command } from 'commander'; | ||
import { extractAstNode } from './util.js'; | ||
import { generateOutput } from './generator.js'; | ||
import { NodeFileSystem } from 'langium/node'; | ||
import * as url from 'node:url'; | ||
import * as fs from 'node:fs/promises'; | ||
import * as path from 'node:path'; | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
const packagePath = path.resolve(__dirname, '..', 'package.json'); | ||
const packageContent = await fs.readFile(packagePath, 'utf-8'); | ||
|
||
export const generateAction = async (source: string, destination: string): Promise<void> => { | ||
const services = create<%= LanguageName %>Services(NodeFileSystem).<%= LanguageName %>; | ||
const model = await extractAstNode<<%= EntryName %>>(source, services); | ||
const generatedFilePath = generateOutput(model, source, destination); | ||
console.log(chalk.green(`Code generated succesfully: ${generatedFilePath}`)); | ||
}; | ||
|
||
export default function(): void { | ||
const program = new Command(); | ||
|
||
program.version(JSON.parse(packageContent).version); | ||
|
||
// TODO: use Program API to declare the CLI | ||
const fileExtensions = <%= LanguageName %>LanguageMetaData.fileExtensions.join(', '); | ||
program | ||
.command('generate') | ||
.argument('<file>', `source file (possible file extensions: ${fileExtensions})`) | ||
.argument('<destination>', 'destination file') | ||
.description('Generates code for a provided source file.') | ||
.action(generateAction); | ||
|
||
program.parse(process.argv); | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/generator-langium/templates/packages/cli-minimal/src/util.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { AstNode, LangiumCoreServices, LangiumDocument } from 'langium'; | ||
import chalk from 'chalk'; | ||
import * as path from 'node:path'; | ||
import * as fs from 'node:fs'; | ||
import { URI } from 'langium'; | ||
|
||
export async function extractDocument(fileName: string, services: LangiumCoreServices): Promise<LangiumDocument> { | ||
const extensions = services.LanguageMetaData.fileExtensions; | ||
if (!extensions.includes(path.extname(fileName))) { | ||
console.error(chalk.yellow(`Please choose a file with one of these extensions: ${extensions}.`)); | ||
process.exit(1); | ||
} | ||
|
||
if (!fs.existsSync(fileName)) { | ||
console.error(chalk.red(`File ${fileName} does not exist.`)); | ||
process.exit(1); | ||
} | ||
|
||
const document = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(path.resolve(fileName))); | ||
await services.shared.workspace.DocumentBuilder.build([document], { validation: true }); | ||
|
||
const validationErrors = (document.diagnostics ?? []).filter(e => e.severity === 1); | ||
if (validationErrors.length > 0) { | ||
console.error(chalk.red('There are validation errors:')); | ||
for (const validationError of validationErrors) { | ||
console.error(chalk.red( | ||
`line ${validationError.range.start.line + 1}: ${validationError.message} [${document.textDocument.getText(validationError.range)}]` | ||
)); | ||
} | ||
process.exit(1); | ||
} | ||
|
||
return document; | ||
} | ||
|
||
export async function extractAstNode<T extends AstNode>(fileName: string, services: LangiumCoreServices): Promise<T> { | ||
return (await extractDocument(fileName, services)).parseResult?.value as T; | ||
} | ||
|
||
interface FilePathData { | ||
destination: string, | ||
name: string | ||
} | ||
|
||
export function extractDestinationAndName(destination: string): FilePathData { | ||
return { | ||
destination: path.dirname(destination), | ||
name: path.basename(destination) | ||
}; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/generator-langium/templates/packages/cli-minimal/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "out", | ||
"declarationDir": "out" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../language/tsconfig.src.json" | ||
} | ||
], | ||
"include": [ | ||
"src/**/*.ts" | ||
] | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...packages/language/src/language-id.langium → .../language-example/src/language-id.langium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.