module Rake::TaskManager

The TaskManager module is a mixin for managing tasks.

Attributes

record_task_metadata[RW]
last_comment[RW]

Track the last comment made in the Rakefile.

last_description[RW]

Track the last comment made in the Rakefile.

Public Class Methods

new() click to toggle source
Calls superclass method
   # File lib/rake/task_manager.rb
 9 def initialize
10   super
11   @tasks = Hash.new
12   @rules = Array.new
13   @scope = Array.new
14   @last_description = nil
15 end

Public Instance Methods

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

Find a matching task for task_name.

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

Clear all tasks in this application.

    # File lib/rake/task_manager.rb
157 def clear
158   @tasks.clear
159   @rules.clear
160 end
create_rule(*args, &block) click to toggle source
   # File lib/rake/task_manager.rb
17 def create_rule(*args, &block)
18   pattern, _, deps = resolve_args(args)
19   pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
20   @rules << [pattern, deps, block]
21 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
197 def current_scope
198   @scope.dup
199 end
define_task(task_class, *args, &block) click to toggle source
   # File lib/rake/task_manager.rb
23 def define_task(task_class, *args, &block)
24   task_name, arg_names, deps = resolve_args(args)
25   task_name = task_class.scope_name(@scope, task_name)
26   deps = [deps] unless deps.respond_to?(:to_ary)
27   deps = deps.collect {|d| d.to_s }
28   task = intern(task_class, task_name)
29   task.set_arg_names(arg_names) unless arg_names.empty?
30   if Rake::TaskManager.record_task_metadata
31     add_location(task)
32     task.add_description(get_description(task))
33   end
34   task.enhance(deps, &block)
35 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
127 def enhance_with_matching_rule(task_name, level=0)
128   fail Rake::RuleRecursionOverflowError,
129     "Rule Recursion Too Deep" if level >= 16
130   @rules.each do |pattern, extensions, block|
131     if pattern.match(task_name)
132       task = attempt_rule(task_name, extensions, block, level)
133       return task if task
134     end
135   end
136   nil
137 rescue Rake::RuleRecursionOverflowError => ex
138   ex.add_target(task_name)
139   fail ex
140 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
203 def in_namespace(name)
204   name ||= generate_name
205   @scope.push(name)
206   ns = NameSpace.new(self, @scope)
207   yield(ns)
208   ns
209 ensure
210   @scope.pop
211 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
39 def intern(task_class, task_name)
40   @tasks[task_name.to_s] ||= task_class.new(task_name, self)
41 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
167 def lookup(task_name, initial_scope=nil)
168   initial_scope ||= @scope
169   task_name = task_name.to_s
170   if task_name =~ /^rake:/
171     scopes = []
172     task_name = task_name.sub(/^rake:/, '')
173   elsif task_name =~ /^(\^+)/
174     scopes = initial_scope[0, initial_scope.size - $1.size]
175     task_name = task_name.sub(/^(\^+)/, '')
176   else
177     scopes = initial_scope
178   end
179   lookup_in_scope(task_name, scopes)
180 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
59 def resolve_args(args)
60   if args.last.is_a?(Hash)
61     deps = args.pop
62     resolve_args_with_dependencies(args, deps)
63   else
64     resolve_args_without_dependencies(args)
65   end
66 end
synthesize_file_task(task_name) click to toggle source
   # File lib/rake/task_manager.rb
52 def synthesize_file_task(task_name)
53   return nil unless File.exist?(task_name)
54   define_task(Rake::FileTask, task_name)
55 end
tasks() click to toggle source

List of all defined tasks in this application.

    # File lib/rake/task_manager.rb
143 def tasks
144   @tasks.values.sort_by { |t| t.name }
145 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
149 def tasks_in_scope(scope)
150   prefix = scope.join(":")
151   tasks.select { |t|
152     /^#{prefix}:/ =~ t.name
153   }
154 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
216 def add_location(task)
217   loc = find_location
218   task.locations << loc if loc
219   task
220 end
attempt_rule(task_name, extensions, block, level) click to toggle source

Attempt to create a rule given the list of prerequisites.

    # File lib/rake/task_manager.rb
245 def attempt_rule(task_name, extensions, block, level)
246   sources = make_sources(task_name, extensions)
247   prereqs = sources.collect { |source|
248     trace_rule level, "Attempting Rule #{task_name} => #{source}"
249     if File.exist?(source) || Rake::Task.task_defined?(source)
250       trace_rule level, "(#{task_name} => #{source} ... EXIST)"
251       source
252     elsif parent = enhance_with_matching_rule(source, level+1)
253       trace_rule level, "(#{task_name} => #{source} ... ENHANCE)"
254       parent.name
255     else
256       trace_rule level, "(#{task_name} => #{source} ... FAIL)"
257       return nil
258     end
259   }
260   task = FileTask.define_task({task_name => prereqs}, &block)
261   task.sources = prereqs
262   task
263 end
find_location() click to toggle source

Find the location that called into the dsl layer.

    # File lib/rake/task_manager.rb
223 def find_location
224   locations = caller
225   i = 0
226   while locations[i]
227     return locations[i+1] if locations[i] =~ /rake\/dsl_definition.rb/
228     i += 1
229   end
230   nil
231 end
generate_name() click to toggle source

Generate an anonymous namespace name.

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

Return the current description, clearing it in the process.

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

Lookup the task name

    # File lib/rake/task_manager.rb
183 def lookup_in_scope(name, scope)
184   n = scope.size
185   while n >= 0
186     tn = (scope[0,n] + [name]).join(':')
187     task = @tasks[tn]
188     return task if task
189     n -= 1
190   end
191   nil
192 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
267 def make_sources(task_name, extensions)
268   result = extensions.collect { |ext|
269     case ext
270     when /%/
271       task_name.pathmap(ext)
272     when %r{/}
273       ext
274     when /^\./
275       task_name.ext(ext)
276     when String
277       ext
278     when Proc
279       if ext.arity == 1
280         ext.call(task_name)
281       else
282         ext.call
283       end
284     else
285       fail "Don't know how to handle rule dependent: #{ext.inspect}"
286     end
287   }
288   result.flatten
289 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]
task :t, :a                 (deprecated)
   # File lib/rake/task_manager.rb
77 def resolve_args_without_dependencies(args)
78   task_name = args.shift
79   if args.size == 1 && args.first.respond_to?(:to_ary)
80     arg_names = args.first.to_ary
81   else
82     arg_names = args
83   end
84   [task_name, arg_names, []]
85 end
trace_rule(level, message) click to toggle source
    # File lib/rake/task_manager.rb
240 def trace_rule(level, message)
241   $stderr.puts "#{"    "*level}#{message}" if Rake.application.options.trace_rules
242 end