module Rake::TaskManager

The TaskManager module is a mixin for managing tasks.

Attributes

last_comment[RW]

Track the last comment made in the Rakefile.

last_description[RW]

Track the last comment made in the Rakefile.

Public Instance Methods

[](task_name, scopes=nil) click to toggle source

Find a matching task for task_name.

# File lib/rake/task_manager.rb, line 57
def [](task_name, scopes=nil)
  task_name = task_name.to_s
  self.lookup(task_name, scopes) or
    enhance_with_matching_rule(task_name) or
    synthesize_file_task(task_name) or
    fail "Don't know how to build task '#{task_name}'"
end
clear() click to toggle source

Clear all tasks in this application.

# File lib/rake/task_manager.rb, line 159
def clear
  @tasks.clear
  @rules.clear
end
current_scope() click to toggle source

Return the list of scope names currently active in the task manager.

# File lib/rake/task_manager.rb, line 199
def current_scope
  @scope
end
enhance_with_matching_rule(task_name, level=0) click to toggle source

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 lib/rake/task_manager.rb, line 129
def enhance_with_matching_rule(task_name, level=0)
  fail Rake::RuleRecursionOverflowError,
    "Rule Recursion Too Deep" if level >= 16
  @rules.each do |pattern, args, extensions, block|
    if pattern.match(task_name)
      task = attempt_rule(task_name, args, extensions, block, level)
      return task if task
    end
  end
  nil
rescue Rake::RuleRecursionOverflowError => ex
  ex.add_target(task_name)
  fail ex
end
in_namespace(name) { |ns| ... } click to toggle source

Evaluate the block in a nested namespace named name. Create an anonymous namespace if name is nil.

# File lib/rake/task_manager.rb, line 205
def in_namespace(name)
  name ||= generate_name
  @scope = Scope.new(name, @scope)
  ns = NameSpace.new(self, @scope)
  yield(ns)
  ns
ensure
  @scope = @scope.tail
end
intern(task_class, task_name) click to toggle source

Lookup a task. Return an existing task if found, otherwise create a task of the current type.

# File lib/rake/task_manager.rb, line 52
def intern(task_class, task_name)
  @tasks[task_name.to_s] ||= task_class.new(task_name, self)
end
lookup(task_name, initial_scope=nil) click to toggle source

Lookup a task, using scope and the scope hints in the task name. This method performs straight lookups without trying to synthesize file tasks or rules. Special scope names (e.g. '^') are recognized. If no scope argument is supplied, use the current scope. Return nil if the task cannot be found.

# File lib/rake/task_manager.rb, line 169
def lookup(task_name, initial_scope=nil)
  initial_scope ||= @scope
  task_name = task_name.to_s
  if task_name =~ /^rake:/
    scopes = Scope.make
    task_name = task_name.sub(/^rake:/, '')
  elsif task_name =~ /^(\^+)/
    scopes = initial_scope.trim($1.size)
    task_name = task_name.sub(/^(\^+)/, '')
  else
    scopes = initial_scope
  end
  lookup_in_scope(task_name, scopes)
end
resolve_args(args) click to toggle source

Resolve the arguments for a task/rule. Returns a triplet of [task_name, arg_name_list, prerequisites].

# File lib/rake/task_manager.rb, line 72
def resolve_args(args)
  if args.last.is_a?(Hash)
    deps = args.pop
    resolve_args_with_dependencies(args, deps)
  else
    resolve_args_without_dependencies(args)
  end
end
tasks() click to toggle source

List of all defined tasks in this application.

# File lib/rake/task_manager.rb, line 145
def tasks
  @tasks.values.sort_by { |t| t.name }
end
tasks_in_scope(scope) click to toggle source

List of all the tasks defined in the given scope (and its sub-scopes).

# File lib/rake/task_manager.rb, line 151
def tasks_in_scope(scope)
  prefix = scope.path
  tasks.select { |t|
    /^#{prefix}:/ =~ t.name
  }
end

Private Instance Methods

add_location(task) click to toggle source

Add a location to the locations field of the given task.

# File lib/rake/task_manager.rb, line 218
def add_location(task)
  loc = find_location
  task.locations << loc if loc
  task
end
attempt_rule(task_name, args, extensions, block, level) click to toggle source

Attempt to create a rule given the list of prerequisites.

# File lib/rake/task_manager.rb, line 248
def attempt_rule(task_name, args, extensions, block, level)
  sources = make_sources(task_name, extensions)
  prereqs = sources.map { |source|
    trace_rule level, "Attempting Rule #{task_name} => #{source}"
    if File.exist?(source) || Rake::Task.task_defined?(source)
      trace_rule level, "(#{task_name} => #{source} ... EXIST)"
      source
    elsif parent = enhance_with_matching_rule(source, level + 1)
      trace_rule level, "(#{task_name} => #{source} ... ENHANCE)"
      parent.name
    else
      trace_rule level, "(#{task_name} => #{source} ... FAIL)"
      return nil
    end
  }
  task = FileTask.define_task(task_name, {args => prereqs}, &block)
  task.sources = prereqs
  task
end
find_location() click to toggle source

Find the location that called into the dsl layer.

# File lib/rake/task_manager.rb, line 225
def find_location
  locations = caller
  i = 0
  while locations[i]
    return locations[i + 1] if locations[i] =~ /rake\/dsl_definition.rb/
    i += 1
  end
  nil
end
generate_name() click to toggle source

Generate an anonymous namespace name.

# File lib/rake/task_manager.rb, line 236
def generate_name
  @seed ||= 0
  @seed += 1
  "_anon_#{@seed}"
end
get_description(task) click to toggle source

Return the current description, clearing it in the process.

# File lib/rake/task_manager.rb, line 298
def get_description(task)
  desc = @last_description
  @last_description = nil
  desc
end
lookup_in_scope(name, scope) click to toggle source

Lookup the task name

# File lib/rake/task_manager.rb, line 185
def lookup_in_scope(name, scope)
  loop do
    tn = scope.path_with_task_name(name)
    task = @tasks[tn]
    return task if task
    break if scope.empty?
    scope = scope.tail
  end
  nil
end
make_sources(task_name, extensions) click to toggle source

Make a list of sources from the list of file name extensions / translation procs.

# File lib/rake/task_manager.rb, line 270
def make_sources(task_name, extensions)
  result = extensions.map { |ext|
    case ext
    when /%/
      task_name.pathmap(ext)
    when %r{/}
      ext
    when /^\./
      task_name.ext(ext)
    when String
      ext
    when Proc, Method
      if ext.arity == 1
        ext.call(task_name)
      else
        ext.call
      end
    else
      fail "Don't know how to handle rule dependent: #{ext.inspect}"
    end
  }
  result.flatten
end
resolve_args_without_dependencies(args) click to toggle source

Resolve task arguments for a task or rule when there are no dependencies declared.

The patterns recognized by this argument resolving function are:

task :t
task :t, [:a]
# File lib/rake/task_manager.rb, line 89
def resolve_args_without_dependencies(args)
  task_name = args.shift
  if args.size == 1 && args.first.respond_to?(:to_ary)
    arg_names = args.first.to_ary
  else
    arg_names = args
  end
  [task_name, arg_names, []]
end