Rake tricks

June 19, 2007

Recently, I had some plots I was trying to generate. The neat part was that everything broke down into little peices. I looked at using Rake to put everything together. The nice thing about Rake is being able to set up the rules for generating each piece, and letting the computer sort out what needs to be done.

Here are the steps I needed

  1. Generate .yaml files (I was doing a parameter sweep of a particular function)
  2. Run a script that takes the .yaml file and outputs x,y,z triples as a .dat file
  3. Generate a script for gnuplot to plot the .dat files as .png
  4. Run gnuplot with the script

I scoured a bunch of online resources about Rake. The rule and file list tricks seem to not be covered as much (or I’m not very bright) and they took me a long time to figure out. On the off chance that someone else isn’t clear about them, here is the Rakefile I used (with the calculation parts removed.)

SRC = FileList['*.yaml']
DAT = SRC.ext('dat')
GP = SRC.ext('gp')
PNG = SRC.ext('png')

task :default => [:png]

task :png => [:dat, :gp]

task :dat => DAT
task :gp => GP
task :png => PNG

rule '.png' => ['.gp', '.dat'] do |f|
  puts "png test #{f.name} #{f.source}"
  sh "touch #{f.name}"

rule '.dat' => ['.yaml'] do |f|
  puts "dat test #{f.name} #{f.source}"
  sh "touch #{f.name}"

rule '.gp' => ['.yaml'] do |f|
  puts "gp test #{f.name} #{f.source}"
  sh "touch #{f.name}"

So the SRC variable becomes a list of all the .yaml files. Changing the extension to png, and making a task of :png => PNG, makes a set of file tasks for the (non-existant) .png files. The rules then give the details of how to generate the .png from the required files.

The thing that was most interesting about this was the ability to define the rules to generate files, without having to specify all the steps for each file, which would be pages and pages (I ended up with over 800 .yaml files). All I had to do was generate the .yaml files, and then run rake.


Rake Docs

Rake Tutorial from Jim Weirich (creator of Rake)Update: Fixed link

Rake Documentation

Automating tasks with Rake (registration required)