Tidbits on software development, technology, and other geeky stuff

NuGet Releases with Rake

Every time I want to release a new version of Cron Expression Descriptor to NuGet, there is a bit of work to be done.  I had been planning on creating a MsBuild file to handle all the release tasks including **versioning, building, packaging, pushing and tagging **the Git repo but honestly, MsBuild is a pain to deal with because of its overly verbose XML syntax and need to install MsBuild Contrib to do anything beyond basic tasks.  Also, the installation process for MsBuild Contrib has always left a bad taste in my mouth: download zip, copy/paste to Program Files… directory, add a bunch of XML to your .msbuild file to reference.

As I’ve been working with Rails more, I’ve become familiar with Rake, the MsBuild equivalent for the Ruby world.  Also, at Austin Code Camp a few weeks ago I saw Chris Holt use Rake for some .NET builds “because it is easy”.  Searching around online, I found Albacore, which provides a “suite of rake tasks that is intended to make building .NET systems easier”.  I decided to give Rake a shot for releasing Cron Expression Descriptor and it is working great.

The first thing I did was run ‘**gem install albacore’ **to install Albacore.  Then I downloaded the NuGet command line utility, placed it in a folder somewhere and added this folder to the path, so that nuget.exe could be executed from anywhere.  Also, I placed my .nuspec file (previously generated by NuGet) in the project directory.  Once I added the following Rakefile in the same directory as my Cron Expression Descriptor project I can simply run ‘rake release[“1.7.0″]’, from the directory where my Rakefile resides,** **to release a new version of Cron Expression Descriptor to NuGet.

This Rakefile does all the dirty work including updating the AssemblyInfo with the version number (passed in as parameter), building in Release mode, **packaging **the NuGet package, **pushing **NuGet repository, and tagging my Git repository with the current release number.  Releases are a breeze now!

#Rakefile
#Example usage: rake release["1.7.0"]

require 'albacore'

project_name = "CronExpressionDescriptor"

task :version, [:version_number] do |t, args|
  desc "Version the assembly"
    assemblyinfo :version do |asm|
    asm.version = args.version_number
    asm.file_version = args.version_number
    asm.input_file = './Properties/AssemblyInfo.cs'
    asm.output_file = './Properties/AssemblyInfo.cs'
   end
end

task :build do
  desc 'Build the solution in the Release configuration'
  msbuild :build do |msb|
    msb.solution = project_name + '.csproj'
    msb.targets = [ :Clean, :Build ]
    msb.properties = { :Configuration => 'Release' }
   end
end

task :package => [:build] do
  desc "create the nuget package"
  sh "nuget.exe pack #{project_name}.csproj -Prop Configuration=Release -OutputDirectory bin\\Release -Verbosity normal"
end

task :push, [:version_number] do |t, args|
  sh "nuget.exe push bin\\Release\\#{project_name}.#{args.version_number}.nupkg"
end

task :tag, [:version_number] do |t, args|
  sh "git tag v#{args.version_number}"
  sh "git push --tags"
end

task :release, [:version_number] => [:version, :package, :push, :tag] do |t, args|
  desc "v#{args.version_number} Released!"
end

Discuss on Twitter