Overview
If you started with Ruby and you’re still around a month later, you
should spend a couple of hours and learn the basics of Rake. It’s a
venerable item in the Ruby toolkit, and if you haven’t used tools like
make
before you’re in for a treat.
Background
Rake describes itself as a A make-like build utility for Ruby, and you might think it’s limited to building “code,” but that’s not so. That’s just the beginning. So long as you keep your mind open, and remember that it’s Ruby under the covers, you can keep using it to “build” “things” that have nothing to do with code.
Getting Started
To get going, watch these two videos from Jim Weirich, the inventor of Rake:
These talks are well done, and a great investment of your time.
Basically Good Things to Know
Once you’ve watched the videos, if you find yourself on a project using Rake, you’ll need to know about the tasks that are available to you and where to find the source code behind them.
What tasks exist?
rake -T
shows all described rake tasksrake -T whatever
shows all described rake tasks beginning with whateverrake -P
shows all rake tasks and their dependenciesrake -W whatever
shows where the tasks beginning with whatever are defined.
As a user of Rake, I am sometimes surprised by the notion that if a
task doesn’t have a description, then it doesn’t appear in the output
of rake -T
or rake -W
. One of these (-T
) is intentional, while
the other (-W
) is a bug, and will be fixed sooner or later.
Where’s the source?
The fastest way to get up to speed with Rake is to read Rake code.
If rake -W
isn’t helping you find source, or you don’t have a
specific command to look for, you can generally find Rake tasks in the
following locations:
- The Rakefile at the root of your project
- Files ending in “.rake” within the
rakelib/
directory underneath the root of your project - Files ending in “.rake” within the
lib/tasks/
directory (if you’re using Rails) - Embedded within gems that you’re using. These are usually the most obscure to track down only because they aren’t explicitly defined nearby.
What is going on here?
Once you do manage to track down the definition of a rake task or two, the first time you read through Rakefiles there’s an element of mystery.
It can be a rather terse DSL, which you’ll probably come to appreciate over time, but initially, leaves you thinking “What just happened?”
Be sure to watch the videos above (with an eye for FileList
s and the
file
and directory
tasks) and remember to hang in there.
FileLists
One specific piece of Rake mystery has to do with two things named
CLEAN
and CLOBBER
. These are references to FileLists in Rakefiles.
FileLists allows you to describe a set of files, and new instances of
it are commonly created with []
rather than an explicit .new
:
You can hold off on learning about FileLists until you want to clean stuff up or start writing tasks that operate on sets of files.
At any rate, don’t slug through this stuff: the path to happiness here is to watch the videos and then follow this link through to the documentation.
Other Things to Note
Many people have written oodles about Rake.
Rather than go down that path again, I’ll stop and say that sooner or later you you should check out:
How Rake handles accessing environment variables, specifically how it simulates
ENV['NAME']
if you write run thisrake blah NAME=value
How Rake handles global Rakefiles (tasks that live outside of any given project) via
rake -g
In Closing
Rake has been around for a long time, and there’s a bunch to learn. However, you can get started initially with just a few hours and this investment will pay itself off within a week or two, and then only keep paying dividends.
Because it’s so well known, a number of tools claim that they are easier to understand because they follow a ‘rake like syntax.’ This means taking the time to understand Rake now has other, non-rake benefits as well.