-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgulpfile.js
547 lines (459 loc) · 12.6 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
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
"use strict";
const appdmg = require("appdmg");
const pkg = require("./package.json");
const fs = require("fs");
const path = require("path");
const exec = require("child_process").exec;
const execSync = require("child_process").execSync;
const zip = require("gulp-zip");
const del = require("del");
const NwBuilder = require("nw-builder");
const gulp = require("gulp");
const yarn = require("gulp-yarn");
const rename = require("gulp-rename");
const os = require("os");
const git = require("git-rev-sync");
const commandExistsSync = require("command-exists").sync;
const DIST_DIR = "./dist/";
const APPS_DIR = "./apps/";
const DEBUG_DIR = "./debug/";
const RELEASE_DIR = "./release/";
var nwBuilderOptions = {
version: "0.36.4",
files: "./dist/**/*",
macIcns: './images/icon_128.icns',
macPlist: {
CFBundleDisplayName: "KISS GUI",
CFBundleIdentifier: "com.flyduino.kissgui"
},
// winIco: './images/icon_128.ico',
zip: false
};
//-----------------
//Pre tasks operations
//-----------------
const SELECTED_PLATFORMS = getInputPlatforms();
//-----------------
//Tasks
//-----------------
gulp.task(
"clean",
gulp.parallel(clean_dist, clean_apps, clean_debug, clean_release)
);
gulp.task("clean-dist", clean_dist);
gulp.task("clean-apps", clean_apps);
gulp.task("clean-debug", clean_debug);
gulp.task("clean-release", clean_release);
gulp.task("clean-cache", clean_cache);
var distBuild = gulp.series(dist_src);
var distRebuild = gulp.series(clean_dist, distBuild);
gulp.task("dist", distRebuild);
var appsBuild = gulp.series(
gulp.parallel(clean_apps, distRebuild),
apps,
gulp.parallel(listPostBuildTasks(APPS_DIR))
);
gulp.task("apps", appsBuild);
var debugBuild = gulp.series(
distBuild,
debug,
gulp.parallel(listPostBuildTasks(DEBUG_DIR)),
start_debug
);
gulp.task("debug", debugBuild);
var releaseBuild = gulp.series(
gulp.parallel(clean_release, appsBuild),
gulp.parallel(listReleaseTasks())
);
gulp.task("release", releaseBuild);
gulp.task("default", debugBuild);
// -----------------
// Helper functions
// -----------------
// Get platform from commandline args
// #
// # gulp <task> [<platform>]+ Run only for platform(s) (with <platform> one of --linux64, --linux32, --osx64, --win32, --win64, or --chromeos)
// #
function getInputPlatforms() {
var supportedPlatforms = [
"linux64",
"linux32",
"osx64",
"win32",
"win64",
"chromeos"
];
var platforms = [];
var regEx = /--(\w+)/;
console.log(process.argv);
for (var i = 3; i < process.argv.length; i++) {
var arg = process.argv[i].match(regEx)[1];
if (supportedPlatforms.indexOf(arg) > -1) {
platforms.push(arg);
} else {
console.log("Unknown platform: " + arg);
process.exit();
}
}
if (platforms.length === 0) {
var defaultPlatform = getDefaultPlatform();
if (supportedPlatforms.indexOf(defaultPlatform) > -1) {
platforms.push(defaultPlatform);
} else {
console.error(
`Your current platform (${os.platform()}) is not a supported build platform. Please specify platform to build for on the command line.`
);
process.exit();
}
}
if (platforms.length > 0) {
console.log("Building for platform(s): " + platforms + ".");
} else {
console.error("No suitable platforms found.");
process.exit();
}
return platforms;
}
// Gets the default platform to be used
function getDefaultPlatform() {
var defaultPlatform;
switch (os.platform()) {
case "darwin":
defaultPlatform = "osx64";
break;
case "linux":
defaultPlatform = "linux64";
break;
case "win32":
defaultPlatform = "win32";
break;
default:
defaultPlatform = "";
break;
}
return defaultPlatform;
}
function getPlatforms() {
return SELECTED_PLATFORMS.slice();
}
function removeItem(platforms, item) {
var index = platforms.indexOf(item);
if (index >= 0) {
platforms.splice(index, 1);
}
}
function getRunDebugAppCommand(arch) {
switch (arch) {
case "osx64":
return "open " + path.join(DEBUG_DIR, pkg.name, arch, pkg.name + ".app");
break;
case "linux64":
case "linux32":
return path.join(DEBUG_DIR, pkg.name, arch, pkg.name);
break;
case "win32":
case "win64":
return path.join(DEBUG_DIR, pkg.name, arch, pkg.name + ".exe");
break;
default:
return "";
break;
}
}
function getReleaseFilename(platform, ext) {
return (
pkg.name +
"_" +
pkg.version +
"-" +
git.branch() +
"_" +
git.short() +
"-" +
platform +
"." +
ext
);
}
function clean_dist() {
return del([DIST_DIR + "**"], { force: true });
}
function clean_apps() {
return del([APPS_DIR + "**"], { force: true });
}
function clean_debug() {
return del([DEBUG_DIR + "**"], { force: true });
}
function clean_release() {
return del([RELEASE_DIR + "**"], { force: true });
}
function clean_cache() {
return del(["./cache/**"], { force: true });
}
// Real work for dist task. Done in another task to call it via
// run-sequence.
function dist_src() {
var distSources = [
// CSS files
'./main.css',
'./js/libraries/jquery.minicolors.css',
'./js/libraries/jquery-ui.css',
'./js/libraries/jquery-ui.structure.min.css',
'./js/libraries/jquery-ui.theme.min.css',
'./js/plugins/jquery.kiss.aux.css',
'./js/plugins/jquery.kiss.warning.css',
'./content/*.css',
// JavaScript
'./js/libraries/github.js',
'./js/libraries/hex_parser.js',
'./js/libraries/imu.js',
'./js/libraries/jquery-3.3.1.min.js',
'./js/libraries/jquery.minicolors.min.js',
'./js/libraries/jquery-ui.min.js',
'./js/libraries/semver.js',
'./js/libraries/stm32usbdfu.js',
'./js/libraries/three.js',
'./js/libraries/three.min.js',
'./js/libraries/i18n/*.js',
'./js/plugins/jquery.kiss.*.js',
'./js/android_otg_serial.js',
'./js/chrome_serial.js',
'./js/connection_handler.js',
'./js/gui.js',
'./js/input_validation.js',
'./js/port_handler.js',
'./js/protocol.js',
'./js/serial.js',
'./js/startup.js',
'./js/websocket_serial.js',
// Tabs
'./start.js',
'./main.js',
'./content/*.js',
// everything else
'./eventPage.js',
'./cordova.js', // For cordova
'./*.html',
'./content/*.html',
'./images/*',
'./images/**/*',
'./js/libraries/images/*.png',
'./i18n/*.json',
'./PRESET_PID.txt', // PID presets
'./README.md' // Readme including links for driver
];
return (
gulp
.src(distSources, { base: "." })
.pipe(gulp.src("manifest.json", { passthrougth: true }))
.pipe(gulp.src("package.json", { passthrougth: true }))
// .pipe(gulp.src('changelog.html', { passthrougth: true }))
.pipe(gulp.dest(DIST_DIR))
.pipe(
yarn({
production: true,
ignoreScripts: true
})
)
);
}
// Create runnable app directories in ./apps
function apps(done) {
var platforms = getPlatforms();
removeItem(platforms, "chromeos");
buildNWApps(platforms, "normal", APPS_DIR, done);
}
function listPostBuildTasks(folder, done) {
var platforms = getPlatforms();
var postBuildTasks = [];
if (platforms.indexOf("linux32") != -1) {
postBuildTasks.push(function post_build_linux32(done) {
return post_build("linux32", folder, done);
});
}
if (platforms.indexOf("linux64") != -1) {
postBuildTasks.push(function post_build_linux64(done) {
return post_build("linux64", folder, done);
});
}
// We need to return at least one task, if not gulp will throw an error
if (postBuildTasks.length == 0) {
postBuildTasks.push(function post_build_none(done) {
done();
});
}
return postBuildTasks;
}
function post_build(arch, folder, done) {
if (arch === "linux32" || arch === "linux64") {
// Copy Ubuntu launcher scripts to destination dir
var launcherDir = path.join(folder, pkg.name, arch);
console.log("Copy Ubuntu launcher scripts to " + launcherDir);
return gulp.src("assets/linux/**").pipe(gulp.dest(launcherDir));
}
return done();
}
// Create debug app directories in ./debug
function debug(done) {
var platforms = getPlatforms();
removeItem(platforms, "chromeos");
buildNWApps(platforms, "sdk", DEBUG_DIR, done);
}
function buildNWApps(platforms, flavor, dir, done) {
if (platforms.length > 0) {
var builder = new NwBuilder(
Object.assign(
{
buildDir: dir,
platforms: platforms,
flavor: flavor
},
nwBuilderOptions
)
);
builder.on("log", console.log);
builder.build(function(err) {
if (err) {
console.log("Error building NW apps: " + err);
clean_debug();
process.exit(1);
}
done();
});
} else {
console.log("No platform suitable for NW Build");
done();
}
}
function start_debug(done) {
var platforms = getPlatforms();
if (platforms.length === 1) {
var run = getRunDebugAppCommand(platforms[0]);
console.log("Starting debug app (" + run + ")...");
exec(run);
} else {
console.log("More than one platform specified, not starting debug app");
}
done();
}
// Create distribution package (zip) for windows and linux platforms
function release_zip(arch) {
var src = path.join(APPS_DIR, pkg.name, arch, "**");
var output = getReleaseFilename(arch, "zip");
var base = path.join(APPS_DIR, pkg.name, arch);
return compressFiles(src, base, output, "KISS GUI");
}
// Create distribution package for chromeos platform
function release_chromeos() {
var src = path.join(DIST_DIR, "**");
var output = getReleaseFilename("chromeos", "zip");
var base = DIST_DIR;
return compressFiles(src, base, output, ".");
}
// Compress files from srcPath, using basePath, to outputFile in the RELEASE_DIR
function compressFiles(srcPath, basePath, outputFile, zipFolder) {
return gulp
.src(srcPath, { base: basePath })
.pipe(
rename(function(actualPath) {
actualPath.dirname = path.join(zipFolder, actualPath.dirname);
})
)
.pipe(zip(outputFile))
.pipe(gulp.dest(RELEASE_DIR));
}
// Create distribution package for macOS platform
function osx64_sign(done) {
if (commandExistsSync("tmp/codesign.sh")) {
console.log("Codesign activity...");
execSync("tmp/codesign.sh", function(error, stdOut, stdErr) {
});
} else {
console.log("No valid script for codesign");
}
//release_zip("osx64",done);
release_osx64(done);
return done();
}
function release_osx64(done) {
// Create DMG
createDirIfNotExists(RELEASE_DIR);
const ee = appdmg({
target: path.join(RELEASE_DIR, getReleaseFilename("macOS", "dmg")),
basepath: path.join(APPS_DIR, pkg.name, "osx64"),
specification: {
title: "KISS GUI",
contents: [
{ x: 370, y: 170, type: "link", path: "/Applications" },
{
x: 90,
y: 170,
type: "file",
path: pkg.name + ".app",
name: "KISS GUI.app"
}
],
icon: path.join(__dirname, 'images/icon_128.icns'),
background: path.join(__dirname, 'images/dmg-background.png'),
format: "UDZO",
window: {
size: {
width: 600,
height: 400
}
}
}
}).on("progress", function(info) {
if (info.type == "step-begin")
console.log(info.title + " [" + info.current + "/" + info.total + "]");
else console.log("..");
});
ee.on("error", function(err) {
console.log(err);
});
return done();
}
// Create the dir directory, with write permissions
function createDirIfNotExists(dir) {
fs.mkdir(dir, "0775", function(err) {
if (err) {
if (err.code !== "EEXIST") {
throw err;
}
}
});
}
// Create a list of the gulp tasks to execute for release
function listReleaseTasks(done) {
var platforms = getPlatforms();
var releaseTasks = [];
if (platforms.indexOf("chromeos") !== -1) {
releaseTasks.push(release_chromeos);
}
if (platforms.indexOf("linux64") !== -1) {
releaseTasks.push(function release_linux64_zip() {
return release_zip("linux64");
});
}
if (platforms.indexOf("linux32") !== -1) {
releaseTasks.push(function release_linux32_zip() {
return release_zip("linux32");
});
}
if (platforms.indexOf("osx64") !== -1) {
releaseTasks.push(function release_osx64_dmg(done) {
return osx64_sign(done);
});
}
if (platforms.indexOf("win32") !== -1) {
releaseTasks.push(function release_win32_zip() {
return release_zip("win32");
});
}
if (platforms.indexOf("win64") !== -1) {
releaseTasks.push(function release_win64_zip() {
return release_zip("win64");
});
}
return releaseTasks;
}