-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
369 lines (309 loc) · 11.2 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
module.exports = function(grunt) {
'use strict';
var moment = require('moment'),
path = require('path'),
fs = require('fs-plus'),
asar = require('asar');
var os = (function(){
var platform = process.platform;
if (/^win/.test(platform)) { return "windows"; }
if (/^darwin/.test(platform)) { return "mac"; }
if (/^linux/.test(platform)) { return "linux"; }
return null;
})();
var exe = {
windows: "electron.exe",
mac: "Electron.app/Contents/MacOS/Electron",
linux: "electron"
};
var electron_path = "electron";
//------------------------------------------------------------------------------
// ShellJS
//------------------------------------------------------------------------------
require('shelljs/global');
// shelljs/global makes the following imports:
// cwd, pwd, ls, find, cp, rm, mv, mkdir, test, cat,
// str.to, str.toEnd, sed, grep, which, echo,
// pushd, popd, dirs, ln, exit, env, exec, chmod,
// tempdir, error
var shellconfig = require('shelljs').config;
shellconfig.silent = false; // hide shell cmd output?
shellconfig.fatal = false; // stop if cmd failed?
//------------------------------------------------------------------------------
// Grunt Config
//------------------------------------------------------------------------------
grunt.initConfig({
less: {
options: {
compress: true
},
default: {
files: {
'resources/public/css/main.min.css': 'less/main.less'
}
}
},
watch: {
options: {
atBegin: true
},
less: {
files: "less/*.less",
tasks: "less:default"
}
},
'download-electron': {
version: '0.31.0',
outputDir: 'electron'
}
});
//------------------------------------------------------------------------------
// Third-party tasks
//------------------------------------------------------------------------------
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-download-electron');
if (os === "mac") {
grunt.loadNpmTasks('grunt-appdmg');
}
grunt.loadNpmTasks('winresourcer');
//------------------------------------------------------------------------------
// Setup Tasks
//------------------------------------------------------------------------------
grunt.registerTask('setup', [
'download-electron',
'ensure-config-exists',
'run-app-bower'
]);
grunt.registerTask('ensure-config-exists', function() {
pushd("app");
if (!test("-f", "config.json")) {
grunt.log.writeln("Creating default config.json...");
cp("example.config.json", "config.json");
}
popd();
});
grunt.registerTask('run-app-bower', function() {
exec("bower install");
});
grunt.registerTask('cljsbuild-prod', function() {
grunt.log.writeln("\nCleaning and building ClojureScript production files...");
exec("lein do clean, with-profile production cljsbuild once");
});
grunt.registerTask('launch', function(async) {
var IsAsync = (async == "true");
grunt.log.writeln("\nLaunching development version...");
var local_exe = exe[os];
exec(path.join(electron_path, local_exe) + " app", {async:IsAsync});
});
grunt.registerTask('check-old', function() {
grunt.log.writeln("\nChecking clojure dependencies");
exec("lein ancient :all", {silent:false});
grunt.log.writeln("\nChecking npm dependencies");
exec("npm outdated", {silent:false});
grunt.log.writeln("\nChecking bower dependencies");
exec("bower list", {silent:false});
});
//------------------------------------------------------------------------------
// Release
//------------------------------------------------------------------------------
grunt.registerTask('release', function() {
var build = getBuildMeta();
var paths = getReleasePaths(build);
var done = this.async();
prepRelease( build, paths);
copyElectronAndBuildToRelease( build, paths);
setReleaseConfig( build, paths);
installNodeDepsToRelease( build, paths);
stampRelease( build, paths);
makeAsarRelease( build, paths, done);
});
grunt.registerTask('fresh-release', ['cljsbuild-prod', 'release']);
//------------------------------------------------------------------------------
// Release - config
//------------------------------------------------------------------------------
var electronShell = {
windows: {
exeToRename: "electron.exe",
renamedExe: "asterion.exe",
resources: "resources",
installExt: "exe"
},
mac: {
exeToRename: "Electron.app",
renamedExe: "asterion.app",
plist: "Electron.app/Contents/Info.plist",
resources: "Electron.app/Contents/Resources",
installExt: "dmg"
},
linux: {
exeToRename: "electron",
renamedExe: "asterion",
resources: "resources"
}
}[os];
function getBuildMeta() {
grunt.log.writeln("Getting project metadata...");
var tokens = cat("project.clj").split(" ");
var build = {
name: tokens[1],
version: tokens[2].replace(/"/g, "").trim(),
date: moment().format("YYYY-MM-DD"),
commit: exec("git rev-list HEAD --count", {silent:true}).output.trim()
};
build.releaseName = build.name + "-v" + build.version + "-" + os;
grunt.log.writeln("name: "+build.name.cyan);
grunt.log.writeln("version: "+build.version.cyan);
grunt.log.writeln("date: "+build.date.cyan);
grunt.log.writeln("commit: "+build.commit.cyan);
grunt.log.writeln("release: "+build.releaseName.cyan);
return build;
}
function getReleasePaths(build) {
var paths = {
electron: "electron",
builds: "builds",
devApp: "app",
rootPkg: "package.json"
};
paths.release = path.join(paths.builds, build.releaseName);
paths.resources = path.join(paths.release, electronShell.resources);
paths.install = paths.release + "." + electronShell.installExt;
paths.releaseApp = paths.resources + path.sep + paths.devApp;
paths.devPkg = paths.devApp + "/package.json";
paths.prodCfg = paths.devApp + "/prod.config.json";
paths.releasePkg = paths.releaseApp + "/package.json";
paths.releaseCfg = paths.releaseApp + "/config.json";
paths.exeToRename = path.join(paths.release, electronShell.exeToRename);
paths.renamedExe = path.join(paths.release, electronShell.renamedExe);
paths.releaseResources = path.join(paths.resources, paths.devApp, "components");
return paths;
}
//------------------------------------------------------------------------------
// Release - subtasks
//------------------------------------------------------------------------------
function prepRelease(build, paths) {
grunt.log.writeln("\nCleaning previous release...");
mkdir('-p', paths.builds);
rm('-rf', paths.install, paths.release);
}
function copyElectronAndBuildToRelease(build, paths) {
grunt.log.writeln("\nCopying Electron and asterion to release folder...");
grunt.log.writeln(paths.electron + " ==> " + paths.release.cyan);
grunt.log.writeln(paths.devApp + " ==> " + paths.resources.cyan);
cp('-r', paths.electron + "/", paths.release);
cp('-r', paths.devApp, paths.resources);
//delete extra resources
rm('-rf', path.join(paths.releaseApp, "js", "p", "out"));
}
function setReleaseConfig(build, paths) {
grunt.log.writeln("\nRemoving config to force default release settings...");
rm('-f', paths.releaseCfg);
cp(paths.prodCfg, paths.releaseCfg);
}
function installNodeDepsToRelease(build, paths) {
grunt.log.writeln("\nCopying node dependencies to release...");
cp('-f', paths.rootPkg, paths.releaseApp);
pushd(paths.releaseApp);
exec('npm install --no-optional --production');
popd();
cp('-f', paths.devPkg, paths.releaseApp);
}
function makeAsarRelease(build, paths, done) {
var new_path = path.join(paths.releaseApp, "..", "app.asar");
grunt.log.writeln("Release: " + paths.releaseApp + ", Asar: " + new_path);
asar.createPackage(paths.releaseApp, new_path , function(err) {
grunt.log.writeln("Asar file created.");
cp(path.join(paths.releaseApp, "img", "logo.icns"), paths.resources);
rm('-rf', path.join(paths.releaseApp));
switch (os) {
case "mac": finalizeMacRelease( build, paths); break;
case "linux": finalizeLinuxRelease( build, paths); break;
case "windows": finalizeWindowsRelease( build, paths); break;
}
done(err);
});
}
function stampRelease(build, paths) {
grunt.log.writeln("\nStamping release with build metadata...");
var pkg = grunt.file.readJSON(paths.releasePkg);
pkg.version = build.version;
pkg["build-commit"] = build.commit;
pkg["build-date"] = build.date;
JSON.stringify(pkg, null, " ").to(paths.releasePkg);
}
function updateVersionInReadme(build, paths) {
grunt.log.writeln("\nUpdating version and download links in readme...");
sed('-i', /v\d+\.\d+/g, "v"+build.version, "README.md");
}
//------------------------------------------------------------------------------
// Release - finalization
//------------------------------------------------------------------------------
function finalizeMacRelease(build, paths) {
grunt.log.writeln("\nChanging Electron app icon and bundle name...");
var plist = path.join(__dirname, paths.release, electronShell.plist);
exec("defaults write " + plist + " CFBundleIconFile logo.icns");
exec("defaults write " + plist + " CFBundleDisplayName asterion");
exec("defaults write " + plist + " CFBundleName asterion");
exec("defaults write " + plist + " CFBundleIdentifier com.example");
mv(paths.exeToRename, paths.renamedExe);
var app = paths.renamedExe;
grunt.log.writeln("\nCreating dmg image...");
grunt.config.set("appdmg", {
options: {
"title": "asterion",
"background": "scripts/dmg/TestBkg.png",
"icon-size": 80,
"contents": [
{ "x": 448, "y": 344, "type": "link", "path": "/Applications" },
{ "x": 192, "y": 344, "type": "file", "path": app }
]
},
target: {
dest: paths.install
}
});
grunt.task.run("appdmg");
}
function finalizeLinuxRelease(build, paths) {
mv(paths.exeToRename, paths.renamedExe);
}
function finalizeWindowsRelease(build, paths) {
grunt.log.writeln("\nChanging electron app icon and bundle name...");
mv(paths.exeToRename, paths.renamedExe);
var app = paths.renamedExe;
grunt.config.set("winresourcer", {
main: {
operation: "Update",
exeFile: app,
resourceType: "Icongroup",
resourceName: "1",
resourceFile: "app/img/logo.ico"
}
});
grunt.task.run("winresourcer");
grunt.config.set("makensis", {
version: build.version,
releaseDir: paths.release,
outFile: paths.install
});
grunt.task.run("makensis");
}
grunt.registerTask('makensis', function() {
grunt.log.writeln("\nCreating installer...");
var config = grunt.config.get("makensis");
exec(["makensis",
"/DPRODUCT_VERSION=" + config.version,
"/DRELEASE_DIR=../" + config.releaseDir,
"/DOUTFILE=../" + config.outFile,
"scripts/build-windows-exe.nsi"].join(" "));
});
//------------------------------------------------------------------------------
// Test Tasks
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Default Task
//------------------------------------------------------------------------------
grunt.registerTask('default', ['setup']);
// end module.exports
};