Nov 04, 2008 / .net ~ rake ~ ruby
Build, test, refactor - a simple .NET Rake script

I just blogged about setting up Rake with .NET. Based on a few resources online (and some Ruby experience), I've cobbled together a quick Rakefile.rb that can build, test and analyze a solution.

  • Build calls MSBuild on the Visual Studio solution file.
  • Test calls NUnit on all DLLs with Test in the name
  • Analysis calls NDepend which will then give me a list of top 10 refactorings.

What's nice is being able to just type rake and all these steps are performed. I know you can do this with Nant, but you have to admit the Rakefile.rb is very concise! If I want to run tasks individually, I can also type:

rake compile
rake test
rake analyze

Here's the Rakefile.rb script:

#Rakefile.rb

require 'rake'
require 'build_helper'

task :default => [:compile, :test, :analyze]

desc "MSBuild solution"
task :compile do  
  msbuild :squilbo 
end

desc "NUnit tests"
task :test do
  files = FileList['**/*.Test.*.dll'].exclude(/obj\//)
  files.each do |file|
    sh "#{NUNIT} #{file}"
  end
end

desc "NDepend analysis"
task :analyze do    
  sh "#{NDEPEND} \"#{current_directory}\\NDepend.Core.Xml\""  
end

This file also includes another file - build_helper.rb, which contains constants and useful functions. This could have been included inline, but I thought it might be neater to keep it separate.

#build_helper.rb

def msbuild(solution)    
  sh "#{MSBUILD} #{solution}.sln"
end

def current_directory
    Dir.pwd.gsub(/\//,'\\')
end

#
# Put your own definitions here. You could also load from a YAML or XML file
#

NUNIT = 'C:\\Program Files\\NUnit 2.4.7\\bin\\nunit-console-x86.exe'
NDEPEND = 'C:\\Program Files\\NDepend\\NDepend.Console.exe'
MSBUILD = 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe'
SOLUTION_FOLDER = current_directory

This is all just the first 30 minutes of playing with Rake, so I'm sure there are many better ways of doing things. But, thought I'd share the first experiences.

I'm looking forward to playing with NDepend more (they were kind enough to give me a license), my main goal is to get it tightly integrated into my development process so I can actually get some real, ongoing benefit from it.


You may also like...