[boilerplate bypath=”rake”]

Here's the Rakefile we've been working on for the last few episodes. It finds Markdown source files in a sources subdirectory of a project, and produces a parallel hierarchy of HTML files in an outputs subdirectory.

Now that we are recreating the input file hierarchy in an outputs directory, we need to ensure that the destination directory exists before generating any HTML files. An easy way to do this in Rake is to use a directory task. This is like a file task, except for a directory. But unlike a file task, we don't have to supply any code for how to make the directory appear if it doesn't already exist. Simply by specifying the task, we are giving Rake implicit instructions to create the directory if it is needed.

We add this directory to the list of dependencies for the .html rule.

Now when we run rake, we can see that it creates the directory before beginning to generate the HTML files. Unfortunately, it runs into a problem as it tries to build the appendix.html file. Since this file is in a subdirectory of the sources directory, we want the HTML output file to be in a corresponding subdirectory of the outputs directory. But this subdirectory doesn't yet exist.

To ensure this or any other intermediate directory exists before producing an HTML file, we could execute a mkdir -p shell command, using #pathmap to pass just the directory portion of the target filename.

But Rake gives us a shortcut for this. Instead of running a shell command, we can use a mkdir_p method right in the task:

Now when we run rake, it ensures the target directory exists before each markdown-to-HTML transformation.

Often when writing build scripts it's convenient to have an easy way to quickly blow away all of the generated files. Let's add a task to handle this. Once again, instead of running a shell, we'll use a Rake helper method called rm_rf. This mirrors the shell rm -rf command, which recursively deletes files and directories without any warnings or confirmation.

Rake has a long list of these file operation helper methods, all of them named after their UNIX shell equivalents. They are handy for several reasons. For one thing, since they are native Ruby methods we can pass files and file lists to them directly without any kind of string interpolation.

They are also sensitive to the Rake “quiet” flag. We can run a Rake command with the -q flag, and it will do any work needed, but this time without logging to STDOUT.

Almost all of these helpers are inherited straight from the Ruby FileUtils standard library. So if you want to see a list of all that's available, just check out the FileUtils documentation.

That's all for today. Happy hacking!

(This post is also available in Japanese from Nilquebe Blog.)

[boilerplate bypath=”rake-end”]

Published by Avdi Grimm

2 Comments

  1. Thanks for the topic! I sent some money to the fund. I am glad people keep promoting it. I wish I was half the decent man Jim was.

    Reply
  2. The Jim Weirich https links don’t work. Seems they need to be changed to http. Parts 1-4 of your rake series are similarly effected, and I assume any other pages referencing that site are as well.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *