forked from HadoukenIO/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
444 lines (390 loc) · 15.1 KB
/
Gruntfile.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
/*
Copyright 2017 OpenFin Inc.
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.
*/
'use strict';
/**
*
* Build tasks to facilitate the creation of an asar file.
*
*/
const fs = require('fs');
const path = require('path');
const asar = require('asar');
const nativeBuilder = require('electron-rebuild');
const wrench = require('wrench');
const dependencies = Object.keys(require('./package.json').dependencies).map(dep => `${dep}/**`);
const srcFiles = ['src/**/*.js', 'index.js', 'Gruntfile.js'];
//optional dependencies that we ship.
const optionalDependencies = [
'hadouken-js-adapter/**',
'runtime-p2p/**'
];
// https://github.com/beautify-web/js-beautify#options
// (Options in above-linked page are hyphen-separarted but here must be either camelCase or underscore_separated.)
const beautifierOptions = {
js: {
braceStyle: 'collapse,preserve-inline'
}
};
try {
var openfinSign = require('openfin-sign');
} catch (err) {
openfinSign = function() {};
}
// OpenFin commercial license
const commercialLic = `/*
Copyright 2017 OpenFin Inc.
Licensed under OpenFin Commercial License you may not use this file except in compliance with your Commercial License.
Please contact OpenFin Inc. at [email protected] to obtain a Commercial License.
*/
`;
// Open-source license
const openSourceLic = `/*
Copyright 2017 OpenFin Inc.
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.
*/
`;
module.exports = (grunt) => {
// The default task is to build and and package resulting in an asar file in ./out/
grunt.registerTask('default', ['build-pac']);
grunt.registerTask('deploy', ['build-dev', 'copy-local']);
// Load all grunt tasks matching the ['grunt-*', '@*/grunt-*'] patterns
require('load-grunt-tasks')(grunt);
grunt.initConfig({
copy: {
lib: {
files: [{
cwd: './node_modules',
expand: true,
src: [dependencies, optionalDependencies],
dest: 'staging/core/node_modules'
}]
},
etc: { //other artifacts that need copying
files: [{
src: ['package.json', 'blank.ico'],
dest: 'staging/core/'
}]
},
login: { //login dialog artifacts that need copying
files: [{
src: ['src/login/*'],
dest: 'staging/core/'
}]
},
certificate: { //certificate dialog artifacts that need copying
files: [{
src: ['src/certificate/*'],
dest: 'staging/core/'
}]
}
},
// Transpile TypeScript to JavaScript
ts: {
default: {
tsconfig: true
}
},
// Lint TypeScript files
tslint: {
options: {
// todo: use 'node_modules/tslint-microsoft-contrib/tslint.json'
// when transition to TypeScript is fully done
configuration: grunt.file.readJSON('tslint.json'),
rulesDirectory: 'node_modules/tslint-microsoft-contrib',
force: false
},
files: {
src: [
'src/**/*.ts',
'!src/browser/api/notifications/*.ts',
'!src/**/*.d.ts',
'test/**/*.ts',
'!test/**/*.d.ts',
]
}
},
babel: {
dist: {
files: [{
expand: true,
src: srcFiles,
dest: 'staging/core'
}]
}
},
jshint: {
src: srcFiles,
options: {
jshintrc: './.jshintrc'
}
},
watch: { //just build and lint for now
scripts: {
files: srcFiles,
tasks: ['jshint', 'jsbeautifier'],
options: {}
},
deploy: {
files: srcFiles,
tasks: ['deploy']
}
},
jsbeautifier: {
default: {
src: ['src/**/*.js', 'index.js'],
options: beautifierOptions
},
'git-pre-commit': {
src: ['src/**/*.js', 'index.js'],
options: Object.assign({ mode: 'VERIFY_ONLY' }, beautifierOptions)
}
},
mochaTest: {
default: {
src: ['staging/core/test/**.js']
}
}
});
grunt.registerTask('build-dev', [
'license',
'jshint',
'jsbeautifier:default',
'clean',
'babel',
'tslint',
'ts',
'mochaTest',
'copy:lib',
'copy:etc',
'copy:login',
'copy:certificate',
'sign-files'
]);
grunt.registerTask('test', [
'license',
'jshint',
'jsbeautifier',
'clean',
'babel',
'tslint',
'ts',
'mochaTest',
]);
grunt.registerTask('build-pac', [
'license',
'jshint',
'jsbeautifier',
'clean',
'babel',
'tslint',
'ts',
'mochaTest',
'copy',
'build-deploy-modules',
'sign-files',
'package',
'sign-asar'
]);
grunt.registerTask('typescript', [
'tslint',
'ts'
]);
grunt.registerTask('sign-files', function() {
wrench.readdirSyncRecursive('staging/core').forEach(function(filename) {
let filepath = path.join('staging', 'core', filename);
if (!fs.statSync(filepath).isDirectory()) {
openfinSign(filepath);
}
});
grunt.log.ok('Finished signing files.');
});
grunt.registerTask('sign-asar', function() {
openfinSign('out/app.asar');
grunt.log.ok('Finished signing asar.');
});
grunt.registerTask('clean', 'clean the out house', function() {
wrench.rmdirSyncRecursive('staging', true);
wrench.rmdirSyncRecursive('out', true);
});
grunt.registerTask('build-deploy-modules', 'Build native modules', function() {
const done = this.async();
const nativeModVersion = grunt.option('nmv') || 'v5.10.0';
const nodeHeaderVersion = grunt.option('nhv') || 'v0.37.5';
const rebuildNativeVersion = grunt.option('rnv') || '0.37.5';
const outdir = './staging/core/node_modules';
const arch = grunt.option('arch') || 'ia32';
grunt.log.ok('Checking if rebuilding native modules is required.');
nativeBuilder.shouldRebuildNativeModules(undefined, nativeModVersion).then(function(shouldBuild) {
if (!shouldBuild) {
grunt.log.ok('Skipping native builds.');
done();
return true;
}
grunt.log.ok('Installing headers.');
return nativeBuilder.installNodeHeaders(nodeHeaderVersion, undefined, undefined, 'ia32')
.then(function() {
// Ensure directory tree exists
grunt.file.mkdir(outdir);
grunt.log.ok('Building native modules.');
nativeBuilder.rebuildNativeModules(rebuildNativeVersion, outdir, undefined, undefined, arch).then(function() {
done();
});
});
}).catch(function(e) {
grunt.log.error('Building modules didn\'t work!');
grunt.log.error(e);
done();
});
});
grunt.registerTask('package', 'Package in an asar', function() {
const done = this.async();
asar.createPackage('staging/core', 'out/app.asar', function() {
grunt.log.ok('Finished packaging as asar.');
wrench.rmdirSyncRecursive('staging', true);
grunt.log.ok('Cleaned up staging.');
done();
});
});
grunt.registerTask('copy-local', function() {
const target = grunt.option('target');
const done = this.async();
if (!target) {
grunt.log.ok('No target specified...skipping local deploy.');
done();
} else {
const asarFile = path.join(target, 'app.asar');
const asarFileBk = path.join(target, 'app.asar.bk');
const defaultAppFolder = path.join(target, 'default_app');
const origin = './staging/core';
if (fs.existsSync(asarFile)) {
fs.renameSync(asarFile, asarFileBk);
grunt.log.ok(`renamed: ${asarFile} to: ${asarFileBk}`);
}
wrench.copyDirRecursive(origin, defaultAppFolder, {
forceDelete: true
}, function() {
grunt.log.ok(`Deployed to: ${defaultAppFolder}`);
done();
});
}
});
/**
* This task goes through the list of all files that need to have
* a license and validates that they have proper licenses.
* Usage:
* 1. grunt license - default
* 2. grunt license:add - will add open-source license to all non-commercial files
* 3. grunt license:remove - will remove licenses from all files
*/
grunt.registerTask('license', (option) => {
let foundLicensingProblem = false;
// List of files that must have OpenFin commercial license
const ofLicensedFiles = [
'src/browser/api/external_application.ts',
'src/browser/api_protocol/external_application.js',
'src/browser/api_protocol/transport_strategy/base_handler.ts',
'src/browser/api_protocol/transport_strategy/ws_strategy.ts',
'src/browser/api_protocol/api_handlers/authorization.js',
'src/browser/api_protocol/api_handlers/api_policy_processor.ts',
'src/browser/api_protocol/api_handlers/external_application.ts',
'src/browser/api_protocol/api_handlers/mesh_middleware.ts',
'src/browser/port_discovery.ts',
'src/browser/rvm/rvm_message_bus.ts',
'src/browser/rvm/runtime_initiated_topics/app_assets.ts',
'src/browser/remote_subscriptions.ts',
'src/browser/rvm/runtime_initiated_topics/rvm_info.js',
'src/browser/rvm/utils.ts',
'src/browser/external_window_event_adapter.js',
'src/browser/connection_manager.ts',
'src/browser/plugins.ts',
'src/browser/transport.ts',
'src/browser/transports/socket_server.js',
'src/browser/transports/wm_copydata.ts',
'src/browser/transports/base.ts',
'src/browser/transports/chromium_ipc.ts',
'src/browser/transports/electron_ipc.ts'
];
// List of files that need to have some kind of license
const allFilesForLicense = grunt.file.expand(
'src/**/*.ts',
'src/**/*.js',
'test/**/*.ts',
'test/**/*.js',
'index.ts',
'index.js'
);
// Goes through all the files and verifies licenses
allFilesForLicense.forEach(filePartPath => {
const fileFullPath = path.join(__dirname, filePartPath);
let fileContent = String(fs.readFileSync(fileFullPath));
// When given 'remove' option, just remove the license
if (option === 'remove') {
fileContent = fileContent.replace(openSourceLic, '');
fileContent = fileContent.replace(commercialLic, '');
fs.writeFileSync(fileFullPath, fileContent);
grunt.log.writeln(`Removed license from ${filePartPath}`['yellow'].bold);
return;
}
// OpenFin commercial license file
if (ofLicensedFiles.includes(filePartPath)) {
// Remove open-source license from a commercial file
if (fileContent.includes(openSourceLic)) {
fileContent = fileContent.replace(openSourceLic, '');
fs.writeFileSync(fileFullPath, fileContent);
grunt.log.writeln(`Removed open-source license from OpenFin commercial file ${filePartPath}`['yellow'].bold);
}
// Add license if missing
if (!fileContent.includes(commercialLic)) {
fileContent = commercialLic + fileContent;
fs.writeFileSync(fileFullPath, fileContent);
grunt.log.writeln(`Added missing OpenFin commercial license to ${filePartPath}`['yellow'].bold);
}
}
// Open-sourced files or new files that are missing a license
else {
// File has commercial license but is not added to the list of commercial files
if (fileContent.includes(commercialLic)) {
grunt.log.writeln(`Found commercial license in ${filePartPath}, but file is `['yellow'].bold +
`not added to Grunt's list of commercial files. Please, add it.`['yellow'].bold);
}
// File is missing any kind of license
else if (!fileContent.includes(openSourceLic)) {
// When calling this task with an 'add' option, it will add open-source license
// to all the files that are missing a license and are not specified as commercial
if (option === 'add') {
fileContent = openSourceLic + fileContent;
fs.writeFileSync(fileFullPath, fileContent);
grunt.log.writeln(`Added open-source license to ${filePartPath}`['yellow'].bold);
} else {
grunt.log.writeln(`File ${filePartPath} is missing a license`['red'].bold);
foundLicensingProblem = true;
}
}
}
});
if (foundLicensingProblem) {
// Abort Grunt if there are problems found with licensing
grunt.fail.fatal('Aborted due to problems with licensing'['red'].bold);
} else {
grunt.log.writeln(`Licensing task is done`['green'].bold);
}
});
};