module Rake::TaskManager

  1. lib/rake/task_manager.rb
Parent: Rake

The TaskManager module is a mixin for managing tasks.

Attributes

last_description [RW]

Track the last comment made in the Rakefile.

record_task_metadata [RW]

Public Class methods

new ()
[show source]
    # File lib/rake/task_manager.rb, line 9
 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

create_rule (*args, &block)
[show source]
    # File lib/rake/task_manager.rb, line 17
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
define_task (task_class, *args, &block)
[show source]
    # File lib/rake/task_manager.rb, line 23
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
intern (task_class, task_name)

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

[show source]
    # File lib/rake/task_manager.rb, line 39
39:     def intern(task_class, task_name)
40:       @tasks[task_name.to_s] ||= task_class.new(task_name, self)
41:     end
[] (task_name, scopes=nil)

Find a matching task for task_name.

[show source]
    # File lib/rake/task_manager.rb, line 44
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
synthesize_file_task (task_name)
[show source]
    # File lib/rake/task_manager.rb, line 52
52:     def synthesize_file_task(task_name)
53:       return nil unless File.exist?(task_name)
54:       define_task(Rake::FileTask, task_name)
55:     end
resolve_args (args)

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

[show source]
    # File lib/rake/task_manager.rb, line 59
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
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.

[show source]
     # File lib/rake/task_manager.rb, line 127
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
tasks ()

List of all defined tasks in this application.

[show source]
     # File lib/rake/task_manager.rb, line 143
143:     def tasks
144:       @tasks.values.sort_by { |t| t.name }
145:     end
tasks_in_scope (scope)

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

[show source]
     # File lib/rake/task_manager.rb, line 149
149:     def tasks_in_scope(scope)
150:       prefix = scope.join(":")
151:       tasks.select { |t|
152:         /^#{prefix}:/ =~ t.name
153:       }
154:     end
clear ()

Clear all tasks in this application.

[show source]
     # File lib/rake/task_manager.rb, line 157
157:     def clear
158:       @tasks.clear
159:       @rules.clear
160:     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.

[show source]
     # File lib/rake/task_manager.rb, line 167
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
current_scope ()

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

[show source]
     # File lib/rake/task_manager.rb, line 197
197:     def current_scope
198:       @scope.dup
199:     end
in_namespace (name)

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

[show source]
     # File lib/rake/task_manager.rb, line 203
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

Private Instance methods

resolve_args_without_dependencies (args)

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)
[show source]
    # File lib/rake/task_manager.rb, line 77
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
lookup_in_scope (name, scope)

Lookup the task name

[show source]
     # File lib/rake/task_manager.rb, line 183
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
add_location (task)

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

[show source]
     # File lib/rake/task_manager.rb, line 216
216:     def add_location(task)
217:       loc = find_location
218:       task.locations << loc if loc
219:       task
220:     end
find_location ()

Find the location that called into the dsl layer.

[show source]
     # File lib/rake/task_manager.rb, line 223
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 ()

Generate an anonymous namespace name.

[show source]
     # File lib/rake/task_manager.rb, line 234
234:     def generate_name
235:       @seed ||= 0
236:       @seed += 1
237:       "_anon_#{@seed}"
238:     end
trace_rule (level, message)
[show source]
     # File lib/rake/task_manager.rb, line 240
240:     def trace_rule(level, message)
241:       $stderr.puts "#{"    "*level}#{message}" if Rake.application.options.trace_rules
242:     end
attempt_rule (task_name, extensions, block, level)

Attempt to create a rule given the list of prerequisites.

[show source]
     # File lib/rake/task_manager.rb, line 245
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
make_sources (task_name, extensions)

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

[show source]
     # File lib/rake/task_manager.rb, line 267
267:     def make_sources(task_name, extensions)
268:       result = extensions.collect { |ext|
269:         case ext
270:         when /%/
271:           task_name.pathmap(ext)
272:         when %{/}
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
get_description (task)

Return the current description, clearing it in the process.

[show source]
     # File lib/rake/task_manager.rb, line 295
295:     def get_description(task)
296:       desc = @last_description
297:       @last_description = nil
298:       desc
299:     end