Rake (software)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Rake
Developer(s) Jim Weirich
Stable release 0.8.7 / May 15, 2009 (2009-05-15)
Written in Ruby
Operating system Cross-platform
Type Software development tools
License MIT License
Website http://rake.rubyforge.org/

Rake is a software task management tool. It allows you to specify tasks and describe dependencies as well as to group tasks in a namespace.

It is similar to SCons and Make, but it has a number of differences. The tool is written in the Ruby programming language, and the Rakefiles (equivalent of Makefiles in Make) use Ruby syntax. It was originated by Jim Weirich.

Rake uses Ruby's anonymous function blocks to define various tasks, allowing the use of the Ruby syntax. It has a library of common tasks: for example, functions to do common file-manipulation tasks and a library to remove compiled files (the "clean" task). Like Make, Rake can also synthesize tasks based on patterns (for example, automatically building a file compilation task based on filename patterns). Rake is now part of the standard library from Ruby version 1.9 onward.

Contents

Example [edit]

Below is an example of a simple Rake script to build a C HelloWorld program.

  file 'hello.o' => ['hello.c'] do
    sh 'cc -c -o hello.o hello.c'
  end
  file 'hello' => ['hello.o'] do
    sh 'cc -o hello hello.o'
  end

http://rake.rubyforge.org/doc/rakefile_rdoc.html

Rules [edit]

When a file is named as a prerequisite, but it does not have a file task defined for it, Rake will attempt to synthesize a task by looking at a list of rules supplied in the Rakefile.

Suppose we were trying to invoke task "mycode.o", but no tasks are defined for it. But the rakefile has a rule that look like this ...

rule '.o' => ['.c'] do |t|
  sh "cc #{t.source} -c -o #{t.name}"
end

This rule will synthesize any task that ends in ".o". It has a prerequisite a source file with an extension of ".c" must exist. If Rake is able to find a file named "mycode.c", it will automatically create a task that builds "mycode.o" from "mycode.c".

If the file "mycode.c" does not exist, rake will attempt to recursively synthesize a rule for it.

When a task is synthesized from a rule, the source attribute of the task is set to the matching source file. This allows us to write rules with actions that reference the source file.

Advanced rules [edit]

Any regular expression may be used as the rule pattern. Additionally, a proc may be used to calculate the name of the source file. This allows for complex patterns and sources.

The following rule is equivalent to the example above.

rule( %r\.o$/ => [
  proc {|task_name| task_name.sub(%r\.[^.]+$/, '.c') }
]) do |t|
  sh "cc {t.source} -c -o #{t.name}"
end

NOTE: Because of a quirk in Ruby syntax, parenthesis are required on rule when the first argument is a regular expression.

The following rule might be used for Java files.

rule '.java' => [
  proc { |tn| tn.sub(%r\.class$/, '.java').sub(%r^classes\//, 'src/') }
] do |t|
  java_compile(t.source, t.name)
end

Below is an example of a simple Rake recipe.

namespace :cake do
  desc 'make pancakes'
  task :pancake => [:flour,:milk,:egg,:baking_powder] do
     puts "sizzle"
  end
  task :butter do
    puts "cut 3 tablespoons of butter into tiny squares"
  end
  task :flour => :butter do
    puts "use hands to knead butter squares into 1{{frac|1|2}} cup flour"
  end
  task :milk do
    puts "add 1{{frac|1|4}} cup milk"
  end
  task :egg do
   puts "add 1 egg"
  end
  task :baking_powder do
   puts "add 3{{frac|1|2}} teaspoons baking powder"
  end
end

See also [edit]

External links [edit]