Using IronRuby and Rake for .NET Builds

January 11th, 2011 by Chris Coneybeer Leave a reply »

I have been working on getting Hudson CI slave server setup at Integrum for all of our .NET builds and during the process I came across a problem with the MSBuild plugin for Hudson that caused me just to go grab NAnt and start creating the XML build files that we needed. While I was working on the first files I was thinking of the tools that the Rails developers at Integrum used for their builds and heard the discussions about Rake tasks. So after doing a little bit of investigation I found that plenty of others had done it so I started down the path of using IronRuby and Rake for our builds. The goal of this post is to get you introduced and setup with running your first simple build using IronRuby, Rake and Albacore.

Install IronRuby

First off you are going to need to get IronRuby installed. IronRuby is a version of the Ruby language that targets the .NET framework and is built using the Dynamic Language Runtime (DLR). I have spent very little time in Ruby and figured all this would be a great way to get started.  Head on over to the project on codeplex and download the install package, I used the latest version (v.1.1.1).

After the install is completed you will need to reboot your system for the path changes to take effect or until then you can open a command prompt and enter the following to update the path:

SET PATH=%PATH%;"C:\Program Files (x86)\IronRuby 1.1\bin\"

Now inside the same command prompt window go ahead and enter “ir” and if everything has been successful you will be prensented with the IronRuby interpreter. Go ahead and enter the following to give it a go:

puts "Hello world"

After hitting enter you should be presented with the output. You can type “exit” at anytime to leave the interpreter.

Installing Rake

Now that we have IronRuby installed we are ready to get Rake installed. Rake (Ruby Make) was created to provide an alternative for building Ruby applications and aimed to provide the same capabilities for builds as make does. One of the primary attractions to it is the fact that it does not use XML files for defining your builds. You will be instead creating Rake files (Rakefile.rb) that define the builds via code.

Rake is installed using the gem installs which is done by using the igem command for IronRuby. To install the Rake gem run the following command:

igem install rake

This will take care of downloading and installing the library for you. Once the install has completed you can test the install by entering “rake” at the command line. You should be presented with an error “rake aborted” because no rake file was found.

Now lets build a simple hello world example using rake. To do this we are going to be creating a file named rakefile.rb so you may want to switch over to a temp directory. Create the rakefile.rb file and enter the following:

task :default => [:hello_world]

desc "Just says hello to the world"
task :hello_world do
	puts "Hello world from Rake"
end

After you have saved the file as “rakefile.rb” you can process it using Rake by just entering rake in the command line while still in the directory that you created the file in. You should be presented with the expected output “Hello world from Rake”. The first line in this file defines the default task to run if none is specified and then starting on line 3 we have the task of just outputting text. This simple rake file only performs one task and you can simple include multiple tasks in a single file. Change the rakefile.rb to match the following and then run rake:

task :default => [:hello_world]

desc "Just says hello to the world"
task :hello_world do
	puts "Hello world from Rake"
end

desc "Just hello to you"
task :hello_you do
	puts "Hello you"
end

desc "Hello to you and the world"
task :hello_to_all =>
	[:hello_world,:hello_you] do
	puts "Hello to everyone"
end

Since by running just rake you are only running the default task specified all you will get is the same “Hello world from rake” output. But if you enter the following:

rake hello_to_all

It will run that task which in turn executes the other tasks specified and you will get all 3 lines written out.

Albacore for .NET Rake Tasks

Rake is used to run the tasks that we need but where do these tasks come from? This is where the Albacore library comes in. The tasks that are provided with the library are to access and use the common tools for building and running tasks for .NET projects. Instead of you having to learn how to build your own tasks the library provides many. To install Albacore we will use igem just like we did for rake:

igem install albacore

Now that the library is installed we can create a simple console project in a solution and then use rake to run a build on the solution. I have attached the project to this post so we do not have to go through the standard Hello World app building. There is a project folder that contains the RakeFile.rb and a src folder that contains the solution to be built. Here is the contents of the RakeFile.rb used to build the solution.

require 'rake'
require 'albacore'

task :default => [:full]
task :full => [:standard_build]

msbuild :standard_build do |msb|
  msb.properties :configuration => :Release
  msb.targets :Build
  msb.solution = "src/BuildHelloWorld.sln"
end

This file uses the albacore task to run MSBuild against our solution. The task requires that you specify a couple pieces of configuration information like the configuration to be built, the target (such as build, clean etc) and then the location of the Solution file to be used. There are many other configuration options available and you can get more info over at the wiki pages on github. The wiki provides information on the tasks that are available such as NUnit, SQLCmd and PLink.

So far I have found using this method very easy to manage and also very easy to read. Working with the tasks instead of XML just takes pain out of building upon the files and makes them easier to follow. I will be doing some more posts on using Hudson and going further with builds using Albacore tasks.

Download Project

Be Sociable, Share!
Advertisement

Leave a Reply