Take Control of Your .NET Builds with Rake and Albacore

If Rake is a gateway drug to Ruby, then Derick Bailey is your dealer. He's created a project named Albacore which makes building your .NET projects stupid easy with Rake. Doing anything in angle brackets for msbuild was painful for me. I write code for a living, so it just makes sense to write code to build my stuff.

Lately I've been doing some work with our builds and TeamCity. A coworker pointed me to Rake and next I discovered Albacore. I just wanted to take a moment to show you how simple it is to set up a build that compiles your code, runs your tests and assembles the output.

require 'albacore'

PRODUCT_NAME = "Autofac.Settings"
BUILD_PATH = File.expand_path("build")
TOOLS_PATH = File.expand_path("tools")
LIB_PATH = File.expand_path("lib")

configuration = ENV['Configuration'] || "Debug"

task :default => :all

task :all => [:clean,:dependencies,:build,:specs,:copy]

task :clean do
	rmtree BUILD_PATH
end

task :dependencies do
	#future post. 😉
end

msbuild :build=>[:dependencies] do |msb|
	msb.properties :configuration => configuration
	msb.targets :Clean, :Build
	msb.verbosity = "minimal"
	msb.solution = "#{PRODUCT_NAME}.sln"
end

mspec :specs => [:build] do |mspec|
	mspec.command = "lib/Machine.Specifications/tools/mspec-clr4.exe"
	mspec.assemblies Dir.glob('specs/**/*Specs.dll')
end

task :copy => [:specs] do
	Dir.glob("src/**/*.csproj") do |proj|
		name=File.basename(proj,".csproj")
		puts "Copying output for #{name}"
		src=File.dirname(proj)
		dest = "#{BUILD_PATH}/#{name}/"
		mkdir_p(dest)
		cp_r("#{src}/bin/#{configuration}/.",dest)
	end
end

:default
So, let's start from the top. Line 10 defines a default task. This is what will get called when you just call rake without any arguments from the command line.

:clean
Line 14 defines a task which just nukes the build output directory. This makes sure we don't accidentally leave artifacts around from a previous build.

:build
Line 22 is my first albacore task. This is the task where I'm compiling my code. Line 23 would be 'Debug' or 'Release' if you're using the default build configurations. The line after is where I tell it to clean the build output and then Build. Point it at a solution file and you're good to go. Easy enough.

:specs
Line 29 is another albacore task to run my Machine.Specifications based tests. Tell it where mspec lives and what assemblies contain your tests. Done.

:copy
Line 34 is a simple file copy task to assemble the build output from src/ and copy them to the build folder. Find all of the project files and go to bin/{config} and get the output files. Move them to a folder with the name of the project.

That's about it. Thanks to Derek Greer for getting me started with Rake. I was able to look at his sample Rakefile and start hacking away. Within a few minutes I had my own rakefile running with albacore tasks. Ruby is pretty straightforward and fun. Playing with Ruby via Rake just makes me want to write more Ruby.

Since I'm a Ruby n00b, I'm sure my Ruby is less than perfect. If you have some suggestions for me to make my code suck less, please leave a comment.

Cross posted from Fresh Brewed Code. If you haven't taken a look over there, please take a moment to see what we've been up to.