Rake tasks

Your first task

Rake looks for a file named Rakefile in the current directory and its parents, then runs the requested task.

A task pairs a name with a block, and rake greet runs the block from the terminal.

task :greet do puts "Hello from Rake" end

rake greet

Hello from Rake

Describe tasks

The desc line documents the task below it for the task list.

Run rake -T to see every described task with its one line summary.

desc "Say hello" task :greet do puts "Hello from Rake" end

rake -T

rake greet # Say hello

Dependencies

Tasks declare prerequisites that run first, which models real build order.

Greet depends on prepare, so preparing always happens before greeting.

task :prepare do puts "Preparing" end task greet: :prepare do puts "Hello from Rake" end

rake greet

Preparing
Hello from Rake

Default task

The default task runs when the command names no task at all.

Point default at greet to make a bare rake call prepare and greet in order.

task :prepare do puts "Preparing" end task greet: :prepare do puts "Hello from Rake" end task default: :greet

rake

Preparing
Hello from Rake

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? $tasks = {} def task(name, &block) $tasks[name] = block end task(:greet) { puts "Hello from Rake" } $tasks[:greet].call

    Answer

    Hello from Rake

    A task line is a method call that stores the block, and calling the block is what runs the body.

  2. What does this program print? $order = [] def task(name, deps: [], &block) deps.each { |dep| $order << dep } $order << name block.call end task(:greet, deps: [:prepare]) { puts $order.join(" -> ") }

    Answer

    prepare -> greet

    The prerequisite runs before the task that names it, which is the build order the lesson shows.

  3. What does a Rakefile contain?

    Answer

    Ruby code with task, desc and dependency calls

    A Rakefile is Ruby, and task is a method the Rake DSL defines.

  4. Which command lists the tasks that have a description?

    Answer

    rake -T

    The -T option prints every described task with its one-line summary.

  5. What does this program print? $described = {} $current = nil def desc(text) $current = text end def task(name) $described[name] = $current end desc "Say hello" task :greet p $described

    Answer

    {greet: "Say hello"}

    desc stores the text and the task that follows it takes that text, which is how the task list gets its summary.

  6. What does task greet: :prepare do ... end mean?

    Answer

    The greet task depends on prepare, which runs first

    The hash form names a prerequisite, and Rake runs it before the body of the task.

  7. What does a bare rake run?

    Answer

    The default task

    The default task is what a command with no task name runs.

  8. What does a task that depends on another task do when the dependency already ran in the same invocation?

    Answer

    Rake runs it once per invocation

    Rake keeps track of the tasks of one invocation, so a shared prerequisite runs once.

  9. What does this program print? tasks = { greet: "Say hello", build: "Build the site" } tasks.each { |name, text| puts "rake #{name} # #{text}" }

    Answer

    rake greet # Say hello / rake build # Build the site

    The loop writes one line per task in the shape rake -T uses, with two spaces before the comment.

  10. Where does Rake look for the Rakefile?

    Answer

    In the current directory and its parents

    Rake searches upward from the working directory, which is why a command works from a subfolder of the project.

Article author: Arthur Isaev