.Net Build Automation using Ruby Make (Rake)
This is a step by step (albeit simple) guide to using RAKE for building your .Net solutions. Before we get into how to use Rake, lets start with answering a few questions about why in the world you would want to use RAKE to build your .Net solutions.
Why use Rake?
Installing Rake
Now that you are somewhat intrigued, let me get you started with Rake. Here is a step by step guide for getting Rake installed:
- Download Ruby
- After installing Ruby, select "Start Command Prompt with Ruby" from the Start Menu.
-
Execute the following commands in the command prompt (these commands will install Rake and supporting plugins for .Net compilations):
- gem install rake
- gem install albacore
- gem install win32-file
After Rake is installed, you can start using it. All you need to do is:
- Create a text file called "Rakefile" (with no extension) inside of your working directory.
- You can then start up Command Prompt with Ruby, navigate to the working directory and run the following command: rake
- Ruby Make will look for a file called Rakefile, and execute its contents.
Creating a Rakefile
Now that you have Rake installed, we can start creating a Rakefile.
Simple Rake
Here is really a simple Rakefile that builds a .Net solution called SampleApp.sln. This Rakefile takes an MSBuild path, a working directory, a solution name and calls "sh" to execute the build. When executing the rake command from command line, the task named "default" will automatically be executed.
# location of the MSBuild on this computer
@msBuildPath = "#{ENV['SystemRoot']}\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild.exe"
#this is how you get the working directory
@workingDirectory = pwd
task :default do
#shell out to msbuild giving it the solution path
@solutionFile = "#{@workingDirectory}\\SampleApp.sln"
sh "\"#{@msBuildPath}\" \"#{@solutionFile}\""
end
Multiple Tasks
This Rakefile executes two separate tasks. One of them builds the project and the other runs unit tests in the solution.
# location of the MSBuild on this computer
@msBuildPath = "#{ENV['SystemRoot']}\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild.exe"
@workingDirectory = pwd #this is how you get the working directory
@msTestPath = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\mstest.exe"
#this is the default tasks that executes the other tasks
task :default => [:build, :runtests];
task :build do
@solutionFile = "#{@workingDirectory}\\SampleApp.sln"
sh "\"#{@msBuildPath}\" \"#{@solutionFile}\""
end
task :runtests do
cd "#{@workingDirectory}\\SampleApp.UnitTests\\bin\\Debug"
sh "\"#{@msTestPath}\" /testcontainer:SampleApp.UnitTests.dll"
end
Composing Tasks
This Rakefile has a task called "integration" that runs all of the default tasks and also runs integration tests. To call the integration task, pass the task name as a command line argument, for example: "rake integration" will execute the integration task as opposed to the default task.
# location of the MSBuild on this computer
@msBuildPath = "#{ENV['SystemRoot']}\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild.exe"
@workingDirectory = pwd #this is how you get the working directory
@msTestPath = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\mstest.exe"
task :default => [:build, :unittests];
task :integration => [:default, :integrationtests];
task :build do
@solutionFile = "#{@workingDirectory}\\SampleApp.sln"
sh "\"#{@msBuildPath}\" \"#{@solutionFile}\""
end
task :unittests do
cd "#{@workingDirectory}\\SampleApp.UnitTests\\bin\\Debug"
sh "\"#{@msTestPath}\" /testcontainer:SampleApp.UnitTests.dll"
end
task :integrationtests do
cd "#{@workingDirectory}\\SampleApp.IntegrationTests\\bin\\Debug"
sh "\"#{@msTestPath}\" /testcontainer:SampleApp.IntegrationTests.dll"
end
Raising Errors
Sometimes you want to fail the build if something doesn't go as planned. In this example, the build will be failed if a file doesn't exist in a particular location.
task :default do
@someFile = "#{@workingDirectory}\\README.txt"
if !File.exists?(@someFile)
raise "#{@someFile} does not exist."
end
end
Using Albacore
Albacore is a suite of Rake Build Tasks for .Net Solutions. Los Techies has a nice little blog post on it. (The script below came from Los Techies, I've added it here for conveniance).
require 'albacore'
desc "Run a complete Albacore build sample"
task :sample => ['albacore:assemblyinfo', 'albacore:msbuild']
desc "Run a sample build using the MSBuildTask"
Rake::MSBuildTask.new(:msbuild) do |msb|
msb.properties :configuration => :Debug
msb.targets [:Clean, :Build]
msb.solution = "lib/spec/support/TestSolution/TestSolution.sln"
end
desc "Run a sample assembly info generator"
Rake::AssemblyInfoTask.new(:assemblyinfo) do |asm|
asm.version = "0.1.2.3"
asm.company_name = "a test company"
asm.product_name = "a product name goes here"
asm.title = "my assembly title"
asm.description = "this is the assembly description"
asm.copyright = "copyright some year, by some legal entity"
asm.custom_attributes :SomeAttribute => "some value goes here", :AnotherAttribute => "with some data"
asm.output_file = "lib/spec/support/AssemblyInfo/AssemblyInfo.cs"
end
Rake can do some pretty neat stuff. Hopefully this blog post helped you get started. Here is the Rake home page.
Written: 9/22/2010