building .net projects with rake

ruby logo

I’ve been wearing my ruby slippers to work and I need to share the joy that is rake for .net projects. I’ve created/modified some rake tasks to ease automation and development of .net projects. Basically I wrap nunit and msbuild in rake and then use the wonder that is rake to create a clean and simple build script. Combine this with cruisecontrol.rb and you have a very nice automated build/testing system in place. Add watir/watin support to taste for that added punch.

You may be wondering why anyone would want to use rake when there are already multiple native build systems (nant and msbuild off the top of my head).

    <property name="myprj.basedir" value="c:\" />
    <property name="filename" value="${path::combine(myprj.basedir,'version.txt')}" />
    <if test="${not file::exists(filename) or file::get-length(filename) = 0}">
        <echo message="The version file ${filename} doesn't exist or is empty!" />

prison picture Why would I want to use xml to write logical expressions? Seriously who wakes up in the morning and looks forward to that? I sure don’t. I’d rather use a full fledged programming language, if not for power than for clarity. Anything other than a real language, be it ruby, python or powershell is a pointless waste of time in my opinion. Why spend hours of your life fighting to express yourself within an xml-based cage. Check out this msbuild snippet:

        <MDForm Include="TestForm1.cs">
                <Name>Ryan Flynn</Name>
        <MDFormOther Include="..\..\**\MySubProject\*.cs">
                <Name>Mark A. Guzman</Name>

That looks like a lot of junk to me.

How about this as a reasonable alternative:

    desc "compile cs files" do |csc|
      csc.files = FileList[ "**/MySubProject/*.cs" ]

Simple, concise and dare I say fun.

Now let’s take that last snippet a bit further, as msbuild will do all we want based on the solution (sln) file most of the time.

    desc "Run NUnit" do |nunit|
      nunit.files = FileList['**/*Tests.dll'].exclude(/obj\//)

    namespace :build do
      desc "Compile Debug" do |msbld| = FileList['**/*.sln'].exclude(/obj\//)
        msbld.config = "Debug"

      desc "Compile Release" do |msbld| = FileList['**/*.sln'].exclude(/obj\//)
        msbld.config = "Release"

I think you can guess what’s going on here. Now I can do rake build:release and have it run msbuild against the sln file. While that’s spiffy, it gets much nicer when you add in automated testing using nunit and watir combined with rspec. Plug this all into cc.rb or even and you get a very nice simple build system with automated testing. All of that with clean and concise syntax.

I’ve zipped up the current versions of the rake tasks, you can get them here. Hopefully we can build a nice kit for people wanting to find ruby a niche in their environment.