Eria
Eria

Reputation: 3182

Bump a specific version number on SVN using Grunt

I have an AngularJS module, built with Grunt. The version of this module is managed in the package.json file.

What I need to do

I need to create a grunt task to release the module when needed. Here what this task must do :

  1. Perform a tag of the current module files on SVN (and it has to be SVN, not GIT).
  2. Upgrade the version in package.json to a given version (for example, I will pass --version = X.Y.Z as option to the grunt task). I don't want a solution based only on "patch", "minor" or "major" upgrades.
  3. Commit the change on the package.json file.

What I found so far

grunt-bump allows me to pass a specific version, using --setversion option. But it cannot commit the change on SVN, it's only working with GIT.

grunt-svn-bump allows me to commit on SVN, but I can't find a way to specify the next version. And it cannot perform the "tag" part.

grunt-svn-tag allows me to tag the files on SVN repository.

Do you know another grunt plugin that could fit ? Any help will be appreciated.

Upvotes: 2

Views: 867

Answers (2)

Eria
Eria

Reputation: 3182

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'
]);

Upvotes: 2

Pjetr
Pjetr

Reputation: 1382

small disclaimer, I don't use SVN or Grunt, but GIT and Gulp, so I don't really know the syntaxes to any of those.

That being said, I would not put this in a grunt/gulp task, but I would create a NPM task to just run a small shell-script. npm release X.Y.Z
The shellscript could contain something like this:

#!/usr/bin/env bash

VERSION=$2
echo "gulp bump $VERSION"
gulp bump $VERSION

echo "staging package.json"
git add package.json

echo "commit release"
git commit -m "release $VERSION"
git tag -a "$VERSION" -m "release $VERSION"
git push origin --tags

Now I haven't tested this syntax, but something along these lines is how I would try it.

Upvotes: 0

Related Questions