forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-base.js
1097 lines (1035 loc) · 42.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'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'),
Insight = require('insight'),
fs = require('fs'),
shelljs = require('shelljs'),
ejs = require('ejs');
module.exports = Generator;
function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
this.env.options.appPath = this.config.get('appPath') || 'src/main/webapp';
}
util.inherits(Generator, yeoman.generators.NamedBase);
/**
* A a new script to the application, in the index.html file.
*
* This is used to add AngularJS controllers or components to the application.
*/
Generator.prototype.addJavaScriptToIndex = function (script) {
try {
var fullPath = 'src/main/webapp/index.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="scripts/' + script + '"></script>'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + script + '.js ' + chalk.yellow('not added.\n'));
}
};
/**
* A a new message format to the application, in the index.html file.
*
* This is used for internationalization.
*/
Generator.prototype.addMessageformatLocaleToIndex = function (script) {
try {
var fullPath = 'src/main/webapp/index.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="bower_components/messageformat/locale/' + script + '"></script>'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + script + '.js ' + chalk.yellow('not added.\n'));
}
};
/**
* Add a new menu element, at the root of the menu.
*
* @param {string} routerName - The name of the AngularJS router that is added to the menu.
* @param {string} glyphiconName - The name of the Glyphicon (from Bootstrap) that will be displayed.
* @param {boolean} enableTranslation - If translations are enabled or not
*/
Generator.prototype.addElementToMenu = function (routerName, glyphiconName, enableTranslation) {
try {
var fullPath = 'src/main/webapp/scripts/components/navbar/navbar.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-add-element-to-menu',
splicable: [
'<li ui-sref-active="active" ><a ui-sref="' + routerName + '" data-toggle="collapse" data-target=".navbar-collapse.in"><span class="glyphicon glyphicon-' + glyphiconName + '"></span>\n' +
'  <span ' + ( enableTranslation ? 'translate="global.menu.' + routerName + '"':'' ) + '>' + _s.humanize(routerName) + '</span></a></li>'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + routerName + ' ' + chalk.yellow('not added to menu.\n'));
}
};
/**
* Add a new menu element to the admin menu.
*
* @param {string} routerName - The name of the AngularJS router that is added to the admin menu.
* @param {string} glyphiconName - The name of the Glyphicon (from Bootstrap) that will be displayed.
* @param {boolean} enableTranslation - If translations are enabled or not
*/
Generator.prototype.addElementToAdminMenu = function (routerName, glyphiconName, enableTranslation) {
try {
var fullPath = 'src/main/webapp/scripts/components/navbar/navbar.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-add-element-to-admin-menu',
splicable: [
'<li ui-sref-active="active" ><a ui-sref="' + routerName + '" data-toggle="collapse" data-target=".navbar-collapse.in"><span class="glyphicon glyphicon-' + glyphiconName + '"></span>\n' +
'  <span ' + ( enableTranslation ? 'translate="global.menu.admin.' + routerName + '"':'' ) + '>' + _s.humanize(routerName) + '</span></a></li>'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + routerName + ' ' + chalk.yellow('not added to admin menu.\n'));
}
};
/**
* Add a new entity in the "entities" menu.
*
* @param {string} routerName - The name of the AngularJS router (which by default is the name of the entity).
* @param {boolean} enableTranslation - If translations are enabled or not
*/
Generator.prototype.addEntityToMenu = function (routerName, enableTranslation) {
try {
var fullPath = 'src/main/webapp/scripts/components/navbar/navbar.html';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-add-entity-to-menu',
splicable: [
'<li ui-sref-active="active" ><a ui-sref="' + routerName + '" data-toggle="collapse" data-target=".navbar-collapse.in"><span class="glyphicon glyphicon-asterisk"></span>\n' +
'  <span ' + ( enableTranslation ? 'translate="global.menu.entities.' + routerName + '"':'' ) + '>' + _s.humanize(routerName) + '</span></a></li>'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + routerName + ' ' + chalk.yellow('not added to menu.\n'));
}
};
/**
* A a new element in the "global.json" translations.
*
* @param {string} key - Key for the menu entry
* @param {string} value - Default translated value
* @param {string} language - The language to which this translation should be added
*/
Generator.prototype.addElementTranslationKey = function(key, value, language) {
var fullPath = 'src/main/webapp/i18n/' + language + '/global.json';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-menu-add-element',
splicable: [
'"' + key + '": "' + value + '",'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + language + chalk.yellow(' not added as a new entity in the menu.\n'));
}
};
/**
* A a new element in the admin section of "global.json" translations.
*
* @param {string} key - Key for the menu entry
* @param {string} value - Default translated value
* @param {string} language - The language to which this translation should be added
*/
Generator.prototype.addAdminElementTranslationKey = function(key, value, language) {
var fullPath = 'src/main/webapp/i18n/' + language + '/global.json';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-menu-add-admin-element',
splicable: [
'"' + key + '": "' + value + '",'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + language + chalk.yellow(' not added as a new entry in the admin menu.\n'));
}
};
/**
* A a new entity in the "global.json" translations.
*
* @param {string} key - Key for the entity name
* @param {string} value - Default translated value
* @param {string} language - The language to which this translation should be added
*/
Generator.prototype.addEntityTranslationKey = function(key, value, language) {
var fullPath = 'src/main/webapp/i18n/' + language + '/global.json';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-menu-add-entry',
splicable: [
'"' + key + '": "' + value + '",'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + language + chalk.yellow(' not added as a new entity in the menu.\n'));
}
};
/**
* A a new entry as a root param in "global.json" translations.
*
* @param {string} key - Key for the entry
* @param {string} value - Default translated value or object with multiple key and translated value
* @param {string} language - The language to which this translation should be added
*/
Generator.prototype.addGlobalTranslationKey = function(key, value, language) {
var fullPath = 'src/main/webapp/i18n/' + language + '/global.json';
try {
jhipsterUtils.rewriteJSONFile(fullPath, function(jsonObj) {
jsonObj[key] = value;
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow('. Reference to ') + '(key: ' + key + ', value:' + value + ')' + chalk.yellow(' not added to global translations.\n'));
}
};
/**
* Add a translation key to all installed languages
*
* @param {string} key - Key for the entity name
* @param {string} value - Default translated value
* @param {string} method - The method to be run with provided key and value from above
* @param {string} enableTranslation - specify if i18n is enabled
*/
Generator.prototype.addTranslationKeyToAllLanguages = function(key, value, method, enableTranslation) {
if(enableTranslation) {
this.getAllInstalledLanguages().forEach(function(language) {
this[method](key, value, language);
}, this);
}
};
/**
* get all the languages installed currently
*/
Generator.prototype.getAllInstalledLanguages = function () {
var languages = [];
this.getAllSupportedLanguages().forEach(function(language) {
try {
var stats = fs.lstatSync('src/main/webapp/i18n/' + language);
if (stats.isDirectory()) {
languages.push(language);
}
} catch(e) {
// An exception is thrown if the folder doesn't exist
// do nothing as the language might not be installed
}
});
return languages;
}
/**
* get all the languages supported by JHipster
*/
Generator.prototype.getAllSupportedLanguages = function () {
return [
'ca',
'zh-cn',
'zh-tw',
'da',
'nl',
'de',
'en',
'fr',
'gl',
'hu',
'it',
'ja',
'ko',
'pl',
'pt-br',
'pt-pt',
'ro',
'ru',
'es',
'sv',
'tr',
'ta'
];
}
/**
* Add new social configuration in the "application.yml".
*
* @param {string} name - social name (twitter, facebook, ect.)
* @param {string} clientId - clientId
* @param {string} clientSecret - clientSecret
* @param {string} comment - url of how to configure the social service
*/
Generator.prototype.addSocialConfiguration = function(name, clientId, clientSecret, comment) {
var fullPath ='src/main/resources/config/application.yml';
try {
console.log(chalk.yellow(' update ') + fullPath);
var config = '';
if (comment) {
config += '# ' + comment + '\n ';
}
config += name + ':\n' +
' clientId: ' + clientId + '\n' +
' clientSecret: ' + clientSecret + '\n';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-add-social-configuration',
splicable: [
config
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow('. Reference to ') + 'social configuration ' + name + chalk.yellow(' not added.\n'));
}
};
/**
* Add a new dependency in the "bower.json".
*
* @param {string} name - dependency name
* @param {string} version - dependency version
*/
Generator.prototype.addBowerDependency = function(name, version) {
var fullPath ='bower.json';
try {
jhipsterUtils.rewriteJSONFile(fullPath, function(jsonObj) {
jsonObj.dependencies[name] = version;
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow('. Reference to ') + 'bower dependency (name: ' + name + ', version:' + version + ')' + chalk.yellow(' not added.\n'));
}
};
/**
* Add a new override configuration in the "bower.json".
*
* @param {string} bowerPackageName - Bower package name use in dependencies
* @param {array} main - You can specify which files should be selected
* @param {boolean} isIgnored - Default: false, Set to true if you want to ignore this package.
* @param {object} dependencies - You can override the dependencies of a package. Set to null to ignore the dependencies.
*
*/
Generator.prototype.addBowerOverride = function(bowerPackageName, main, isIgnored, dependencies) {
var fullPath = 'bower.json';
try {
jhipsterUtils.rewriteJSONFile(fullPath, function(jsonObj) {
var override = {};
if (main != null && main.length > 0) {
override['main'] = main;
}
if (isIgnored) {
override['ignore'] = true;
}
if (dependencies) {
override['dependencies'] = dependencies;
}
if (jsonObj.overrides === undefined) {
jsonObj.overrides = {};
}
jsonObj.overrides[bowerPackageName] = override;
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow('. Reference to ') + 'bower override configuration (bowerPackageName: ' + bowerPackageName + ', main:' + JSON.stringify(main) + ', ignore:' + isIgnored + ')' + chalk.yellow(' not added.\n'));
}
};
/**
* Add a new parameter in the ".bowerrc".
*
* @param {string} key - name of the parameter
* @param {string, obj, bool, etc.} value - value of the parameter
*/
Generator.prototype.addBowerrcParameter = function(key, value) {
var fullPath ='.bowerrc';
try {
console.log(chalk.yellow(' update ') + fullPath);
jhipsterUtils.rewriteJSONFile(fullPath, function(jsonObj) {
jsonObj[key] = value;
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow('. Reference to ') + 'bowerrc parameter (key: ' + key + ', value:' + value + ')' + chalk.yellow(' not added.\n'));
}
};
/**
* Add a new module to the angular application in "app.js".
*
* @param {string} moduleName - module name
*
*/
Generator.prototype.addAngularJsModule = function(moduleName) {
var fullPath = 'src/main/webapp/scripts/app/app.js';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-angularjs-add-module',
splicable: [
"'" + moduleName + "',"
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + moduleName + chalk.yellow(' not added to JHipster app.\n'));
}
};
/**
* Add a new configuration to the angular application in "app.js".
*
* @param {array} moduleConfigNames - modules name to import in your config
* @param {string} config - javascript to put inside config
* @param {string} comment - comment to add before the .config() to describe the config
*
* example:
*
* moduleConfigNames = ['moduleName1', 'moduleName2']
* config = 'moduleName1.doSomething();\nmoduleName2.doOtherthing();'
* comment = 'I am a config test'
*
* // I am a config test
* .config(function(moduleName1, moduleName2) {
* moduleName1.doSomething();
* moduleName2.doOtherthing();
* });
*
*/
Generator.prototype.addAngularJsConfig = function(moduleConfigNames, config, comment) {
var fullPath = 'src/main/webapp/scripts/app/app.js';
var configBlock = '';
if (comment) {
configBlock += '// ' + comment + '\n ';
}
configBlock += '.config(function (' + moduleConfigNames.join(', ') + ') {\n';
configBlock += ' ' + config.replace(/\n/g, '\n ') + '\n';
configBlock += ' })';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-angularjs-add-config',
splicable: [
configBlock
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Configuration not added to JHipster app.\n'));
}
};
/**
* Add a new interceptor to the angular application in "app.js".
* The interceptor should be in its own .js file inside scripts/components/interceptor folder
* @param {string} interceptorName - angular name of the interceptor
*
*/
Generator.prototype.addAngularJsInterceptor = function(interceptorName) {
var fullPath = 'src/main/webapp/scripts/app/app.js';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-angularjs-add-interceptor',
splicable: [
'$httpProvider.interceptors.push(\'' + interceptorName + '\');'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Interceptor not added to JHipster app.\n'));
}
};
/**
* A a new changelog to the Liquibase master.xml file.
*
* @param {string} changelogName - The name of the changelog (name of the file without .xml at the end).
*/
Generator.prototype.addChangelogToLiquibase = function (changelogName) {
try {
var fullPath = 'src/main/resources/config/liquibase/master.xml';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-liquibase-add-changelog',
splicable: [
'<include file="classpath:config/liquibase/changelog/' + changelogName + '.xml" relativeToChangelogFile="false"/>'
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + changelogName + '.xml ' + chalk.yellow('not added.\n'));
}
};
/**
* A a new column to a Liquibase changelog file for entity.
*
* @param {string} filePath - The full path of the changelog file.
* @param {string} content - The content to be added as column, can have multiple columns as well
*/
Generator.prototype.addColumnToLiquibaseEntityChangeset = function (filePath, content) {
try {
jhipsterUtils.rewriteFile({
file: filePath,
needle: 'jhipster-needle-liquibase-add-column',
splicable: [
content
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + filePath + chalk.yellow(' or missing required jhipster-needle. Column not added.\n') + e);
}
};
/**
* Add a new social connection factory in the SocialConfiguration.java file.
*
* @param {string} socialName - name of the social module. ex: 'facebook'
* @param {string} socialParameter - parameter to send to social connection ex: 'public_profile,email'
* @param {string} buttonColor - color of the social button. ex: '#3b5998'
* @param {string} buttonHoverColor - color of the social button when is hover. ex: '#2d4373'
*/
Generator.prototype.addSocialButton = function (isUseSass, socialName, socialParameter, buttonColor, buttonHoverColor) {
var socialServicefullPath = 'src/main/webapp/scripts/app/account/social/social.service.js';
var loginfullPath = 'src/main/webapp/scripts/app/account/login/login.html';
var registerfullPath = 'src/main/webapp/scripts/app/account/register/register.html';
try {
console.log(chalk.yellow('\nupdate ') + socialServicefullPath);
var serviceCode = "case '" + socialName + "': return '"+ socialParameter +"';";
jhipsterUtils.rewriteFile({
file: socialServicefullPath,
needle: 'jhipster-needle-add-social-button',
splicable: [
serviceCode
]
});
var buttonCode = '<jh-social ng-provider="'+ socialName +'"></jh-social>';
console.log(chalk.yellow('update ') + loginfullPath);
jhipsterUtils.rewriteFile({
file: loginfullPath,
needle: 'jhipster-needle-add-social-button',
splicable: [
buttonCode
]
});
console.log(chalk.yellow('update ') + registerfullPath);
jhipsterUtils.rewriteFile({
file: registerfullPath,
needle: 'jhipster-needle-add-social-button',
splicable: [
buttonCode
]
});
var buttonStyle = '.jh-btn-' + socialName + ' {\n' +
' background-color: ' + buttonColor + ';\n' +
' border-color: rgba(0, 0, 0, 0.2);\n' +
' color: #fff;\n' +
'}\n\n' +
'.jh-btn-' + socialName + ':hover, .jh-btn-' + socialName + ':focus, .jh-btn-' + socialName + ':active, .jh-btn-' + socialName + '.active, .open > .dropdown-toggle.jh-btn-' + socialName + ' {\n' +
' background-color: ' + buttonHoverColor + ';\n' +
' border-color: rgba(0, 0, 0, 0.2);\n' +
' color: #fff;\n' +
'}';
this.addMainCSSStyle(isUseSass, buttonStyle,'Add sign in style for ' + socialName);
} catch (e) {
console.log(chalk.yellow('\nUnable to add social button modification.\n' + e));
}
};
/**
* Add a new social connection factory in the SocialConfiguration.java file.
*
* @param {string} javaDir - default java directory of the project (JHipster var)
* @param {string} importPackagePath - package path of the ConnectionFactory class
* @param {string} socialName - name of the social module
* @param {string} connectionFactoryClassName - name of the ConnectionFactory class
* @param {string} configurationName - name of the section in the config yaml file
*/
Generator.prototype.addSocialConnectionFactory = function (javaDir, importPackagePath, socialName, connectionFactoryClassName, configurationName) {
var fullPath = javaDir + 'config/social/SocialConfiguration.java';
try {
console.log(chalk.yellow('\nupdate ') + fullPath);
var javaImport = 'import ' + importPackagePath +';\n';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-add-social-connection-factory-import-package',
splicable: [
javaImport
]
});
var clientId = socialName + 'ClientId';
var clientSecret = socialName + 'ClientSecret';
var javaCode = '// ' + socialName + ' configuration\n' +
' String ' + clientId + ' = environment.getProperty("spring.social.' + configurationName + '.clientId");\n' +
' String ' + clientSecret + ' = environment.getProperty("spring.social.' + configurationName + '.clientSecret");\n' +
' if (' + clientId + ' != null && ' + clientSecret + ' != null) {\n' +
' log.debug("Configuring ' + connectionFactoryClassName + '");\n' +
' connectionFactoryConfigurer.addConnectionFactory(\n' +
' new ' + connectionFactoryClassName + '(\n' +
' ' + clientId + ',\n' +
' ' + clientSecret + '\n' +
' )\n' +
' );\n' +
' } else {\n' +
' log.error("Cannot configure ' + connectionFactoryClassName + ' id or secret null");\n' +
' }\n';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-add-social-connection-factory',
splicable: [
javaCode
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Social connection ') + e + ' ' + chalk.yellow('not added.\n'));
}
};
/**
* Add new css style to the angular application in "main.css".
*
* @param {string} style - css to add in the file
* @param {string} comment - comment to add before css code
*
* example:
*
* style = '.jhipster {\n color: #baa186;\n}'
* comment = 'New JHipster color'
*
* * ==========================================================================
* New JHipster color
* ========================================================================== *
* .jhipster {
* color: #baa186;
* }
*
*/
Generator.prototype.addMainCSSStyle = function(isUseSass, style, comment) {
if (isUseSass) {
this.addMainSCSSStyle(style, comment);
}
var fullPath = 'src/main/webapp/assets/styles/main.css';
var styleBlock = '';
if (comment) {
styleBlock += '/* ==========================================================================\n';
styleBlock += comment + '\n';
styleBlock += '========================================================================== */\n';
}
styleBlock += style + '\n';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-css-add-main',
splicable: [
styleBlock
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Style not added to JHipster app.\n'));
}
};
/**
* Add new scss style to the angular application in "main.scss".
*
* @param {string} style - scss to add in the file
* @param {string} comment - comment to add before css code
*
* example:
*
* style = '.success {\n @extend .message;\n border-color: green;\n}'
* comment = 'Message'
*
* * ==========================================================================
* Message
* ========================================================================== *
* .success {
* @extend .message;
* border-color: green;
* }
*
*/
Generator.prototype.addMainSCSSStyle = function(style, comment) {
var fullPath = 'src/main/scss/main.scss';
var styleBlock = '';
if (comment) {
styleBlock += '/* ==========================================================================\n';
styleBlock += comment + '\n';
styleBlock += '========================================================================== */\n';
}
styleBlock += style + '\n';
try {
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-scss-add-main',
splicable: [
styleBlock
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Style not added to JHipster app.\n'));
}
};
/**
* Add a new Maven dependency.
*
* @param {groupId} dependency groupId
* @param {artifactId} dependency artifactId
* @param {version} explicit dependency version number
* @param {other} explicit other thing: scope, exclusions...
*/
Generator.prototype.addMavenDependency = function (groupId, artifactId, version, other) {
try {
var fullPath = 'pom.xml';
var dependency = '<dependency>\n' +
' <groupId>' + groupId + '</groupId>\n' +
' <artifactId>' + artifactId + '</artifactId>\n';
if (version) {
dependency += ' <version>' + version + '</version>\n';
}
if (other) {
dependency += other + '\n';
}
dependency += ' </dependency>';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-maven-add-dependency',
splicable: [
dependency
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + 'maven dependency (groupId: ' + groupId + ', artifactId:' + artifactId + ', version:' + version + ')' + chalk.yellow(' not added.\n'));
}
};
/**
* Add a new Maven plugin.
*
* @param {groupId} plugin groupId
* @param {artifactId} plugin artifactId
* @param {version} explicit plugin version number
* @param {other} explicit other thing: executions, configuration...
*/
Generator.prototype.addMavenPlugin = function (groupId, artifactId, version, other) {
try {
var fullPath = 'pom.xml';
var plugin = '<plugin>\n' +
' <groupId>' + groupId + '</groupId>\n' +
' <artifactId>' + artifactId + '</artifactId>\n';
if (version) {
plugin += ' <version>' + version + '</version>\n';
}
if (other) {
plugin += other + '\n';
}
plugin += ' </plugin>';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-maven-add-plugin',
splicable: [
plugin
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + 'maven plugin (groupId: ' + groupId + ', artifactId:' + artifactId + ', version:' + version + ')' + 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: 'jhipster-needle-gradle-buildscript-dependency',
splicable: [
'classpath group: \'' + group + '\', name: \'' + name + '\', version: \'' + version + '\''
]
});
} catch (e) {
console.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) {
try {
var fullPath = 'build.gradle';
jhipsterUtils.rewriteFile({
file: fullPath,
needle: 'jhipster-needle-gradle-dependency',
splicable: [
scope + ' group: \'' + group + '\', name: \'' + name + '\', version: \'' + version + '\''
]
});
} catch (e) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + group + ':' + name + ':' + version + 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) {
console.log(chalk.yellow('\nUnable to find ') + fullPath + chalk.yellow(' or missing required jhipster-needle. Reference to ') + name + chalk.yellow(' not added.\n'));
}
};
/**
* Generate a date to be used by Liquibase changelogs.
*/
Generator.prototype.dateFormatForLiquibase = function () {
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var year = "" + now_utc.getFullYear();
var month = "" + (now_utc.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
var day = "" + now_utc.getDate(); if (day.length == 1) { day = "0" + day; }
var hour = "" + now_utc.getHours(); if (hour.length == 1) { hour = "0" + hour; }
var minute = "" + now_utc.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
var second = "" + now_utc.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + "" + month + "" + day + "" + hour + "" + minute + "" + second;
};
/**
* 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) {
console.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) {
console.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.json
*
* @param {moduleConfig} configuration object for the module
*/
Generator.prototype.registerModule = function(moduleConfig) {
try {
var modulesJsonFile = '.jhipster-modules.json';
var modules;
var error, duplicate;
if (shelljs.test('-f', modulesJsonFile)) {
// file is present append to it
try {
var modulesString = fs.readFileSync(modulesJsonFile, 'utf8');
modules = JSON.parse(modulesString);
duplicate = _.findIndex(modules, moduleConfig) !== -1;
} catch (err) {
error = true;
console.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);
fs.writeFile(modulesJsonFile, JSON.stringify(modules, null, 4), 'utf8',function (err) {
if (err) return console.log('Error while writing module configuration' + err);
});
}
} catch (err) {
console.log('\n' + chalk.bold.red('Could not add jhipster module configuration' + err));
}
};
/**
* 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 entityJsonString = fs.readFileSync(file, 'utf8');
var entityJson = JSON.parse(entityJsonString);
entityJson[key] = value;
fs.writeFile(file, JSON.stringify(entityJson, null, 4), 'utf8',function (err) {
if (err) return console.log('Error while writing entity configuration' + err);
});
} catch (err) {
console.log(chalk.red('The Jhipster entity configuration file could not be read!') + err);
}
}