I was bored looking for an existing task that would fit, so I finally created it, based on the code of grunt-bump and grunt-svn-bump :
grunt.registerTask('custom_bump', 'Custom task for bumping version after a release', function(){
var semver = require('semver');
var shell = require('shelljs');
function shellRun( cmd ){
if (grunt.option('dryRun')) {
grunt.log.writeln('Command (not running because of dryRun option): ' + cmd);
} else {
grunt.verbose.writeln('Running: ' + cmd);
var result = shell.exec(cmd, {silent:true});
if (result.code !== 0) {
grunt.log.error('Error (' + result.code + ') ' + result.output);
}
}
}
// Options
var options = this.options({
filepath: 'package.json',
commit: true,
commitMessage : 'New version following a release'
});
// Recover the next version of the component
var nextVersion = grunt.option('nextVersion');
if( !nextVersion ){
grunt.fatal( 'Next version is not defined.', 3 );
}
else if( !semver.valid( nextVersion ) ){
grunt.warn( 'Next version is invalid.', 3 );
}
// Upgrade version into package.json
var filepath = options.filepath;
var file = grunt.file.readJSON( filepath );
var currentVersion = file.version;
if( semver.lte( nextVersion, currentVersion ) ){
grunt.warn( 'Next version is lesser or equal than current version.' );
}
file.version = nextVersion;
grunt.log.write( 'Bumping version in ' + filepath + ' from ' + currentVersion + ' to ' + nextVersion + '... ' );
grunt.file.write( filepath, JSON.stringify( file, null, 2 ) );
grunt.log.ok();
// Commit the changed package.json file
if( options.commit ){
var message =
grunt.log.write( 'Committing ' + filepath + '... ' );
shellRun( 'svn commit "' + filepath + '" -m "' + options.commitMessage + '"' );
grunt.log.ok();
}
// Update the config for next tasks
var configProperty = 'pkg';
grunt.log.write( 'Updating version in ' + configProperty + ' config... ' );
var config = grunt.config( configProperty );
if( config ){
config.version = nextVersion;
grunt.config( configProperty, config );
grunt.log.ok();
} else {
grunt.log.warn( "Cannot update pkg config !" );
}
grunt.log.ok( 'Version updated from ' + currentVersion + ' to ' + nextVersion + '.' );
});
My 'release' task uses grunt-svn-tag and my custom bump task.
grunt.initConfig({
// ...
svn_tag: {
current: {
options: {
tag: '<%= pkg.name %>-<%= pkg.version %>'
}
}
}
});
grunt.registerTask('release', [
'svn_tag:current',
'custom_bump'
]);