Vom 17.-19. Oktober bin ich in Leipzig beim .NET Open Spaces 2008. Falls jemand Lust hat sich zwecks Keysigning o.ä, dort mit mir zu treffen, lässt sich das bestimmt arrangieren.
RSpec'ing a Rake TaskLib
I thought it would be a good idea to use RSpec for creating custom Rake TaskLib’s, but I couldn’t find any examples for this. So here’s a pretty simple one from a project I’m working on at the moment. A TaskLib for creating a Mono resource generator task.
This TaskLib should create a file task for a *.resources file, depending on a *.resx file:
require 'rake'
describe ResgenTask do
it 'should create a file task with the *.resource file as target and *.resx as dependency' do
ResgenTask.new 'foo.resources'
Rake::Task.tasks.should satisfy do |tasks|
for task in tasks
if task.name == 'foo.resources' and task.prerequisites.include?('foo.resx')
break true
end
end
false
end
end
end
This doesn’t work because of the missing ResgenTask, so we create it:
require 'rake/tasklib'
class ResgenTask < Rake::TaskLib
def initialize(target)
end
end
The specification now fails:
>spec resgentask_spec.rb --format specdoc
ResgenTask
- should create a file task with the *.resource file as target and *.resx as dependency (FAILED - 1)
1)
'ResgenTask should create a file task with the *.resource file as target and *.resx as dependency' FAILED
expected to satisfy block
./resgentask_spec.rb:11:
Finished in 0.005009 seconds
1 example, 1 failure
But before we implement this specification, let’s refactor it a little bit. This is much more readable:
it 'should create a file task with the *.resource file as target and *.resx as dependency' do
ResgenTask.new 'foo.resources'
Rake::Task.tasks.should have_file_task('foo.resources').depending_on('foo.resx')
end
Because there is no have_file_task, we need to create a custom matcher:
module RakeMatchers
class FileTaskMatcher
def initialize(filename)
@target = filename
@source = nil
end
def depending_on(source)
@source = source
return self
end
def matches?(tasks)
@tasks = tasks
for task in tasks
if (task.name == @target) and (not @sources or task.prerequisites.include?(@source))
return true
end
end
return false
end
def failure_message
"expected file target #{@target} with dependency #{@source} to be in task list [#{@tasks.join(' ')}]"
end
def negative_failure_message
"expected file target #{@target} with dependency #{@source} not to be in task list [#{@tasks.join(' ')}]"
end
end
def have_file_task(filename)
return FileTaskMatcher.new(filename)
end
end
In order to be able to use this matcher, we simply include it:
describe ResgenTask do
include RakeMatchers
it 'should create a file task with the *.resource file as target and *.resx as dependency' do
ResgenTask.new 'foo.resources'
Rake::Task.tasks.should have_file_task('foo.resources').depending_on('foo.resx')
end
end
It still fails, but with a much nicer output:
>spec resgentask_spec.rb --format specdoc
ResgenTask
- should create a file task with the *.resource file as target and *.resx as dependency (FAILED - 1)
1)
'ResgenTask should create a file task with the *.resource file as target and *.resx as dependency' FAILED
expected file target foo.resources with dependency foo.resx to be in task list []
./resgentask_spec.rb:52:
Finished in 0.005116 seconds
1 example, 1 failure
Time to implement this specification:
class ResgenTask < Rake::TaskLib
def initialize(target)
file target => target.gsub('.resources', '.resx')
end
end
The next specification will describe the behavior of the file task:
it 'should create a file task that runs resgen2' do
ResgenTask.new 'foo.resources'
end
But wait a moment! Before letting ResgenTask.new create a new file task, the old tasks should be dropped:
describe ResgenTask do
include RakeMatchers
before :each do
Rake::Task.clear
@tasklib = ResgenTask.new 'foo.resources'
end
Ok - now we can think about, how to test the generated task. What it should do, is run sh(‘resgen2 foo.resources foo.resx’). We don’t want to actually run sh(), so we need to mock it. sh() is defined in the module FileUtils. My first guess was, to use FileUtils.should_receive(:sh), but this didn’t work. To be honest, I have no idea, what’s the right place to mock sh(). But the simplest solution is to create a mocked sh() member method on the task lib:
it 'should create a file task that runs resgen2' do
@tasklib.should_receive(:sh).with('resgen2 foo.resources foo.resx')
Rake::Task['foo.resources'].execute(nil)
end
The specification fails, so let’s implement it:
class ResgenTask < Rake::TaskLib
def initialize(target)
file target => target.gsub('.resources', '.resx') do |task|
sh("resgen2 #{task.name} #{task.prerequisites}")
end
end
end
To add an extra goody, the compiled resource file should be removed on the clean target automatically:
it 'should add the *.resources file to the clean target' do
CLEAN.should include('foo.resources')
end
This can be implemented as:
def initialize(target)
CLEAN.include(target)
file target => target.gsub('.resources', '.resx') do |task|
The final set of specs now looks like this:
describe ResgenTask do
include RakeMatchers
before :each do
Rake::Task.clear
CLEAN.celar
@tasklib = ResgenTask.new 'foo.resources'
end
it 'should create a file task with the *.resource file as target and *.resx as dependency' do
Rake::Task.tasks.should have_file_task('foo.resources').depending_on('foo.resx')
end
it 'should create a file task that runs resgen2' do
@tasklib.should_receive(:sh).with('resgen2 foo.resources foo.resx')
Rake::Task['foo.resources'].execute(nil)
end
it 'should add the *.resources file to the clean target' do
CLEAN.should include('foo.resources')
end
end
…and is implemented by:
class ResgenTask < Rake::TaskLib
def initialize(target)
CLEAN.include(target)
file target => target.gsub('.resources', '.resx') do |task|
sh("resgen2 #{task.name} #{task.prerequisites}")
end
end
end
It’s a pretty simple example, but it should be enough to get started.
Evaluating build tools for altdotmono.org - part two
Three days ago I thought about which build tool to use for the altdotmono.org projects. I came up with the conclusion, that CMake, SCons and Autotools should be on my short list.
First a small correction: I wrote, that Rant doesn’t have any native CSharp support. That’s not true! It has a CSharp generator, that can be used as simple as this:
import 'csharp'
gen CSharp, "example.dll", :sources => sys["**/*.cs"]
Unfortunately it doesn’t work very well. Simply adding a reference makes it fail:
gen CSharp, "example.dll", :sources => sys["**/*.cs"],
:libs => ["System.Web"]
rant: [ERROR] in file `/usr/lib/ruby/1.8/rant/rantlib.rb', line 577:
in prerequisites: no such file or task: `System.Web'
This should be easy to fix in Rant, I just added a bug report. Sadly Rant doesn’t seem to be very popular. There has been no commit within a year, only ten bug reports within 3 years and although Rant is packaged in Debian, not a single package in Lenny uses Rant as a build tool. (But I should also add, that not more than six packages build-depend on Rake compared to more than 100 packages depending on CMake and more then 50 packages depending on SCons.)
Rake is much more popular but misses some features, that Rant provides (e.g. MD5 checksums instead of timestamps for dependency checking). I haven’t really considered Rake yet, because I couldn’t find any project using it for building C#/Mono projects. But after playing around with SCons and CMake I didn’t had the feeling of having found the right tool, so I tried Rake anyway.
I wrote a custom TaskLib supporting gmcs within half an hour, so that my Rakefile for building two libs now looks like this:
require 'rake'
require 'rake/gmcs'
task :default => ['StructureMap.dll', 'StructureMap.AutoMocking.dll']
signingKey = 'structuremap.snk'
#
# The Core StructureMap Assembly
#
sources = FileList['StructureMap/**/*.cs']
references = ['System.Web', 'System.Configuration']
Rake::GmcsTask.new 'StructureMap.dll' => {:sources => sources,
:references => references, :keyfile => signingKey}
#
# StructureMap Rhino.Mocks automocking support
#
sources = FileList['StructureMap.AutoMocking/**/*.cs']
references = ['#../../Rhino.Mocks.dll', '#StructureMap.dll']
Rake::GmcsTask.new 'StructureMap.AutoMocking.dll' => {:sources => sources,
:references => references, :keyfile => signingKey}
This comes pretty close to what I would like to have. The Gmcs task basically reads:
“Build StructureMap from the list of sources, linking to the list of references and sign it with the signingKey.”
GmcsTask will create a Rake file task and will automatically make it pre-depend on the sources, the local references and the key file. This means, that StructureMape.Automocking.dll will only be built. if the timestamp of the sources, of StructureMap.dll, RhinoMocks.dll or the key file change. Because I can’t make a dependency to a reference called “System.Web”, I needed a way to specify which references are local file dependencies and which are not. This is simply done by prefixing the reference with ‘#’, something I saw in the SCons build files of Diva.
I think this is pretty easy and it can probably be made even easier by writing a Visual Studio *.csproj-Parser, which extracts the sources, references and resources from the project files. And by even adding a solution parser, the whole build of multiple projects might be stripped down to something like:
task :default => SolutionParser.new('StructureMap.sln').ProjectTasks
“Easy Peasy” as Jamie Oliver would say. I’m not sure if it is a good idea to completely rely on the Visual Studio solution and project files, but I think I will try to drive the Rake approach for building the Alltdotmono projects a little bit further.
Searching for the holy grail of build systems
For my new OpenSource project ”altdotnet.org”, which is about making OSS from the Microsoft .NET world available to Linux/Mono, I have to decide now, which build system to use. There’s a bunch of build tools to be considered: NAnt, Autotools, CMake, SCons, Waf, Rant, Rake, Waf.
While NAnt is more or less the only tool, which may already be used by the original author in the Microsoft .NET world, it’s not simply portable to Linux/Mono. Often NAnt is just used to simply run msbuild on the project files or solutions. But the msbuild counterpart on Mono isn’t very actively maintained and doesn’t support VisualStudio 2008 yet. Besides this NAnt is too much XML for my taste.
Autotools seems to be the preferred approach on Linux. A quick scan for *.dll’s in the Debian project showed, that most of these packages use Autotools for building Mono libs and applications. When I asked someone from the Debian Mono packaging project, how to package something using Nant, he also advised me, to go with a plain Makefile and Autotools instead. Monodevelop can create the required Autotools files and takes cares of all the dirty details, so this is not so hard to get going. But I’m not a big Autotools fan. I love it as a user, when ./configure and make just work out of the box, but as a Maintainer I hate Autotools, because I never fully understood, how everything really works inside.
CMake and the Python based SCons offer an alternative to the Autotools. Both don’t really come with any native support for C#/Mono, but can easily be used for this. CMake is used within the KDE bindings to build Mono libs and a quick search for Scons brought up at least two projects (Diva and DCSharp) using it for building Mono applications. So there is plenty of sample code available to getting started with. While CMake uses it’s own macro language and SCons is plain Python, the learning curve for CMake is probably a little bit steeper. But while looking at the above mentioned samples, I could understand most of the CMake and SCons code out of the box, so this probably isn’t much of an issue.
Waf is an SCons spin-off, but it isn’t available on Debian yet and I couldn’t find any projects targeting Mono, that use Waf.
The ruby based build tools Rant and Rake don’t have any native support for building Mono applications as well. While Rant surely is a nice tool, it seems to suffer from a lack of support. Upstream development seems to be stalled. There hasn’t been a single commit in over a year. The Rake development seems to be much more active, so I would probably prefer this. Besides this I’ve already used Rake for small C++ toy projects and of course for Ruby projects, so I’m kinda familiar with it. But there doesn’t seem to be any projects (besides IronRuby) that decided to use Rake, so I would more or less have to build everything from ground up.
There are also other interesting build tools out there (like e.g. the Boo Build System, formerly known as “Boobs”), which I simply haven’t considered, just because of their low popularity. As a Debian maintainer I prefer a “grown up” and well supported build system, which is already included in the Distribution and surely other distribution maintainers do so as well. So to make distribution packaging as easy as possible, I’ll stick with one of the major build tools.
Currently CMake, SCons and Autotools are on the shortlist. If I haven’t missed any cool “Off Broadway” tools, it will be one of these. I’ll probably give CMake and SCons a try and then decide, if one of them works well enough for my needs to avoid Autotools.
StructureMap on Mono - only two failing tests left
After struggling with some tiny incompatibilities in StructureMap and bugs in Mono, StructureMap now passes nearly all of it’s tests. The final two failing tests are caused by a compiler bug, which I hope will be fixed soon (Marek Safar seems to be very fast in fixing compiler bugs - thanks a lot!).




