<< back to blog

Build, test, refactor - a simple .NET Rake script

Nov 04, 2008, .net ~ rake ~ ruby

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:


require 'rake'
require 'build_helper'

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

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

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

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

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.


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

def current_directory

# 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...

blog comments powered by Disqus