forked from CesiumGS/gltf-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
338 lines (313 loc) · 12.4 KB
/
gulpfile.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
'use strict';
var Cesium = require('cesium');
var child_process = require('child_process');
var fsExtra = require('fs-extra');
var gulp = require('gulp');
var Jasmine = require('jasmine');
var jasmineSpecReporter = require('jasmine-spec-reporter');
var open = require('open');
var path = require('path');
var Promise = require('bluebird');
var yargs = require('yargs');
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;
var argv = yargs.argv;
// Add third-party node module binaries to the system path
// since some tasks need to call them directly.
var environmentSeparator = process.platform === 'win32' ? ';' : ':';
var nodeBinaries = path.join(__dirname, 'node_modules', '.bin');
process.env.PATH += environmentSeparator + nodeBinaries;
var specFiles = ['**/*.js', '!node_modules/**', '!coverage/**', '!dist/**'];
function excludeCompressedTextures(jasmine) {
var excludedSpecs = ['compressTexturesSpec.js', 'compressTexturesMultipleFormatsSpec.js'];
var specFiles = jasmine.specFiles;
var specsLength = specFiles.length;
var excludedLength = excludedSpecs.length;
for (var i = specsLength - 1; i >= 0; --i) {
for (var j = 0; j < excludedLength; ++j) {
if (specFiles[i].indexOf(excludedSpecs[j]) > -1) {
specFiles.splice(i, 1);
break;
}
}
}
}
gulp.task('test', function (done) {
var jasmine = new Jasmine();
jasmine.loadConfigFile('specs/jasmine.json');
if (defined(argv.excludeCompressedTextures)) {
// Exclude compressTexturesSpec for Travis builds
// Travis runs Ubuntu 12.04.5 which has glibc 2.15, while crunch requires glibc 2.22 or higher
excludeCompressedTextures(jasmine);
}
jasmine.addReporter(new jasmineSpecReporter.SpecReporter({
displaySuccessfulSpec: !defined(argv.suppressPassed) || !argv.suppressPassed
}));
jasmine.execute();
jasmine.onComplete(function (passed) {
done(argv.failTaskOnError && !passed ? 1 : 0);
});
});
gulp.task('test-watch', function () {
gulp.watch(specFiles).on('change', function () {
//We can't simply depend on the test task because Jasmine
//does not like being run multiple times in the same process.
try {
child_process.execSync('jasmine JASMINE_CONFIG_PATH=specs/jasmine.json', {
stdio: [process.stdin, process.stdout, process.stderr]
});
} catch (exception) {
console.log('Tests failed to execute.');
}
});
});
gulp.task('coverage', function () {
fsExtra.removeSync('coverage/server');
// Exclude compressTexturesSpec from coverage for Travis builds
// Travis runs Ubuntu 12.04.5 which has glibc 2.15, while crunch requires glibc 2.22 or higher
var additionalExcludes = '';
if (defined(argv.excludeCompressedTextures)) {
additionalExcludes += '-x "specs/lib/compressTexturesSpec.js"';
additionalExcludes += '-x "specs/lib/compressTexturesMultipleFormatsSpec.js"';
}
child_process.execSync('nyc' +
' --all' +
' --reporter=lcov' +
' --dir coverage' +
' -x "specs/**" -x "bin/**" -x "coverage/**" -x "dist/**" -x "index.js" -x "gulpfile.js"' + additionalExcludes +
' node_modules/jasmine/bin/jasmine.js' +
' JASMINE_CONFIG_PATH=specs/jasmine.json', {
stdio: [process.stdin, process.stdout, process.stderr]
});
open('coverage/lcov-report/index.html');
});
function amdify(source, subDependencyMapping) {
var fullMatch;
var variableName;
var requireVariable;
var requirePath;
source = source.replace(/\r\n/g, '\n');
var outputSource = source;
// find module exports
var returnValue;
var findModuleExportsRegex = /module.exports\s*=\s*(.*?);\n/;
var findModuleExports = findModuleExportsRegex.exec(source);
if (defined(findModuleExports && findModuleExports.length > 0)) {
fullMatch = findModuleExports[0];
returnValue = findModuleExports[1];
// remove module.exports from output source
outputSource = outputSource.replace(fullMatch, '');
}
// create require mapping for dependencies
var findRequireRegex = /var\s+(.+?)\s*=\s*require\('(.+?)'\);\n/g;
var findRequire = findRequireRegex.exec(source);
var requireMapping = {};
while (defined(findRequire) && findRequire.length > 0) {
fullMatch = findRequire[0];
variableName = findRequire[1];
requirePath = findRequire[2];
requireMapping[variableName] = requirePath;
// remove requires from output source
outputSource = outputSource.replace(fullMatch, '');
findRequire = findRequireRegex.exec(source);
}
// find places where sub-dependencies are pulled from a require
var subdependencyMapping = {};
var removeRequireMapping = [];
for (requireVariable in requireMapping) {
if (requireMapping.hasOwnProperty(requireVariable)) {
requirePath = requireMapping[requireVariable];
var findSubdependencyString = 'var\\s+(.+?)\\s*?=\\s*?' + requireVariable + '\\.(.+?);\n';
var findSubdependencyRegex = new RegExp(findSubdependencyString, 'g');
var findSubdependency = findSubdependencyRegex.exec(source);
while (defined(findSubdependency) && findSubdependency.length > 0) {
var mapping = subDependencyMapping[requirePath];
if (!defined(mapping)) {
throw new DeveloperError('Build Failed: Module sub-dependency found for ' + requirePath + ' with no defined mapping behavior.');
}
removeRequireMapping.push(requireVariable);
fullMatch = findSubdependency[0];
variableName = findSubdependency[1];
var subdependencyPath = findSubdependency[2];
subdependencyMapping[variableName] = mapping.prefix + subdependencyPath;
// remove sub-dependency declarations from output source
outputSource = outputSource.replace(fullMatch, '');
findSubdependency = findSubdependencyRegex.exec(source);
}
}
}
// Top-level modules can be removed if mapped
while (removeRequireMapping.length > 0) {
var removeVariableName = removeRequireMapping.pop();
delete requireMapping[removeVariableName];
}
// join sub-dependencies with requireMapping
for (var subdependencyVariable in subdependencyMapping) {
if (subdependencyMapping.hasOwnProperty(subdependencyVariable)) {
requireMapping[subdependencyVariable] = subdependencyMapping[subdependencyVariable];
}
}
// amdify source
// indent
outputSource = outputSource.replace(/\n/g, '\n ');
// wrap define header
var variables = [];
var paths = [];
for (var variable in requireMapping) {
if (requireMapping.hasOwnProperty(variable)) {
variables.push(variable);
paths.push(requireMapping[variable]);
}
}
var definePathsHeader = '\'' + paths.join('\',\n \'') + '\'';
var defineVariablesHeader = variables.join(',\n ');
var defineHeader = '/*global define*/\n' +
'define([\n' +
' ' + definePathsHeader + '\n' +
' ], function(\n' +
' ' + defineVariablesHeader + ') {\n ';
var defineFooter = '\n});\n';
if (defined(returnValue)) {
defineFooter = '\n return ' + returnValue + ';' + defineFooter;
}
outputSource = defineHeader + outputSource + defineFooter;
// remove repeat newlines
outputSource = outputSource.replace(/\n\s*\n/g, '\n\n');
return outputSource;
}
function combine(source) {
var fullMatch;
var variableName;
var requirePath;
source = source.replace(/\r\n/g, '\n');
var outputSource = source;
// find module exports
var returnValue;
var findModuleExportsRegex = /module.exports\s*=\s*(.*?);\n/;
var findModuleExports = findModuleExportsRegex.exec(source);
if (defined(findModuleExports && findModuleExports.length > 0)) {
fullMatch = findModuleExports[0];
returnValue = findModuleExports[1];
// remove module.exports from output source
outputSource = outputSource.replace(fullMatch, '');
}
// create require mapping for dependencies
var findRequireRegex = /var\s+(.+?)\s*=\s*require\('(.+?)'\);\n/g;
var findRequire = findRequireRegex.exec(source);
var requireMapping = {};
while (defined(findRequire) && findRequire.length > 0) {
fullMatch = findRequire[0];
variableName = findRequire[1];
requirePath = findRequire[2];
requireMapping[variableName] = requirePath;
// remove requires from output source
outputSource = outputSource.replace(fullMatch, '');
findRequire = findRequireRegex.exec(source);
}
// combine source
// indent
outputSource = outputSource.replace(/\n/g, '\n ');
// wrap define header
var variables = [];
var paths = [];
for (var variable in requireMapping) {
if (requireMapping.hasOwnProperty(variable)) {
variables.push(variable);
paths.push(requireMapping[variable]);
}
}
var defineHeader = '/*global define*/\n' +
'var ' + returnValue + ' = (function() {\n ';
var defineFooter = '\n}());\n';
if (defined(returnValue)) {
defineFooter = '\n return ' + returnValue + ';' + defineFooter;
}
outputSource = defineHeader + outputSource + defineFooter;
// remove repeat newlines
outputSource = outputSource.replace(/\n\s*\n/g, '\n\n');
return outputSource;
}
gulp.task('build-cesium', function () {
var basePath = 'lib';
var outputDir = 'dist/cesium';
var files = [
'addDefaults.js',
'addExtensionsUsed.js',
'addPipelineExtras.js',
'byteLengthForComponentType.js',
'findAccessorMinMax.js',
'getAccessorByteStride.js',
'getStatistics.js',
'getUniqueId.js',
'numberOfComponentsForType.js',
'parseBinaryGltf.js',
'processModelMaterialsCommon.js',
'techniqueParameterForSemantic.js'
];
var subDependencyMapping = {
cesium : {
prefix : '../../Core/'
}
};
Promise.map(files, function(fileName) {
var filePath = path.join(basePath, fileName);
return fsExtra.readFile(filePath)
.then(function(buffer) {
var source = buffer.toString();
source = amdify(source, subDependencyMapping);
var outputPath = path.join(outputDir, fileName);
return fsExtra.outputFile(outputPath, source);
});
});
});
gulp.task('build-cesium-combine', function () {
var basePath = 'lib';
var outputDir = 'dist/cesium-combined';
var files = [
'getStatistics.js'
];
Promise.map(files, function(fileName) {
var filePath = path.join(basePath, fileName);
return fsExtra.readFile(filePath)
.then(function(buffer) {
var source = buffer.toString();
source = combine(source);
var outputPath = path.join(outputDir, fileName);
return fsExtra.outputFile(outputPath, source);
});
});
});
gulp.task('cloc', function() {
var cmdLine;
var clocPath = path.join('node_modules', 'cloc', 'lib', 'cloc');
//Run cloc on primary Source files only
var source = new Promise(function(resolve, reject) {
cmdLine = 'perl ' + clocPath + ' --quiet --progress-rate=0' +
' lib/';
child_process.exec(cmdLine, function(error, stdout, stderr) {
if (error) {
console.log(stderr);
return reject(error);
}
console.log('Source:');
console.log(stdout);
resolve();
});
});
//If running cloc on source succeeded, also run it on the tests.
return source.then(function() {
return new Promise(function(resolve, reject) {
cmdLine = 'perl ' + clocPath + ' --quiet --progress-rate=0' +
' specs/lib/';
child_process.exec(cmdLine, function(error, stdout, stderr) {
if (error) {
console.log(stderr);
return reject(error);
}
console.log('Specs:');
console.log(stdout);
resolve();
});
});
});
});