Debian Codegraph @

rake.d/vendor/drake-0.9.2.0.3.1/lib/rake/task_manager.rb

raw
1 module Rake
2
3 # The TaskManager module is a mixin for managing tasks.
4 module TaskManager
5 # Track the last comment made in the Rakefile.
6 attr_accessor :last_description
7 alias :last_comment :last_description # Backwards compatibility
8
9 def initialize
10 super
11 @tasks = Hash.new
12 @rules = Array.new
13 @scope = Array.new
14 @last_description = nil
15 end
16
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
22
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
36
37 # Lookup a task. Return an existing task if found, otherwise
38 # create a task of the current type.
39 def intern(task_class, task_name)
40 @tasks[task_name.to_s] ||= task_class.new(task_name, self)
41 end
42
43 # Find a matching task for +task_name+.
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
51
52 def synthesize_file_task(task_name)
53 return nil unless File.exist?(task_name)
54 define_task(Rake::FileTask, task_name)
55 end
56
57 # Resolve the arguments for a task/rule. Returns a triplet of
58 # [task_name, arg_name_list, prerequisites].
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
67
68 # Resolve task arguments for a task or rule when there are no
69 # dependencies declared.
70 #
71 # The patterns recognized by this argument resolving function are:
72 #
73 # task :t
74 # task :t, [:a]
75 # task :t, :a (deprecated)
76 #
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
86 private :resolve_args_without_dependencies
87
88 # Resolve task arguments for a task or rule when there are
89 # dependencies declared.
90 #
91 # The patterns recognized by this argument resolving function are:
92 #
93 # task :t => [:d]
94 # task :t, [a] => [:d]
95 # task :t, :needs => [:d] (deprecated)
96 # task :t, :a, :needs => [:d] (deprecated)
97 #
98 def resolve_args_with_dependencies(args, hash) # :nodoc:
99 fail "Task Argument Error" if hash.size != 1
100 key, value = hash.map { |k, v| [k,v] }.first
101 if args.empty?
102 task_name = key
103 arg_names = []
104 deps = value
105 elsif key == :needs
106 Rake.application.deprecate(
107 "task :t, arg, :needs => [deps]",
108 "task :t, [args] => [deps]",
109 caller.detect { |c| c !~ /\blib\/rake\b/ })
110 task_name = args.shift
111 arg_names = args
112 deps = value
113 else
114 task_name = args.shift
115 arg_names = key
116 deps = value
117 end
118 deps = [deps] unless deps.respond_to?(:to_ary)
119 [task_name, arg_names, deps]
120 end
121 private :resolve_args_with_dependencies
122
123 # If a rule can be found that matches the task name, enhance the
124 # task with the prerequisites and actions from the rule. Set the
125 # source attribute of the task appropriately for the rule. Return
126 # the enhanced task or nil of no rule was found.
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
141
142 # List of all defined tasks in this application.
143 def tasks
144 @tasks.values.sort_by { |t| t.name }
145 end
146
147 # List of all the tasks defined in the given scope (and its
148 # sub-scopes).
149 def tasks_in_scope(scope)
150 prefix = scope.join(":")
151 tasks.select { |t|
152 /^#{prefix}:/ =~ t.name
153 }
154 end
155
156 # Clear all tasks in this application.
157 def clear
158 @tasks.clear
159 @rules.clear
160 end
161
162 # Lookup a task, using scope and the scope hints in the task name.
163 # This method performs straight lookups without trying to
164 # synthesize file tasks or rules. Special scope names (e.g. '^')
165 # are recognized. If no scope argument is supplied, use the
166 # current scope. Return nil if the task cannot be found.
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
181
182 # Lookup the task name
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
193 private :lookup_in_scope
194
195 # Return the list of scope names currently active in the task
196 # manager.
197 def current_scope
198 @scope.dup
199 end
200
201 # Evaluate the block in a nested namespace named +name+. Create
202 # an anonymous namespace if +name+ is nil.
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
212
213 private
214
215 # Add a location to the locations field of the given task.
216 def add_location(task)
217 loc = find_location
218 task.locations << loc if loc
219 task
220 end
221
222 # Find the location that called into the dsl layer.
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
232
233 # Generate an anonymous namespace name.
234 def generate_name
235 @seed ||= 0
236 @seed += 1
237 "_anon_#{@seed}"
238 end
239
240 def trace_rule(level, message)
241 $stderr.puts "#{" "*level}#{message}" if Rake.application.options.trace_rules
242 end
243
244 # Attempt to create a rule given the list of prerequisites.
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
264
265 # Make a list of sources from the list of file name extensions /
266 # translation procs.
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
290
291
292 private
293
294 # Return the current description, clearing it in the process.
295 def get_description(task)
296 desc = @last_description
297 @last_description = nil
298 desc
299 end
300
301 class << self
302 attr_accessor :record_task_metadata
303 TaskManager.record_task_metadata = false
304 end
305 end
306
307 end