123456789_123456789_123456789_123456789_123456789_

Module: Rake::TaskManager

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rake/task_manager.rb

Overview

The TaskManager module is a mixin for managing tasks.

Class Attribute Summary

Instance Attribute Summary

Instance Method Summary

Class Attribute Details

.record_task_metadata (rw)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 319

attr_accessor : # :nodoc:

Instance Attribute Details

#last_description (rw)

Track the last comment made in the Rakefile.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 7

attr_accessor :last_description

Instance Method Details

#[](task_name, scopes = nil)

Find a matching task for task_name.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 54

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 generate_message_for_undefined_task(task_name)
end

#add_location(task) (private)

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 234

def add_location(task)
  loc = find_location
  task.locations << loc if loc
  task
end

#attempt_rule(task_name, task_pattern, args, extensions, block, level) (private)

Attempt to create a rule given the list of prerequisites.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 264

def attempt_rule(task_name, task_pattern, args, extensions, block, level)
  sources = make_sources(task_name, task_pattern, 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

#clear

Clear all tasks in this application.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 175

def clear
  @tasks.clear
  @rules.clear
end

#create_rule(*args, &block)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 17

def create_rule(*args, &block) # :nodoc:
  pattern, args, deps = resolve_args(args)
  pattern = Regexp.new(Regexp.quote(pattern) + "$") if String === pattern
  @rules << [pattern, args, deps, block]
end

#current_scope

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 215

def current_scope
  @scope
end

#define_task(task_class, *args, &block)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 23

def define_task(task_class, *args, &block) # :nodoc:
  task_name, arg_names, deps = resolve_args(args)

  original_scope = @scope
  if String === task_name and
     not task_class.ancestors.include? Rake::FileTask
    task_name, *definition_scope = *(task_name.split(":").reverse)
    @scope = Scope.make(*(definition_scope + @scope.to_a))
  end

  task_name = task_class.scope_name(@scope, task_name)
  deps = [deps] unless deps.respond_to?(:to_ary)
  deps = deps.map { |d| Rake.from_pathname(d).to_s }
  task = intern(task_class, task_name)
  task.set_arg_names(arg_names) unless arg_names.empty?
  if Rake::TaskManager.
    add_location(task)
    task.add_description(get_description(task))
  end
  task.enhance(deps, &block)
ensure
  @scope = original_scope
end

#enhance_with_matching_rule(task_name, level = 0)

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.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 145

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 && pattern.match(task_name)
      task = attempt_rule(task_name, pattern, args, extensions, block, level)
      return task if task
    end
  end
  nil
rescue Rake::RuleRecursionOverflowError => ex
  ex.add_target(task_name)
  fail ex
end

#find_location (private)

Find the location that called into the dsl layer.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 241

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_did_you_mean_suggestions(task_name)

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 68

def generate_did_you_mean_suggestions(task_name)
  return "" unless defined?(::DidYouMean::SpellChecker)

  suggestions = ::DidYouMean::SpellChecker.new(dictionary: @tasks.keys).correct(task_name.to_s)
  if ::DidYouMean.respond_to?(:formatter)# did_you_mean v1.2.0 or later
    ::DidYouMean.formatter.message_for(suggestions)
  elsif defined?(::DidYouMean::Formatter) # before did_you_mean v1.2.0
    ::DidYouMean::Formatter.new(suggestions).to_s
  else
    ""
  end
end

#generate_message_for_undefined_task(task_name)

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 62

def generate_message_for_undefined_task(task_name)
  message = "Don't know how to build task '#{task_name}' "\
            "(See the list of available tasks with `#{Rake.application.name} --tasks`)"
  message + generate_did_you_mean_suggestions(task_name)
end

#generate_name (private)

Generate an anonymous namespace name.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 252

def generate_name
  @seed ||= 0
  @seed += 1
  "_anon_#{@seed}"
end

#get_description(task) (private)

Return the current description, clearing it in the process.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 312

def get_description(task)
  desc = @last_description
  @last_description = nil
  desc
end

#in_namespace(name)

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 221

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

#initialize

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 9

def initialize # :nodoc:
  super
  @tasks = Hash.new
  @rules = Array.new
  @scope = Scope.make
  @last_description = nil
end

#intern(task_class, task_name)

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 49

def intern(task_class, task_name)
  @tasks[task_name.to_s] ||= task_class.new(task_name, self)
end

#lookup(task_name, initial_scope = nil)

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.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 185

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

#lookup_in_scope(name, scope) (private)

Lookup the task name

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 201

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, task_pattern, extensions) (private)

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 286

def make_sources(task_name, task_pattern, extensions)
  result = extensions.map { |ext|
    case ext
    when /%/
      task_name.pathmap(ext)
    when %r{/}
      ext
    when /^\./
      source = task_name.sub(task_pattern, ext)
      source == ext ? task_name.ext(ext) : source
    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(args)

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 88

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

#resolve_args_with_dependencies(args, hash) (private)

This method is for internal use only.

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

The patterns recognized by this argument resolving function are:

task :t => [:d]
task :t, [a] => [:d]
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 124

def resolve_args_with_dependencies(args, hash) # :nodoc:
  fail "Task Argument Error" if hash.size != 1
  key, value = hash.map { |k, v| [k, v] }.first
  if args.empty?
    task_name = key
    arg_names = []
    deps = value || []
  else
    task_name = args.shift
    arg_names = key
    deps = value
  end
  deps = [deps] unless deps.respond_to?(:to_ary)
  [task_name, arg_names, deps]
end

#resolve_args_without_dependencies(args) (private)

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]
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 105

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

#synthesize_file_task(task_name)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 81

def synthesize_file_task(task_name) # :nodoc:
  return nil unless File.exist?(task_name)
  define_task(Rake::FileTask, task_name)
end

#tasks

List of all defined tasks in this application.

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 161

def tasks
  @tasks.values.sort_by { |t| t.name }
end

#tasks_in_scope(scope)

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

[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 167

def tasks_in_scope(scope)
  prefix = scope.path
  tasks.select { |t|
    /^#{prefix}:/ =~ t.name
  }
end

#trace_rule(level, message) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rake/task_manager.rb', line 258

def trace_rule(level, message) # :nodoc:
  options.trace_output.puts "#{"    " * level}#{message}" if
    Rake.application.options.trace_rules
end