-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
generator-base-blueprint.js
421 lines (383 loc) · 15.5 KB
/
generator-base-blueprint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* Copyright 2013-2020 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const packagejs = require('../package.json');
const { packageNameToNamespace } = require('./utils');
const BaseGenerator = require('./generator-base');
const { mergeBlueprints, parseBluePrints, loadBlueprintsFromConfiguration, normalizeBlueprintName } = require('../utils/blueprint');
/**
* This is the base class for a generator that can be extended through a blueprint.
*
* The method signatures in public API should not be changed without a major version change
*/
module.exports = class JHipsterBaseBlueprintGenerator extends BaseGenerator {
constructor(args, opts) {
super(args, opts);
// Add base template folder.
this.jhipsterTemplatesFolders = [this.templatePath()];
this.fromBlueprint = this.rootGeneratorName() !== 'generator-jhipster';
if (this.fromBlueprint) {
this.blueprintStorage = this._getStorage();
this.blueprintConfig = this.blueprintStorage.createProxy();
// jhipsterContext is the original generator
this.jhipsterContext = opts.jhipsterContext;
if (this.jhipsterContext) {
// Fallback to the original generator if the file does not exists in the blueprint.
this.jhipsterTemplatesFolders.push(this.jhipsterContext.templatePath());
}
}
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_initializing() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_prompting() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_configuring() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_composing() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_loading() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_preparing() {
return {};
}
/**
* @private
* Execute custom priorities if they are not declared
* Should be used by jhipster official generators only.
* @returns {Object} tasks
*/
_missingPreDefault() {
let tasks = {};
if (this.sbsBlueprint) return tasks;
if (this._isPriorityMissing('composing', 'default')) {
tasks = { ...tasks, ...this._composing() };
}
if (this._isPriorityMissing('loading', 'default')) {
tasks = { ...tasks, ...this._loading() };
}
if (this._isPriorityMissing('preparing', 'default')) {
tasks = { ...tasks, ...this._preparing() };
}
if (this._isPriorityMissing('preparingRelationships', 'default')) {
tasks = { ...tasks, ...this._preparingRelationships() };
}
return tasks;
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_default() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_writing() {
return {};
}
/**
* @private
* Execute custom priorities if they are not declared
* Should be used by jhipster official generators only.
* @returns {Object} tasks
*/
_missingPostWriting() {
let tasks = {};
if (this.sbsBlueprint) return tasks;
if (this._isPriorityMissing('postWriting', 'writing')) {
tasks = { ...tasks, ...this._postWriting() };
}
return tasks;
}
/**
* Public API method used by the getter and also by Blueprints
*/
_postWriting() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_install() {
return {};
}
/**
* Public API method used by the getter and also by Blueprints
* @returns {Object} tasks
*/
_end() {
return {};
}
/**
* @private
* Detect if a priority is implemented in the super class but missing in current one.
* That indicates the blueprint was not updated with the custom priorities.
* @param {string} priorityName - Priority to be checked.
* @param {sring} destPriority - Priority that the task is related to for logging purpose.
* @return {boolean} true if the priority is missing.
*/
_isPriorityMissing(priorityName, destPriority = 'related') {
const ownPrototype = Object.getPrototypeOf(this);
if (
!Object.getOwnPropertyDescriptor(ownPrototype, priorityName) &&
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(ownPrototype), priorityName)
) {
this.warning(
`Priority ${priorityName} is missing for generator ${this.options.namespace}. Merging into ${destPriority} priority.`
);
return true;
}
return false;
}
/**
* @private
* Instantiates the blueprint generators, if any.
* @param {string} subGen - sub generator
* @param {any} extraOptions - extra options to pass to blueprint generator
* @return {true} useBlueprints - true if one or more blueprints generators have been constructed; false otherwise
*/
instantiateBlueprints(subGen, extraOptions) {
if (this.options.help) {
// Ignore blueprint registered options.
return false;
}
let useBlueprints = false;
if (!this.configOptions.blueprintConfigured) {
this.configOptions.blueprintConfigured = true;
this._configureBlueprints();
}
const blueprints = this.jhipsterConfig.blueprints;
if (blueprints && blueprints.length > 0) {
blueprints.forEach(blueprint => {
const blueprintGenerator = this._composeBlueprint(blueprint.name, subGen, extraOptions);
if (blueprintGenerator) {
if (blueprintGenerator.sbsBlueprint) {
// If sbsBlueprint, add templatePath to the original generator templatesFolder.
this.jhipsterTemplatesFolders.unshift(blueprintGenerator.templatePath());
} else {
// If the blueprints does not sets sbsBlueprint property, ignore normal workflow.
useBlueprints = true;
}
}
});
}
return useBlueprints;
}
/**
* @private
* Configure blueprints.
*/
_configureBlueprints() {
let argvBlueprints = this.options.blueprints || '';
// check for old single blueprint declaration
const blueprint = this.options.blueprint;
if (blueprint) {
this.warning('--blueprint option is deprecated. Please use --blueprints instead');
if (!argvBlueprints.split(',').includes(blueprint)) {
argvBlueprints = `${blueprint},${argvBlueprints}`;
}
}
const blueprints = mergeBlueprints(parseBluePrints(argvBlueprints), loadBlueprintsFromConfiguration(this.config));
// EnvironmentBuilder already looks for blueprint when running from cli, this is required for tests.
// Can be removed once the tests uses EnvironmentBuilder.
const missingBlueprints = blueprints
.filter(blueprint => !this.env.isPackageRegistered(packageNameToNamespace(blueprint.name)))
.map(blueprint => blueprint.name);
if (missingBlueprints.length > 0) {
this.env.lookup({ filterPaths: true, packagePatterns: missingBlueprints });
}
// OtherModules is used to create package.json dependencies.
let otherModules = this.jhipsterConfig.otherModules || [];
if (blueprints && blueprints.length > 0) {
blueprints.forEach(blueprint => {
blueprint.version = this._findBlueprintVersion(blueprint.name) || blueprint.version;
});
// Remove potential previous value to avoid duplicates
otherModules = otherModules.filter(module => blueprints.findIndex(blueprint => blueprint.name === module.name) === -1);
otherModules.push(...blueprints);
}
this.jhipsterConfig.blueprints = blueprints;
this.jhipsterConfig.otherModules = otherModules;
if (!this.options.skipChecks) {
const namespaces = blueprints.map(blueprint => packageNameToNamespace(blueprint.name));
// Verify if the blueprints hava been registered.
const missing = namespaces.filter(namespace => !this.env.isPackageRegistered(namespace));
if (missing && missing.length > 0) {
this.error(`Some blueprints were not found ${missing}, you should install them manually`);
}
}
}
/**
* @private
* Compose external blueprint module
* @param {string} blueprint - name of the blueprint
* @param {string} subGen - sub generator
* @param {any} options - options to pass to blueprint generator
* @return {Generator|undefined}
*/
_composeBlueprint(blueprint, subGen, extraOptions = {}) {
blueprint = normalizeBlueprintName(blueprint);
if (!this.configOptions.skipChecks && !this.options.skipChecks) {
this._checkBlueprint(blueprint);
}
const generatorName = packageNameToNamespace(blueprint);
const generatorNamespace = `${generatorName}:${subGen}`;
if (!this.env.isPackageRegistered(generatorName)) {
this.env.lookup({ filterPaths: true, packagePatterns: blueprint });
}
if (!this.env.get(generatorNamespace)) {
this.debug(
`No blueprint found for blueprint ${chalk.yellow(blueprint)} and ${chalk.yellow(
subGen
)} subgenerator: falling back to default generator`
);
return undefined;
}
const finalOptions = {
...this.options,
configOptions: this.configOptions,
...extraOptions,
jhipsterContext: this,
};
const blueprintGenerator = this.composeWith(generatorNamespace, finalOptions, true);
if (blueprintGenerator instanceof Error) {
throw blueprintGenerator;
}
this.info(`Using blueprint ${chalk.yellow(blueprint)} for ${chalk.yellow(subGen)} subgenerator`);
return blueprintGenerator;
}
/**
* @private
* Try to retrieve the package.json of the blueprint used, as an object.
* @param {string} blueprintPkgName - generator name
* @return {object} packageJson - retrieved package.json as an object or undefined if not found
*/
_findBlueprintPackageJson(blueprintPkgName) {
const blueprintGeneratorName = packageNameToNamespace(blueprintPkgName);
const blueprintPackagePath = this.env.getPackagePath(blueprintGeneratorName);
if (!blueprintPackagePath) {
this.warning(`Could not retrieve packagePath of blueprint '${blueprintPkgName}'`);
return undefined;
}
return JSON.parse(fs.readFileSync(path.join(blueprintPackagePath, 'package.json')));
}
/**
* @private
* Try to retrieve the version of the blueprint used.
* @param {string} blueprintPkgName - generator name
* @return {string} version - retrieved version or empty string if not found
*/
_findBlueprintVersion(blueprintPkgName) {
const blueprintPackageJson = this._findBlueprintPackageJson(blueprintPkgName);
if (!blueprintPackageJson || !blueprintPackageJson.version) {
this.warning(`Could not retrieve version of blueprint '${blueprintPkgName}'`);
return undefined;
}
return blueprintPackageJson.version;
}
/**
* @private
* Check if the generator specified as blueprint is installed.
* @param {string} blueprint - generator name
*/
_checkBlueprint(blueprint) {
if (blueprint === 'generator-jhipster') {
this.error(`You cannot use ${chalk.yellow(blueprint)} as the blueprint.`);
}
if (!this._findBlueprintPackageJson(blueprint)) {
this.error(
`The ${chalk.yellow(blueprint)} blueprint provided is not installed. Please install it using command ${chalk.yellow(
`npm i -g ${blueprint}`
)}.`
);
}
}
/**
* @private
* Check if the generator specified as blueprint has a version compatible with current JHipster.
* @param {string} blueprintPkgName - generator name
*/
_checkJHipsterBlueprintVersion(blueprintPkgName) {
const blueprintPackageJson = this._findBlueprintPackageJson(blueprintPkgName);
if (!blueprintPackageJson) {
this.warning(`Could not retrieve version of JHipster declared by blueprint '${blueprintPkgName}'`);
return;
}
const mainGeneratorJhipsterVersion = packagejs.version;
const blueprintJhipsterVersion = blueprintPackageJson.dependencies && blueprintPackageJson.dependencies['generator-jhipster'];
if (blueprintJhipsterVersion) {
if (mainGeneratorJhipsterVersion !== blueprintJhipsterVersion) {
this.error(
`The installed ${chalk.yellow(
blueprintPkgName
)} blueprint targets JHipster v${blueprintJhipsterVersion} and is not compatible with this JHipster version. Either update the blueprint or JHipster. You can also disable this check using --skip-checks at your own risk`
);
}
return;
}
const blueprintPeerJhipsterVersion =
blueprintPackageJson.peerDependencies && blueprintPackageJson.peerDependencies['generator-jhipster'];
if (blueprintPeerJhipsterVersion) {
if (semver.satisfies(mainGeneratorJhipsterVersion, blueprintPeerJhipsterVersion)) {
return;
}
this.error(
`The installed ${chalk.yellow(
blueprintPkgName
)} blueprint targets JHipster ${blueprintPeerJhipsterVersion} and is not compatible with this JHipster version. Either update the blueprint or JHipster. You can also disable this check using --skip-checks at your own risk`
);
}
this.warning(`Could not retrieve version of JHipster declared by blueprint '${blueprintPkgName}'`);
}
};