@simplix-boot/cli 0.0.0-stage → 1.3.1
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.ko.md +82 -0
- package/README.md +81 -2
- package/bin/simplix-boot.js +6 -0
- package/package.json +45 -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 +83 -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 +195 -0
- package/src/lib/version-utils.js +97 -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,321 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { analyzeEntity } from '../entity-analyzer.js';
|
|
4
|
+
import { findEntityFile } from '../file-utils.js';
|
|
5
|
+
import { findNameField } from '../name-field.js';
|
|
6
|
+
import { defaultSearchOperators } from '../search-operators.js';
|
|
7
|
+
import { analyzeReferenceParent } from './parents.js';
|
|
8
|
+
import { getBaseEntityPath } from './paths.js';
|
|
9
|
+
|
|
10
|
+
export function processEmbeddedIdFields(embeddedIdFields, config) {
|
|
11
|
+
if (!embeddedIdFields || !Array.isArray(embeddedIdFields)) return;
|
|
12
|
+
|
|
13
|
+
embeddedIdFields.forEach(field => {
|
|
14
|
+
const isSortableType =
|
|
15
|
+
field.type === 'String' || field.type === 'Integer' || field.type === 'Long' || field.type === 'UUID';
|
|
16
|
+
|
|
17
|
+
const fieldConfig = {
|
|
18
|
+
sortable: isSortableType,
|
|
19
|
+
views: ['list', 'detail', 'edit', 'batchUpdate'],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
if (field.typeKind === 'basic') {
|
|
23
|
+
fieldConfig.searchOperators = ['equals'];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
config.fields[`${field.parentField}.${field.name}`] = fieldConfig;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build a map of FK ID fields that reference specific entities.
|
|
32
|
+
*/
|
|
33
|
+
export function buildFkIdFieldMap(fields) {
|
|
34
|
+
const fkToEntityMap = new Map();
|
|
35
|
+
const idToEntityMap = new Map();
|
|
36
|
+
|
|
37
|
+
if (!Array.isArray(fields)) {
|
|
38
|
+
return { fkToEntityMap, idToEntityMap };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fields.forEach(field => {
|
|
42
|
+
if (field.typeKind === 'basic' && field.type === 'String') {
|
|
43
|
+
const possibleEntityFieldName = field.name.replace(/Id$/, '');
|
|
44
|
+
|
|
45
|
+
if (possibleEntityFieldName !== field.name) {
|
|
46
|
+
const entityField = fields.find(f => f.name === possibleEntityFieldName && f.typeKind === 'entity');
|
|
47
|
+
|
|
48
|
+
if (entityField) {
|
|
49
|
+
const entityType = entityField.actualType || entityField.type;
|
|
50
|
+
fkToEntityMap.set(`${entityType}:${possibleEntityFieldName}`, field.name);
|
|
51
|
+
idToEntityMap.set(field.name, { entityType, entityField });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return { fkToEntityMap, idToEntityMap };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function processRegularField(
|
|
61
|
+
context,
|
|
62
|
+
field,
|
|
63
|
+
config,
|
|
64
|
+
idFieldName,
|
|
65
|
+
embeddedIdFields,
|
|
66
|
+
fkFieldMaps = { fkToEntityMap: new Map(), idToEntityMap: new Map() }
|
|
67
|
+
) {
|
|
68
|
+
if (!config || !config.fields) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (embeddedIdFields && embeddedIdFields.some(ef => ef.name === field.name)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const embeddedIdFieldNames = Object.keys(config.fields)
|
|
77
|
+
.filter(key => key.includes('.'))
|
|
78
|
+
.map(key => key.split('.')[0]);
|
|
79
|
+
|
|
80
|
+
if (embeddedIdFieldNames.includes(field.name)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const fieldConfig = createFieldConfig(field, idFieldName);
|
|
85
|
+
|
|
86
|
+
if (!fieldConfig) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
addFieldMetadata(field, fieldConfig);
|
|
91
|
+
setSearchOperators(field, fieldConfig, idFieldName);
|
|
92
|
+
|
|
93
|
+
const { fkToEntityMap, idToEntityMap } = fkFieldMaps;
|
|
94
|
+
|
|
95
|
+
if (field.typeKind === 'entity') {
|
|
96
|
+
const entityType = field.actualType || field.type;
|
|
97
|
+
const fkIdFieldKey = `${entityType}:${field.name}`;
|
|
98
|
+
const hasFkIdField = fkToEntityMap.has(fkIdFieldKey);
|
|
99
|
+
|
|
100
|
+
if (hasFkIdField) {
|
|
101
|
+
fieldConfig.sortable = false;
|
|
102
|
+
fieldConfig.views = ['list', 'detail'];
|
|
103
|
+
fieldConfig.searchOperators = [];
|
|
104
|
+
} else {
|
|
105
|
+
processEntityReference(context, field, fieldConfig);
|
|
106
|
+
}
|
|
107
|
+
} else if (field.typeKind === 'map' || field.isMap) {
|
|
108
|
+
processMapField(field, fieldConfig);
|
|
109
|
+
} else if (field.typeKind === 'value') {
|
|
110
|
+
processValueField(field, fieldConfig);
|
|
111
|
+
} else if (idToEntityMap.has(field.name)) {
|
|
112
|
+
const { entityType, entityField } = idToEntityMap.get(field.name);
|
|
113
|
+
processFkIdFieldReference(context, field, fieldConfig, entityType, entityField);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
config.fields[field.name] = fieldConfig;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function processFkIdFieldReference(context, field, fieldConfig, entityType, _entityField) {
|
|
120
|
+
if (!fieldConfig) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
fieldConfig.searchOperators = ['equals', 'in'];
|
|
125
|
+
fieldConfig.reference = {
|
|
126
|
+
entity: entityType,
|
|
127
|
+
idField: field.name,
|
|
128
|
+
idType: field.type,
|
|
129
|
+
nameField: '<__MODIFY__>',
|
|
130
|
+
multiple: false,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const baseEntityPath = getBaseEntityPath(context);
|
|
134
|
+
const srcPath = path.join(context.root, baseEntityPath);
|
|
135
|
+
const referenceEntityPath = findEntityFile(srcPath, entityType);
|
|
136
|
+
|
|
137
|
+
if (referenceEntityPath) {
|
|
138
|
+
updateFkIdReferenceInfo(context, referenceEntityPath, fieldConfig, baseEntityPath, srcPath);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function updateFkIdReferenceInfo(context, referenceEntityPath, fieldConfig, baseEntityPath, srcPath) {
|
|
143
|
+
if (!referenceEntityPath || !fs.existsSync(referenceEntityPath)) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const referenceEntityContent = fs.readFileSync(referenceEntityPath, 'utf8');
|
|
148
|
+
const {
|
|
149
|
+
idField: referenceIdField,
|
|
150
|
+
fields: referenceFields,
|
|
151
|
+
parentClass: referenceParentClass,
|
|
152
|
+
} = analyzeEntity(referenceEntityContent);
|
|
153
|
+
|
|
154
|
+
let actualReferenceIdField = referenceIdField;
|
|
155
|
+
|
|
156
|
+
if (!actualReferenceIdField && referenceParentClass && referenceParentClass.importPath) {
|
|
157
|
+
const parentInfo = analyzeReferenceParent(context, referenceParentClass, baseEntityPath, srcPath, referenceFields);
|
|
158
|
+
actualReferenceIdField = parentInfo.actualReferenceIdField;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
actualReferenceIdField = actualReferenceIdField || 'id';
|
|
162
|
+
fieldConfig.reference.nameField = findNameField(referenceFields, actualReferenceIdField);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function createFieldConfig(field, idFieldName) {
|
|
166
|
+
return {
|
|
167
|
+
sortable: isSortableField(field),
|
|
168
|
+
views: getFieldViews(field, idFieldName),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// A boolean is filtered, never sorted (isSortableSearchType in utils/search-operators.js says why).
|
|
173
|
+
export function isSortableField(field) {
|
|
174
|
+
return (
|
|
175
|
+
field.type === 'String' ||
|
|
176
|
+
field.type === 'Integer' ||
|
|
177
|
+
field.type === 'Long' ||
|
|
178
|
+
field.type === 'Double' ||
|
|
179
|
+
field.type === 'BigDecimal' ||
|
|
180
|
+
field.type === 'LocalDate' ||
|
|
181
|
+
field.type === 'LocalDateTime' ||
|
|
182
|
+
field.type === 'ZonedDateTime' ||
|
|
183
|
+
field.type === 'Instant' ||
|
|
184
|
+
field.typeKind === 'enum'
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function getFieldViews(field, idFieldName) {
|
|
189
|
+
if (field.name === idFieldName) {
|
|
190
|
+
return ['list', 'detail', 'edit'];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (/Count$/i.test(field.name) || field.name === 'version') {
|
|
194
|
+
return ['list', 'detail'];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (field.type === 'String' || /sort|order/i.test(field.name)) {
|
|
198
|
+
return ['list', 'detail', 'edit'];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return ['list', 'detail', 'edit', 'batchUpdate'];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function addFieldMetadata(field, fieldConfig) {
|
|
205
|
+
if (field.columnInfo) {
|
|
206
|
+
if (field.columnInfo.nullable !== undefined) {
|
|
207
|
+
fieldConfig.required = !field.columnInfo.nullable;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (field.columnInfo.unique) {
|
|
211
|
+
fieldConfig.unique = true;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (field.columnInfo.length) {
|
|
215
|
+
fieldConfig.maxLength = field.columnInfo.length;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (field.isNaturalId) fieldConfig.naturalId = true;
|
|
220
|
+
if (field.isLob) fieldConfig.largeText = true;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function setSearchOperators(field, fieldConfig, idFieldName) {
|
|
224
|
+
if (field.typeKind === 'basic') {
|
|
225
|
+
if (field.name === idFieldName) {
|
|
226
|
+
fieldConfig.searchOperators = ['equals'];
|
|
227
|
+
} else {
|
|
228
|
+
fieldConfig.searchOperators = getSearchOperatorsForType(field.type);
|
|
229
|
+
}
|
|
230
|
+
} else if (field.typeKind === 'enum') {
|
|
231
|
+
fieldConfig.searchOperators = ['equals', 'in'];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function getSearchOperatorsForType(type) {
|
|
236
|
+
return defaultSearchOperators(type);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function processMapField(field, fieldConfig) {
|
|
240
|
+
if (!fieldConfig) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
fieldConfig.sortable = false;
|
|
245
|
+
fieldConfig.searchOperators = [];
|
|
246
|
+
fieldConfig.views = ['detail', 'edit'];
|
|
247
|
+
fieldConfig.type = `Map<String, String>`;
|
|
248
|
+
delete fieldConfig.maxLength;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** A value kept in the row (a record or an embeddable in a JSON column): read and written whole, never searched. */
|
|
252
|
+
export function processValueField(field, fieldConfig) {
|
|
253
|
+
if (!fieldConfig) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
fieldConfig.sortable = false;
|
|
258
|
+
fieldConfig.searchOperators = [];
|
|
259
|
+
fieldConfig.views = ['detail', 'edit'];
|
|
260
|
+
fieldConfig.type = field.isCollection ? `${field.collectionType}<${field.actualType}>` : field.type;
|
|
261
|
+
delete fieldConfig.maxLength;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function processEntityReference(context, field, fieldConfig) {
|
|
265
|
+
if (!fieldConfig) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
fieldConfig.searchOperators = ['equals'];
|
|
270
|
+
fieldConfig.views = ['list', 'detail', 'edit'];
|
|
271
|
+
fieldConfig.reference = {
|
|
272
|
+
entity: field.actualType || field.type,
|
|
273
|
+
idField: 'id',
|
|
274
|
+
idType: 'Long',
|
|
275
|
+
nameField: '<__MODIFY__>',
|
|
276
|
+
multiple: field.isCollection,
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const baseEntityPath = getBaseEntityPath(context);
|
|
280
|
+
const srcPath = path.join(context.root, baseEntityPath);
|
|
281
|
+
const referenceEntityPath = findEntityFile(srcPath, field.actualType || field.type);
|
|
282
|
+
|
|
283
|
+
if (referenceEntityPath) {
|
|
284
|
+
updateReferenceInfo(context, referenceEntityPath, fieldConfig, baseEntityPath, srcPath);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function updateReferenceInfo(context, referenceEntityPath, fieldConfig, baseEntityPath, srcPath) {
|
|
289
|
+
if (!referenceEntityPath || !fs.existsSync(referenceEntityPath)) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const referenceEntityContent = fs.readFileSync(referenceEntityPath, 'utf8');
|
|
294
|
+
const {
|
|
295
|
+
idField: referenceIdField,
|
|
296
|
+
fields: referenceFields,
|
|
297
|
+
parentClass: referenceParentClass,
|
|
298
|
+
} = analyzeEntity(referenceEntityContent);
|
|
299
|
+
|
|
300
|
+
let actualReferenceIdField = referenceIdField;
|
|
301
|
+
let idFieldType = null;
|
|
302
|
+
|
|
303
|
+
if (!actualReferenceIdField && referenceParentClass && referenceParentClass.importPath) {
|
|
304
|
+
const parentInfo = analyzeReferenceParent(context, referenceParentClass, baseEntityPath, srcPath, referenceFields);
|
|
305
|
+
actualReferenceIdField = parentInfo.actualReferenceIdField;
|
|
306
|
+
idFieldType = parentInfo.idFieldType;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
actualReferenceIdField = actualReferenceIdField || 'id';
|
|
310
|
+
|
|
311
|
+
const idFieldInfo = referenceFields.find(f => f.name === actualReferenceIdField);
|
|
312
|
+
if (idFieldInfo) {
|
|
313
|
+
idFieldType = idFieldInfo.type;
|
|
314
|
+
} else {
|
|
315
|
+
idFieldType = 'UUID';
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
fieldConfig.reference.idField = actualReferenceIdField;
|
|
319
|
+
fieldConfig.reference.idType = idFieldType;
|
|
320
|
+
fieldConfig.reference.nameField = findNameField(referenceFields, actualReferenceIdField);
|
|
321
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { analyzeEntity } from '../entity-analyzer.js';
|
|
4
|
+
import { entityTypeResolver, findParentEntityPath } from './paths.js';
|
|
5
|
+
|
|
6
|
+
export function analyzeParentClass(context, parentClass, baseEntityPath, srcPath, processedClasses) {
|
|
7
|
+
let parentFields = [];
|
|
8
|
+
let finalIdField = null;
|
|
9
|
+
|
|
10
|
+
if (parentClass && parentClass.importPath && !processedClasses.has(parentClass.name)) {
|
|
11
|
+
processedClasses.add(parentClass.name);
|
|
12
|
+
|
|
13
|
+
const parentEntityName = parentClass.name;
|
|
14
|
+
let parentEntityPath = findParentEntityPath(context, parentClass, baseEntityPath, srcPath);
|
|
15
|
+
|
|
16
|
+
if (parentEntityPath && fs.existsSync(parentEntityPath)) {
|
|
17
|
+
context.log(`Found parent class: ${parentEntityName} at ${path.relative(context.root, parentEntityPath)}`);
|
|
18
|
+
const parentContent = fs.readFileSync(parentEntityPath, 'utf8');
|
|
19
|
+
const parentAnalysis = analyzeEntity(parentContent, { isEntityType: entityTypeResolver(context, srcPath) });
|
|
20
|
+
parentFields = parentAnalysis.fields;
|
|
21
|
+
|
|
22
|
+
context.log(` Analyzed ${parentEntityName}: found ${parentFields.length} fields`);
|
|
23
|
+
if (parentAnalysis.parentClass) {
|
|
24
|
+
context.log(
|
|
25
|
+
` Parent ${parentEntityName} extends ${parentAnalysis.parentClass.name} (import: ${parentAnalysis.parentClass.importPath})`
|
|
26
|
+
);
|
|
27
|
+
} else {
|
|
28
|
+
context.log(` Parent ${parentEntityName} does not extend another class`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (parentAnalysis.idField) {
|
|
32
|
+
finalIdField = parentAnalysis.idField;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Recursively analyze grandparent classes
|
|
36
|
+
if (parentAnalysis.parentClass) {
|
|
37
|
+
context.log(`Parent ${parentEntityName} extends ${parentAnalysis.parentClass.name}, analyzing recursively...`);
|
|
38
|
+
const grandparentResult = analyzeParentClass(
|
|
39
|
+
context,
|
|
40
|
+
parentAnalysis.parentClass,
|
|
41
|
+
baseEntityPath,
|
|
42
|
+
srcPath,
|
|
43
|
+
processedClasses
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (grandparentResult.parentFields && grandparentResult.parentFields.length > 0) {
|
|
47
|
+
grandparentResult.parentFields.forEach(field => {
|
|
48
|
+
if (!parentFields.find(f => f.name === field.name)) {
|
|
49
|
+
parentFields.push(field);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!finalIdField && grandparentResult.finalIdField) {
|
|
55
|
+
finalIdField = grandparentResult.finalIdField;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!finalIdField && !parentAnalysis.parentClass) {
|
|
60
|
+
finalIdField = 'id';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { parentFields, finalIdField };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function mergeFields(parentFields, fields) {
|
|
69
|
+
if (!fields || !Array.isArray(fields)) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const mergedFields = [...fields];
|
|
74
|
+
|
|
75
|
+
parentFields.forEach(field => {
|
|
76
|
+
if (!mergedFields.find(f => f.name === field.name)) {
|
|
77
|
+
mergedFields.push(field);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
fields.length = 0;
|
|
82
|
+
fields.push(...mergedFields);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function analyzeReferenceParent(context, referenceParentClass, baseEntityPath, srcPath, referenceFields) {
|
|
86
|
+
const parentEntityPath = findParentEntityPath(context, referenceParentClass, baseEntityPath, srcPath);
|
|
87
|
+
let actualReferenceIdField = null;
|
|
88
|
+
let idFieldType = null;
|
|
89
|
+
|
|
90
|
+
if (parentEntityPath && fs.existsSync(parentEntityPath)) {
|
|
91
|
+
const parentContent = fs.readFileSync(parentEntityPath, 'utf8');
|
|
92
|
+
const parentAnalysis = analyzeEntity(parentContent);
|
|
93
|
+
|
|
94
|
+
actualReferenceIdField = parentAnalysis.idField || 'id';
|
|
95
|
+
|
|
96
|
+
const mergedReferenceFields = [...parentAnalysis.fields];
|
|
97
|
+
referenceFields.forEach(field => {
|
|
98
|
+
if (!mergedReferenceFields.find(f => f.name === field.name)) {
|
|
99
|
+
mergedReferenceFields.push(field);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
referenceFields.length = 0;
|
|
104
|
+
referenceFields.push(...mergedReferenceFields);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { actualReferenceIdField, idFieldType };
|
|
108
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createEntityTypeResolver } from '../entity-type-resolver.js';
|
|
4
|
+
import { findEntityFile } from '../file-utils.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The directory entity sources are scanned under: `entity.scanPath`, the older
|
|
8
|
+
* `entity.basePath` and `baseEntityPath`, or the Maven source root.
|
|
9
|
+
*
|
|
10
|
+
* @param {object} context the config command's context, which carries the project configuration
|
|
11
|
+
* @returns {string} the path, relative to the project root
|
|
12
|
+
*/
|
|
13
|
+
export function getBaseEntityPath(context) {
|
|
14
|
+
const config = context.config;
|
|
15
|
+
|
|
16
|
+
if (config?.entity?.scanPath) return config.entity.scanPath;
|
|
17
|
+
if (config?.entity?.basePath) return config.entity.basePath;
|
|
18
|
+
if (config?.baseEntityPath) return config.baseEntityPath;
|
|
19
|
+
return 'src/main/java';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The directory repository sources are scanned under. A project that derives the repository
|
|
24
|
+
* package from the entity's scans the entity path.
|
|
25
|
+
*
|
|
26
|
+
* @param {object} context the config command's context
|
|
27
|
+
* @returns {string | null} the path, relative to the project root, or null when not configured
|
|
28
|
+
*/
|
|
29
|
+
export function getRepositoryScanPath(context) {
|
|
30
|
+
const config = context.config;
|
|
31
|
+
|
|
32
|
+
if (!config) return null;
|
|
33
|
+
|
|
34
|
+
if (config.repository?.deriveFromEntity) {
|
|
35
|
+
return config.entity?.scanPath || config.entity?.basePath || null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return config.repository?.scanPath || config.repository?.basePath || null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Finds the module path of an entity's repository, read from the repository source's package.
|
|
43
|
+
*
|
|
44
|
+
* @param {object} context the config command's context
|
|
45
|
+
* @param {string} entityName the entity class name
|
|
46
|
+
* @param {string} modulePath the entity's own module path
|
|
47
|
+
* @returns {string | null} the repository's module path when it differs from the entity's, else null
|
|
48
|
+
*/
|
|
49
|
+
export function findRepositoryModulePath(context, entityName, modulePath) {
|
|
50
|
+
const repoScanPath = getRepositoryScanPath(context);
|
|
51
|
+
|
|
52
|
+
if (!repoScanPath) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const repoFilePath = findEntityFile(path.join(context.root, repoScanPath), `${entityName}Repository`);
|
|
57
|
+
|
|
58
|
+
if (!repoFilePath) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const packageMatch = fs.readFileSync(repoFilePath, 'utf8').match(/package\s+([\w.]+);/);
|
|
63
|
+
|
|
64
|
+
if (!packageMatch) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const repoPackage = packageMatch[1];
|
|
69
|
+
const domainIndex = repoPackage.indexOf('.domain.');
|
|
70
|
+
|
|
71
|
+
if (domainIndex === -1) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const repoModulePath = repoPackage
|
|
76
|
+
.substring(domainIndex + '.domain.'.length)
|
|
77
|
+
.split('.')
|
|
78
|
+
.filter(part => part !== 'repository')
|
|
79
|
+
.join('/');
|
|
80
|
+
|
|
81
|
+
return repoModulePath === modulePath ? null : repoModulePath;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function findParentEntityPath(context, parentClass, baseEntityPath, srcPath) {
|
|
85
|
+
let parentEntityPath = null;
|
|
86
|
+
|
|
87
|
+
if (parentClass.importPath) {
|
|
88
|
+
// Try direct import path resolution first
|
|
89
|
+
const importPathParts = parentClass.importPath.split('.');
|
|
90
|
+
const domainIndex = importPathParts.indexOf('domain');
|
|
91
|
+
|
|
92
|
+
if (domainIndex !== -1) {
|
|
93
|
+
// Convert import path to file path relative to scan root
|
|
94
|
+
// e.g., dev.accesscore.domain.core.entity.BaseEntity -> core/entity/BaseEntity.java
|
|
95
|
+
const pathFromDomain = importPathParts.slice(domainIndex + 1).join('/');
|
|
96
|
+
|
|
97
|
+
// Try to find in the scan path directly
|
|
98
|
+
const candidatePaths = [
|
|
99
|
+
// Try full import path under scan root
|
|
100
|
+
path.join(context.root, baseEntityPath, pathFromDomain + '.java'),
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
for (const candidate of candidatePaths) {
|
|
104
|
+
if (fs.existsSync(candidate)) {
|
|
105
|
+
parentEntityPath = candidate;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Fallback to recursive search
|
|
111
|
+
if (!parentEntityPath) {
|
|
112
|
+
parentEntityPath = findEntityFile(srcPath, parentClass.name);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!parentEntityPath) {
|
|
118
|
+
parentEntityPath = findEntityFile(srcPath, parentClass.name);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return parentEntityPath;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function extractModulePath(entityContent, _entityName) {
|
|
125
|
+
const packageMatch = entityContent.match(/package\s+([\w.]+);/);
|
|
126
|
+
let modulePath = '<__MODIFY__>';
|
|
127
|
+
|
|
128
|
+
if (packageMatch) {
|
|
129
|
+
const fullPackage = packageMatch[1];
|
|
130
|
+
const domainIndex = fullPackage.indexOf('.domain.');
|
|
131
|
+
|
|
132
|
+
if (domainIndex !== -1) {
|
|
133
|
+
const pathAfterDomain = fullPackage.substring(domainIndex + '.domain.'.length);
|
|
134
|
+
const pathParts = pathAfterDomain.split('.');
|
|
135
|
+
const prefixesToRemove = ['entities', 'entity', 'domain', 'domains'];
|
|
136
|
+
let filteredParts = pathParts;
|
|
137
|
+
|
|
138
|
+
if (filteredParts.length > 0 && prefixesToRemove.includes(filteredParts[0])) {
|
|
139
|
+
filteredParts = filteredParts.slice(1);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
filteredParts = filteredParts.filter(part => part !== 'entity');
|
|
143
|
+
modulePath = filteredParts.join('/');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return modulePath;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The one resolver per source tree that answers whether a field's type is an @Entity. */
|
|
151
|
+
export function entityTypeResolver(context, srcPath) {
|
|
152
|
+
if (!context.entityTypeResolvers) context.entityTypeResolvers = new Map();
|
|
153
|
+
if (!context.entityTypeResolvers.has(srcPath))
|
|
154
|
+
context.entityTypeResolvers.set(srcPath, createEntityTypeResolver(srcPath));
|
|
155
|
+
return context.entityTypeResolvers.get(srcPath);
|
|
156
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import yaml from 'js-yaml';
|
|
2
|
+
|
|
3
|
+
export function createBaseConfig(entityName, modulePath, idFieldName, nameField, repositoryModulePath) {
|
|
4
|
+
const config = {
|
|
5
|
+
entity: entityName,
|
|
6
|
+
modulePath,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// Add promoteModule placeholder for user to fill in
|
|
10
|
+
config.promoteModule = '<__MODIFY__>';
|
|
11
|
+
|
|
12
|
+
// Add repositoryModulePath right after modulePath if it differs
|
|
13
|
+
if (repositoryModulePath !== null && repositoryModulePath !== undefined && repositoryModulePath !== modulePath) {
|
|
14
|
+
config.repositoryModulePath = repositoryModulePath;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
config.idField = idFieldName;
|
|
18
|
+
config.nameField = nameField;
|
|
19
|
+
config.thymeleafBaseDir = entityName
|
|
20
|
+
.replace(/([A-Z])/g, '/$1')
|
|
21
|
+
.replace(/^\//, '')
|
|
22
|
+
.toLowerCase();
|
|
23
|
+
config.defaultSortField = '<__MODIFY__>';
|
|
24
|
+
config.defaultSortDirection = 'desc';
|
|
25
|
+
config.fields = {};
|
|
26
|
+
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function formatYamlContent(ymlContent) {
|
|
31
|
+
return ymlContent
|
|
32
|
+
.replace(/^entity:.*$/m, '$&\n')
|
|
33
|
+
.replace(/^fields:$/m, '\nfields:\n')
|
|
34
|
+
.replace(/^ {2}(\w+):$/gm, '\n $1:')
|
|
35
|
+
.replace(/^ {2}(\w+\.\w+):$/gm, '\n $1:')
|
|
36
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
37
|
+
.replace(/^(\s+)(views|searchOperators):\n\s+-\s+(.+(?:\n\s+-\s+.+)*)/gm, (_, indent, key, items) => {
|
|
38
|
+
const values = items.split('\n').map(line => line.replace(/\s+-\s+/, '').trim());
|
|
39
|
+
return `${indent}${key}: [${values.join(', ')}]`;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Serializes an entity configuration as the yml a project keeps under `.simplix/entity`.
|
|
45
|
+
*
|
|
46
|
+
* @param {object} config the entity configuration
|
|
47
|
+
* @returns {string} the yml text
|
|
48
|
+
*/
|
|
49
|
+
export function dumpEntityConfig(config) {
|
|
50
|
+
return formatYamlContent(
|
|
51
|
+
yaml.dump(config, {
|
|
52
|
+
indent: 2,
|
|
53
|
+
lineWidth: -1,
|
|
54
|
+
quotingType: '"',
|
|
55
|
+
flowStyle: false,
|
|
56
|
+
styles: {
|
|
57
|
+
'!!null': 'empty',
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
}
|