langium-tools 0.1.1 → 0.2.0
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
CHANGED
|
@@ -30,7 +30,10 @@
|
|
|
30
30
|
<br />
|
|
31
31
|
<a href="#documentation"><strong>Explore the docs »</strong></a>
|
|
32
32
|
<br />
|
|
33
|
+
<a href="https://borisbrodski.github.io/langium-tools"><strong>Explore the typedocs »</strong></a>
|
|
33
34
|
<br />
|
|
35
|
+
<br />
|
|
36
|
+
|
|
34
37
|
<a href="https://github.com/borisbrodski/langium-tools/issues/new?labels=bug&template=bug-report---.md">Report Bug</a>
|
|
35
38
|
·
|
|
36
39
|
<a href="https://github.com/borisbrodski/langium-tools/issues/new?labels=enhancement&template=feature-request---.md">Request Feature</a>
|
|
@@ -77,10 +80,6 @@
|
|
|
77
80
|
|
|
78
81
|
## About The Project
|
|
79
82
|
|
|
80
|
-
<!--
|
|
81
|
-
[![Product Name Screen Shot][product-screenshot]](https://example.com)
|
|
82
|
-
-->
|
|
83
|
-
|
|
84
83
|
This project is a collection of tools, that should power up DSL development with Langium:
|
|
85
84
|
|
|
86
85
|
Language features:
|
|
@@ -346,9 +345,14 @@ Adding a Target
|
|
|
346
345
|
manager.addTarget({
|
|
347
346
|
name: "CUSTOM_TARGET",
|
|
348
347
|
overwrite: false,
|
|
348
|
+
clean: true
|
|
349
349
|
});
|
|
350
350
|
```
|
|
351
351
|
|
|
352
|
+
- `overwrite`: Default value for `overwrite` option for `createFile`.
|
|
353
|
+
- `clean`: Remove all files not involved in generation. Files with the same content will not be removed and recreated to pervent file timestamps changes and filesystem events.
|
|
354
|
+
|
|
355
|
+
|
|
352
356
|
##### Using Targets
|
|
353
357
|
|
|
354
358
|
Specify the target when creating files:
|
|
@@ -594,7 +598,10 @@ You can customize the behavior by providing custom implementations for optional
|
|
|
594
598
|
#### Example: Custom Validation
|
|
595
599
|
|
|
596
600
|
```typescript
|
|
597
|
-
function customValidateDocuments(
|
|
601
|
+
function customValidateDocuments(
|
|
602
|
+
services: DslServices<MyDslService, MyDslSharedService>,
|
|
603
|
+
documents: LangiumDocument<MyModel>[],
|
|
604
|
+
) {
|
|
598
605
|
documents.forEach((doc) => {
|
|
599
606
|
const issues = getDocumentIssues(doc);
|
|
600
607
|
expect(issues.length).toBe(0);
|
|
@@ -676,7 +683,8 @@ const parsedDocument = await parseWithMarks(
|
|
|
676
683
|
|
|
677
684
|
state [[[UnusedRule]]];
|
|
678
685
|
`,
|
|
679
|
-
"[[[",
|
|
686
|
+
"[[[",
|
|
687
|
+
"]]]",
|
|
680
688
|
);
|
|
681
689
|
```
|
|
682
690
|
|
|
@@ -802,8 +810,8 @@ test("document contains specific issue", async () => {
|
|
|
802
810
|
##### Asserting No Errors
|
|
803
811
|
|
|
804
812
|
```typescript
|
|
805
|
-
test(
|
|
806
|
-
const document = await parse(
|
|
813
|
+
test("document has no errors", async () => {
|
|
814
|
+
const document = await parse("validCode");
|
|
807
815
|
expect(document).toHaveNoErrors();
|
|
808
816
|
});
|
|
809
817
|
```
|
|
@@ -811,25 +819,25 @@ test('document has no errors', async () => {
|
|
|
811
819
|
##### Asserting Specific Issues with Markers
|
|
812
820
|
|
|
813
821
|
```typescript
|
|
814
|
-
test(
|
|
822
|
+
test("document has expected validation issue at marker", async () => {
|
|
815
823
|
const dslCode = adjusted`
|
|
816
824
|
grammar MyLanguage
|
|
817
825
|
entry Rule: name=ID;
|
|
818
826
|
<<<UnusedRule>>>: name=ID;
|
|
819
827
|
terminal ID: /\\^?[_a-zA-Z][\\w_]*/;
|
|
820
828
|
`;
|
|
821
|
-
const parsedDocument = await parseWithMarks(parse, dslCode,
|
|
829
|
+
const parsedDocument = await parseWithMarks(parse, dslCode, "<<<", ">>>");
|
|
822
830
|
|
|
823
831
|
expect(parsedDocument).toHaveDocumentIssues([
|
|
824
832
|
{
|
|
825
833
|
source: DocumentIssueSource.VALIDATION, // Optional
|
|
826
834
|
severity: DocumentIssueSeverity.WARNING, // Optional
|
|
827
|
-
message:
|
|
835
|
+
message: "This rule is declared but never referenced.",
|
|
828
836
|
markerId: 0, // Optional
|
|
829
837
|
},
|
|
830
838
|
]);
|
|
831
839
|
});
|
|
832
|
-
|
|
840
|
+
```
|
|
833
841
|
|
|
834
842
|
##### Asserting Issue Matching Regex
|
|
835
843
|
|
|
@@ -1028,6 +1036,7 @@ generateJavaFile("MyClass", "com.example.project", generatorManager, (imp) => ex
|
|
|
1028
1036
|
##### Explanation:
|
|
1029
1037
|
|
|
1030
1038
|
- **Imports**:
|
|
1039
|
+
|
|
1031
1040
|
- The passed `imp` function is used to import `java.util.Properties`. It returns the appropriate class name (`Properties` in this case), handling imports automatically.
|
|
1032
1041
|
|
|
1033
1042
|
- **Generated Output**:
|
|
@@ -1119,6 +1128,8 @@ Don't forget to give the project a star! Thanks again!
|
|
|
1119
1128
|
|
|
1120
1129
|
4. Open a Pull Request
|
|
1121
1130
|
|
|
1131
|
+
5. Release with release-it
|
|
1132
|
+
|
|
1122
1133
|
### Top contributors:
|
|
1123
1134
|
|
|
1124
1135
|
<a href="https://github.com/borisbrodski/langium-tools/graphs/contributors">
|
|
@@ -1166,6 +1177,5 @@ Project Link: [https://github.com/borisbrodski/langium-tools](https://github.com
|
|
|
1166
1177
|
[issues-url]: https://github.com/borisbrodski/langium-tools/issues
|
|
1167
1178
|
[license-shield]: https://img.shields.io/github/license/borisbrodski/langium-tools.svg?style=for-the-badge
|
|
1168
1179
|
[license-url]: https://github.com/borisbrodski/langium-tools/blob/master/LICENSE.txt
|
|
1169
|
-
[product-screenshot]: images/screenshot.png
|
|
1170
1180
|
[Typescript]: https://shields.io/badge/TypeScript-3178C6?logo=TypeScript&logoColor=FFF&style=flat-square
|
|
1171
1181
|
[Typescript-url]: https://www.typescriptlang.org/
|
package/dist/base/arrays.d.ts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
declare global {
|
|
2
2
|
interface Array<T> {
|
|
3
|
+
/**
|
|
4
|
+
* Sorts an array in place.
|
|
5
|
+
* This method mutates the array and returns a reference to the same array.
|
|
6
|
+
* @param key Function used to determine the order of the elements. It is expected to return
|
|
7
|
+
* a comparable value for this item to sort upon.
|
|
8
|
+
* ```ts
|
|
9
|
+
* [{size: 11, name: 'John'}, {size: 22, name: 'Max'}].sortBy(obj => obj.name)
|
|
10
|
+
* ```
|
|
11
|
+
* @returns this
|
|
12
|
+
*/
|
|
3
13
|
sortBy<K extends keyof T, V>(key: K | ((item: T) => V)): this;
|
|
4
14
|
}
|
|
5
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Sorts an array in place.
|
|
18
|
+
* This method mutates the array and returns a reference to the same array.
|
|
19
|
+
* @param key Function used to determine the order of the elements. It is expected to return
|
|
20
|
+
* a comparable value for this item to sort upon.
|
|
21
|
+
* ```ts
|
|
22
|
+
* [{size: 11, name: 'John'}, {size: 22, name: 'Max'}].sortBy(obj => obj.name)
|
|
23
|
+
* ```
|
|
24
|
+
* @returns list (sorting in place)
|
|
25
|
+
*/
|
|
6
26
|
export declare function arraySortBy<T, K extends keyof T, V>(list: T[], key: K | ((item: T) => V)): T[];
|
package/dist/base/arrays.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
Array.prototype.sortBy = function (key) {
|
|
2
2
|
return arraySortBy(this, key);
|
|
3
3
|
};
|
|
4
|
+
/**
|
|
5
|
+
* Sorts an array in place.
|
|
6
|
+
* This method mutates the array and returns a reference to the same array.
|
|
7
|
+
* @param key Function used to determine the order of the elements. It is expected to return
|
|
8
|
+
* a comparable value for this item to sort upon.
|
|
9
|
+
* ```ts
|
|
10
|
+
* [{size: 11, name: 'John'}, {size: 22, name: 'Max'}].sortBy(obj => obj.name)
|
|
11
|
+
* ```
|
|
12
|
+
* @returns list (sorting in place)
|
|
13
|
+
*/
|
|
4
14
|
export function arraySortBy(list, key) {
|
|
5
15
|
const comparator = typeof key === 'function'
|
|
6
16
|
? key
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AstNode, LangiumDocument, WorkspaceFolder } from 'langium';
|
|
2
2
|
import { type DefaultSharedModuleContext, type LangiumSharedServices } from 'langium/lsp';
|
|
3
3
|
import { GeneratorManager, GeneratedContentManager, GeneratorTarget } from '../generator/generated-content-manager.js';
|
|
4
|
+
import "../base/arrays.js";
|
|
4
5
|
type DslServices<SERVICES, SHARED_SERVICES> = {
|
|
5
6
|
shared: LangiumSharedServices & SHARED_SERVICES;
|
|
6
7
|
} & SERVICES;
|
|
@@ -5,6 +5,7 @@ import fs from 'fs';
|
|
|
5
5
|
import { expect, test } from 'vitest';
|
|
6
6
|
import { getDocumentIssueSummary } from '../base/document-issues.js';
|
|
7
7
|
import { GeneratedContentManager, DEFAULT_TARGET } from '../generator/generated-content-manager.js';
|
|
8
|
+
import "../base/arrays.js";
|
|
8
9
|
export var GeneratorMode;
|
|
9
10
|
(function (GeneratorMode) {
|
|
10
11
|
GeneratorMode[GeneratorMode["Generate"] = 0] = "Generate";
|
|
@@ -166,6 +167,7 @@ function writeMetaData(generatedContent, metadataJsonFile, target) {
|
|
|
166
167
|
generatedContent.getGeneratedContent(target).forEach((contentData, fileName) => {
|
|
167
168
|
metadata.generatedFiles.push({ filename: fileName, overwrite: contentData.overwrite });
|
|
168
169
|
});
|
|
170
|
+
metadata.generatedFiles.sortBy(m => m.filename);
|
|
169
171
|
fs.writeFileSync(metadataJsonFile, JSON.stringify(metadata, null, 2));
|
|
170
172
|
}
|
|
171
173
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langium-tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A set of tools for Langium",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@types/node": "^22.0.0",
|
|
32
32
|
"change-case": "^5.4.4",
|
|
33
33
|
"escape-string-regexp": "^5.0.0",
|
|
34
|
-
"langium": "^3.
|
|
34
|
+
"langium": "^3.4.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"vitest": "~2.0.5"
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@eslint/js": "^9.8.0",
|
|
41
41
|
"globals": "^15.8.0",
|
|
42
|
-
"typedoc": "^0.
|
|
43
|
-
"typedoc-plugin-extras": "^
|
|
44
|
-
"typedoc-plugin-markdown": "^
|
|
42
|
+
"typedoc": "^0.27.0",
|
|
43
|
+
"typedoc-plugin-extras": "^4.0.0",
|
|
44
|
+
"typedoc-plugin-markdown": "^3.0.0",
|
|
45
45
|
"typescript": "^5.5.4",
|
|
46
46
|
"typescript-eslint": "^7.18.0",
|
|
47
47
|
"vitest": "~2.0.5"
|
|
@@ -26,14 +26,18 @@ export type GeneratedContent = Map<
|
|
|
26
26
|
export interface GeneratorTarget {
|
|
27
27
|
/** The name of the target. */
|
|
28
28
|
name: string;
|
|
29
|
+
|
|
29
30
|
/** Default value for the `overwrite` flag for the target. */
|
|
30
31
|
overwrite: boolean;
|
|
32
|
+
|
|
33
|
+
/** Clean directory before generating. All files besides generated ones will be removed. */
|
|
34
|
+
clean: boolean;
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
/**
|
|
34
38
|
* The default target used when no specific target is provided.
|
|
35
39
|
*/
|
|
36
|
-
export const DEFAULT_TARGET: GeneratorTarget = { name: 'DEFAULT', overwrite: true };
|
|
40
|
+
export const DEFAULT_TARGET: GeneratorTarget = { name: 'DEFAULT', overwrite: true, clean: false };
|
|
37
41
|
|
|
38
42
|
/**
|
|
39
43
|
* Options for creating a file.
|
|
@@ -300,12 +304,39 @@ export class GeneratedContentManager {
|
|
|
300
304
|
}
|
|
301
305
|
|
|
302
306
|
/**
|
|
307
|
+
* Recursively lists all files in the given directory.
|
|
308
|
+
*
|
|
309
|
+
* @param dir - The directory to scan for files.
|
|
310
|
+
* @returns A promise resolving to an array of absolute file paths.
|
|
311
|
+
* @private
|
|
312
|
+
*/
|
|
313
|
+
private async listAllFiles(dir: string): Promise<string[]> {
|
|
314
|
+
const entries = await fsPromises.readdir(dir, { withFileTypes: true });
|
|
315
|
+
const files = await Promise.all(
|
|
316
|
+
entries.map(async (entry) => {
|
|
317
|
+
const fullPath = path.join(dir, entry.name);
|
|
318
|
+
if (entry.isDirectory()) {
|
|
319
|
+
return this.listAllFiles(fullPath);
|
|
320
|
+
} else if (entry.isFile()) {
|
|
321
|
+
return [fullPath];
|
|
322
|
+
} else {
|
|
323
|
+
return []; // Skip symbolic links, etc.
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
return files.flat();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
303
331
|
* Writes the generated content for a target to the file system asynchronously.
|
|
304
332
|
*
|
|
305
333
|
* Existing files are only overwritten if the overwrite flag is set (default behavior).
|
|
306
334
|
* If the file already exists and the content is the same, the file is not overwritten,
|
|
307
335
|
* preserving the timestamp and not triggering file system file change events.
|
|
308
336
|
*
|
|
337
|
+
* If the `clean` flag for the target is `true`, files in the output directory that are not
|
|
338
|
+
* part of the generated content will be deleted after writing.
|
|
339
|
+
*
|
|
309
340
|
* @param outputDir - The output directory where the files will be written.
|
|
310
341
|
* @param target - The target name whose content should be written.
|
|
311
342
|
* If not provided, the default target is used.
|
|
@@ -314,6 +345,7 @@ export class GeneratedContentManager {
|
|
|
314
345
|
*/
|
|
315
346
|
async writeToDisk(outputDir: string, target?: string): Promise<void> {
|
|
316
347
|
const generatedContent = this.getGeneratedContent(target);
|
|
348
|
+
const targetConfig = this.#getTarget(target);
|
|
317
349
|
|
|
318
350
|
// Ensure the output directory exists
|
|
319
351
|
try {
|
|
@@ -323,6 +355,22 @@ export class GeneratedContentManager {
|
|
|
323
355
|
throw error;
|
|
324
356
|
}
|
|
325
357
|
|
|
358
|
+
// Collect existing files if cleaning is enabled
|
|
359
|
+
let existingFiles: Set<string> | undefined;
|
|
360
|
+
if (targetConfig.clean) {
|
|
361
|
+
try {
|
|
362
|
+
const files = await this.listAllFiles(outputDir);
|
|
363
|
+
existingFiles = new Set(files);
|
|
364
|
+
} catch (error) {
|
|
365
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
366
|
+
existingFiles = new Set(); // Directory doesn't exist yet
|
|
367
|
+
} else {
|
|
368
|
+
console.error(`Error listing files in "${outputDir}": ${(error as Error).message}`);
|
|
369
|
+
throw error;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
326
374
|
for (const [file, content] of generatedContent) {
|
|
327
375
|
const absoluteFilePath = path.join(outputDir, file);
|
|
328
376
|
const fileDir = path.dirname(absoluteFilePath);
|
|
@@ -335,6 +383,20 @@ export class GeneratedContentManager {
|
|
|
335
383
|
throw error;
|
|
336
384
|
}
|
|
337
385
|
|
|
386
|
+
// If cleaning, check if this generated file exists and remove it from the set
|
|
387
|
+
if (existingFiles) {
|
|
388
|
+
try {
|
|
389
|
+
const realPath = await fsPromises.realpath(absoluteFilePath);
|
|
390
|
+
existingFiles.delete(realPath); // Remove actual OS-reported path
|
|
391
|
+
} catch (error) {
|
|
392
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
|
|
393
|
+
console.error(`Error resolving path "${absoluteFilePath}": ${(error as Error).message}`);
|
|
394
|
+
throw error;
|
|
395
|
+
}
|
|
396
|
+
// File doesn't exist yet, will be created below
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
338
400
|
let fileExists = false;
|
|
339
401
|
let existingContent = '';
|
|
340
402
|
|
|
@@ -364,5 +426,17 @@ export class GeneratedContentManager {
|
|
|
364
426
|
throw error;
|
|
365
427
|
}
|
|
366
428
|
}
|
|
429
|
+
|
|
430
|
+
// Clean up files that weren't generated
|
|
431
|
+
if (targetConfig.clean && existingFiles && existingFiles.size > 0) {
|
|
432
|
+
for (const fileToDelete of existingFiles) {
|
|
433
|
+
try {
|
|
434
|
+
await fsPromises.unlink(fileToDelete);
|
|
435
|
+
} catch (error) {
|
|
436
|
+
console.error(`Error deleting file "${fileToDelete}": ${(error as Error).message}`);
|
|
437
|
+
// Log and continue to avoid stopping on single file failure
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
367
441
|
}
|
|
368
442
|
}
|