This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbuild_tools.js
281 lines (254 loc) · 8.41 KB
/
build_tools.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
/**
* @fileoverview Closure Builder - Build tools
*
* @license Copyright 2015 Google Inc. All Rights Reserved.
*
* 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
*
* http://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.
*
* @author [email protected] (Markus Bordihn)
*/
let fs = require('fs-extra');
let BuildType = require('./build/types.js');
let SnifferTools = require('./tools/sniffer.js');
/**
* Build Tools.
* @constructor
* @struct
* @final
*/
let BuildTools = function() {};
/**
* Detects the needed compiler types.
* @param {!BuildConfig} config
* @return {!string}
*/
BuildTools.detectType = function(config) {
if (config.hasSoyFiles() > 0) {
if (config.hasClosureFiles() === 0) {
return BuildType.SOY;
} else {
return BuildType.SOY_CLOSURE;
}
} else if (config.hasClosureFiles()) {
return BuildType.CLOSURE;
} else if (config.hasNodeFiles()) {
return BuildType.NODEJS;
} else if (config.hasJsFiles()) {
return BuildType.JAVASCRIPT;
} else if (config.hasClosureStylesheetsFiles()) {
return BuildType.CLOSURE_STYLESHEETS;
} else if (config.hasCssFiles()) {
return BuildType.CSS;
} else if (config.hasMarkdownFiles()) {
return BuildType.MARKDOWN;
} else if (config.hasResourceFiles()) {
return BuildType.RESOURCES;
} else {
return BuildType.UNKNOWN;
}
};
/**
* @param {!array|string} files
* @param {boolean=} opt_all Show all files and folders.
* @param {boolean=} opt_exclude_test Exclude test files.
* @return {array}
*/
BuildTools.sortFiles = function(files, opt_all, opt_exclude_test) {
if (!Array.isArray(files)) {
files = [files];
}
let fileList = [];
let knownFile = {};
for (let i = files.length - 1; i >= 0; i--) {
let file = files[i];
if (file.constructor === Array) {
for (let i2 = file.length - 1; i2 >= 0; i2--) {
let subFile = file[i2];
if (!knownFile[subFile] &&
(opt_all || subFile.indexOf('.') !== -1)) {
fileList.push(subFile);
knownFile[subFile] = true;
}
}
} else {
if (!knownFile[file] &&
(opt_all || file.indexOf('.') !== -1)) {
fileList.push(file);
knownFile[file] = true;
}
}
}
if (opt_exclude_test) {
return BuildTools.filterTestFiles(fileList);
}
return fileList;
};
/**
* Returns the needed build requirements for the given config.
* @param {BuildConfig} config
* @return {Object} Build requirements
*/
BuildTools.getBuildRequirements = function(config) {
let depsConfig = this.scanFiles(config.deps);
let srcsConfig = this.scanFiles(config.srcs, config.name);
let soyConfig = this.scanFiles(config.soy);
let mdConfig = this.scanFiles(config.markdown);
return {
closureFiles: [].concat(depsConfig.closureFiles, srcsConfig.closureFiles),
cssFiles: [].concat(srcsConfig.cssFiles),
closureStylesheetsFiles: [].concat(srcsConfig.closureStylesheetsFiles),
jsFiles: [].concat(depsConfig.jsFiles, srcsConfig.jsFiles),
markdownFiles: [].concat(mdConfig.markdownFiles),
nodeFiles: [].concat(srcsConfig.nodeFiles),
soyFiles: [].concat(depsConfig.soyFiles, soyConfig.soyFiles,
srcsConfig.soyFiles),
entryPoint: (depsConfig.entryPoint || srcsConfig.entryPoint),
requireClosureExport: (srcsConfig.requireClosureExport),
requireClosureLibrary: (depsConfig.requireClosureLibrary ||
srcsConfig.requireClosureLibrary),
requireClosureLibraryUI: (depsConfig.requireClosureLibraryUI ||
srcsConfig.requireClosureLibraryUI),
requiredECMAVersion: [depsConfig.requiredECMAVersion,
srcsConfig.requiredECMAVersion].sort().pop(),
requireSoyLibrary: (depsConfig.requireSoyLibrary ||
srcsConfig.requireSoyLibrary || soyConfig.requireSoyLibrary),
requireSoyi18n: (soyConfig.requireSoyi18n || srcsConfig.requireSoyi18n),
};
};
/**
* Scan files for certain file types and return list of files and requirements.
* @param {!array|string} files
* @param {string=} opt_entry_point
* @return {Object}
*/
BuildTools.scanFiles = function(files, opt_entry_point) {
if (!Array.isArray(files)) {
files = [files];
}
let closureFiles = [];
let closureStylesheetsFiles = [];
let cssFiles = [];
let entryPoint = '';
let jsFiles = [];
let markdownFiles = [];
let nodeFiles = [];
let requireClosureExport = false;
let requireClosureLibrary = false;
let requireClosureLibraryUI = false;
let requireSoyLibrary = false;
let requireSoyi18n = false;
let requiredECMAVersion = '';
let soyFiles = [];
for (let i = files.length - 1; i >= 0; i--) {
let file = files[i];
// Handling soy files.
if (file.endsWith('.soy')) {
let soyContent = fs.readFileSync(file, 'utf8');
if (soyContent.includes('{i18n}') &&
soyContent.includes('{/i18n}')) {
requireSoyi18n = true;
}
requireSoyLibrary = true;
soyFiles.push(file);
// Handling JavaScript files.
} else if (file.endsWith('.js')) {
let fileContent = fs.readFileSync(file, 'utf8');
if (fileContent.includes('goog.provide(') ||
fileContent.includes('goog.require(') ||
fileContent.includes('goog.module(')) {
if (fileContent.includes(' * @export')) {
requireClosureExport = true;
}
closureFiles.push(file);
} else if (fileContent.includes('require(') &&
fileContent.includes('module.exports')) {
nodeFiles.push(file);
} else {
jsFiles.push(file);
}
// Validating possible entry points.
if (opt_entry_point) {
if (fileContent.includes('goog.provide(\'' + opt_entry_point + '\'') ||
fileContent.includes('goog.module(\'' + opt_entry_point + '\'')) {
entryPoint = opt_entry_point;
}
}
// Require closure library ?
if (fileContent.includes('goog.require(\'goog.') ||
fileContent.includes('goog.require("goog.')) {
if (fileContent.includes('goog.require(\'goog.ui') ||
fileContent.includes('goog.require("goog.ui')) {
requireClosureLibraryUI = true;
}
requireClosureLibrary = true;
}
// Require soy library ?
if (fileContent.includes('goog.require(\'soy') ||
fileContent.includes('goog.require(\'soydata')) {
requireSoyLibrary = true;
}
// Detecting min. ECMA Script version ?
let ECMAScriptVersion = SnifferTools.getECMAScriptVersion(fileContent);
if (ECMAScriptVersion && requiredECMAVersion) {
if (ECMAScriptVersion.split('_')[1] >
requiredECMAVersion.split('_')[1]) {
requiredECMAVersion = ECMAScriptVersion;
}
} else if (ECMAScriptVersion) {
requiredECMAVersion = ECMAScriptVersion;
}
// Handling Closure stylesheets (.gss) file.
} else if (file.endsWith('.gss')) {
closureStylesheetsFiles.push(file);
// Handling CSS files.
} else if (file.endsWith('.css')) {
cssFiles.push(file);
// Handling Markdown files.
} else if (file.endsWith('.md')) {
markdownFiles.push(file);
}
}
return {
closureFiles: closureFiles,
cssFiles: cssFiles,
entryPoint: entryPoint,
closureStylesheetsFiles: closureStylesheetsFiles,
jsFiles: jsFiles,
markdownFiles: markdownFiles,
nodeFiles: nodeFiles,
soyFiles: soyFiles,
requireClosureExport: requireClosureExport,
requireClosureLibrary: requireClosureLibrary,
requireClosureLibraryUI: requireClosureLibraryUI,
requiredECMAVersion: requiredECMAVersion,
requireSoyLibrary: requireSoyLibrary,
requireSoyi18n: requireSoyi18n,
};
};
/**
* @param {array} files
* @return {array}
*/
BuildTools.filterTestFiles = function(files) {
for (let i = files.length - 1; i >= 0; i--) {
let file = files[i];
if (file.indexOf('_test.js') !== -1 ||
file.indexOf('_testhelper.js') !== -1 ||
file.indexOf('/demos/') !== -1 ||
file.indexOf('/deps.js') !== -1) {
files.splice(i, 1);
}
}
return files;
};
module.exports = BuildTools;