rake: part 1
rake db:migrate. rake db:reset. rake -T. rake is one of those Ruby magics that are easy to take for granted.
Rake is a build system that was created by the late Jim Weirich. He unvelied rake during a presentation called “Why aren’t you using Ruby?” at RubyConf Uruguay in 2013. It’s now the most widely downloaded Ruby gem.
Nothing like a real life example to make rake make sense. Imagine you’re hungry. Easy enough.
You could write out these tasks as a series with dependencies. The dependencies make sense. You want to make mac and cheese but before doing so you need to boil water. But before boiling water it wouldn’t make sense if you didn’t have pasta and cheese so you have to buy pasta and cheese and that requires going to the store.
This illustrates how Rake is a dependency based computational model. Big phrase, but understandable. It’s different from a the typical imperative style (using methods).
Then you need to write rake. Jim called this ‘micro-rake’ in his demo because it is essentially a small version of what rake does.
For each argument in the argument list, you find the task. You have a global hash called TASK. Then you define a task method that takes a name, dependencies, and block. And then in that you create a Task object and pass the name, deps, and block in that and you save it, by taking the tasks hash and saving it by its name.
The constructor takes the @name, @deps, and the @action block and stores them off.
The execute method takes the action block and calls it. And the invoke method prevents the task from executing twice. Finally you require the tasks file. 28 lines of code and you can make mac and cheese!
If this looks simple, it’s because it is. According to Mr. Weirich, it took him about half an hour to do this.
What is cool about this is that it shows how rake is built in Ruby, as compared to make which is an external DSL that uses a custom syntax.
But this isn’t really make. ~20 minutes later he subclassed task as a file task, added a check to see if it was out of date as compared to its to dependencies and he had a file task. So in under an hour’s worth of work he had the version first of rake working!
Anatomy of a rake task
Being written in Ruby, a rakefile scaffold is simple. The first line provides space to write the description, which is what will be returned when you run the rake -T command after the name of the task. If you don’t include a description, the task won’t show up on listings.
The name of the task has to start with a colon. Before the task is executed, it executes all of the prerequisite tasks.
Part 2: What you can do with Rake
Next up is to look at some of the capabilities of rake. In general, rake has two kinds of task: regular tasks — similar to tasks in ant and file tasks — similar to tasks in make. You can build your own rake tasks, namespace tasks by bundling them together, and even schedule them.
Rake is a build language, similar in purpose to make and ant. Like make and ant it's a Domain Specific Language, unlike…martinfowler.com