Rakefiles are Ruby scripts:
anything legal in a Ruby script is also allowed in a Rakefile
Tasks; file and directory tasks; descriptions; prerequisites
============================================================
A minimal task:
task :mytask do
# actions
end
A task with a description and prerequisites (req):
desc "A description of mytask"
task :mytask => [:req_task, "req_file"] do
# Any number of actions
end
A file task, with a body that uses properties of the task object `t`,
and string interpolation using `#{...}`:
file "prog.exe" => ["a.o", "b.o"] do |t|
sh "cc -o #{t.name} #{t.prerequisites.join(' ')}"
end
Create a directory (and the directories above it, as needed):
directory "testdata/examples/doc"
Rules; FileList; tasks that take arguments; parallel execution
==============================================================
This rule says: "For every .c file, create a file task to produce the
corresponding .o file." The `source` attribute of each task is set to
the matching source file.
rule '.o' => ['.c'] do |t|
sh "cc #{t.source} -c -o #{t.name}"
end
This rule does the same thing, but using a regex as the rule pattern,
and a proc to calculate the name of the source file.
rule( /\.o$/ => [
proc { |task_name| task_name.sub(/\.[^.]+$/, '.c') }
]) do |t|
sh "cc #{t.source} -c -o #{t.name}"
end
FileLists collect all files that match a shell pattern:
Filelist['*.o'].each do |f|
file finalprogram => f
end
A task that can take arguments (and supplies default values):
task :print_name, [:first, :last] => [:req_task] do |t, args|
# specify default parameters:
args.with_defaults(:first => "John", :last => "Dough")
puts "First name is #{args.first}"
end
To invoke a task with arguments from the command line:
rake "print_name[James, Bond]"
(The quotation marks are because task+arguments must be a single string.)
Parallel execution of prerequisites:
multitask :copy_files => [:copy_src, :copy_doc, :copy_bin] do
puts "All Copies Complete"
end
Programming with tasks
======================
Any Ruby file (including other Rakefiles) can be included with a
standard Ruby `require` command
To access a task object:
Rake::Task[:mytask].add_description
or
task :mytask => [:prereq, ] do |t|
# p pretty-prints, for debugging purposes.
p t.timestamp
end
* t.add_description(description)
Add a description to the task.
* t.arg_description
Argument description (nil if none).
* t.arg_names
Name of arguments for this task.
* t.clear
Clear the existing prerequisites and actions of a rake task.
* t.clear_actions
Clear the existing actions on a rake task.
* t.clear_prerequisites
Clear the existing prerequisites of a rake task.
* t.enhance(deps = nil, &block)
Enhance a task with prerequisites or actions.
* t.execute(args = nil)
Execute the actions associated with this task.
* t.inspect, t.investigation
Return a string describing the internal state of a task.
* t.invoke(*args)
Invoke the task if it is needed.
* t.invoke_prerequisites(task_args, invocation_chain)
Invoke all the prerequisites of a task.
* t.name
Name of the task, including any namespace qualifiers.
* t.name_with_args
Name of task with argument list description.
* t.needed?
Is this task needed?
* t.reenable
Reenable the task, allowing it to be executed if the task is
invoked again.
* t.set_arg_names(args)
Set the names of the arguments for this task.
* t.source
First source from a rule (nil if no sources).
* t.timestamp
Timestamp for this task.
* t.to_s
Return task name.