This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utils.class.js
265 lines (233 loc) · 10.8 KB
/
Utils.class.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
/**
* Created by francesco on 13/07/17.
*/
var fs = require('fs-extra');
var path = require('path');
var format = require("string-template");
let _ = require('lodash');
var Handlebars = require('handlebars');
var helpers = require('handlebars-helpers')();
Handlebars.registerHelper('escape', function(variable) {
return variable.replace(/(['"])/g, '\\$1');
});
Handlebars.registerHelper('raw-helper', function(options) {
return options.fn();
});
var gutil = require('gulp-util');
var inquirer = require('inquirer');
module.exports = class Utils {
static findKeyInObjectArray(list, id, key, secondaryKey) {
if (!key) key = "name";
if (!secondaryKey) secondaryKey = "display_name";
return list.find(el => el[key] == id || (el[secondaryKey] && el[secondaryKey] == id));
}
static mergeObjs(obj1, obj2, overwrite) {
if (!_.isEmpty(obj2))
Object.keys(obj2).forEach((key) => { if (overwrite || obj1[key] == undefined) obj1[key] = obj2[key] } );
return obj1
}
static buildProjectObject(project_info, language, build_tool) {
language.language = language.name;
delete language.name;
if (language.var_templates)
delete language.var_templates;
if (build_tool.var_templates)
delete build_tool.var_templates;
if (language.questions)
delete language.questions;
if (build_tool.questions)
delete build_tool.questions;
if (build_tool.dependencies)
project_info.dependencies = _.concat(project_info.dependencies, build_tool.dependencies);
if (build_tool.npm_dependencies)
if (project_info.npm_dependencies)
project_info.npm_dependencies = _.concat(project_info.npm_dependencies, build_tool.npm_dependencies);
else
project_info.npm_dependencies = build_tool.npm_dependencies;
project_info = Utils.mergeObjs(project_info, language, true);
project_info.build_tool = build_tool;
return project_info;
}
static mkdirs(folderPath, mode) {
var folders = [];
var tmpPath = path.normalize(folderPath);
var exists = fs.existsSync(tmpPath);
while (!exists) {
folders.push(tmpPath);
tmpPath = path.join(tmpPath, '..');
exists = fs.existsSync(tmpPath);
}
for (var i = folders.length - 1; i >= 0; i--) {
fs.mkdirSync(folders[i], mode);
}
}
static pickSelection(message, list) {
return new Promise((resolve, reject) => {
if (list.length > 1) {
let optionsList = list.map(el => (el.display_name) ? {name: el.display_name, value: el} : {name: el.name, value: el});
inquirer.prompt({ name: 'answer', message: message, type: 'list', choices: optionsList }).then(function (answer) {
resolve(answer.answer);
});
} else {
resolve(list[0]);
}
});
}
static checkIfDirIsEmpty(dir) {
return !['pom.xml', 'build.gradle', 'package.json', 'src', 'target'].some(function (el) {
return fs.existsSync(dir + path.sep + el);
});
}
static loadGeneratorTemplates(templates, generator_key, language_key = null) {
let result;
let templatesDir;
if (language_key)
templatesDir = path.resolve(path.join(__project_templates, generator_key, language_key));
else
templatesDir = path.resolve(path.join(__project_templates, generator_key));
if (templates instanceof Array) {
result = [];
templates = templates.map((template) => path.join(templatesDir, template));
templates.forEach((templatePath) => {
try {
let templateSource = fs.readFileSync(templatePath, 'utf-8');
result.push(Handlebars.compile(templateSource, {noEscape: true}));
} catch (e) {}
});
} else {
result = {};
Object.keys(templates).map((key) => {
try {
let templateSource = fs.readFileSync(path.join(templatesDir, templates[key]), 'utf-8');
result[key] = Handlebars.compile(templateSource, {noEscape: true});
} catch (e) {}
});
}
return result;
}
static loadSingleTemplate(template, generator_key, language_key = undefined) {
let templatesDir;
if (language_key)
templatesDir = path.resolve(path.join(__project_templates, generator_key, language_key));
else
templatesDir = path.resolve(path.join(__project_templates, generator_key));
let templateSource = fs.readFileSync(path.join(templatesDir, template), 'utf-8');
return Handlebars.compile(templateSource, {noEscape: true});
}
static loadBuildFilesTemplates(templates, build_file_key) {
let result = [];
let buildFilesDir = path.resolve(path.join(__build_files_templates, build_file_key));
templates = templates.map((template) => path.join(buildFilesDir, template));
templates.forEach((templatePath) => {
let templateSource = fs.readFileSync(templatePath, 'utf-8');
result.push(Handlebars.compile(templateSource, {noEscape: true}));
});
return result;
}
static writeFilesArraySync(files) {
for (let i in files) {
let finalPath = path.resolve(path.join(process.cwd(), files[i].path));
fs.mkdirpSync(path.dirname(finalPath));
gutil.log("Writing file ", gutil.colors.cyan(path.relative(process.cwd(), finalPath)));
fs.writeFileSync(finalPath, files[i].content, 'utf-8');
}
}
static processVariablesTemplates(obj, var_templates) {
if (var_templates) {
Object.keys(var_templates).forEach((key) => {
if (var_templates[key] instanceof Function)
obj[key] = var_templates[key](obj);
else
obj[key] = format(var_templates[key], obj)
});
}
return obj;
}
static processQuestions(questions, var_templates, project_info) {
return new Promise((resolve, reject) => {
if (questions)
inquirer.prompt(questions).then(answers => {
resolve(Utils.processVariablesTemplates(answers, var_templates));
});
});
}
static processLanguage(allowedLanguages, project_info) {
return new Promise((resolve, reject) => {
Utils.pickSelection("Which language: ", allowedLanguages)
.then((language) => {
Utils.pickSelection("Which build tool: ", language.build_tools).then((build_tool) => {
if (language.questions) {
inquirer.prompt(language.questions).then(answers => {
let result = {
language: Utils.processVariablesTemplates(Utils.mergeObjs(language, answers, true), language.var_templates),
build_tool: Utils.processVariablesTemplates(build_tool, build_tool.var_templates)
};
if (project_info)
result.project_info = Utils.buildProjectObject(project_info, language, build_tool);
resolve(result);
});
} else {
let result = {
language: Utils.processVariablesTemplates(language, language.var_templates),
build_tool: Utils.processVariablesTemplates(build_tool, build_tool.var_templates)
};
if (project_info)
result.project_info = Utils.buildProjectObject(project_info, language, build_tool);
resolve(result);
}
})
});
});
}
static generateRenderingFunction(generator_name) {
return function (project_info) {
// Load templates
let templatesFunctions = Utils.loadGeneratorTemplates(project_info.templates, generator_name, project_info.language);
let buildFilesTemplatesFunctions = Utils.loadBuildFilesTemplates(project_info.build_tool.templates, project_info.build_tool.name);
// Some lodash magic
return _.concat(
_.zipWith(
project_info.templates.map(p => path.join(project_info.src_dir, p)), // Prepend to paths the src_dir path
templatesFunctions.map(template => template(project_info)), // Render templates
(path, content) => new Object({path: path, content: content}) // Push into the array in a form {path: path, content: content}
),
_.zipWith(
project_info.build_tool.templates,
buildFilesTemplatesFunctions.map(template => template(project_info)),
(path, content) => new Object({path: path, content: content})
)
)
}
}
static generateComplexRenderingFunction(generator_name) {
return function (project_info) {
let result = [];
_.forOwn(project_info.templates, (value, key) => {
let templatesFunctions = Utils.loadGeneratorTemplates(value, generator_name, project_info.language);
result = _.concat(result, _.zipWith(
value.map(p => path.join(project_info[key + "_dir"], p)),
templatesFunctions.map(template => template(project_info)),
(path, content) => new Object({path: path, content: content})
));
});
let buildFilesTemplatesFunctions = Utils.loadBuildFilesTemplates(project_info.build_tool.templates, project_info.build_tool.name);
return _.concat(
result,
_.zipWith(
project_info.build_tool.templates,
buildFilesTemplatesFunctions.map(template => template(project_info)),
(path, content) => new Object({path: path, content: content})
)
)
}
}
static generateGenerationFunction(languagesMetadata, renderFunction) {
return function(project_info, done) {
// Process questions about the language
Utils.processLanguage(languagesMetadata, project_info).then((result) => {
Utils.writeFilesArraySync(renderFunction(result.project_info));
done();
});
}
}
}