I attended Developer Day in Durham last weekend, and had a blast. The event was very much Ruby-centric, which is a little out of my comfort zone. However, there were plenty of things that I’m bringing back to my day job on .NET and Java projects.
The first two things I resolved myself to do: 1)write less XML, and 2) try to incorporate scripting languages (starting with Ruby) into my routine tasks. This led me to Rake. Rake helps me accomplish both goals at one time. I get to stop writing XML for my build scripts, and use Ruby to build my projects.
There are a lot of blog posts on Rake and even some on Rake with .NET. However, I didn’t find one that got me running build scripts in Rake from scratch. I’m focused on the simplest case for .NET. If you want more general Rake information, go to Martin Fowler’s article. If you want a more complete (and very entertaining) tutorial, check out the Rails Envy site.
So here it goes…
1. Get Ruby
I’m on a windows machine, so I used the One Click Installer. I ran the installer, re-booted my machine, and was good to go with Ruby (Rake is delivered as part of the Ruby install).
2. Find a Project that Needs Building
I set up a very simple solution that has two class libraries (DoStuffInDotNet and TestStuffInDotNet). One has a single class with limited functionality. The other has a single NUnit test. I have also included a tools folder, which contains the NUnit test framework.
3. Create a Rake File
I created a file called rakefile.rb. I’ll break down each part of the file below.
The first line sets up the default task. In this case, it is calling the build task. So, from this folder, “build” executes by simply typing in “rake”.
task :default => :build
The next line shows that “build” is a composite task that is comprised of the “clean”, “compile”, and “test” tasks.
task :build => [:clean, :compile, :test]
The “clean” task is just calling Ruby’s FileUtils class to remove the contents of the “build” folder.
task :clean do
FileUtils.rm_rf("build")
end
The “compile” task calls MSBuild to execute on the solution under the AutomatedDebug configuration. AutomatedDebug is a setting I created in Visual Studio’s Configuration Manager. The only difference between this and the default Debug configuration is that my output path is set to “..\..\build\Debug\”. This will put the output of the rake build into the “DoStuffInDotNet\build” folder.
task :compile do
params = '/t:Rebuild /nologo /v:m /p:Configuration=AutomatedDebug src\DoStuffInDotNet.sln'
msbuid = 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe'
sh "#{msbuid} #{params}"
end
The final task is “test”. Rake uses the current execution directory as its working directory, so I am using FileUtils to change the working directory to the “build\Debug” folder. Then I execute NUnit against the test dll.
task :test do
FileUtils.cd 'build\\Debug'
exec "..\\..\\tools\\nunit\\nunit-console.exe TestStuffInDotNet.dll"
end
From the cmd window, you can just run the command “rake” from the directory that contains rakefile.rb.
In effort of completeness, below is the full code in rakefile.rb. Like I’ve stated early, this is a stupidly simple example. However, I hope that it can help you get started if you are looking to start from ground zero.
task :default => :build
task :build => [:clean, :compile, :test]
task :clean do
FileUtils.rm_rf("build")
end
task :compile do
params = '/t:Rebuild /nologo /v:m /p:Configuration=AutomatedDebug src\DoStuffInDotNet.sln'
msbuid = 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe'
sh "#{msbuid} #{params}"
end
task :test do
FileUtils.cd 'build\\Debug'
exec "..\\..\\tools\\nunit\\nunit-console.exe TestStuffInDotNet.dll"
end