Rake Ruby task runner

Version 13.4.2 · source

Start here

rake
Run the default task
rake <task>
Run one task
rake -T
List tasks that have a desc
rake -P
Show tasks and their prerequisites
rake -n <task>
Dry run: show what would run
rake -j <N>
Run with N threads
rake <task>[<arg>,<arg>]
Pass task arguments
rake -t <task>
Trace invoke and execute

Concepts

  1. Rakefile Ruby DSL
  2. Task graph tasks + prereqs
  3. Prerequisites each runs once
  4. Task block Ruby code
Rake resolves prerequisites before running a task

Run

rake
Run the default task
rake <task> <task>
Run several tasks in order
rake <ns>:<task>
Run a namespaced task
rake -n <task>
Dry run: print tasks without executing
rake -j <N>
Run up to N tasks concurrently
Only tasks that are independent run in parallel; multitask and -m force it.
rake -m
Treat all tasks as multitasks
rake -f <file>
Use another Rakefile
rake -N
Do not search parent directories
rake -R <dir>
Auto-load .rake files from dir
Default is rakelib/.
rake -g
Use system-wide rakefiles only
rake <NAME>=<value>
Set an ENV variable for the run
bundle exec rake <task>
Run with the bundled gem versions

Inspect

rake -T
List tasks that have a desc
rake -T <pattern>
List tasks matching pattern
rake -T -A
List all tasks, including undocumented
rake -D <task>
Show the long description of tasks
rake -P
List tasks with prerequisites
rake -W <task>
Show where a task is defined
rake -W --comments
Locate only tasks with comments
rake --rules
Trace how rules resolve tasks
rake -V
Print the Rake version
rake -H
Show help

Debug and output

rake -t
Trace invoke and execute, with backtraces
rake --backtrace
Show full backtraces on errors
rake --suppress-backtrace <regex>
Hide matching backtrace lines
rake --job-stats
Show job statistics after the run
rake -v
Echo shell commands run by FileUtils
rake -q
Do not echo FileUtils commands
rake -s
Quiet, and no in directory message
rake -X
Hide deprecation warnings
rake -r <lib>
Require a library before the Rakefile
rake -I <dir>
Add dir to the require path
rake -e '<ruby>'
Run Ruby code and exit
rake -p '<ruby>'
Run Ruby code, print result, exit
rake -E '<ruby>'
Run Ruby code, then continue

Define tasks

task :<name> do ... end
Define a task
task <name>: %w[<a> <b>]
Task with prerequisites
task default: :<name>
Set the default task
desc '<text>'
Describe the next task for -T
namespace :<ns> do ... end
Group tasks under a prefix
task :<name>, [:<a>, :<b>] do |t, args|
Task with named arguments
args.with_defaults(<a>: <v>)
Default values for arguments
args.extras
Positional args beyond the declared ones
task :<name> => :environment
Load Rails app first (Rails)
Rake::Task[:<name>].invoke
Call a task once from Ruby
Skipped if it already ran in this process.
Rake::Task[:<name>].reenable
Allow a task to run again
Rake::Task[:<name>].execute
Run a task's actions, ignoring prerequisites
Rake::Task[:<name>].clear
Remove actions and prerequisites
task :<name>, [:<a>] => [:<pre>]
Arguments plus prerequisites
^<task> / rake:<task>
Parent / top-level lookup inside a namespace
import '<file>'
Load a file after the Rakefile
multitask :<name> => %w[<a> <b>]
Run prerequisites in parallel

Files and helpers

file '<out>' => %w[<src>] do |t|
Rebuild when a source is newer
directory '<path>'
Create a directory (and parents) on demand
rule '.o' => '.c' do |t|
Pattern rule for generated files
t.name / t.source
Target name / rule source in a block
t.prerequisites
Prerequisite names in a block
FileList['<glob>']
Lazy list of files by glob
FileList[...].ext('<.o>')
Change extensions
FileList[...].exclude('<glob>')
Drop matching files
sh '<cmd>'
Run a shell command, fail the task on error
sh '<cmd>', verbose: false
Run quietly
ruby '<file.rb>'
Run a Ruby script with the current Ruby
cp / mv / rm_rf / mkdir_p
FileUtils helpers available in tasks
require 'rake/clean'
Add clean and clobber tasks
CLEAN.include('<glob>')
Files removed by rake clean
CLOBBER.include('<glob>')
Files removed by rake clobber
Rake::TestTask.new do |t| ... end
Define a test task
rake <task>[<a>,<b>]
Pass arguments on the command line
rake 'name[a b,c]'
Quote arguments that contain spaces

Recipes

Documented tasks with arguments
desc "Deploy to an environment"task :deploy, [:env] => :build do |t, args|  args.with_defaults(env: "staging")  sh "./deploy.sh #{args.env}"end# rake deploy[production]
Compile C files incrementally
SRC = FileList["src/*.c"]OBJ = SRC.ext("o")rule ".o" => ".c" do |t|  sh "cc -c -o #{t.name} #{t.source}"endfile "app" => OBJ do |t|  sh "cc -o #{t.name} #{t.prerequisites.join(' ')}"end
Namespaces and a default task
namespace :db do  desc "Run migrations"  task :migrate do ... endendtask default: %w[db:migrate test]
Inspect a big Rakefile
rake -T -Arake -P | grep <task>rake -W <task>rake -n -t <task>