Using Grunt to update WMAppManifest.xml version number (Windows Phone Manifest)

In my previous post I shows how to use Grunt.js to update the AssemblyInfo.cs file. In this post I am going to show you how to update the WMAppManifest.xml file. This is the manifest file which is used by Windows Phone projects to determine settings. I wanted to be able to bump the version number inside this file and boy was this a PITA.

I thought this would be pretty easy, I mean how hard could it be it is only an xml file. Turns out this was a bit trickier than I had expected. I had kinda expected there to be an existing grunt package to help update an xml file, there is not (I have plans to package this as a NPM in the near future).

In order to get my code below to work you will need to download 2 NPM packages
1) XPath – https://www.npmjs.org/package/xpath
2) XmlDom – https://www.npmjs.org/package/xmldom

Once we have our 2 packages downloaded we can use the code below to extract the value out of our document and update it.

// include these outside the module.exports call
var xpath = require('xpath');
var dom = require('xmldom').DOMParser;

// this should be inside your grunt.initConfig method
grunt.registerTask('updateManifestVersion', function(){
    var xml = grunt.file.read('../Properties/WMAppManifest.xml');
    var doc = new dom().parseFromString(xml);
    
    var node = xpath.select("//Deployment/App/@Version", doc);
    
    grunt.log.writeln('Current WMAppManifest Version: ' + node[0].value);        
    
    node[0].value = currentVersion;
    
    grunt.log.writeln('Updated WMAppManifest Version: ' + node[0].value);
    
    grunt.file.write("../Properties/WMAppManifest.xml", doc);
});

Lets breakdown the code above.

First we need to reference both xpath and xmlDom, this is done via the 2 require statements.

Next we created a simple inline grunt task to handle all of the logic.

  1. Read in the WMAppManifest file
  2. Select the correct element via XPath (god I hate XPath).
  3. Update its version in the dom to the value of my currentVersion variable (this is populated outside of this call)
  4. Write the value of the dom back into my WMAppManifest.xml file

If all is setup correct I can call this task on the commandline via ‘grunt updateManifestVersion’

Till next time,

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

Leave a Reply