Automate Nuget pack and push using Grunt and Node.js
Automation is fun, even when its simple. I try to automate as much as I can in my professional and personal life, if it makes sense. Even if it saves you five minutes in a work day, that's over 1.5 hours of time you get back every month. So in this case, automating the build and publish of the few nuget packages I maintain will allow me to spend that captured time somewhere else. Granted the return of the initial investment needs to make sense as well. Unless your like me and often just do things to learn them, then it's time well spent regardless.
To get started with grunt on Windows, you may need to do a few things to get your environment ready. First, install Node.js. Easiest ways are to grab the installer from the website or if you have Chocolaty installed, run
cinst nodejs.install from the command line. Finally you'll need to install the Grunt CLI tools via the Node Package Manger (npm). From the command prompt type
npm install -g grunt-cli which will install the cli tools globally and add it to your system path. For more in depth details on getting started with Grunt, see the docs here http://gruntjs.com/getting-started.
The package.json file will define your build dependencies via the devDependencies property. This is technically not required, but its very useful for quickly adding the grunt modules needed for your build via
npm install. This will also keep you from committing the node_modules directory, which, just like with the nuget packages directory, is generally not something you want to do.
The Gruntfile is essentially your build definition. This is where you'll define and register all the tasks in your build. In this example, I'm defining two tasks, the default task and the publish task. The aptly named
default task is what will run by default when you execute the
grunt command, which is equivalent to
grunt default. The second task
publish is nearly identical to the default task with the addition of the nugetpush configuration.
All in all, this is a very simple set of tasks, which is exactly what I was after. I'm utilizing the grunt-contrib-clean module to wipe the
dist directory prior to the build. I'm then using the grunt-shell module to execute the
mkdir command ensuring the
dist directory exists. Followed finally by the grunt-nuget module which will allow me to both pack and push the nuget package. If you dig into the node_modules directory, you'll find that the grunt-nuget module is utilizing Nuget.exe and has wrapped most of its arguments for pack and push into the options parameter. As an alternative to this module, you could easily just include your own version of the Nuget.exe and use the shell module to accomplish the same thing. I personally think the module makes it a little cleaner since I won't have to carry the Nuget.exe in my repo, but everyone's requirements are different. Also, in the case of Nuget.exe, Mono would be required if running this on a non-windows platform.