Automating Tasks with Rake
Even when your software is written, tested, and packaged, you e still not done. Youve got to start working on the next version, and the next… Every release you do, in some cases every change you make to your code, will send you running through a maze of repetitive tasks that have nothing to do with programming.
Fortunately, theres a way to automate these tasks, and the best part is that you can do it by writing more Ruby code. The answer is Rake.
Rake is a build language, Rubys answer to Unix make and Javas Ant. It lets you define tasks: named code bocks that carry out specific actions, like building a gem or running a set of unit tests. Invoke Rake, and your predefined tasks will happily do the work you once did: compiling C extensions, splicing files together, running unit tests, or packaging a new release of your software. If you can define it, Rake can run it.
Rake is available as the rake gem; if youve installed Rails, you already have it. Unlike most gems, it doesn just install libraries: it installs a command-line program called rake, which contains the logic for actually performing Rake tasks. For ease of use, you may need to add to your PATH environment variable the directory containing the rake script: something like /usr/lib/ruby/gems/1.8/gems/rake-0.6.2/bin/. That way you can just run rake from the command line.
A Rakefile is just a Ruby source file that has access to some special methods: task, file, directory, and a few others. Calling one of these methods defines a task, which can be run by the command-line rake program, or called as a dependency by other tasks.
The most commonly used method is the generic one: task. This method takes the name of the task to define, and a code block that implements the task. Heres a simple Rakefile that defines two tasks, cross_bridge and build_bridge, one of which depends on the other. It designates cross_bridge as the default task by defining a third task called default which does nothing except depend on cross_bridge.
# Rakefile desc "Cross the bridge." task :cross_bridge => [:build_bridge] do puts "Im crossing the bridge." end desc "Build the bridge" task :build_bridge do puts Bridge construction is complete. end task :default => [:cross_bridge]
Call this file Rakefile, and itll be automatically picked up by the rake command when you run the command in its directory. Here are some sample runs:
$ rake Bridge construction is complete. Im crossing the bridge. $ rake build_bridge Bridge construction is complete.
Note all the stuff I didn have to do. I didn have to write code to process command-line options and run the appropriate tasks: the rake command does that. The rake command also takes care of loading the Rake libraries, so I didn have to recite require statements at the beginning of my Rakefile. I certainly didn have to learn a whole new programming language or a new file format: just one new Ruby method and its arguments.
Adapt the recipes in this chapter to your projects Rakefile, and a lot of the auxilliary work that surrounds a software project will simply disappear. You won have to remember to run unit tests or generate documentation after every change, because it will happen as a side effect of things you do anyway. If your unit tests fail, so will your attempt to release your project, and you won be embarrassed by bugs.
Whenever you ask yourself: "What was the command to …?", just invoke rake with the -T option. It will print a list of available tasks and a description of each:
$ rake -T (in /home/leonardr/my_project/) rake build_bridge # Build the bridge. rake cross_bridge # Cross the bridge.
Nothing says you can only use Rake in Ruby projects. Most Rake tasks simply run external programs and move disk files around: the same things tasks do in other build languages. You can use Rake as a replacement for make, build static web sites with it, or automate any other repetitive action made up of smaller, interlocking actions.
Here are some more resources for automating tasks with Ruby:
- The site http://docs.rubyrake.org/ provides a tutorial, a user guide, and examples for Rake.
- The generated RDoc for Rake has a good overview of the special methods available to Rakefiles (http://rake.rubyforge.org/files/doc/rakefile_rdoc.html)
Категории