-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
Gruntfile.js
613 lines (543 loc) · 23.6 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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
'use strict';
module.exports = function (grunt) {
const CI = Boolean(process.env.CI);
grunt.registerTask('default', [
'clean',
'bower',
'bower_concat',
'copy',
'uglify',
'cssmin',
]);
/****************************************
* Admin only tasks *
****************************************/
grunt.registerTask('publish', 'ADMIN: Create a new release tag and generate new CHANGES.md', [
'exec:checkReturnBranch', // Save the branch we are currently on, so we can return here
// 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
'_getLastVersion', // Get last tagged version
'_getNextVersion', // Get next version to set
// Start merging and releasing
'exec:git:merge:develop --strategy-option theirs', // Merge develop into master
// 'default', // Run default task, grunt
// 'exec:updateTranslations', // Update translations
'exec:bumpVersion', // Update version in pyproject.toml
'exec:gitListChanges', // List changes from since last tag
'exec:gitListTags', // Get list of tags
'_genchanges:yes', // Generate changelog
'exec:commitChangedFiles:yes', // Commit the changelog
'exec:gitTagNextVersion:yes', // Create new release tag
'exec:gitPush:origin:master:tags', // Push master + tags
'exec:pypi', // PBuild and push wheel to pypi
'exec:git:checkout:develop', // Go back to develop
'exec:git:merge:master --strategy-option theirs', // Merge master back into develop
'exec:gitPush:origin:develop:tags', // Push develop + tags
'exec:checkReturnBranch:true', // Go back to the branch we were on
]);
grunt.registerTask('genchanges', 'Generate CHANGES.md file', () => {
grunt.task.run(['exec:gitListTags', '_genchanges']);
});
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.', () => {
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:checkReturnBranch', // 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
'_getNextVersion:true', // To set the today string in grunt.config
'exec:deleteTodayTags', // Delete all local tags matching today's date
'exec:git:fetch:origin --tags', // Pull tags back from remote
'exec:checkReturnBranch:true', // Go back to the branch we were on
]);
});
/****************************************
* 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
updateTranslations: {cmd: 'poe update_translations'},
checkReturnBranch: {
cmd(checkout) {
let command = 'git branch --show-current';
if (checkout) {
command = 'git checkout ' + grunt.config('return_branch') + ' && ' + command;
}
return command;
},
stdout: false,
callback(error, stdout) {
const output = stdout.trim();
if (output.length === 0) {
grunt.fatal('Could not find out what branch you were on!', 0);
}
grunt.config('return_branch', output);
},
},
bumpVersion: {
cmd() {
grunt.task.requires('_getNextVersion');
return 'poetry version ' + grunt.config('nextVersion');
},
callback(error, stdout) {
stdout = stdout.trim();
if (stdout.length === 0) {
grunt.fatal('Failed to bump version!.', 0);
}
if (!/Bumping version from \d{4}(?:\.\d{1,2}){2}(\.\d*)?/.test(stdout)) {
grunt.fatal('Did the version update in pyproject.toml?');
}
},
},
// Delete tags from today
deleteTodayTags: {
cmd() {
grunt.task.requires('_getNextVersion');
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'},
// Build and push to pypi
pypi: {cmd: 'poetry publish --build'},
// Publish/Releases
git: {
cmd(cmd, branch) {
branch = branch ? ' ' + branch : '';
return 'git ' + cmd + branch;
},
},
commitChangedFiles: { // Choose what to commit.
cmd(ci) {
grunt.config('stop_no_changes', Boolean(ci));
return 'git status -s -- pyproject.toml sickchill/locale/ sickchill/gui/ CHANGES.md';
},
stdout: true,
callback(error, stdout) {
stdout = stdout.trim();
if (stdout.length === 0) {
grunt.fatal('No changes to commit.', 0);
}
let commitMessage = [];
const commitPaths = [];
const isRelease = stdout.match(/pyproject.toml/gm);
if (isRelease) {
grunt.task.requires('_getNextVersion');
commitMessage.push('Release version ' + grunt.config('nextVersion'));
commitPaths.push('pyproject.toml');
}
const changes = stdout.match(/CHANGES.md/gm);
if (changes) {
commitMessage.push('Update Changelog');
commitPaths.push('CHANGES.md');
}
if (/sickchill\/gui\/.*(vendor|core)\.min\.(js|css)$/gm.test(stdout)) {
commitMessage.push('Grunt');
commitPaths.push('sickchill/gui/**/vendor.min.*', 'sickchill/gui/**/core.min.*');
}
if (/sickchill\/locale\/.*(pot|po|mo|json)$/gm.test(stdout)) {
commitMessage.push('Update translations');
commitPaths.push('sickchill/locale/');
}
if (commitMessage.length === 0 || commitPaths.length === 0) {
if (grunt.config('stop_no_changes')) {
grunt.fatal('Nothing to commit, aborting', 0);
} else {
grunt.log.ok('No extra changes to commit'.green);
}
} else {
commitMessage = commitMessage.join(', ');
grunt.config('commit_msg', commitMessage);
grunt.config('commit_paths', commitPaths.join(' '));
grunt.task.run('exec:commitCombined');
}
},
},
commitCombined: {
cmd() {
// Grunt.task.requires('commitChangedFiles');
const message = grunt.config('commit_msg');
const paths = grunt.config('commit_paths');
if (!message || !paths) {
grunt.fatal('Call exec:commitChangedFiles instead!');
}
return 'git add -- ' + paths;
},
callback(error) {
if (!error) {
if (CI) { // Workaround for CI (with -m "text" the quotes are within the message)
const messageFilePath = 'commit-msg.txt';
grunt.file.write(messageFilePath, grunt.config('commit_msg'));
grunt.task.run('exec:git:commit:-F ' + messageFilePath);
} else {
grunt.task.run('exec:git:commit:-m "' + grunt.config('commit_msg') + '"');
}
}
},
},
gitListChanges: {
cmd() {
grunt.task.requires('_getLastVersion');
return 'git log --oneline --first-parent --pretty=format:%s ' + grunt.config('lastVersion') + '..HEAD';
},
stdout: false,
callback(error, stdout) {
const commits = stdout.trim().replaceAll(/`/gm, '').replaceAll(/^\([\w\s,.\-+_/>]+\)\s/gm, '').replaceAll(/"/gm, '\\"'); // Removes ` and tag information
if (commits) {
grunt.config('commits', commits);
} else {
grunt.fatal('Getting new commit list failed!');
}
},
},
gitTagNextVersion: {
cmd(sign) {
grunt.task.requires('_getNextVersion');
grunt.task.requires('exec:gitListChanges');
const nextVersion = grunt.config('nextVersion');
grunt.log.ok(('Creating tag ' + nextVersion).green);
sign = sign === 'true' ? '-s ' : '';
return 'git tag ' + sign + nextVersion + ' -m "' + grunt.config('commits') + '"';
},
stdout: false,
},
gitPush: {
cmd(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(error, stdout, stderr) {
grunt.log.write(stderr.replace(process.env.GH_CRED, '[censored]'));
},
},
gitListTags: {
cmd: 'git for-each-ref --sort=refname --format="%(refname:short)|||%(objectname)|||%(contents)\u00B6\u00B6\u00B6" refs/tags/20[2-9][2-9].[0-9]*.[0-9]*',
stdout: false,
callback(error, stdout) {
if (!stdout && !error) {
grunt.fatal('Git command returned no data.');
}
if (error) {
grunt.fatal('Git command failed to execute: ' + error);
}
const allTags = stdout
.replaceAll(/-{5}BEGIN PGP SIGNATURE-{5}(.*\n)+?-{5}END PGP SIGNATURE-{5}\n/g, '')
.split('\u00B6\u00B6\u00B6');
const foundTags = [];
for (const curTag of allTags) {
if (curTag.length > 0) {
const 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 > 0 ? foundTags.at(-1).tag : null),
});
}
}
}
if (foundTags.length > 0) {
grunt.config('all_tags', foundTags.reverse()); // LIFO
} else {
grunt.fatal('Could not get existing tags information');
}
},
},
},
});
/****************************************
* Internal tasks *
*****************************************/
grunt.registerTask('_getLastVersion', '(internal) do not run', () => {
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('lastVersion', version);
});
grunt.registerTask('_getNextVersion', '(internal) do not run', skipPost => {
const date = new Date();
const year = date.getFullYear();
const day = date.getDate();
const month = date.getMonth() + 1;
const hours = date.getUTCHours();
const minutes = date.getUTCMinutes();
const seconds = date.getUTCSeconds();
let nextVersion = year.toString() + '.' + month.toString().padStart(2 - month.length, '0') + '.' + day.toString().padStart(2 - day.length, '0');
grunt.config('today', nextVersion); // Needed for resetting failed publishing.
if (skipPost) {
return;
}
grunt.task.requires('_getLastVersion');
const lastVersion = grunt.config('lastVersion');
if (nextVersion === lastVersion) {
grunt.log.writeln('Let\'s only release once a day, or semver is broken. We can fix this when we do away with grunt');
nextVersion += '.' + hours + minutes + seconds;
}
grunt.config('nextVersion', nextVersion);
});
grunt.registerTask('_genchanges', '(internal) do not run', isRelease => {
// Actual generate changes
grunt.task.requires('exec:gitListTags');
const allTags = grunt.config('all_tags');
if (!allTags) {
grunt.fatal('No tags information was received.');
}
const file = grunt.config('changesmd_file', './CHANGES.md');
if (!grunt.file.exists(file)) {
grunt.fatal('Specified CHANGES file does not exist: ' + file);
}
function processCommit(row) {
return row
// Link issue n return numbers, style links of issues and pull requests
.replaceAll(/([\w\-.]+\/[\w\-.]+)?#(\d+)|https?:\/\/github.com\/([\w\-.]+\/[\w\-.]+)\/(issues|pull)\/(\d+)/gm,
(all, repoL, numberL, repoR, typeR, numberR) => {
if (numberL) { // RepoL, numL = user/repo#1234 style
return '[' + (repoL ? repoL : '') + '#' + numberL + '](https://github.com/' + (repoL ? repoL : 'SickChill/SickChill') + '/issues/' + numberL + ')';
}
if (numberR) { // RepoR, type, numR = https://github/user/repo/issues/1234 style
return '[#' + numberR + ']' + '(https://github.com/' + repoR + '/' + typeR + '/' + numberR + ')';
}
})
// Shorten and link commit hashes
.replaceAll(/([a-f\d]{40}(?![a-f\d]))/g, sha1 => '[' + sha1.slice(0, 7) + '](https://github.com/SickChill/SickChill/commit/' + sha1 + ')')
// Remove tag information
.replaceAll(/^\([\w\s,.\-+/>]+\)\s/gm, '')
// Remove commit hashes from start
.replaceAll(/^[a-f\d]{7} /gm, '')
// Style messages that contain lists
.replaceAll(/( {3,}\*)(?!\*)/g, '\n -')
// Escapes markdown __ tags
.replaceAll(/__/gm, '\\_\\_')
// Add * to the first line only
.replace(/^(\w)/, '* $1')
.replaceAll(/^.*merge branch '(develop|master)'.*$/gim, '');
}
function processArray(array, output) {
for (const row of new Set(array)) {
const result = processCommit(row);
if (result.length > 3) {
output += result;
output += '\n';
}
}
if (output) {
output += '\n';
}
return output;
}
let contents = '';
if (isRelease) {
grunt.task.requires('_getLastVersion');
grunt.task.requires('_getNextVersion');
grunt.task.requires('exec:gitListChanges');
const lastVersion = grunt.config('lastVersion');
const nextVersion = grunt.config('nextVersion');
if (!nextVersion) {
grunt.fatal('Can not generate changelog for a release without the next version information!');
}
if (!lastVersion) {
grunt.fatal('Can not generate changelog for a release without the last version information!');
}
contents += '### ' + nextVersion + '\n';
contents += '\n';
contents += '[full changelog](https://github.com/SickChill/SickChill/compare/' + lastVersion + '...' + nextVersion + ')\n';
contents += '\n';
const commits = grunt.config('commits');
contents = processArray(commits, contents);
}
for (const tag of allTags) {
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';
}
contents = processArray(tag.message, contents);
}
if (contents) {
grunt.file.write(file, contents.replace(/\n*$/, '\n'));
return true;
}
grunt.fatal('Received no contents to write to file, aborting');
});
};