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.

Tagged with:  
  • Anonymous

    Awesome and timely post. We’ve been talking at work about using Albacore. Looking forward to digging in.

    • Anonymous

      Be careful! You might enjoy Ruby so much that your next post will be titled “Why I’m leaving .NET” ;)

      • Anonymous

        Yeah… not any time soon ;)

  • http://twitter.com/PhatBoyG Chris Patterson

    Great work, Albacore is indeed, full of awesome.

    • Anonymous

      I looked at the Mass Transit rakefile while I was trying to learn some of this. 

  • http://sharplearningcurve.com/blog Alex Robson

    Great post man! I’ve always heard about Albacore but never made time to dig into it. Definitely looking forward to trying it out some time thanks to your post :D Looking forward to the follow-ups.

    • Anonymous

      Thanks! Anything beats wading through miles of XML. 

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1000

  • http://www.dougrohm.com/blog drohm

    Would love to see the :dependencies task :)

    • Anonymous

      Next post. I should have it up in a few days.