| 1 |
module.exports = function (grunt, wpmudev) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
grunt.registerTask('wpmudev_branchwork', function (type, task_id, version) { |
| 5 |
var done = this.async(); |
| 6 |
|
| 7 |
if (arguments.length < 1) { |
| 8 |
grunt.log.error("The task requires at least 1 param: bug ID (e.g. Asana bug IDREF - the last numeric portion of the URL path"); |
| 9 |
grunt.log.writeln("You can also specify the source version to work off as second argument (git tag) - otherwise, the task works off current branch, latest commit"); |
| 10 |
done(); |
| 11 |
} |
| 12 |
if (arguments.length < 2) { |
| 13 |
grunt.log.writeln("No version (git tag) specified - working off current branch, latest commit"); |
| 14 |
} |
| 15 |
|
| 16 |
version = version || false; |
| 17 |
task_id = task_id || false; |
| 18 |
var branch = task_id.replace(/[^-0-9a-zA-Z]/g, '-').replace('/-+/', '-').toLowerCase(), |
| 19 |
is_asana_taskid = !!task_id.match(/^[0-9]+$/), |
| 20 |
cmdparts = (version ? ['checkout', version, '-b'] : ['checkout', '-b']), |
| 21 |
task_ref = (is_asana_taskid ? 'https://app.asana.com/0/' + task_id + '/' + task_id : task_id), |
| 22 |
chlog_msg = task_ref + ' (' + grunt.template.today('yyyy-mm-dd') + ')', |
| 23 |
commit_msg = task_ref |
| 24 |
; |
| 25 |
|
| 26 |
if ('fix' === type) { |
| 27 |
chlog_msg = '- Fix: ' + chlog_msg; |
| 28 |
commit_msg = 'Fixes: ' + commit_msg; |
| 29 |
branch = 'fix/' + branch; |
| 30 |
} else if ('feature' === type) { |
| 31 |
chlog_msg = '- Add: ' + chlog_msg; |
| 32 |
commit_msg = 'Implements: ' + commit_msg; |
| 33 |
branch = 'new/' + branch; |
| 34 |
} else { |
| 35 |
chlog_msg = '- Task: ' + chlog_msg; |
| 36 |
commit_msg = 'Task: ' + commit_msg; |
| 37 |
branch = 'other/' + branch; |
| 38 |
} |
| 39 |
cmdparts.push(branch); |
| 40 |
|
| 41 |
if (!is_asana_taskid) { |
| 42 |
grunt.log.warn("Your task description does NOT seem to be an Asana task ID!".red.bold); |
| 43 |
grunt.log.writeln("Ideally, all work should have an Asana tasks reference. Please, consider creating an Asana task first.".yellow); |
| 44 |
grunt.log.writeln("Your task branch will still be created now, though."); |
| 45 |
} |
| 46 |
|
| 47 |
// First up, create a branch and use that: |
| 48 |
grunt.log.writeln("Creating the target work branch"); |
| 49 |
grunt.util.spawn({ |
| 50 |
cmd: 'git', |
| 51 |
args: cmdparts |
| 52 |
}, function (error, result) { |
| 53 |
if (error) { |
| 54 |
grunt.log.error("We encountered an error creating the target branch", result); |
| 55 |
grunt.log.error("Aborting"); |
| 56 |
done(); |
| 57 |
} |
| 58 |
// Next up, add our fix to the changelog: |
| 59 |
if (grunt.file.exists("changelog.txt")) { |
| 60 |
grunt.log.writeln("Updating the changelog with temporary (versionless) info..."); |
| 61 |
var changelog = grunt.file.read("changelog.txt"); |
| 62 |
|
| 63 |
// Find the most recent release line and stuff our changelog message above |
| 64 |
var res = changelog.replace(/\r\n/g, "\n").replace(/\r/, "\n").replace(/(\n\d\.\d.*?\n-{5,})/, chlog_msg + "\n$1"); |
| 65 |
grunt.file.write("changelog.txt", res); |
| 66 |
} |
| 67 |
|
| 68 |
// Make the initial commit for the task - just the changelog change |
| 69 |
grunt.log.writeln("Making the initial commit in your work branch"); |
| 70 |
grunt.util.spawn({ |
| 71 |
cmd: 'git', |
| 72 |
args: ['commit', '-am', commit_msg] |
| 73 |
}, function (error, result) { |
| 74 |
if (error) { |
| 75 |
grunt.log.error("We encountered an error making the initial commit in bugfix branch", result); |
| 76 |
grunt.log.error("Aborting"); |
| 77 |
done(); |
| 78 |
} |
| 79 |
grunt.log.writeln("All good, let's get on with the actual work now!"); |
| 80 |
done(); |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
}); |
| 85 |
|
| 86 |
grunt.registerTask('bugfix', function (bug_id, version) { |
| 87 |
version = version ? ':' + version : ''; |
| 88 |
grunt.task.run('wpmudev_branchwork:fix:' + bug_id + version); |
| 89 |
}); |
| 90 |
|
| 91 |
grunt.registerTask('featureadd', function (task_id, version) { |
| 92 |
version = version ? ':' + version : ''; |
| 93 |
grunt.task.run('wpmudev_branchwork:feature:' + task_id + version); |
| 94 |
}); |
| 95 |
|
| 96 |
}; |
| 97 |
|