Rake is a great tool for creating build scripts even if you primarily develop on Windows and normally don’t use Ruby. In my last article I showed you how easy it is to set it up and get started. This time I am going to show you some more examples of how it can be used.
One thing I really like about Rake build scripts is that you can divide the work into many small tasks that can be developed and tested individually. When you got them all working it is just a matter of chaining them together using the Rake dependency mechanism and you’re done. This speeds up the development of build scripts quite a lot in my opinion.
So how do we setup dependencies between tasks in Rake? Well, it’s very easy. The Ruby language makes the syntax pretty sleek as well:
task :deploy do puts "Deploying..." end task :default => :deploy do puts "Done!" end
In this example I have two tasks called deploy and default. The default task is dependent on deploy, which means that Rake will run the deploy task first and if it is successful it will continue with the default task. Pretty simple really.
You can also create empty tasks that simply depend on others. If you have more than one dependency just surround the tasks in brackets:
task :release => [:newversion, :deploy, :package]
By default Rake will run the default task. If you want Rake to run the release task instead you can do that:
Internal and public tasks
Once I started filling my build script with different tasks I ran into the situation where I wanted some tasks to be considered “internal”. I wanted to run them on my own for testing purposes, but they were not meant to be run by others. Instead I wanted to expose some sort of public API of tasks to be run from the command line to guide the user of what was possible. Rake has a way of handling this by decorating tasks with the special keyword “desc”. If you put a “desc” statement before the task it becomes the public documentation for that task, which to me is a great way of exposing it as part of a public API. This is how you do it:
desc "Perform a deploy and package a release version" task :release => [:newversion, :deploy, :package]
To view a list of documented tasks you simply run Rake with the -T parameter. Here is an example of how this looks in one of my projects:
This gives a nice list of all the commands available to a user that is new to my project. I like this a lot.
If you want a little bit more involved example you can take a look at the rakefile that I use for one of my projects. You can find it on GitHub.
Next time I will show you how to use Rake to build .NET projects. Stay tuned!