module Rake::TaskManager
The TaskManager
module is a mixin for managing tasks.
Attributes
Track the last comment made in the Rakefile.
Track the last comment made in the Rakefile.
Public Class Methods
Source
# 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
Source
# 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
Find a matching task for task_name
.
Source
# File lib/rake/task_manager.rb 157 def clear 158 @tasks.clear 159 @rules.clear 160 end
Clear all tasks in this application.
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
Source
# File lib/rake/task_manager.rb 197 def current_scope 198 @scope.dup 199 end
Return the list of scope names currently active in the task manager.
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
Source
# 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
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.
Source
# 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
Evaluate the block in a nested namespace named name
. Create an anonymous namespace if name
is nil.
Source
# 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 a task. Return an existing task if found, otherwise create a task of the current type.
Source
# 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
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.
Source
# 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
Resolve the arguments for a task/rule. Returns a triplet of [task_name, arg_name_list, prerequisites].
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
Source
# File lib/rake/task_manager.rb 143 def tasks 144 @tasks.values.sort_by { |t| t.name } 145 end
List of all defined tasks in this application.
Source
# 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
List of all the tasks defined in the given scope (and its sub-scopes).
Private Instance Methods
Source
# 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
Add a location to the locations field of the given task.
Source
# 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
Attempt to create a rule given the list of prerequisites.
Source
# 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
Find the location that called into the dsl layer.
Source
# File lib/rake/task_manager.rb 234 def generate_name 235 @seed ||= 0 236 @seed += 1 237 "_anon_#{@seed}" 238 end
Generate an anonymous namespace name.
Source
# File lib/rake/task_manager.rb 295 def get_description(task) 296 desc = @last_description 297 @last_description = nil 298 desc 299 end
Return the current description, clearing it in the process.
Source
# 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
Lookup the task name
Source
# 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
Make a list of sources from the list of file name extensions / translation procs.
Source
# 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
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)
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