Skip to content

Commit

Permalink
feat(log): automatically split notes based on release version
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Apr 7, 2013
1 parent bbc204c commit 5545fe4
Showing 1 changed file with 94 additions and 49 deletions.
143 changes: 94 additions & 49 deletions tasks/changelog.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,125 @@
'use strict';

var fs = require('fs');
var template = fs.readFileSync(__dirname + '/../template/changelog.md', 'utf8');

module.exports = function (grunt) {

// based on: https://github.com/angular-ui/bootstrap/commit/9a683eebccaeb6bfc742e6768562eb19ddd2216c
grunt.registerTask('changelog', 'generate a changelog from git metadata', function () {

var changeFrom = this.args[0],
changeTo = this.args[1] || 'HEAD';
var options = this.options({
versionRegex: /^v(\d+)(.\d+)*(-.*)?$/gim, // generate from the last version until this one
// dest: 'CHANGELOG.md',
prepend: true // false to append
});

var done = grunt.task.current.async();
var child = grunt.util.spawn({
cmd: process.platform === 'win32' ?
'git.cmd' : 'git',
args: [
var template;
if (options.templateFile) {
template = grunt.file.read(options.templateFile);
} else {
template = fs.readFileSync(__dirname + '/../template/changelog.md', 'utf8');
}

var gitArgs;
if (this.args.length > 0) {
var changeFrom = this.args[0];
var changeTo = this.args[1] || 'HEAD';
gitArgs = [
'log',
changeFrom + '..' + changeTo,
'--oneline'
]
];
} else {
gitArgs = [
'log',
'--oneline'
];
}

var done = grunt.task.current.async();
grunt.util.spawn({
cmd: process.platform === 'win32' ?
'git.cmd' : 'git',
args: gitArgs
}, function (err, stdout, stderr) {

if (stderr) {
return done(false);
}

var changelog = {};

// based on: https://github.com/angular/angular.js/blob/master/changelog.js#L50
// see also: https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/
var COMMIT_MSG_REGEXP = /^(.*)\((.*)\)\:\s(.*)$/;
var gitlog = stdout.toString().split('\n').reverse();

if (stderr) {
done(false);
} else {
gitlog.forEach(function (logItem) {
var sha1 = logItem.slice(0, 7);
var fullMsg = logItem.slice(8);

var changeType,
changeScope,
changeMessage;

if (COMMIT_MSG_REGEXP.test(fullMsg)) {
// if it conforms to the changelog style
var msgMatches = fullMsg.match(COMMIT_MSG_REGEXP);
changeType = msgMatches[1];
changeScope = msgMatches[2];
changeMessage = msgMatches[3];
} else {
// otherwise
changeType = 'other';
changeMessage = fullMsg;
// if no range was specified, attempt to find the last version commit
if (gitArgs.length === 2) {
var i, spl;
for (i = 0; i < gitlog.length; i++) {
if (options.versionRegex.test(gitlog[i].toString().substr(8))) {
spl = i;
break;
}
}
if (spl) {
gitlog = gitlog.slice(spl + 1);
}
}

if (!changelog[changeType]) {
changelog[changeType] = {};
}
if (!changelog[changeType][changeScope]) {
changelog[changeType][changeScope] = [];
}
changelog[changeType][changeScope].push({
sha1: sha1,
msg: changeMessage
});
gitlog.forEach(function (logItem) {
var sha1 = logItem.slice(0, 7);
var fullMsg = logItem.slice(8);

var changeType,
changeScope,
changeMessage;

if (COMMIT_MSG_REGEXP.test(fullMsg)) {
// if it conforms to the changelog style
var msgMatches = fullMsg.match(COMMIT_MSG_REGEXP);
changeType = msgMatches[1];
changeScope = msgMatches[2];
changeMessage = msgMatches[3];
} else {
// otherwise
changeType = 'other';
changeMessage = fullMsg;
}

if (!changelog[changeType]) {
changelog[changeType] = {};
}
if (!changelog[changeType][changeScope]) {
changelog[changeType][changeScope] = [];
}
changelog[changeType][changeScope].push({
sha1: sha1,
msg: changeMessage
});
});

console.log(grunt.template.process(template, {
data: {
changelog: changelog,
today: grunt.template.today('yyyy-mm-dd'),
version : grunt.config('pkg.version')
}
}));
var newLog = grunt.template.process(template, {
data: {
changelog: changelog,
today: grunt.template.today('yyyy-mm-dd'),
version : grunt.config('pkg.version')
}
});

done();
if (options.dest) {
var log = grunt.file.read(options.dest);
if (options.prepend) {
log = newLog + log;
} else {
log += newLog;
}
grunt.file.write(options.dest, log);
} else {
console.log(newLog);
}
});

});
});
};

0 comments on commit 5545fe4

Please sign in to comment.