@simplix-boot/cli 0.0.0-stage → 2.0.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/LICENSE +25 -0
- package/LICENSE_KO +28 -0
- package/README.md +144 -2
- package/bin/simplix-boot.js +4 -0
- package/package.json +44 -4
- package/src/cli.js +103 -0
- package/src/commands/config.js +129 -0
- package/src/commands/generate.js +291 -0
- package/src/commands/generated.js +125 -0
- package/src/commands/init.js +41 -0
- package/src/commands/promote.js +172 -0
- package/src/commands/templates.js +100 -0
- package/src/commands/version.js +134 -0
- package/src/lib/analysis/fields.js +217 -0
- package/src/lib/analysis/imports.js +76 -0
- package/src/lib/analysis/structure.js +193 -0
- package/src/lib/analysis/types.js +94 -0
- package/src/lib/args.js +69 -0
- package/src/lib/comment-extractor.js +141 -0
- package/src/lib/config/fields.js +321 -0
- package/src/lib/config/parents.js +108 -0
- package/src/lib/config/paths.js +156 -0
- package/src/lib/config/yml.js +61 -0
- package/src/lib/console.js +70 -0
- package/src/lib/controller-name.js +33 -0
- package/src/lib/entity-analyzer.js +49 -0
- package/src/lib/entity-type-resolver.js +80 -0
- package/src/lib/entity-type.js +35 -0
- package/src/lib/errors.js +17 -0
- package/src/lib/export-config.js +74 -0
- package/src/lib/file-utils.js +77 -0
- package/src/lib/generate/file-plan.js +155 -0
- package/src/lib/generate/search-fields.js +111 -0
- package/src/lib/generate/settings.js +111 -0
- package/src/lib/generate/support-classes.js +47 -0
- package/src/lib/generate/template-vars.js +183 -0
- package/src/lib/git-utils.js +18 -0
- package/src/lib/name-field.js +42 -0
- package/src/lib/project.js +121 -0
- package/src/lib/promote-files.js +132 -0
- package/src/lib/rendered-output.js +13 -0
- package/src/lib/search-operators.js +87 -0
- package/src/lib/templates.js +50 -0
- package/src/lib/tracked-render.js +200 -0
- package/src/lib/version-check.js +194 -0
- package/src/lib/version-utils.js +89 -0
- package/templates/controller/rest/EntityRestController.java.template +517 -0
- package/templates/controller/web/EntityWebController.java.template +211 -0
- package/templates/dto/EntityDTOs.java.template +981 -0
- package/templates/repository/EntityRepository.java.template +78 -0
- package/templates/service/EntityService.java.template +790 -0
- package/templates/service/EntityTreeService.java.template +352 -0
- package/templates/test/EntityServiceTest.java.template +937 -0
- package/templates/test/EntityTreeServiceTest.java.template +842 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const ANSI = {
|
|
2
|
+
bold: [1, 22],
|
|
3
|
+
dim: [2, 22],
|
|
4
|
+
red: [31, 39],
|
|
5
|
+
green: [32, 39],
|
|
6
|
+
yellow: [33, 39],
|
|
7
|
+
blue: [34, 39],
|
|
8
|
+
cyan: [36, 39],
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether escape sequences are written: never under NO_COLOR, always under FORCE_COLOR, otherwise
|
|
13
|
+
* only to a terminal.
|
|
14
|
+
*/
|
|
15
|
+
function colorEnabled() {
|
|
16
|
+
const { NO_COLOR, FORCE_COLOR, TERM } = process.env;
|
|
17
|
+
|
|
18
|
+
if (NO_COLOR !== undefined && NO_COLOR !== '') return false;
|
|
19
|
+
if (FORCE_COLOR !== undefined) return FORCE_COLOR !== '0' && FORCE_COLOR !== 'false';
|
|
20
|
+
return Boolean(process.stdout.isTTY) && TERM !== 'dumb';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function paint(style, text) {
|
|
24
|
+
if (!colorEnabled()) return String(text);
|
|
25
|
+
const [open, close] = ANSI[style];
|
|
26
|
+
return `\u001b[${open}m${text}\u001b[${close}m`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Text styles, each a function from text to text. */
|
|
30
|
+
export const style = {
|
|
31
|
+
bold: text => paint('bold', text),
|
|
32
|
+
dim: text => paint('dim', text),
|
|
33
|
+
red: text => paint('red', text),
|
|
34
|
+
green: text => paint('green', text),
|
|
35
|
+
yellow: text => paint('yellow', text),
|
|
36
|
+
cyan: text => paint('cyan', text),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** The status marks a message opens with. */
|
|
40
|
+
export const symbols = {
|
|
41
|
+
get info() {
|
|
42
|
+
return paint('blue', 'ℹ');
|
|
43
|
+
},
|
|
44
|
+
get success() {
|
|
45
|
+
return paint('green', '✔');
|
|
46
|
+
},
|
|
47
|
+
get warning() {
|
|
48
|
+
return paint('yellow', '⚠');
|
|
49
|
+
},
|
|
50
|
+
get error() {
|
|
51
|
+
return paint('red', '✖');
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Where a command writes: progress and reports as lines, and a machine-readable result as the one
|
|
57
|
+
* line a script reads.
|
|
58
|
+
*
|
|
59
|
+
* @returns {{ log: (message?: string) => void, result: (text: string) => void }}
|
|
60
|
+
*/
|
|
61
|
+
export function createOutput() {
|
|
62
|
+
return {
|
|
63
|
+
log(message = '') {
|
|
64
|
+
process.stdout.write(`${message}\n`);
|
|
65
|
+
},
|
|
66
|
+
result(text) {
|
|
67
|
+
process.stdout.write(`${text}\n`);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the REST controller class name for an entity.
|
|
4
|
+
*
|
|
5
|
+
* The entity yml may set `controllerName` to name a controller other than
|
|
6
|
+
* `<Entity>RestController` (for example an `Admin<Entity>RestController` whose class
|
|
7
|
+
* name is already part of a published operation id). `generate` and `promote` both
|
|
8
|
+
* read the name through this function, so the file written and the file promoted
|
|
9
|
+
* always agree.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} entityName the entity class name
|
|
12
|
+
* @param {object} [entityConfig] the parsed `.simplix/entity/<Entity>.yml`
|
|
13
|
+
* @returns {string} the controller class name
|
|
14
|
+
* @throws {Error} when `controllerName` is set but is not a Java class name
|
|
15
|
+
*/
|
|
16
|
+
export function resolveControllerName(entityName, entityConfig) {
|
|
17
|
+
if (!entityName) {
|
|
18
|
+
throw new Error('entityName is required to resolve the controller name');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const configured = entityConfig?.controllerName;
|
|
22
|
+
if (configured === undefined || configured === null || configured === '') {
|
|
23
|
+
return `${entityName}RestController`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (typeof configured !== 'string' || !/^[A-Z][A-Za-z0-9_]*$/.test(configured)) {
|
|
27
|
+
throw new CliError(
|
|
28
|
+
`controllerName in ${entityName}.yml must be a Java class name starting with an upper-case letter, got: ${JSON.stringify(configured)}`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return configured;
|
|
33
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { analyzeEmbeddedId, extractIdInfo, extractImplementedInterfaces, extractParentClass } from './analysis/structure.js';
|
|
2
|
+
import { analyzeFields } from './analysis/fields.js';
|
|
3
|
+
import { analyzeImports, processImports } from './analysis/imports.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Analyzes an entity source: its ID, imports, parent class, interfaces and fields.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} entityContent the entity's Java source
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {(fullyQualifiedName: string) => (boolean|undefined)} [options.isEntityType] answers whether
|
|
11
|
+
* a field's type is an `@Entity` (see `createEntityTypeResolver`). A non-basic, non-enum type it
|
|
12
|
+
* answers false for is a plain value (`typeKind: 'value'`); a type it cannot answer for, or every
|
|
13
|
+
* such type when it is absent, is read as a related entity.
|
|
14
|
+
* @returns {object} the analysis
|
|
15
|
+
*/
|
|
16
|
+
export function analyzeEntity(entityContent, { isEntityType } = {}) {
|
|
17
|
+
// Extract ID field information
|
|
18
|
+
const { idType, embeddedIdField, idField } = extractIdInfo(entityContent);
|
|
19
|
+
|
|
20
|
+
// Extract package and imports
|
|
21
|
+
const imports = analyzeImports(entityContent);
|
|
22
|
+
|
|
23
|
+
// Extract parent class information
|
|
24
|
+
const parentClass = extractParentClass(entityContent);
|
|
25
|
+
|
|
26
|
+
// Extract implemented interfaces
|
|
27
|
+
const implementedInterfaces = extractImplementedInterfaces(entityContent);
|
|
28
|
+
|
|
29
|
+
// Analyze regular fields
|
|
30
|
+
const contentLines = entityContent.split('\n');
|
|
31
|
+
const analyzedFields = analyzeFields(contentLines, imports, entityContent, isEntityType);
|
|
32
|
+
|
|
33
|
+
// Analyze embeddedId fields if present
|
|
34
|
+
const embeddedIdFields = analyzeEmbeddedId(entityContent, embeddedIdField, idType);
|
|
35
|
+
|
|
36
|
+
// Process imports for entity references
|
|
37
|
+
const importInfo = processImports(entityContent);
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
fields: analyzedFields,
|
|
41
|
+
idType,
|
|
42
|
+
idField,
|
|
43
|
+
imports: importInfo,
|
|
44
|
+
embeddedIdFields,
|
|
45
|
+
embeddedIdField,
|
|
46
|
+
parentClass,
|
|
47
|
+
implementedInterfaces,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Whether a Java source declares a JPA entity: a line that starts with `@Entity`, bare, with
|
|
6
|
+
* arguments or fully qualified. `@EntityListeners` and a mention inside a comment do not count.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} source the Java source
|
|
9
|
+
* @returns {boolean} true when the source carries the `@Entity` mark
|
|
10
|
+
*/
|
|
11
|
+
export function declaresEntity(source) {
|
|
12
|
+
return /^\s*@(?:jakarta\.persistence\.|javax\.persistence\.)?Entity\b(?![.\w])/m.test(source);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Builds the question the entity analyzer asks of a field's type: is it an `@Entity`?
|
|
17
|
+
*
|
|
18
|
+
* The answer comes from the type's own source under `srcPath`, found by its simple name and
|
|
19
|
+
* matched by its package. A field whose type is not an entity (a record or an embeddable kept in a
|
|
20
|
+
* JSON column, a converted value) is a plain value, not a relation.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} srcPath the directory the entity sources live under
|
|
23
|
+
* @returns {(fullyQualifiedName: string) => (boolean|undefined)} true for an entity, false for a
|
|
24
|
+
* type whose source declares none, undefined when no source in the tree matches the name
|
|
25
|
+
*/
|
|
26
|
+
export function createEntityTypeResolver(srcPath) {
|
|
27
|
+
let sourcesBySimpleName;
|
|
28
|
+
const answers = new Map();
|
|
29
|
+
|
|
30
|
+
const index = () => {
|
|
31
|
+
if (sourcesBySimpleName) return sourcesBySimpleName;
|
|
32
|
+
sourcesBySimpleName = new Map();
|
|
33
|
+
const walk = dir => {
|
|
34
|
+
let entries;
|
|
35
|
+
try {
|
|
36
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') return;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
const full = path.join(dir, entry.name);
|
|
44
|
+
if (entry.isDirectory()) {
|
|
45
|
+
walk(full);
|
|
46
|
+
} else if (entry.isFile() && entry.name.endsWith('.java')) {
|
|
47
|
+
const simpleName = entry.name.slice(0, -'.java'.length);
|
|
48
|
+
const list = sourcesBySimpleName.get(simpleName) ?? [];
|
|
49
|
+
list.push(full);
|
|
50
|
+
sourcesBySimpleName.set(simpleName, list);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
walk(srcPath);
|
|
56
|
+
return sourcesBySimpleName;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return fullyQualifiedName => {
|
|
60
|
+
if (!fullyQualifiedName) return undefined;
|
|
61
|
+
if (answers.has(fullyQualifiedName)) return answers.get(fullyQualifiedName);
|
|
62
|
+
|
|
63
|
+
const dot = fullyQualifiedName.lastIndexOf('.');
|
|
64
|
+
const simpleName = fullyQualifiedName.slice(dot + 1);
|
|
65
|
+
const packageName = dot < 0 ? '' : fullyQualifiedName.slice(0, dot);
|
|
66
|
+
let answer;
|
|
67
|
+
|
|
68
|
+
for (const file of index().get(simpleName) ?? []) {
|
|
69
|
+
const source = fs.readFileSync(file, 'utf8');
|
|
70
|
+
const declared = source.match(/^\s*package\s+([\w.]+)\s*;/m)?.[1] ?? '';
|
|
71
|
+
if (declared === packageName) {
|
|
72
|
+
answer = declaresEntity(source);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
answers.set(fullyQualifiedName, answer);
|
|
78
|
+
return answer;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the JPA entity class a generator input describes.
|
|
4
|
+
*
|
|
5
|
+
* The argument given to `generate` and `promote` names the yml (`.simplix/entity/<Name>.yml`) and
|
|
6
|
+
* the prefix of every generated class (`<Name>Service`, `<Name>DTOs`, `<Name>ListDTO`). The yml's
|
|
7
|
+
* `entity` key names the entity class those classes are built over. The two are the same for the
|
|
8
|
+
* usual one surface per entity; they differ when a second surface is generated over an entity that
|
|
9
|
+
* already has one, for example a self-scoped `MyOrder.yml` with `entity: Order` beside `Order.yml`.
|
|
10
|
+
* The prefix keeps the second surface's classes, bean names, DTO names and API tag apart from the
|
|
11
|
+
* first one's.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} name the generator argument, which is the yml's file name
|
|
14
|
+
* @param {object} [entityConfig] the parsed yml
|
|
15
|
+
* @returns {string} the entity class name
|
|
16
|
+
* @throws {Error} when `entity` is set but is not a Java class name
|
|
17
|
+
*/
|
|
18
|
+
export function resolveEntityType(name, entityConfig) {
|
|
19
|
+
if (!name) {
|
|
20
|
+
throw new Error('the entity name is required to resolve the entity class');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const configured = entityConfig?.entity;
|
|
24
|
+
if (configured === undefined || configured === null || configured === '') {
|
|
25
|
+
return name;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof configured !== 'string' || !/^[A-Z][A-Za-z0-9_]*$/.test(configured)) {
|
|
29
|
+
throw new CliError(
|
|
30
|
+
`entity in ${name}.yml must be a Java class name starting with an upper-case letter, got: ${JSON.stringify(configured)}`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return configured;
|
|
35
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A failure the person running the command can act on: a missing file, a bad option, a
|
|
3
|
+
* configuration the command cannot use. The command line prints its message and exits with its
|
|
4
|
+
* code; any other error is a defect and surfaces with its stack.
|
|
5
|
+
*/
|
|
6
|
+
export class CliError extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} message what went wrong and what to do about it
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {number} [options.exitCode] the process exit code
|
|
11
|
+
*/
|
|
12
|
+
constructor(message, { exitCode = 1 } = {}) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'CliError';
|
|
15
|
+
this.exitCode = exitCode;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { CliError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the opt-in list export an entity yml asks for.
|
|
4
|
+
*
|
|
5
|
+
* The export endpoint is generated only when the yml sets `export`, either `true` or an
|
|
6
|
+
* object. An entity that sets nothing gets no export endpoint. The rows written are the
|
|
7
|
+
* list DTO unless `export.dto` names an inner class of the entity's DTO container, which
|
|
8
|
+
* is then built from each list row through its static `of(<ListDTO>)` factory.
|
|
9
|
+
*
|
|
10
|
+
* ```yaml
|
|
11
|
+
* export: true # list DTO, file named after the path
|
|
12
|
+
* export:
|
|
13
|
+
* dto: AuditEventExportDTO # rows mapped through AuditEventExportDTO.of(row)
|
|
14
|
+
* baseName: audit-events # file name without extension
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param {string} classPrefix the class name prefix of the generated classes
|
|
18
|
+
* @param {object} [entityConfig] the parsed `.simplix/entity/<Entity>.yml`
|
|
19
|
+
* @param {string} templatePath the request path the controller is mapped under
|
|
20
|
+
* @returns {{enabled: false} | {enabled: true, dto: string, mapper: string, baseName: string}}
|
|
21
|
+
* @throws {Error} when `export` or one of its keys has the wrong shape
|
|
22
|
+
*/
|
|
23
|
+
export function resolveExportConfig(classPrefix, entityConfig, templatePath) {
|
|
24
|
+
if (!classPrefix) {
|
|
25
|
+
throw new Error('classPrefix is required to resolve the export');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const configured = entityConfig?.export;
|
|
29
|
+
if (configured === undefined || configured === null || configured === false) {
|
|
30
|
+
return { enabled: false };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const fileName = `${classPrefix}.yml`;
|
|
34
|
+
let options;
|
|
35
|
+
if (configured === true) {
|
|
36
|
+
options = {};
|
|
37
|
+
} else if (typeof configured === 'object' && !Array.isArray(configured)) {
|
|
38
|
+
options = configured;
|
|
39
|
+
} else {
|
|
40
|
+
throw new CliError(`export in ${fileName} must be true or an object, got: ${JSON.stringify(configured)}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const unknown = Object.keys(options).filter(key => key !== 'dto' && key !== 'baseName');
|
|
44
|
+
if (unknown.length > 0) {
|
|
45
|
+
throw new CliError(`export in ${fileName} has unknown keys: ${unknown.join(', ')} (allowed: dto, baseName)`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (options.dto !== undefined && (typeof options.dto !== 'string' || !/^[A-Z][A-Za-z0-9_]*$/.test(options.dto))) {
|
|
49
|
+
throw new CliError(`export.dto in ${fileName} must be a Java class name, got: ${JSON.stringify(options.dto)}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (
|
|
53
|
+
options.baseName !== undefined &&
|
|
54
|
+
(typeof options.baseName !== 'string' || !/^[A-Za-z0-9._-]+$/.test(options.baseName))
|
|
55
|
+
) {
|
|
56
|
+
throw new CliError(
|
|
57
|
+
`export.baseName in ${fileName} must be a file name of letters, digits, dots, dashes or underscores, got: ${JSON.stringify(options.baseName)}`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const listDto = `${classPrefix}ListDTO`;
|
|
62
|
+
const dto = options.dto || listDto;
|
|
63
|
+
const segments = String(templatePath || '')
|
|
64
|
+
.split('/')
|
|
65
|
+
.filter(Boolean);
|
|
66
|
+
const baseName = options.baseName || segments[segments.length - 1] || classPrefix;
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
enabled: true,
|
|
70
|
+
dto,
|
|
71
|
+
mapper: dto === listDto ? '' : `.map(${dto}::of)`,
|
|
72
|
+
baseName,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { CliError } from './errors.js';
|
|
4
|
+
|
|
5
|
+
/** The files that mark the root of a Gradle or Maven build. */
|
|
6
|
+
const BUILD_FILES = ['build.gradle', 'build.gradle.kts', 'pom.xml'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Finds a Java source by its simple class name anywhere under a directory.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} srcPath the directory to search
|
|
12
|
+
* @param {string} className the simple class name, without `.java`
|
|
13
|
+
* @returns {string | null} the first matching file, or null when the directory holds none
|
|
14
|
+
*/
|
|
15
|
+
export function findEntityFile(srcPath, className) {
|
|
16
|
+
if (!srcPath) return null;
|
|
17
|
+
|
|
18
|
+
let entries;
|
|
19
|
+
try {
|
|
20
|
+
entries = fs.readdirSync(srcPath, { withFileTypes: true });
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') return null;
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
const entryPath = path.join(srcPath, entry.name);
|
|
28
|
+
if (isDirectory(entry, entryPath)) {
|
|
29
|
+
const found = findEntityFile(entryPath, className);
|
|
30
|
+
if (found) return found;
|
|
31
|
+
} else if (entry.name === `${className}.java`) {
|
|
32
|
+
return entryPath;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isDirectory(entry, entryPath) {
|
|
40
|
+
if (entry.isDirectory()) return true;
|
|
41
|
+
if (!entry.isSymbolicLink()) return false;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
return fs.statSync(entryPath).isDirectory();
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (error.code === 'ENOENT') return false;
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks that the entity sources can be read: the scan path is a directory, and the project root
|
|
53
|
+
* or a directory above it holds a Gradle or Maven build file.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} srcPath the directory the entity sources live under
|
|
56
|
+
* @param {string} root the project root
|
|
57
|
+
* @throws {CliError} when the scan path is missing or no build file is found
|
|
58
|
+
*/
|
|
59
|
+
export function validateProjectStructure(srcPath, root) {
|
|
60
|
+
if (!fs.existsSync(srcPath)) {
|
|
61
|
+
throw new CliError(`Invalid project structure. Expected path: ${srcPath}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!fs.statSync(srcPath).isDirectory()) {
|
|
65
|
+
throw new CliError(`Invalid project structure. Not a directory: ${srcPath}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let current = root;
|
|
69
|
+
for (;;) {
|
|
70
|
+
if (BUILD_FILES.some(name => fs.existsSync(path.join(current, name)))) return;
|
|
71
|
+
const parent = path.dirname(current);
|
|
72
|
+
if (parent === current) break;
|
|
73
|
+
current = parent;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
throw new CliError(`No build file found in ${root}. Expected one of: ${BUILD_FILES.join(', ')}`);
|
|
77
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { resolveControllerName } from '../controller-name.js';
|
|
3
|
+
import { locateTemplate, projectTemplateDir } from '../templates.js';
|
|
4
|
+
|
|
5
|
+
const COMPONENT_TYPES = ['repository', 'service', 'controllerRest', 'controllerWeb', 'dto'];
|
|
6
|
+
|
|
7
|
+
const FILE_SUFFIXES = {
|
|
8
|
+
repository: 'Repository',
|
|
9
|
+
service: 'Service',
|
|
10
|
+
controllerRest: 'RestController',
|
|
11
|
+
controllerWeb: 'Controller',
|
|
12
|
+
dto: 'DTOs',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** The template each component is rendered from unless the project configuration maps another. */
|
|
16
|
+
const DEFAULT_TEMPLATES = {
|
|
17
|
+
repository: 'repository/EntityRepository.java.template',
|
|
18
|
+
service: 'service/EntityService.java.template',
|
|
19
|
+
controllerRest: 'controller/rest/EntityRestController.java.template',
|
|
20
|
+
controllerWeb: 'controller/web/EntityWebController.java.template',
|
|
21
|
+
dto: 'dto/EntityDTOs.java.template',
|
|
22
|
+
serviceTest: 'test/EntityServiceTest.java.template',
|
|
23
|
+
restControllerTest: 'test/EntityRestControllerTest.java.template',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const TEST_TYPES = ['serviceTest', 'restControllerTest'];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The components no template ships for: a project that wants one provides its template, and a
|
|
30
|
+
* project that does not is not told on every run that it is missing.
|
|
31
|
+
*/
|
|
32
|
+
const OPTIONAL_TYPES = ['restControllerTest'];
|
|
33
|
+
|
|
34
|
+
const TEST_SUFFIXES = {
|
|
35
|
+
serviceTest: 'ServiceTest',
|
|
36
|
+
restControllerTest: 'RestControllerTest',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** The template a tree entity's service is rendered from, and its test. */
|
|
40
|
+
const TREE_SERVICE_TEMPLATE = 'service/EntityTreeService.java.template';
|
|
41
|
+
const TREE_SERVICE_TEST_TEMPLATE = 'test/EntityTreeServiceTest.java.template';
|
|
42
|
+
|
|
43
|
+
function entry(root, templateFile, targetPath) {
|
|
44
|
+
return {
|
|
45
|
+
templateFile,
|
|
46
|
+
template: locateTemplate(root, templateFile),
|
|
47
|
+
// Where the project would put its own copy, which is what a "not found" message points at.
|
|
48
|
+
projectTemplate: path.join(projectTemplateDir(root), templateFile),
|
|
49
|
+
path: targetPath,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function testBasePathOf(settings) {
|
|
54
|
+
return settings.testBasePath || settings.baseTargetPath.replace('/main/', '/test/');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Lists the files `generate` writes for one entity.
|
|
59
|
+
*
|
|
60
|
+
* <p>A component is generated when the project configuration gives it a target path. The files
|
|
61
|
+
* that will be written come back under `files`, and the ones dropped for want of a template
|
|
62
|
+
* under `missingTemplates`, so a file whose template does not exist is never announced as
|
|
63
|
+
* generated and then skipped a line later.
|
|
64
|
+
*
|
|
65
|
+
* @param {object} plan
|
|
66
|
+
* @param {string} plan.root the project root
|
|
67
|
+
* @param {object} plan.settings the resolved generate settings
|
|
68
|
+
* @param {string} plan.ymlName the entity yml's name, which prefixes the generated classes
|
|
69
|
+
* @param {object} plan.entityConfig the parsed entity yml
|
|
70
|
+
* @param {string} plan.modulePath the yml's module path
|
|
71
|
+
* @param {boolean} plan.withTests whether test files are generated
|
|
72
|
+
* @returns {{ files: object[], missingTemplates: object[] }}
|
|
73
|
+
*/
|
|
74
|
+
export function buildFileGenerationList({ root, settings, ymlName, entityConfig, modulePath, withTests }) {
|
|
75
|
+
const { targetPaths, templateMapping, baseTargetPath } = settings;
|
|
76
|
+
const files = [];
|
|
77
|
+
const missingTemplates = [];
|
|
78
|
+
|
|
79
|
+
const addFile = (type, targetPath) => {
|
|
80
|
+
const planned = entry(root, templateMapping[type] || DEFAULT_TEMPLATES[type], targetPath);
|
|
81
|
+
if (planned.template) files.push(planned);
|
|
82
|
+
else if (!OPTIONAL_TYPES.includes(type)) missingTemplates.push(planned);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
COMPONENT_TYPES.filter(type => targetPaths[type]).forEach(type => {
|
|
86
|
+
addFile(
|
|
87
|
+
type,
|
|
88
|
+
path.join(
|
|
89
|
+
root,
|
|
90
|
+
baseTargetPath,
|
|
91
|
+
targetPaths[type].replace('{modulePath}', modulePath),
|
|
92
|
+
type === 'controllerRest'
|
|
93
|
+
? `${resolveControllerName(ymlName, entityConfig)}.java`
|
|
94
|
+
: `${ymlName}${FILE_SUFFIXES[type]}.java`
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (withTests) {
|
|
100
|
+
TEST_TYPES.forEach(type => {
|
|
101
|
+
let testTargetPath = '';
|
|
102
|
+
if (type === 'serviceTest' && targetPaths.service) {
|
|
103
|
+
testTargetPath = targetPaths.service;
|
|
104
|
+
} else if (type === 'restControllerTest' && targetPaths.controllerRest) {
|
|
105
|
+
testTargetPath = targetPaths.controllerRest;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (testTargetPath) {
|
|
109
|
+
addFile(
|
|
110
|
+
type,
|
|
111
|
+
path.join(
|
|
112
|
+
root,
|
|
113
|
+
testBasePathOf(settings),
|
|
114
|
+
testTargetPath.replace('{modulePath}', modulePath),
|
|
115
|
+
type === 'restControllerTest'
|
|
116
|
+
? `${resolveControllerName(ymlName, entityConfig)}Test.java`
|
|
117
|
+
: `${ymlName}${TEST_SUFFIXES[type]}.java`
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { files, missingTemplates };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The files added for an entity that implements `TreeEntity`: its tree service, and that
|
|
129
|
+
* service's test when a template for it exists.
|
|
130
|
+
*
|
|
131
|
+
* @param {object} plan
|
|
132
|
+
* @param {string} plan.root the project root
|
|
133
|
+
* @param {object} plan.settings the resolved generate settings
|
|
134
|
+
* @param {string} plan.ymlName the entity yml's name
|
|
135
|
+
* @param {string} plan.modulePath the yml's module path
|
|
136
|
+
* @returns {object[]}
|
|
137
|
+
*/
|
|
138
|
+
export function treeServiceFiles({ root, settings, ymlName, modulePath }) {
|
|
139
|
+
const { targetPaths, baseTargetPath } = settings;
|
|
140
|
+
if (!targetPaths.service) return [];
|
|
141
|
+
|
|
142
|
+
const serviceDir = targetPaths.service.replace('{modulePath}', modulePath);
|
|
143
|
+
const files = [
|
|
144
|
+
entry(root, TREE_SERVICE_TEMPLATE, path.join(root, baseTargetPath, serviceDir, `${ymlName}TreeService.java`)),
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
const test = entry(
|
|
148
|
+
root,
|
|
149
|
+
TREE_SERVICE_TEST_TEMPLATE,
|
|
150
|
+
path.join(root, testBasePathOf(settings), serviceDir, `${ymlName}TreeServiceTest.java`)
|
|
151
|
+
);
|
|
152
|
+
if (test.template) files.push(test);
|
|
153
|
+
|
|
154
|
+
return files;
|
|
155
|
+
}
|