forked from SickChill/sickchill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
588 lines (556 loc) · 24.5 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
'use strict';
module.exports = function(grunt) {
const CI = Boolean(process.env.CI);
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 (!CI) {
grunt.fatal('This task is only for CI!');
return false;
}
grunt.log.writeln('Running grunt and updating translations...'.magenta);
grunt.task.run([
'exec:git:checkout:master',
'default', // Run default task
'exec:update_translations', // 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', // Checkout develop
'exec:git:rebase:master', // FF develop to the updated master
'exec:git_push:origin:master develop' // Push master and develop
]);
});
/****************************************
* 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('reset_publishing', 'reset the repository back to clean master and develop from remote, and remove the local tag created to facilitate easier testing to the changes made here.', function() {
if (CI) {
grunt.fatal('This task not for CI!');
return false;
}
grunt.log.writeln('Resetting the local repo back to remote heads for develop and master, and undoing any tags...'.red);
grunt.task.run([
'exec:check_return_branch', // Save the branch we are currently on, so we can return here
'exec:git:checkout:master', // Checkout master
'exec:git:reset --hard:origin/master', // Reset back to remote master
'exec:git:checkout:develop', // Check out develop
'exec:git:reset --hard:origin/develop', // Reset back to remote develop
'_get_next_version:true', // To set the today string in grunt.config
'exec:delete_today_tags', // Delete all local tags matching today's date
'exec:git:fetch:origin --tags', // Pull tags back from remote
'exec:check_return_branch:true', // Go back to the branch we were on
]);
});
grunt.registerTask('newrelease', "Pull and merge develop to master, create and push a new release", [
// Make sure we have the newest remote changes locally
'exec:git:checkout:develop', // Switch to develop
'exec:git:pull', // Pull develop
'exec:git:checkout:master', // Switch to master
'exec:git:pull', // Pull master
// Set up old and new version strings
'_get_last_version', // Get last tagged version
'_get_next_version', // Get next version to set
// Start merging and releasing
'exec:git:merge:develop --strategy-option theirs', // Merge develop into master
'exec:bump_version', // Update version in pyproject.toml
'exec:commit_changed_files:yes', // Commit the new changed version
'exec:git_list_changes', // List changes from since last tag
'exec:git_tag_next_version', // Create new release tag
'exec:git_push:origin:master:tags', // Push master + tags
'exec:git:checkout:develop', // Go back to develop
'exec:git:merge:master --strategy-option theirs', // Merge master back into develop
'exec:git_push:origin:develop:tags', // Push develop + tags
]);
grunt.registerTask('genchanges', "Generate CHANGES.md file", function() {
let 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: './sickchill/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/parsers/parser-metric.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/**/*"
],
"jqueryui-touch-punch": [
"jquery.ui.touch-punch.min.js"
]
},
bowerOptions: {
relative: false
}
}
},
copy: {
openSans: {
files: [{
expand: true,
dot: true,
cwd: 'bower_components/open-sans-fontface',
src: [
'fonts/**/*'
],
dest: './sickchill/gui/slick/css/'
}]
},
'fork-awesome': {
files: [{
expand: true,
dot: true,
cwd: 'bower_components/fork-awesome',
src: [
'fonts/**/*',
'css/**/*.min.css',
'css/**/*.css.map'
],
dest: './sickchill/gui/slick/'
}]
},
'font-awesome': {
files: [{
expand: true,
dot: true,
cwd: 'bower_components/font-awesome',
src: [
'fonts/**/*',
'css/**/*.min.css',
'css/**/*.css.map'
],
dest: './sickchill/gui/slick/'
}]
},
glyphicon: {
files: [{
expand: true,
dot: true,
cwd: 'bower_components/bootstrap/fonts',
src: [
'*.eot',
'*.svg',
'*.ttf',
'*.woff',
'*.woff2'
],
dest: './sickchill/gui/slick/fonts/'
}]
}
},
uglify: {
bower: {
files: {
'./sickchill/gui/slick/js/vendor.min.js': ['./dist/bower.js']
}
},
core: {
files: {
'./sickchill/gui/slick/js/core.min.js': ['./sickchill/gui/slick/js/core.js']
}
}
},
cssmin: {
options: {
shorthandCompacting: false,
roundingPrecision: -1
},
bower: {
files: {
'./sickchill/gui/slick/css/vendor.min.css': ['./dist/bower.css']
}
},
core: {
files: {
'./sickchill/gui/slick/css/core.min.css': ['./dist/core.css']
}
}
},
po2json: {
messages: {
options: {
format: 'jed',
singleFile: true
},
files: [{
expand: true,
src: 'sickchill/locale/*/LC_MESSAGES/messages.po',
dest: '',
ext: '' // workaround for relative files
}]
}
},
exec: {
// Translations
'update_translations': {cmd: 'poe update_translations'},
'check_return_branch': {
cmd: function (go_back) {
let command = 'git branch --show-current'
if (Boolean(go_back)) {
command = 'git checkout ' + grunt.config('return_branch') + ' && ' + command;
}
return command;
},
stdout: false,
callback: function(err, stdout) {
stdout = stdout.trim();
if (!stdout.length) {
grunt.fatal('Could not find out what branch you were on!', 0);
}
grunt.config('return_branch', stdout);
},
},
'bump_version': {
cmd: function () {
return 'poetry version ' + grunt.config('next_version');
},
callback: function(err, stdout) {
stdout = stdout.trim();
if (!stdout.length) {
grunt.fatal('No changes to commit.', 0);
}
if (!stdout.match(/Bumping version from \d{4}\.\d{1,2}\.\d{1,2}(\.\d*)?/)) {
grunt.fatal('Did the version update in pyproject.toml?')
}
}
},
// delete tags from today
'delete_today_tags': {
cmd: function () {
return 'git tag -d $(git describe --match "' + grunt.config('today') + '*" --abbrev=0 --tags $(git rev-list --tags --max-count=1))'
},
stderr: false
},
// 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(ci) {
grunt.config('stop_no_changes', Boolean(ci));
return 'git status -s -- pyproject.toml sickchill/locale/ sickchill/gui/';
},
stdout: false,
callback: function(err, stdout) {
stdout = stdout.trim();
if (!stdout.length) {
grunt.fatal('No changes to commit.', 0);
}
let commitMsg = [];
let commitPaths = [];
let isRelease = stdout.match(/pyproject.toml/gm)
if (isRelease) {
commitMsg.push('Release version ' + grunt.config('next_version'));
commitPaths.push('pyproject.toml');
}
if (stdout.match(/sickchill\/gui\/.*(vendor|core)\.min\.(js|css)$/gm)) {
if (!isRelease) {
commitMsg.push('Grunt');
}
commitPaths.push('sickchill/gui/**/vendor.min.*');
commitPaths.push('sickchill/gui/**/core.min.*');
}
if (stdout.match(/sickchill\/locale\/.*(pot|po|mo|json)$/gm)) {
if (!isRelease) {
commitMsg.push('Update translations');
}
commitPaths.push('sickchill/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.GITHUB_RUN_ID && !isRelease) {
commitMsg += ' (build ' + process.env.GITHUB_RUN_ID + ') [skip ci]';
}
grunt.config('commit_msg', commitMsg);
grunt.config('commit_paths', commitPaths.join(' '));
grunt.task.run('exec:commit_combined');
}
}
},
'commit_combined': {
cmd: function() {
const message = grunt.config('commit_msg');
const 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 (!CI) {
grunt.task.run('exec:git:commit:-m "' + grunt.config('commit_msg') + '"');
} else { // Workaround for CI (with -m "text" the quotes are within the message)
const msgFilePath = 'commit-msg.txt';
grunt.file.write(msgFilePath, grunt.config('commit_msg'));
grunt.task.run('exec:git:commit:-F ' + msgFilePath);
}
}
}
},
'git_list_changes': {
cmd: function() { return 'git log --oneline --first-parent --pretty=format:%s ' + grunt.config('last_version') + '..HEAD'; },
stdout: false,
callback: function(err, stdout) {
let commits = stdout.trim()
.replace(/`/gm, '').replace(/^\([\w\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_next_version': {
cmd: function (sign) {
const next_version = grunt.config('next_version');
grunt.log.ok(('Creating tag ' + next_version).green);
sign = sign !== "true" ? '' : '-s ';
return 'git tag ' + sign + next_version + ' -m "' + grunt.config('commits') + '"';
},
stdout: false
},
'git_push': {
cmd: function (remote, branch, tags) {
let 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/20[0-9][0-9].[0-9][0-9].[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.');
}
let allTags = stdout
.replace(/-{5}BEGIN PGP SIGNATURE-{5}(.*\n)+?-{5}END PGP SIGNATURE-{5}\n/g, '')
.split('\xB6\xB6\xB6');
let foundTags = [];
allTags.forEach(function(curTag) {
if (curTag.length) {
let 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() {
const file = grunt.config('changesmd_file');
if (!file) {
grunt.fatal('Missing file path.');
}
let path = file.slice(0, -25); // slices 'sickchill-news/CHANGES.md' (len=25)
if (!path) {
grunt.fatal('path = "' + path + '"');
}
let 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_last_version', '(internal) do not run', function() {
const toml = require('toml');
const file = './pyproject.toml';
if (!grunt.file.exists(file)) {
grunt.fatal("Could not find pyproject.toml, cannot proceed");
}
const version = toml.parse(grunt.file.read(file)).tool.poetry.version;
if (version === null) {
grunt.fatal("Error processing pyproject.toml, cannot proceed")
}
grunt.config('last_version', version)
});
grunt.registerTask('_get_next_version', '(internal) do not run', function(skip_post) {
const date_object = new Date();
const year = date_object.getFullYear();
const day = date_object.getDate();
const month = date_object.getMonth() + 1;
const hours = date_object.getUTCHours();
const minutes = date_object.getUTCMinutes();
const seconds = date_object.getUTCSeconds();
let next_version = year.toString() + '.' + month.toString().padStart(2-month.length, "0") + '.' + day.toString().padStart(2-day.length, "0")
grunt.config('today', next_version); // Needed for resetting failed publishing.
if (Boolean(skip_post)) {
return
}
const last_version = grunt.config('last_version');
if (last_version === undefined || !last_version.length) {
grunt.fatal('Must call _get_last_version first!');
}
if (next_version === last_version) {
grunt.fatal('Let\'s only release once a day, or semver is broken. We can fix this when we do away with grunt')
next_version += '.' + hours + minutes + seconds
}
grunt.config('next_version', next_version);
});
grunt.registerTask('_genchanges', "(internal) do not run", function() {
// actual generate changes
const allTags = grunt.config('all_tags');
if (!allTags) {
grunt.fatal('No tags information was received.');
}
const file = grunt.config('changesmd_file'); // --file=path/to/sickchill.github.io/sickchill-news/CHANGES.md
if (!file) {
grunt.fatal('Missing file path.');
}
let 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 n return 'git tag ' + grunt.config('next_version') + ' -sm "' + grunt.config('commits') + '"';umbers, style links of issues and pull requests
.replace(/([\w\-.]+\/[\w\-.]+)?#(\d+)|https?:\/\/github.com\/([\w\-.]+\/[\w\-.]+)\/(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-f\d]{40}(?![a-f\d]))/g, function(sha1) {
return '[' + sha1.substring(0, 7) + '](https://github.com/SickChill/SickChill/commit/' + sha1 + ')';
})
// remove tag information
.replace(/^\([\w\s,.\-+/>]+\)\s/gm, '')
// remove commit hashes from start
.replace(/^[a-f\d]{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');
}
});
};