Rake (software)

From Wikipedia, the free encyclopedia
Rake
Developer(s)Jim Weirich
Stable release
13.1.0[1] / October 27, 2023; 5 months ago (2023-10-27)
Repository
Written inRuby
Operating systemCross-platform
TypeSoftware development tools
LicenseMIT License
Websiteruby.github.io/rake/

Rake is a software task management and a build automation tool created by Jim Weirich. It allows the user to specify tasks and to describe dependencies as well as to group tasks in a namespace. It is similar to SCons and Make. It and the Rakefiles it uses are written in Ruby.[2] Rake has been part of the standard library of Ruby since version 1.9.[3]

Like Make, Rake can synthesize tasks based on patterns: for example, automatically building a file compilation task based on filename patterns.[relevant?]

Examples[edit]

Below is an example of a simple Rake script to build a C Hello World 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

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. For example, suppose we were trying to invoke task "mycode.o" with no tasks defined for it. If the Rakefile has a rule that looks 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 as a prerequisite that 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 users 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(/\.o$/ =>
  ->(t_name){ t_name.sub /\.o$/, '.c' }) do |t|
  sh "cc #{t.source} -c -o #{t.name}"
end

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

The following rule might be used for Java files:

rule '.class' => ->(t_name){ t_name
    .sub(/\.class$/, '.java')
    .sub(/^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 1/2 cup flour"
  end
  task :milk do
    puts "add 1 1/4 cup milk"
  end
  task :egg do
   puts "add 1 egg"
  end
  task :baking_powder do
   puts "add 3 1/2 teaspoons baking powder"
  end
end

See also[edit]

References[edit]

  1. ^ "Release v13.1.0 · ruby/rake · GitHub". GitHub. October 27, 2023. Retrieved February 28, 2024.
  2. ^ "Rake -- Ruby Make". n.d. Retrieved February 28, 2024.
  3. ^ "NEWS". n.d. Archived from the original on March 4, 2016. Retrieved February 29, 2024.

External links[edit]