forked from esparta-io/generator-android-hipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-base.js
712 lines (652 loc) · 27.7 KB
/
script-base.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
'use strict';
var path = require('path'),
util = require('util'),
_ = require('lodash'),
_s = require('underscore.string'),
yeoman = require('yeoman-generator'),
chalk = require('chalk'),
jhipsterUtils = require('./util.js'),
fs = require('fs'),
shelljs = require('shelljs'),
ejs = require('ejs');
var MODULES_HOOK_FILE = '.jhipster/modules/jhi-hooks.json';
module.exports = Generator;
function Generator() {
yeoman.Base.apply(this, arguments);
}
util.inherits(Generator, yeoman.Base);
Generator.prototype.installGradleDependencies = function (config, update) {
var dependencies = this.fs.readJSON('dependencies.json');
this.addGradleFieldDependency('buildToolsVersion', '"25.0.3"', update);
var parent = [];
var parentKotlin = [];
for (var i = 0; i < dependencies[0].dependencies.length; i++) {
if (dependencies[0].dependencies[i].lang == 'all') {
parent.push(dependencies[0].dependencies[i]);
}
if (dependencies[0].dependencies[i].lang == 'java' && config.language == 'java') {
parent.push(dependencies[0].dependencies[i]);
}
if (dependencies[0].dependencies[i].lang == 'kotlin' && config.language == 'kotlin') {
parentKotlin.push(dependencies[0].dependencies[i]);
}
}
this.addMultipleParentGradleDependency(parent, update, 'build.gradle');
this.addMultipleParentGradleDependency(parentKotlin, update, 'app/build.gradle');
var gradle = dependencies[1].dependencies;
var gradleDependencies = [];
for (var i = 0; i < gradle.length; i++) {
if (gradle[i].lang == 'all') {
gradleDependencies.push(gradle[i]);
}
if (gradle[i].lang == 'java' && config.language == 'java') {
gradleDependencies.push(gradle[i]);
}
if (gradle[i].lang == 'kotlin' && config.language == 'kotlin') {
gradleDependencies.push(gradle[i]);
}
}
if (!update) {
var toRemove = [];
for (var i = 0; i < gradleDependencies.length; i++) {
if (gradleDependencies[i].selection != undefined) {
if (gradleDependencies[i].selection == 'playServices') {
if (config.playServices == undefined || config.playServices.length == 0) {
toRemove.push(gradleDependencies[i]);
} else if (config.playServices != undefined && config.playServices.length > 0) {
if (config.playServices.indexOf(gradleDependencies[i].name.replace('play-services-', '')) == -1) {
toRemove.push(gradleDependencies[i]);
}
}
} else if (config[gradleDependencies[i].selection] == undefined || config[gradleDependencies[i].selection] == false) {
toRemove.push(gradleDependencies[i]);
}
}
}
for (var i = 0; i < toRemove.length; i++) {
gradleDependencies.splice(gradleDependencies.indexOf(toRemove[i]), 1);
}
} else {
var toRemove = [];
for (var i = 0; i < gradleDependencies.length; i++) {
if (gradleDependencies[i].selection != undefined) {
if (gradleDependencies[i].selection == 'playServices') {
if (config.playServices == undefined || config.playServices.length == 0) {
toRemove.push(gradleDependencies[i]);
} else if (config.playServices != undefined && config.playServices.length > 0) {
if (config.playServices.indexOf(gradleDependencies[i].name.replace('play-services-', '')) == -1 && gradleDependencies[i].name != 'play-services-base') {
toRemove.push(gradleDependencies[i]);
}
}
} else if (config[gradleDependencies[i].selection] == undefined || config[gradleDependencies[i].selection] == false) {
toRemove.push(gradleDependencies[i]);
}
} else {
if (gradleDependencies[i].selection == undefined) {
} else if (config[gradleDependencies[i].selection] == undefined || config[gradleDependencies[i].selection] == false) {
toRemove.push(gradleDependencies[i]);
}
}
}
for (var i = 0; i < toRemove.length; i++) {
gradleDependencies.splice(gradleDependencies.indexOf(toRemove[i]), 1);
}
}
this.addMultipleGradleDependency(gradleDependencies, update);
};
Generator.prototype.addComponentInjection = function (name, basePath, packageName, filename) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/components/' + (filename != undefined ? (filename + '.java') : 'ApplicationComponent.java');
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-method',
splicable: [
'void inject(' + name + ' ' + name.charAt(0).toLowerCase() + name.slice(1) + ');'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-import',
splicable: [
'import ' + packageName + '.' + name + ';'
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required android-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.addCustomComponentInjection = function (component, name, basePath, packageName) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/components/' + component + '.java';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-method',
splicable: [
'void inject(' + name + ' ' + name.charAt(0).toLowerCase() + name.slice(1) + ');'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-import',
splicable: [
'import ' + packageName + '.' + name + ';'
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required android-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.addCustomComponentInjectionKotlin = function (component, name, basePath, packageName) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/components/' + component + '.kt';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-method',
splicable: [
'fun inject(' + name.charAt(0).toLowerCase() + name.slice(1) + ': '+name+');'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-import',
splicable: [
'import ' + packageName + '.' + name + ''
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required android-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.updateApplicationModuleToProvide = function (name, basePath, packageName, type, ext) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/modules/ApplicationModule.java';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-module-provides-method',
splicable: [
'@Provides',
'@Singleton',
name + type + ' provide' + name + type + '(Scheduler executor) {',
' return new ' + name + type + 'Impl(executor);',
'}'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-module-provides-import',
splicable: [
'import ' + packageName + '.' + name + type + ';',
'import ' + packageName + '.' + name + type + 'Impl;'
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required android-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.updateApplicationModuleToProvideKotlin = function (name, basePath, packageName, type) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/modules/ApplicationModule.kt';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-module-provides-method',
splicable: [
'@Provides',
'@Singleton',
'fun provide' + name + type + '(executor: Scheduler): '+name + type + ' {',
' return ' + name + type + 'Impl(executor);',
'}'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-module-provides-import',
splicable: [
'import ' + packageName + '.' + name + type + '',
'import ' + packageName + '.' + name + type + 'Impl'
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required android-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.updateApplicationModuleToRepository = function (name, basePath, packageName, remote, local) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/modules/ApplicationModule.java';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-module-provides-method',
splicable: [
'@Provides',
'@Singleton',
name + 'Repository' + ' provide' + name + 'Repository' + '(' + (remote ? 'Retrofit retrofit' : '') + ') {',
' return new ' + name + 'Repository' + 'Impl(' + (remote ? ('new ' + name + 'RemoteRepository(retrofit)') : '') + ((remote && local) ? ', ' : '') + (local ? ('new ' + name + 'LocalRepository()') : '') + ');',
'}'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-module-provides-import',
splicable: [
'import ' + packageName + '.*;',
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required android-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.provideInComponent = function (name, basePath, packageName, type) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/components/ApplicationComponent.java';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-method',
splicable: [
name + type + ' provide' + name + type + '();'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-import',
splicable: [
'import ' + packageName + '.' + name + type + ';'
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.provideInComponentKotlin = function (name, basePath, packageName, type) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/components/ApplicationComponent.kt';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-method',
splicable: [
'fun provide' + name + type + '(): '+name + type
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-import',
splicable: [
'import ' + packageName + '.' + name + type + ''
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
Generator.prototype.addComponentInjectionKotlin = function (name, basePath, packageName, filename) {
try {
var fullPath = 'app/src/main/java/' + basePath + '/di/components/' + (filename != undefined ? (filename + '.kt') : 'ApplicationComponent.kt');
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-method',
splicable: [
'fun inject(' + name.charAt(0).toLowerCase() + name.slice(1) + ' : ' + name + ')'
]
});
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-component-injection-import',
splicable: [
'import ' + packageName + '.' + name + ''
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + name + ' ' + chalk.yellow('not added.\n'));
}
};
/**
* A new Gradle plugin.
*
* @param {group} plugin GroupId
* @param {name} plugin name
* @param {version} explicit plugin version number
*/
Generator.prototype.addGradlePlugin = function (group, name, version) {
try {
var fullPath = 'build.gradle';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-gradle-buildscript-dependency',
splicable: [
'classpath group: \'' + group + '\', name: \'' + name + '\', version: \'' + version + '\''
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + 'classpath: ' + group + ':' + name + ':' + version + chalk.yellow(' not added.\n'));
}
};
/**
* A new dependency to build.gradle file.
*
* @param {scope} scope of the new dependency, e.g. compile
* @param {group} maven GroupId
* @param {name} maven ArtifactId
* @param {version} explicit version number
*/
Generator.prototype.addGradleDependency = function (scope, group, name, version, update) {
try {
var fullPath = 'app/build.gradle';
if (update) {
jhipsterUtils.rewriteReplace({
file: fullPath,
needle: scope + ' "' + group + ':' + name,
splicable: [
scope + ' "' + group + ':' + name + ':' + version + '"'
]
});
//this.log(chalk.green('updated dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
} else {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-gradle-dependency',
splicable: [
scope + ' "' + group + ':' + name + ':' + version + '"'
]
});
//this.log(chalk.green('added dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
}
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + group + ':' + name + ':' + version + chalk.yellow(' not added.\n'));
}
};
Generator.prototype.addMultipleGradleDependency = function (dependencies, update) {
try {
var fullPath = 'app/build.gradle';
if (update) {
jhipsterUtils.rewriteReplaceMultiple({
file: fullPath,
dependencies: dependencies
});
// this.log(chalk.green('updated dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
} else {
jhipsterUtils.rewriteFileMultiple({
file: fullPath,
dependencies: dependencies
});
// this.log(chalk.green('added dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
}
} catch (e) {
this.log(e);
}
};
Generator.prototype.addMultipleParentGradleDependency = function (dependencies, update, fullPath) {
try {
if (update) {
jhipsterUtils.rewriteReplaceMultiple({
file: fullPath,
dependencies: dependencies,
needle : 'android-hipster-needle-gradle-parent-dependency'
});
//this.log(chalk.green('updated dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
} else {
jhipsterUtils.rewriteFileMultiple({
file: fullPath,
dependencies: dependencies,
needle : 'android-hipster-needle-gradle-parent-dependency'
});
//this.log(chalk.green('added dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
}
} catch (e) {
this.log(e);
}
};
Generator.prototype.addGradleParentDependency = function (scope, group, name, version, update) {
try {
var fullPath = 'build.gradle';
if (update) {
jhipsterUtils.rewriteReplace({
file: fullPath,
needle: scope + ' "' + group + ':' + name,
splicable: [
scope + ' "' + group + ':' + name + ':' + version + '"'
]
});
//this.log(chalk.green('updated dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
} else {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'android-hipster-needle-gradle-dependency',
splicable: [
scope + ' "' + group + ':' + name + ':' + version + '"'
]
});
//this.log(chalk.green('added dependency: ' + scope + ' "' + group + ':' + name + ':' + version + '"'));
}
} catch (e) {
this.log(e);
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + group + ':' + name + ':' + version + chalk.yellow(' not added.\n'));
}
};
Generator.prototype.addGradleFieldDependency = function (type, value, update) {
try {
var fullPath = 'app/build.gradle';
jhipsterUtils.rewriteReplace({
file: fullPath,
needle: type,
splicable: [
type + ' ' + value
]
});
this.log(chalk.green('updated field: ' + type + ' "' + value));
} catch (e) {
this.log(e);
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + type + ':' + value + chalk.yellow(' not added.\n'));
}
};
/**
* Apply from an external Gradle build script.
*
* @param {name} name of the file to apply from, must be 'fileName.gradle'
*/
Generator.prototype.applyFromGradleScript = function (name) {
try {
var fullPath = 'build.gradle';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-gradle-apply-from',
splicable: [
'apply from: \'' + name + '.gradle\''
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + name + chalk.yellow(' not added.\n'));
}
};
/**
* Copy templates with all the custom logic applied according to the type.
*
* @param {source} path of the source file to copy from
* @param {dest} path of the destination file to copy to
* @param {action} type of the action to be performed on the template file, i.e: stripHtml | stripJs | template | copy
* @param {_this} context that can be used as the generator instance or data to process template
* @param {_opt} options that can be passed to template method
* @param {template} flag to use template method instead of copy method
*/
Generator.prototype.copyTemplate = function (source, dest, action, _this, _opt, template) {
_this = _this !== undefined ? _this : this;
_opt = _opt !== undefined ? _opt : {};
switch (action) {
case 'stripHtml' :
var regex = '( translate\="([a-zA-Z0-9](\.)?)+")|( translate-values\="\{([a-zA-Z]|\d|\:|\{|\}|\[|\]|\-|\'|\s|\.)*?\}")';
//looks for something like translate="foo.bar.message" and translate-values="{foo: '{{ foo.bar }}'}"
jhipsterUtils.copyWebResource(source, dest, regex, 'html', _this, _opt, template);
break;
case 'stripJs' :
var regex = '[a-zA-Z]+\:(\s)?\[[ \'a-zA-Z0-9\$\,\(\)\{\}\n\.\<\%\=\>\;\s]*\}\]';
//looks for something like mainTranslatePartialLoader: [*]
jhipsterUtils.copyWebResource(source, dest, regex, 'js', _this, _opt, template);
break;
case 'copy' :
_this.copy(source, dest);
break;
default:
_this.template(source, dest, _this, _opt);
}
};
/**
* Copy html templates after stripping translation keys when translation is disabled.
*
* @param {source} path of the source file to copy from
* @param {dest} path of the destination file to copy to
* @param {_this} context that can be used as the generator instance or data to process template
* @param {_opt} options that can be passed to template method
* @param {template} flag to use template method instead of copy
*/
Generator.prototype.copyHtml = function (source, dest, _this, _opt, template) {
this.copyTemplate(source, dest, 'stripHtml', _this, _opt, template);
};
/**
* Copy Js templates after stripping translation keys when translation is disabled.
*
* @param {source} path of the source file to copy from
* @param {dest} path of the destination file to copy to
* @param {_this} context that can be used as the generator instance or data to process template
* @param {_opt} options that can be passed to template method
* @param {template} flag to use template method instead of copy
*/
Generator.prototype.copyJs = function (source, dest, _this, _opt, template) {
this.copyTemplate(source, dest, 'stripJs', _this, _opt, template);
};
/**
* Rewrite the specified file with provided content at the needle location
*
* @param {fullPath} path of the source file to rewrite
* @param {needle} needle to look for where content will be inserted
* @param {content} content to be written
*/
Generator.prototype.rewriteFile = function (filePath, needle, content) {
try {
jhipsterUtils.rewriteFile({
file: filePath,
needle: needle,
splicable: [
content
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + filePath + chalk.yellow(' or missing required needle. File rewrite failed.\n'));
}
};
Generator.prototype.rewriteReplace = function (filePath, needle, content) {
try {
jhipsterUtils.rewriteReplace({
file: filePath,
needle: needle,
splicable: [
content
]
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + filePath + chalk.yellow(' or missing required needle. File rewrite failed.\n'));
}
};
/**
* Replace the pattern/regex with provided content
*
* @param {fullPath} path of the source file to rewrite
* @param {pattern} pattern to look for where content will be replaced
* @param {content} content to be written
* @param {regex} true if pattern is regex
*/
Generator.prototype.replaceContent = function (filePath, pattern, content, regex) {
try {
jhipsterUtils.replaceContent({
file: filePath,
pattern: pattern,
content: content,
regex: regex
});
} catch (e) {
this.log(chalk.yellow('\nUnable to find ') + filePath + chalk.yellow(' or missing required pattern. File rewrite failed.\n') + e);
}
};
/**
* Register a module configuration to .jhipster/modules/jhi-hooks.json
*
* @param {npmPackageName} npm package name of the generator
* @param {hookFor} from which Jhipster generator this should be hooked ( 'entity' or 'app')
* @param {hookType} where to hook this at the generator stage ( 'pre' or 'post')
* @param {callbackSubGenerator}[optional] sub generator to invoke, if this is not given the module's main generator will be called, i.e app
* @param {description}[optional] description of the generator
*/
Generator.prototype.registerModule = function (npmPackageName, hookFor, hookType, callbackSubGenerator, description) {
try {
var modules;
var error, duplicate;
var moduleName = _s.humanize(npmPackageName.replace('generator-jhipster-', ''));
var generatorName = npmPackageName.replace('generator-', '');
var generatorCallback = generatorName + ':' + (callbackSubGenerator ? callbackSubGenerator : 'app');
var moduleConfig = {
name: moduleName + ' generator',
npmPackageName: npmPackageName,
description: description ? description : 'A JHipster module to generate ' + moduleName,
hookFor: hookFor,
hookType: hookType,
generatorCallback: generatorCallback
}
if (shelljs.test('-f', MODULES_HOOK_FILE)) {
// file is present append to it
try {
modules = this.fs.readJSON(MODULES_HOOK_FILE);
duplicate = _.findIndex(modules, moduleConfig) !== -1;
} catch (err) {
error = true;
this.log(chalk.red('The Jhipster module configuration file could not be read!'));
}
} else {
// file not present create it and add config to it
modules = [];
}
if (!error && !duplicate) {
modules.push(moduleConfig);
this.fs.writeJSON(MODULES_HOOK_FILE, modules, null, 4);
}
} catch (err) {
this.log('\n' + chalk.bold.red('Could not add jhipster module configuration'));
}
};
/**
* Add configuration to Entity.json files
*
* @param {file} configuration file name for the entity
* @param {key} key to be added or updated
* @param {value} value to be added
*/
Generator.prototype.updateEntityConfig = function (file, key, value) {
try {
var entityJson = this.fs.readJSON(file);
entityJson[key] = value;
this.fs.writeJSON(file, entityJson, null, 4);
} catch (err) {
this.log(chalk.red('The Jhipster entity configuration file could not be read!') + err);
}
}
/**
* get the module hooks config json
*/
Generator.prototype.getModuleHooks = function () {
var modulesConfig = [];
try {
if (shelljs.test('-f', MODULES_HOOK_FILE)) {
modulesConfig = this.fs.readJSON(MODULES_HOOK_FILE);
}
} catch (err) {
this.log(chalk.red('The module configuration file could not be read!'));
}
return modulesConfig;
}
Generator.prototype.removefile = function (file) {
if (shelljs.test('-f', file)) {
this.log('Removing the file - ' + file);
shelljs.rm(file);
}
}
Generator.prototype.removefolder = function (folder) {
if (shelljs.test('-d', folder)) {
this.log('Removing the folder - ' + folder)
shelljs.rm("-rf", folder);
}
}