forked from sozi-projects/Sozi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
553 lines (496 loc) · 18.3 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
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
548
549
550
551
552
553
module.exports = function(grunt) {
"use strict";
const path = require("path");
const fs = require("fs");
const execSync = require("child_process").execSync;
const envify = require("envify/custom");
const nunjucks = require("nunjucks");
nunjucks.configure({watch: false});
require("load-grunt-tasks")(grunt);
const pkg = grunt.file.readJSON("package.json");
// Get version number from last commit date.
// Format: YY.MM.T (year.month.timestamp).
const rev = execSync("git show -s --format='%cI %ct'").toString().trim().split(" ");
pkg.version = rev[0].slice(2, 4) + "." + rev[0].slice(5, 7) + "." + rev[0].slice(8, 10) + "-" + rev[1];
const buildConfig = grunt.file.readJSON("config.default.json");
const buildConfigJson = grunt.option("config");
if (buildConfigJson) {
try {
const customBuildConfig = grunt.file.readJSON(buildConfigJson);
grunt.verbose.writeln("Using configuration from " + buildConfigJson);
// Overwrite default config
for (let key in customBuildConfig) {
buildConfig[key] = customBuildConfig[key];
}
}
catch (noBuildConfigFound) {
grunt.log.error("Configuration file " + buildConfigJson + " not found.");
}
}
// Remove duplicates from an array.
function dedup(arr) {
return Array.from(new Set(arr));
}
// Get the OS part from a platform identifier.
function getPlatformOS(platform) {
return platform.split("-")[0];
}
// Get the architecture part from a platform identifier.
function getPlatformArch(platform) {
return platform.split("-")[1];
}
grunt.initConfig({
pkg: pkg,
// Check JavaScript source files.
jshint: {
options: {
jshintrc: true
},
all: [ "js/**/*.js" ]
},
// Check CSS source files.
csslint: {
options: {
csslintrc: ".csslintrc"
},
all: [ "css/**/*.css" ]
},
// Generate JavaScript API documentation.
jsdoc: {
options: {
destination: "dist/api",
configure: "jsdoc.json"
},
all: [ "js/**/*.js" ]
},
// Transpile JavaScript source files for Electron and browsers.
babel: {
electron: {
options: {
presets: [["@babel/preset-env", {targets: {node: "12"}}]]
},
files: [{
expand: true,
src: ["js/**/*.js"],
dest: "build/electron"
}]
},
browser: {
options: {
presets: [["@babel/preset-env", {useBuiltIns: "usage", corejs: 3}]]
},
files: [{
expand: true,
src: ["js/**/*.js"],
dest: "build/browser"
}]
}
},
// Generate gettext translation template (pot) file from JavaScript sources.
jspot: {
options: {
keyword: "_",
parserOptions: {
sourceType: "module"
}
},
all: {
src: ["js/**/*.js"],
dest: "locales"
}
},
// Convert gettext translation (po) files to JavaScript.
po2json: {
options: {
fuzzy: false,
singleFile: true,
nodeJs: true,
format: "jed1.x"
},
electron: {
src: ["locales/*.po"],
dest: "build/electron/js/locales.js",
},
browser: {
src: ["locales/*.po"],
dest: "build/browser/js/locales.js",
}
},
// Merge JavaScript modules for browser targets.
browserify: {
editor: {
options: {
external: ["electron", "fs", "process"],
browserifyOptions: {
basedir: "build/browser"
},
configure(b) {
b.transform({global: true}, envify({NODE_ENV: "production"}));
}
},
src: ["build/browser/js/editor.js"],
dest: "build/tmp/js/editor.js"
},
player: {
src: ["build/browser/js/player.js"],
dest: "build/tmp/js/player.js"
},
presenter: {
src: ["build/browser/js/presenter.js"],
dest: "build/tmp/js/presenter.js"
}
},
// Compress the JavaScript code of the editor, player, and presenter
// for browser targets.
uglify: {
options: {
mangle: false,
compress: false
},
editor: {
src: "<%= browserify.editor.dest %>",
dest: "build/browser/js/editor.min.js"
},
player: {
src: "<%= browserify.player.dest %>",
dest: "build/tmp/js/player.min.js"
},
presenter: {
src: "<%= browserify.presenter.dest %>",
dest: "build/tmp/js/presenter.min.js"
}
},
// Precompile templates for the player and presenter,
// inserting the player and presenter JavaScript into the template HTML.
nunjucks_render: {
player: {
src: "templates/player.html",
dest: "build/browser/templates/player.html",
context: {
js: "<%= uglify.player.dest %>"
}
},
presenter: {
src: "templates/presenter.html",
dest: "build/browser/templates/presenter.html",
context: {
js: "<%= uglify.presenter.dest %>"
}
}
},
// Copy assets to the build/ folder.
copy: {
editor: {
files: [
{
expand: true,
src: [
"index-electron.html",
"css/**/*",
"vendor/**/*"
],
dest: "build/electron/"
},
{
expand: true,
src: [
"index-webapp.html",
"css/**/*",
"vendor/**/*"
],
dest: "build/browser/"
},
{
expand: true,
flatten: true,
src: [
"<%= nunjucks_render.player.dest %>",
"<%= nunjucks_render.presenter.dest %>"
],
dest: "build/electron/templates/"
}
]
}
},
// Rename index HTML and JavaScript files depending on the target.
rename: {
webapp_backend: {
src: ["build/browser/js/backend/index-webapp.js"],
dest: "build/browser/js/backend/index.js"
},
webapp_html: {
src: ["build/browser/index-webapp.html"],
dest: "build/browser/index.html"
},
electron_backend: {
src: ["build/electron/js/backend/index-electron.js"],
dest: "build/electron/js/backend/index.js"
},
electron_html: {
src: ["build/electron/index-electron.html"],
dest: "build/electron/index.html"
}
},
// Generate a zip archive of the "add media" Inkscape extension.
compress: {
media: {
options: {
archive: "dist/Sozi-extras-media-<%= pkg.version %>.zip",
cwd: "extras/media"
},
src: [
"extras/media/sozi_extras_media.inx",
"extras/media/sozi_extras_media.py"
]
}
},
// Install all node modules used by the editor into the build/ folder.
"install-dependencies": {
electron: {
options: {
cwd: "build/electron"
}
},
browser: {
options: {
cwd: "build/browser"
}
}
},
// Build the Electron application.
electron: {
editor: {
options: {
name: "Sozi",
dir: "build/electron",
out: "dist",
overwrite: true,
prune: false,
electronVersion: buildConfig.electronVersion,
platform: dedup(buildConfig.platforms.map(getPlatformOS)).join(","),
arch: dedup(buildConfig.platforms.map(getPlatformArch)).join(",")
}
}
},
// Upload the web demonstration of the editor.
rsync: {
options: {
args: ["--verbose", "--update", "--checksum"]
},
editor: {
options: {
src: ["build/browser/*"],
dest: "/var/www/sozi.baierouge.fr/demo/",
host: "[email protected]",
deleteAll: true,
recursive: true
}
},
api: {
options: {
src: ["dist/api"],
dest: "/var/www/sozi.baierouge.fr/",
host: "[email protected]",
deleteAll: true,
recursive: true
}
}
},
newer: {
options: {
override(details, include) {
if (details.task === "nunjucks_render") {
include(fs.statSync(`build/tmp/js/${details.target}.min.js`).mtime > details.time);
}
else {
include(false);
}
}
}
}
});
// Conversion rules between Electron and Debian architecture names.
const debianArchs = {
ia32: "i386",
x64: "amd64"
};
// Conversion rules between Electron and Debian OS names.
const renamedOS = {
darwin: "osx",
win32: "windows"
};
// Generate an installable archive in the dist/ folder for each platform.
for (let platform of buildConfig.platforms) {
// The folder for the current platform.
const distDir = "dist/Sozi-" + platform;
// Extract the components of the platform name.
let platformOS = getPlatformOS(platform);
if (platformOS in renamedOS) {
platformOS = renamedOS[platformOS];
}
const platformArch = getPlatformArch(platform);
// The name of the target folder for the current platform in dist/.
const archiveName = "Sozi-" + pkg.version + "-" + platformOS + "-" + platformArch;
// The renamed folder for the current platform.
const archiveDir = "dist/" + archiveName;
// Copy the installation assets for the target OS.
grunt.config(["copy", platform], {
options: {
mode: true
},
files: [
{
expand: true,
flatten: true,
src: "installation-assets/" + platformOS + "/*",
dest: distDir + "/install/"
},
{
src: "icons/icon-256.png",
dest: distDir + "/install/sozi.png"
}
]
});
// Delete the distribution folder for this platform if it exists.
grunt.config(["clean", platform], {
src: archiveDir
});
// Rename the distribution folder to the archive name.
grunt.config(["rename", platform], {
src: distDir,
dest: archiveDir
});
// Build zip files for Windows, tar.xz for other platforms.
const archiveFormat = platformOS.startsWith("win") ? "zip" : "tar.xz";
grunt.config(["compress", platform], {
options: {
archive: archiveDir + "." + archiveFormat,
cwd: "dist/"
},
src: [archiveDir]
});
// Generate a Debian package for each Linux platform.
if (platformOS === "linux" && platformArch in debianArchs) {
grunt.config(["debian_package", platform], {
version: pkg.version,
platform: platform,
arch: debianArchs[platformArch],
date: new Date().toUTCString().slice(0, -3) + "+0000"
});
}
}
// Copy the installation scripts for each supported platform.
grunt.registerTask("copy-installation-assets", buildConfig.platforms.flatMap(platform => {
return buildConfig.installable.indexOf(getPlatformOS(platform)) >= 0 ? ["copy:" + platform] : [];
}));
// Rename the dist folder of each platform to match archive names.
grunt.registerTask("rename-platforms", buildConfig.platforms.flatMap(platform => {
return ["clean:" + platform, "rename:" + platform];
}));
// Generate a zip archive from a set of files.
grunt.registerMultiTask("compress", function () {
const dest = this.options().archive;
const cwd = this.options().cwd;
const src = this.files.map(f => f.src).flat().map(p => path.relative(cwd, p)).join(" ");
grunt.log.writeln("Compressing " + dest);
if (dest.endsWith(".tar.xz")) {
execSync("tar cJf "+ path.relative(cwd, dest) + " " + src, {cwd});
}
else if (dest.endsWith(".zip")) {
execSync("zip -ry " + path.relative(cwd, dest) + " " + src, {cwd});
}
});
// Generate a zip archive for each platform.
grunt.registerTask("compress-platforms", buildConfig.platforms.map(platform => "compress:" + platform));
// Copy the package.json file with updated version number into the build/ folder.
grunt.registerTask("write_package_json", function () {
grunt.file.write("build/electron/package.json", JSON.stringify(pkg));
grunt.file.write("build/browser/package.json", JSON.stringify(pkg));
});
// Render a template.
grunt.registerMultiTask("nunjucks_render", function () {
for (let file of this.files) {
grunt.file.write(file.dest, nunjucks.render(file.src[0], {
js: grunt.file.read(this.data.context.js)
}));
grunt.log.writeln("File " + file.dest + " created.");
}
});
// Build a Debian package.
grunt.registerMultiTask("debian_package", function () {
const workDir = "dist/packaging-" + this.data.platform;
grunt.file.write(workDir + "/Makefile", nunjucks.render("debian/Makefile", this.data));
grunt.file.write(workDir + "/debian/changelog", nunjucks.render("debian/changelog", this.data));
grunt.file.write(workDir + "/debian/compat", nunjucks.render("debian/compat", this.data));
grunt.file.write(workDir + "/debian/control", nunjucks.render("debian/control", this.data));
grunt.file.write(workDir + "/debian/links", nunjucks.render("debian/links", this.data));
grunt.file.write(workDir + "/debian/rules", nunjucks.render("debian/rules", this.data));
execSync("dpkg-buildpackage -a" + this.data.arch, {cwd: workDir});
});
// Check JavaScript and CSS source files.
grunt.registerTask("lint", ["jshint", "csslint"]);
// Common build task for browser and Electron targets.
grunt.registerTask("build", [
"write_package_json",
"newer:babel",
"browserify:player", // Cannot use 'newer' here due to imports
"browserify:presenter", // Cannot use 'newer' here due to imports
"newer:uglify:player",
"newer:uglify:presenter",
"newer:nunjucks_render",
"newer:po2json",
"newer:copy:editor",
"compress:media"
]);
// Build the Electron application.
grunt.registerTask("electron-build", [
"build",
"rename:electron_backend",
"rename:electron_html",
"install-dependencies",
"electron",
"copy-installation-assets"
]);
// Build the editor for the browser.
grunt.registerTask("web-build", [
"build",
"rename:webapp_backend",
"rename:webapp_html",
"browserify:editor", // Cannot use 'newer' here due to imports
"newer:uglify:editor"
]);
// Build an installable zip archive of the Electron application for all platform.
grunt.registerTask("electron-archive", [
"electron-build",
"rename-platforms",
"compress-platforms"
]);
// Build and upload the editor for the browser.
grunt.registerTask("web-demo", [
"web-build",
"rsync:editor" // Cannot use 'newer' here since 'dest' is not a generated file
]);
// Generate a translation template (pot) file.
grunt.registerTask("pot", [
"newer:babel",
"jspot" // Cannot use 'newer' here since 'dest' is not a generated file
]);
// Build the Electron application, and generate zip archives and Debian packages if applicable.
if (buildConfig.platforms.some(p => getPlatformOS(p) === "linux")) {
grunt.registerTask("deb", [
"electron-build",
"rename-platforms",
"debian_package"
]);
grunt.registerTask("dist", [
"electron-archive",
"debian_package"
]);
}
else {
grunt.registerTask("dist", ["electron-archive"]);
}
grunt.registerTask("api", ["jsdoc", "rsync:api"]);
// Default task: build the Electron application, and generate zip archives
grunt.registerTask("default", ["electron-archive"]);
};