I’m working on some side projects at night and enjoying the chance to try out some new tech before inflicting it upon my coworkers. One such tech is using Rake as a build scripting language instead of NAnt/MSBuild for .NET projects. Some smart people that appear in my Google Reader seemed to like it but I hadn’t yet had a chance to review it for myself.
It’s been really pleasing to work with so far. The Ruby syntax is terse and concise without being opaque, and the benefits of no longer “programming in xml” via NAnt seem obvious now — if you need functionality not available via the standard build library, go write it yourself in Ruby. It’s refactoring-friendly and readable. I still use the VS2008 IDE for most of my work, so I am missing syntax highlighting of Ruby files, but other than that it’s fairly easy to work with.
Here’s the bare minimum of what you need to add Rake to a .NET project:
1) I structure my project with an initial folder structure that looks sort of like this ( blatantly ripped from TreeSurgeon ):
/lib <– 3rd party dlls go here
/src <– solution file goes in here, with VS.NET projects underneath
/tools <– build-related files go here
2. To this project structure, in the root with those three folders, I put a file named “Rakefile”. No extension.
3. In my VS.NET solution, I choose to “Add existing file” at the solution root in Solution Explorer and choose Rakefile. This is to make it easy to edit from the IDE and is not required.
4. In the Rakefile file, I add some bootstrap code like this:
require 'rake/clean'
MSBUILD_PATH = 'C:/Windows/Microsoft.NET/Framework/v3.5/msbuild.exe'
BUILD_DIR = File.expand_path('build')
SOURCE_DIR = File.expand_path('src')
CLEAN.include(FileList[File.join(BUILD_DIR, '*')])
task :default => [:clean, :build]
desc "Compiles source files"
task :build do
solution = File.join(SOURCE_DIR, 'RENAME-ME.sln')
sh "#{MSBUILD_PATH} /property:WarningLevel=4;OutDir=#{BUILD_DIR}/ #{solution}"
end
“REPLACE-ME.sln” needs to be replaced with the name of your Solution.
5. Now it’s Ruby time. If you don’t have it installed, your best bet is the Ruby One-Click installer.
6. Finally, open a command shell to your project root and type in “rake”. You should see MSBuild’s output from compiling your project. Success!
I hope this has been helpful. I’m planning to continue this series with one more post on using ActiveRecord migrations for your .NET projects as well.
Filed under: development, ruby | Leave a Comment »
