Ruby Rake Tutorial

Advertisement

Advertisement

Introduction

The rake utility and Rakefile are Ruby's version of make and Makefile. Personally, I find rake much more pleasant to work with than make. While it is written in Ruby, you can use it as task runner for a project of any language.

Rake was created originally by the late Jim Weirich who can has a lecture on the topic from RailsCons 2012:


IF you want to learn more about all the possibilities, check out the Rakefile documentation.

Install rake

You can check if rake is installed by running one of the following:

rake --help
ruby -S rake --help

If you need to install rake, see your operating system's available packages, or install it using gem:

gem install rake

If you are interested in building from source, you can find it on on GitHub: https://github.com/ruby/rake

Basic rake commands

Here are a few basic rake commands. See rake --help for more details. By default, rake will look for a Rakefile in your current directory. The next section provides an example Rakefile that defines a few tasks.

# Run default task in Rakefile
rake
# Don't actually execute tasks
rake --dry-run
# Execute a specific job
rake <target_name>

Example Rakefile

Here is an example Rakefile that has some basic procedures for building and installing a Debian package .deb. Just run rake inside the directory with the Rakefile.

This is a modified example from a real build project of mine. I have commented out the actual shell commands so that this file can be copy/pasted and used to run rake commands without making any system changes. I left the example commands there there though to demonstrate how you can make use of shell commands by using the backticks. This demonstrates how you can easily convert a Makefile to a Rakefile by still calling the same underlying shell commands while still getting the power of Ruby and improved readability.

# Rakefile
task default: [:clean, :build, :install]

task :clean do
    puts "Cleaning packages"
    # `rm *.deb`
end

task :build do
    puts "Building mypackage-0.0.0"
    # `dpkg -b ./mypackage-0.0.0 ./mypackage-0.0.0.deb`
    puts 'Try `rake install` now.'
end

task :uninstall do
    puts 'Uninstalling mypackage'
    # `sudo apt-get remove -y mypackage`
end

task :install => [:uninstall] do
    puts 'Installing mypackage-0.0.0'
    # `sudo apt-get install -y ./mypackage-0.0.0.deb`
end

Example usage of the above Rakefile:

rake # runs default task (all the steps)
rake clean
rake build
rake uninstall
rake install

Namespaces

You can create separate namespaces to isolate tasks that may have the same name or just to create logical grouping.

# Rakefile
namespace :db do
  task :backup do
    puts 'Performing database backup...'
  end
end

namespace :files do
  task :backup do
    puts 'Performing files backup...'
  end
end

task backup_all: ['db:backup', 'files:backup']
task default: :backup_all

Then you can invoke it in a few ways, depending on which tasks you want to run:

rake
rake backup_all
rake db:backup
rake files:backup

Conclusion

You should have a basic understanding of how to use rake and create a simple Rakefile. Rake has several more powerful features that aren't covered here and I encourge further reading on the topic.

References

Advertisement

Advertisement