Using Grunt to Update the AssemblyInfo.cs version number

Grunt.js is a slick task runner which can be used for stuff other than JavaScript. In fact Our team is using it to build our .Net and Windows Phone code as well.

Recently we just added a new task which will parse the AssemblyInfo.cs file and increment the version number of the Assembly.

Below is our code which we are using to accomplish this.

    grunt.registerTask('updateAssemblyVersion', function(){
        grunt.log.writeln('About to parse the assembly files');
        var assemblyFile ='./AssemblyInfo.cs');
        var lines = assemblyFile.split('\n');
        var version = '';
        var versionParts = '';
        var searchString = 'AssemblyVersion';

        _.each(lines, function (line, index) {
            if (line.indexOf(searchString) != -1) {
                version = line.match(/\d+\.\d+\.\d+\.\d+/g).toString();
                versionParts = version.split('.');

                versionParts[3] = parseInt(versionParts[3], 10) + 1;

                lines[index] = line.replace(version, versionParts.join('.'));

        var updatedAssemblyFile = lines.join('\n');        
        grunt.file.write('./AssemblyInfo.cs', updatedAssemblyFile);

        grunt.log.write('Updated to version: ' + versionParts);

Basically all we are doing is opening up the AssemblyInfo.cs class and reading it in and then splitting it into a string array. Once we have the string array we find the line which matches our tag of AssemblyVersion. Once we find this tag we grab the version, split it apart and increment the last part.

Finally once we have updated the line in the array we re-write the information back to the file and go about our merry way.

In order to get this to work you will need to reference the Underscore NPM inside your script.

Now, I am sure there are other ways, better ways to do this but this works for our team. I am 99% sure my next step is to make this into an NPM package but that is for another day.

Till next time,

This entry was posted in Uncategorized and tagged , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply