Rake dependencies
Posted on 30 Jun 2008 at 10:00AM. Filed under Development, Ruby
Rake tasks are great for all sorts of small tasks. They give you a bit of structure to what would normally be a shell or ruby script. However, the thing I like about rake tasks are dependencies.
A simple example
You see, you can make one task rely on another. Take this.
1 desc "Make coffee" 2 task :coffee => :get_mug do 3 puts "Making a coffee" 4 end 5 6 task :get_mug do 7 puts "Getting a mug" 8 end 9 10 rake get_mug 11 > Getting a mug 12 13 rake coffee 14 > Getting a mug 15 > Making a coffee
Easy to understand isn’t it? coffee depends on get_mug, so get_mug will be
called before the main coffee task every time rake coffee is run.
A better example
You can also redefine task dependencies without altering the original task.
For example, on this blog I have a task which creates index pages for
the category pages. I want this to run everytime the build task is run. I could
modify the original build task, but a much nicer and cleaner way is to
redefine the task and add the dependant task like so.
1 task :build => 'blog:categories:create_indexes'
This will keep any existing dependencies that the build task originally had and also add my new one. Ain’t it pretty?
References
Thanks to the guys at Rails Envy for their tutorial