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 168 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 154 def clear TASKS.clear RULES.clear end
Define a rule for synthesizing tasks.
# File minirake, line 191 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 185 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 209 def enhance_with_matching_rule(task_name, level=0) fail "Rule Recursion Too Deep: #{task_name}" if level >= 16 RULES.each do |pattern, extensions, block| next unless pattern && pattern.match(task_name) sources = extensions.flat_map do |ext| case ext when /%/ task_name.pathmap(ext) when %r{/} ext when /^\./ source = task_name.sub(pattern, ext) source == ext ? task_name.ext(ext) : source when String ext when Proc, Method ext.arity == 1 ? ext.call(task_name) : ext.call else fail "Don't know how to handle rule dependent: #{ext.inspect}" end end prereqs = sources.map do |source| if File.exist?(source) || TASKS[source] source elsif parent = enhance_with_matching_rule(source, level + 1) parent.name else break nil end end if prereqs task = FileTask.define_task(task_name => prereqs, &block) task.source = prereqs.first return task end end nil end
Lookup a task. Return an existing task if found, otherwise create a task of the current type.
# File minirake, line 200 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 251 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 82 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 126 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 @done = true @running = false end
Invoke the task if it is needed. Prerequisites are invoked first.
# File minirake, line 97 def invoke puts "Invoke #{name} (already=[#{@already_invoked}], needed=[#{needed?}])" if $trace return if @already_invoked prerequisites = @prerequisites.collect{ |n| n.is_a?(Proc) ? n.call(name) : n }.flatten prerequisites.each do |n| t = Task[n] unless t.done? return prerequisites.select{|v| v = Task[v]; v && (!v.done? || !v.running?) } end end @already_invoked = true if needed? @running = true if $rake_root_fiber return Fiber.new do self.execute $rake_root_fiber.transfer end else self.execute end end @done = true end