Rake dependencies

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
desc "Make coffee"
task :coffee => :get_mug do
  puts "Making a coffee"
end

task :get_mug do
  puts "Getting a mug"
end

rake get_mug
> Getting a mug

rake coffee
> Getting a mug
> 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