forked from SickChill/sickchill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
479 lines (458 loc) · 19.7 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
'use strict';
module.exports = function(grunt) {
const isTravis = Boolean(process.env.TRAVIS);
grunt.registerTask('default', [
'clean',
'bower',
'bower_concat',
'copy',
'uglify',
'cssmin'
]);
grunt.registerTask('auto_update_trans', 'Update translations on master and push to master & develop', function() {
if (!isTravis) {
grunt.fatal('This task is only for Travis-CI!');
return false;
}
grunt.log.writeln('Running grunt and updating translations...'.magenta);
grunt.task.run([
'exec:git:checkout:master',
'default', // Run default task
'update_trans', // Update translations
'exec:commit_changed_files:yes', // Determine what we need to commit if needed, stop if nothing to commit.
'exec:git:reset --hard', // Reset unstaged changes (to allow for a rebase)
'exec:git:checkout:develop', 'exec:git:rebase:master', // FF develop to the updated master
'exec:git_push:origin:master develop' // Push master and develop
]);
});
grunt.registerTask('update_trans', 'Update translations', function() {
grunt.log.writeln('Updating translations...'.magenta);
var tasks = [
'exec:babel_extract',
'exec:babel_update',
// + crowdin
'exec:babel_compile',
'po2json'
];
if (process.env.CROWDIN_API_KEY) {
tasks.splice(2, 0, 'exec:crowdin_upload', 'exec:crowdin_download'); // insert items at index 2
} else {
grunt.log.warn('Environment variable `CROWDIN_API_KEY` is not set, not syncing with Crowdin.'.bold);
}
grunt.task.run(tasks);
});
/****************************************
* Admin only tasks *
****************************************/
grunt.registerTask('publish', 'ADMIN: Create a new release tag and generate new CHANGES.md', [
'exec:test', // Run tests
'newrelease', // Pull and merge develop to master, create and push a new release
'genchanges' // Update CHANGES.md
]);
grunt.registerTask('newrelease', "Pull and merge develop to master, create and push a new release", [
'exec:git:checkout:develop', 'exec:git:pull', // Pull develop
'exec:git:checkout:master', 'exec:git:pull', // Pull master
'exec:git:merge:develop', // Merge develop into master
'exec:git_get_last_tag', 'exec:git_list_changes', // List changes from since last tag
'_get_next_tag', 'exec:git_tag_new', // Create new release tag
'exec:git_push:origin:master:tags', // Push master + tags
'exec:git:checkout:develop' // Go back to develop
]);
grunt.registerTask('genchanges', "Generate CHANGES.md file", function() {
var file = grunt.option('file'); // --file=path/to/sickchill.github.io/sickchill-news/CHANGES.md
if (!file) {
file = process.env.SICKCHILL_CHANGES_FILE;
}
if (file && grunt.file.exists(file)) {
grunt.config('changesmd_file', file.replace(/\\/g, '/')); // Use forward slashes only.
} else {
grunt.fatal('\tYou must provide a path to CHANGES.md to generate changes.\n' +
'\t\tUse --file=path/to/sickchill.github.io/sickchill-news/CHANGES.md\n' +
'\t\tor set the path in SICKCHILL_CHANGES_FILE (environment variable)');
}
grunt.task.run(['exec:git_list_tags', '_genchanges', 'exec:commit_changelog']);
});
/****************************************
* Task configurations *
****************************************/
require('load-grunt-tasks')(grunt); // loads all grunt tasks matching the ['grunt-*', '@*/grunt-*'] patterns
grunt.initConfig({
clean: {
dist: './dist/',
'bower_components': './bower_components',
fonts: './gui/slick/css/fonts',
options: {
force: true
}
},
bower: {
install: {
options: {
copy: false
}
}
},
'bower_concat': {
all: {
dest: {
js: './dist/bower.js',
css: './dist/bower.css'
},
exclude: [],
dependencies: {},
mainFiles: {
'tablesorter': [
'dist/js/jquery.tablesorter.combined.min.js',
'dist/js/widgets/widget-columnSelector.min.js',
'dist/css/theme.blue.min.css'
],
'bootstrap': [
'dist/css/bootstrap.min.css',
'dist/js/bootstrap.min.js'
],
'bootstrap-formhelpers': [
'dist/js/bootstrap-formhelpers.min.js'
],
'isotope': [
"dist/isotope.pkgd.min.js"
],
"outlayer": [
"item.js",
"outlayer.js"
],
"open-sans-fontface": [
"*.css",
"fonts/**/*"
]
},
bowerOptions: {
relative: false
}
}
},
copy: {
openSans: {
files: [{
expand: true,
dot: true,
cwd: 'bower_components/open-sans-fontface',
src: [
'fonts/**/*'
],
dest: './gui/slick/css/'
}]
},
glyphicon: {
files: [{
expand: true,
dot: true,
cwd: 'bower_components/bootstrap/fonts',
src: [
'*.eot',
'*.svg',
'*.ttf',
'*.woff',
'*.woff2'
],
dest: './gui/slick/fonts/'
}]
}
},
uglify: {
bower: {
files: {
'./gui/slick/js/vender.min.js': ['./dist/bower.js']
}
},
core: {
files: {
'./gui/slick/js/core.min.js': ['./gui/slick/js/core.js']
}
}
},
cssmin: {
options: {
shorthandCompacting: false,
roundingPrecision: -1
},
bower: {
files: {
'./gui/slick/css/vender.min.css': ['./dist/bower.css']
}
},
core: {
files: {
'./gui/slick/css/core.min.css': ['./dist/core.css']
}
}
},
po2json: {
messages: {
options: {
format: 'jed',
singleFile: true
},
files: [{
expand: true,
src: './locale/*/LC_MESSAGES/messages.po',
dest: '',
ext: '' // workaround for relative files
}]
}
},
exec: {
// Translations
'babel_extract': {cmd: 'python setup.py extract_messages'},
'babel_update': {cmd: 'python setup.py update_catalog'},
'crowdin_upload': {cmd: 'crowdin-cli-py upload sources'},
'crowdin_download': {cmd: 'crowdin-cli-py download'},
'babel_compile': {cmd: 'python setup.py compile_catalog'},
// Run tests
'test': {cmd: 'yarn run test || npm run test'},
// Publish/Releases
'git': {
cmd: function (cmd, branch) {
branch = branch ? ' ' + branch : '';
return 'git ' + cmd + branch;
}
},
'commit_changed_files': { // Choose what to commit.
cmd: function(travis) {
grunt.config('stop_no_changes', Boolean(travis));
return 'git status -s -- locale/ gui/';
},
stdout: false,
callback: function(err, stdout) {
stdout = stdout.trim();
if (!stdout.length) {
grunt.fatal('No changes to commit.', 0);
}
var commitMsg = [];
var commitPaths = [];
if (stdout.match(/gui\/.*(vender|core)\.min\.(js|css)$/gm)) {
commitMsg.push('Grunt');
commitPaths.push('gui/**/vender.min.*');
commitPaths.push('gui/**/core.min.*');
}
if (stdout.match(/locale\/.*(pot|po|mo|json)$/gm)) {
commitMsg.push('Update translations');
commitPaths.push('locale/');
}
if (!commitMsg.length || !commitPaths.length) {
if (grunt.config('stop_no_changes')) {
grunt.fatal('Nothing to commit, aborting', 0);
} else {
grunt.log.ok('No extra changes to commit'.green);
}
} else {
commitMsg = commitMsg.join(', ');
if (process.env.TRAVIS_BUILD_NUMBER) {
commitMsg += ' (build ' + process.env.TRAVIS_BUILD_NUMBER + ') [skip ci]';
}
grunt.config('commit_msg', commitMsg);
grunt.config('commit_paths', commitPaths.join(' '));
grunt.task.run('exec:commit_combined');
}
}
},
'commit_combined': {
cmd: function() {
var message = grunt.config('commit_msg');
var paths = grunt.config('commit_paths');
if (!message || !paths) {
grunt.fatal('Call exec:commit_changed_files instead!');
}
return 'git add -- ' + paths;
},
callback: function(err) {
if (!err) {
if (!isTravis) {
grunt.task.run('exec:git:commit:-m "' + grunt.config('commit_msg') + '"');
} else { // Workaround for Travis (with -m "text" the quotes are within the message)
var msgFilePath = 'commit-msg.txt';
grunt.file.write(msgFilePath, grunt.config('commit_msg'));
grunt.task.run('exec:git:commit:-F ' + msgFilePath);
}
}
}
},
'git_get_last_tag': {
cmd: 'git for-each-ref --sort=-refname --count=1 --format "%(refname:short)" refs/tags/v20[0-9][0-9]*',
stdout: false,
callback: function(err, stdout) {
stdout = stdout.trim();
if (/^v\d{4}.\d{1,2}.\d{1,2}.\d+$/.test(stdout)) {
grunt.config('last_tag', stdout);
grunt.log.write(stdout);
} else {
grunt.fatal('Could not get the last tag name. We got: ' + stdout);
}
}
},
'git_list_changes': {
cmd: function() { return 'git log --oneline --first-parent --pretty=format:%s ' + grunt.config('last_tag') + '..HEAD'; },
stdout: false,
callback: function(err, stdout) {
var commits = stdout.trim()
.replace(/`/gm, '').replace(/^\([\w\d\s,.\-+_/>]+\)\s/gm, '').replace(/"/gm, '\\"'); // removes ` and tag information
if (commits) {
grunt.config('commits', commits);
} else {
grunt.fatal('Getting new commit list failed!');
}
}
},
'git_tag_new': {
cmd: function (sign) {
sign = sign !== "true" ? '' : '-s ';
return 'git tag ' + sign + grunt.config('next_tag') + ' -m "' + grunt.config('commits') + '"';
},
stdout: false
},
'git_push': {
cmd: function (remote, branch, tags) {
var pushCmd = 'git push ' + remote + ' ' + branch;
if (tags) {
pushCmd += ' --tags';
}
if (grunt.option('no-push')) {
grunt.log.warn('Pushing with --dry-run ...'.magenta);
pushCmd += ' --dry-run';
}
return pushCmd;
},
stderr: false,
callback: function(err, stdout, stderr) {
grunt.log.write(stderr.replace(process.env.GH_CRED, '[censored]'));
}
},
'git_list_tags': {
cmd: 'git for-each-ref --sort=refname ' +
'--format="%(refname:short)|||%(objectname)|||%(contents)\xB6\xB6\xB6" ' +
'refs/tags/v20[0-9][0-9]*',
stdout: false,
callback: function(err, stdout) {
if (!stdout) {
grunt.fatal('Git command returned no data.');
}
if (err) {
grunt.fatal('Git command failed to execute.');
}
var allTags = stdout
.replace(/-{5}BEGIN PGP SIGNATURE-{5}(.*\n)+?-{5}END PGP SIGNATURE-{5}\n/g, '')
.split('\xB6\xB6\xB6');
var foundTags = [];
allTags.forEach(function(curTag) {
if (curTag.length) {
var explode = curTag.split('|||');
if (explode[0] && explode[1] && explode[2]) {
foundTags.push({
tag: explode[0].trim(),
hash: explode[1].trim(),
message: explode[2].trim().split('\n'),
previous: (foundTags.length ? foundTags.slice(-1)[0].tag : null)
});
}
}
});
if (foundTags.length) {
grunt.config('all_tags', foundTags.reverse()); // LIFO
} else {
grunt.fatal('Could not get existing tags information');
}
}
},
'commit_changelog': {
cmd: function() {
var file = grunt.config('changesmd_file');
if (!file) {
grunt.fatal('Missing file path.');
}
var path = file.slice(0, -25); // slices 'sickchill-news/CHANGES.md' (len=25)
if (!path) {
grunt.fatal('path = "' + path + '"');
}
var pushCmd = 'git push origin master';
if (grunt.option('no-push')) {
grunt.log.warn('Pushing with --dry-run ...'.magenta);
pushCmd += ' --dry-run';
}
return ['cd ' + path, 'git commit -asm "Update changelog"', 'git fetch origin', 'git rebase',
pushCmd].join(' && ');
},
stdout: true
}
}
});
/****************************************
* Internal tasks *
*****************************************/
grunt.registerTask('_get_next_tag', '(internal) do not run', function() {
grunt.config.requires('last_tag');
var lastTag = grunt.config('last_tag');
var lastPatch = lastTag.match(/[0-9]+$/)[0];
lastTag = grunt.template.date(lastTag.replace(/^v|-[0-9]*-?$/g, ''), 'yyyy.mm.dd');
var today = grunt.template.today('yyyy.mm.dd');
var patch = lastTag === today ? (parseInt(lastPatch) + 1).toString() : '1';
var nextTag = 'v' + today + '-' + patch;
grunt.log.ok(('Creating tag ' + nextTag).green);
grunt.config('next_tag', nextTag);
});
grunt.registerTask('_genchanges', "(internal) do not run", function() {
// actual generate changes
var allTags = grunt.config('all_tags');
if (!allTags) {
grunt.fatal('No tags information was received.');
}
var file = grunt.config('changesmd_file'); // --file=path/to/sickchill.github.io/sickchill-news/CHANGES.md
if (!file) {
grunt.fatal('Missing file path.');
}
var contents = "";
allTags.forEach(function(tag) {
contents += '### ' + tag.tag + '\n';
contents += '\n';
if (tag.previous) {
contents += '[full changelog](https://github.com/SickChill/SickChill/compare/' +
tag.previous + '...' + tag.tag + ')\n';
}
contents += '\n';
tag.message.forEach(function (row) {
contents += row
// link issue numbers, style links of issues and pull requests
.replace(/([\w\d\-.]+\/[\w\d\-.]+)?#(\d+)|https?:\/\/github.com\/([\w\d\-.]+\/[\w\d\-.]+)\/(issues|pull)\/(\d+)/gm,
function(all, repoL, numL, repoR, typeR, numR) {
if (numL) { // repoL, numL = user/repo#1234 style
return '[' + (repoL ? repoL : '') + '#' + numL + '](https://github.com/' +
(repoL ? repoL : 'SickChill/SickChill') + '/issues/' + numL + ')';
} else if (numR) { // repoR, type, numR = https://github/user/repo/issues/1234 style
return '[#' + numR + ']' + '(https://github.com/' +
repoR + '/' + typeR + '/' + numR + ')';
}
})
// shorten and link commit hashes
.replace(/([a-f0-9]{40}(?![a-f0-9]))/g, function(sha1) {
return '[' + sha1.substr(0, 7) + '](https://github.com/SickChill/SickChill/commit/' + sha1 + ')';
})
// remove tag information
.replace(/^\([\w\d\s,.\-+/>]+\)\s/gm, '')
// remove commit hashes from start
.replace(/^[a-f0-9]{7} /gm, '')
// style messages that contain lists
.replace(/( {3,}\*)(?!\*)/g, '\n -')
// escapes markdown __ tags
.replace(/__/gm, '\\_\\_')
// add * to the first line only
.replace(/^(\w)/, '* $1');
contents += '\n';
});
contents += '\n';
});
if (contents) {
grunt.file.write(file, contents);
return true;
} else {
grunt.fatal('Received no contents to write to file, aborting');
}
});
};