Return a task with the given name. If the task is not currently known, try to synthesize one from the defined rules. If no rules are found, but an existing file matches the task name, assume it is a file task with no dependencies or actions.
# File minirake, line 141
def [](task_name)
task_name = task_name.to_s
if task = TASKS[task_name]
return task
end
if task = enhance_with_matching_rule(task_name)
return task
end
if File.exist?(task_name)
return FileTask.define_task(task_name)
end
fail "Don't know how to rake #{task_name}"
end
Clear the task list. This cause rake to immediately forget all the tasks that have been assigned. (Normally used in the unit tests.)
# File minirake, line 127
def clear
TASKS.clear
RULES.clear
end
Define a rule for synthesizing tasks.
# File minirake, line 164
def create_rule(args, &block)
pattern, deps = resolve_args(args)
pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
RULES << [pattern, deps, block]
end
Define a task given args and an option block. If a rule with
the given name already exists, the prerequisites and actions are added to
the existing task.
# File minirake, line 158
def define_task(args, &block)
task_name, deps = resolve_args(args)
lookup(task_name).enhance([deps].flatten, &block)
end
If a rule can be found that matches the task name, enhance the task with the prerequisites and actions from the rule. Set the source attribute of the task appropriately for the rule. Return the enhanced task or nil of no rule was found.
# File minirake, line 182
def enhance_with_matching_rule(task_name)
RULES.each do |pattern, extensions, block|
if pattern.match(task_name)
ext = extensions.first
deps = extensions[1..-1]
case ext
when String
source = task_name.sub(/\.[^.]*$/, ext)
when Proc
source = ext.call(task_name)
else
fail "Don't know how to handle rule dependent: #{ext.inspect}"
end
if File.exist?(source)
task = FileTask.define_task({task_name => [source]+deps}, &block)
task.source = source
return task
end
end
end
nil
end
Lookup a task. Return an existing task if found, otherwise create a task of the current type.
# File minirake, line 173
def lookup(task_name)
name = task_name.to_s
TASKS[name] ||= self.new(name)
end
Resolve the arguments for a task/rule.
# File minirake, line 208
def resolve_args(args)
case args
when Hash
fail "Too Many Task Names: #{args.keys.join(' ')}" if args.size > 1
fail "No Task Name Given" if args.size < 1
task_name = args.keys[0]
deps = args[task_name]
deps = [deps] if (String===deps) || (Regexp===deps) || (Proc===deps)
else
task_name = args
deps = []
end
[task_name, deps]
end
Enhance a task with prerequisites or actions. Returns self.
# File minirake, line 78
def enhance(deps=nil, &block)
@prerequisites |= deps if deps
@actions << block if block_given?
self
end
Execute the actions associated with this task.
# File minirake, line 100
def execute
puts "Execute #{name}" if $trace
self.class.enhance_with_matching_rule(name) if @actions.empty?
unless $dryrun
@actions.each { |act| act.call(self) }
end
end
Invoke the task if it is needed. Prerequites are invoked first.
# File minirake, line 90
def invoke
puts "Invoke #{name} (already=[#{@already_invoked}], needed=[#{needed?}])" if $trace
return if @already_invoked
@already_invoked = true
prerequisites = @prerequisites.collect{ |n| n.is_a?(Proc) ? n.call(name) : n }.flatten
prerequisites.each { |n| Task[n].invoke }
execute if needed?
end
Is this task needed?
# File minirake, line 109
def needed?
true
end
Timestamp for this task. Basic tasks return the current time for their time stamp. Other tasks can be more sophisticated.
# File minirake, line 115
def timestamp
prerequisites = @prerequisites.collect{ |n| n.is_a?(Proc) ? n.call(name) : n }.flatten
prerequisites.collect { |n| Task[n].timestamp }.max || Time.now
end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.