Class: Rake::Task
- Inherits:
-
Object
- Object
- Rake::Task
- Defined in:
- lib/rake.rb
Overview
A Task is the basic unit of work in a Rakefile. Tasks have associated actions (possibly more than one) and a list of prerequisites. When invoked, a task will first ensure that all of its prerequisites have an opportunity to run and then it will execute its own actions.
Tasks are not usually created directly using the new method, but rather use the file
and task
convenience methods.
Instance Attribute Summary collapse
-
#application ⇒ Object
Application owning this task.
-
#comment ⇒ Object
Comment for this task.
-
#prerequisites ⇒ Object
readonly
List of prerequisites for a task.
-
#scope ⇒ Object
readonly
Array of nested namespaces names used for task lookup by this task.
- #sources ⇒ Object
Class Method Summary collapse
-
.[](task_name) ⇒ Object
Return a task with the given name.
-
.clear ⇒ Object
Clear the task list.
-
.create_rule(args, &block) ⇒ Object
Define a rule for synthesizing tasks.
-
.define_task(args, &block) ⇒ Object
Define a task given
args
and an option block. -
.scope_name(scope, task_name) ⇒ Object
Apply the scope to the task name according to the rules for this kind of task.
-
.task_defined?(task_name) ⇒ Boolean
TRUE if the task name is already defined.
-
.tasks ⇒ Object
List of all defined tasks.
Instance Method Summary collapse
-
#add_comment(comment) ⇒ Object
Add a comment to the task.
-
#enhance(deps = nil, &block) ⇒ Object
Enhance a task with prerequisites or actions.
-
#execute ⇒ Object
Execute the actions associated with this task.
-
#initialize(task_name, app) ⇒ Task
constructor
Create a task named
task_name
with no actions or prerequisites.. -
#investigation ⇒ Object
Return a string describing the internal state of a task.
-
#invoke ⇒ Object
Invoke the task if it is needed.
-
#invoke_prerequisites ⇒ Object
Invoke all the prerequisites of a task.
-
#name ⇒ Object
Name of the task, including any namespace qualifiers.
-
#needed? ⇒ Boolean
Is this task needed?.
-
#source ⇒ Object
First source from a rule (nil if no sources).
-
#timestamp ⇒ Object
Timestamp for this task.
-
#to_s ⇒ Object
Return task name.
Constructor Details
#initialize(task_name, app) ⇒ Task
Create a task named task_name
with no actions or prerequisites.. use enhance
to add actions and prerequisites.
325 326 327 328 329 330 331 332 333 334 |
# File 'lib/rake.rb', line 325 def initialize(task_name, app) @name = task_name.to_s @prerequisites = FileList[] @actions = [] @already_invoked = false @comment = nil @lock = Mutex.new @application = app @scope = app.current_scope end |
Instance Attribute Details
#application ⇒ Object
Application owning this task.
299 300 301 |
# File 'lib/rake.rb', line 299 def application @application end |
#comment ⇒ Object
Comment for this task.
302 303 304 |
# File 'lib/rake.rb', line 302 def comment @comment end |
#prerequisites ⇒ Object (readonly)
List of prerequisites for a task.
296 297 298 |
# File 'lib/rake.rb', line 296 def prerequisites @prerequisites end |
#scope ⇒ Object (readonly)
Array of nested namespaces names used for task lookup by this task.
305 306 307 |
# File 'lib/rake.rb', line 305 def scope @scope end |
#sources ⇒ Object
314 315 316 |
# File 'lib/rake.rb', line 314 def sources @sources ||= [] end |
Class Method Details
.[](task_name) ⇒ Object
Return a task with the given name. If the task is not currently known, try to synthesize one from the defined rules. If no rules are found, but an existing file matches the task name, assume it is a file task with no dependencies or actions.
454 455 456 |
# File 'lib/rake.rb', line 454 def [](task_name) Rake.application[task_name] end |
.clear ⇒ Object
Clear the task list. This cause rake to immediately forget all the tasks that have been assigned. (Normally used in the unit tests.)
441 442 443 |
# File 'lib/rake.rb', line 441 def clear Rake.application.clear end |
.create_rule(args, &block) ⇒ Object
Define a rule for synthesizing tasks.
471 472 473 |
# File 'lib/rake.rb', line 471 def create_rule(args, &block) Rake.application.create_rule(args, &block) end |
.define_task(args, &block) ⇒ Object
Define a task given args
and an option block. If a rule with the given name already exists, the prerequisites and actions are added to the existing task. Returns the defined task.
466 467 468 |
# File 'lib/rake.rb', line 466 def define_task(args, &block) Rake.application.define_task(self, args, &block) end |
.scope_name(scope, task_name) ⇒ Object
Apply the scope to the task name according to the rules for this kind of task. Generic tasks will accept the scope as part of the name.
478 479 480 |
# File 'lib/rake.rb', line 478 def scope_name(scope, task_name) (scope + [task_name]).join(':') end |
.task_defined?(task_name) ⇒ Boolean
TRUE if the task name is already defined.
459 460 461 |
# File 'lib/rake.rb', line 459 def task_defined?(task_name) Rake.application.lookup(task_name) != nil end |
.tasks ⇒ Object
List of all defined tasks.
446 447 448 |
# File 'lib/rake.rb', line 446 def tasks Rake.application.tasks end |
Instance Method Details
#add_comment(comment) ⇒ Object
Add a comment to the task. If a comment alread exists, separate the new comment with “ / ”.
403 404 405 406 407 408 409 410 411 |
# File 'lib/rake.rb', line 403 def add_comment(comment) return if ! comment if @comment @comment << " / " else @comment = '' end @comment << comment end |
#enhance(deps = nil, &block) ⇒ Object
Enhance a task with prerequisites or actions. Returns self.
337 338 339 340 341 |
# File 'lib/rake.rb', line 337 def enhance(deps=nil, &block) @prerequisites |= deps if deps @actions << block if block_given? self end |
#execute ⇒ Object
Execute the actions associated with this task.
378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/rake.rb', line 378 def execute if application..dryrun puts "** Execute (dry run) #{name}" return end if application..trace puts "** Execute #{name}" end application.enhance_with_matching_rule(name) if @actions.empty? @actions.each { |act| result = act.call(self) } end |
#investigation ⇒ Object
Return a string describing the internal state of a task. Useful for debugging.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/rake.rb', line 415 def investigation result = "------------------------------\n" result << "Investigating #{name}\n" result << "class: #{self.class}\n" result << "task needed: #{needed?}\n" result << "timestamp: #{}\n" result << "pre-requisites: \n" prereqs = @prerequisites.collect {|name| Rake::Task[name]} prereqs.sort! {|a,b| a. <=> b.} prereqs.each do |p| result << "--#{p.name} (#{p.})\n" end latest_prereq = @prerequisites.collect{|n| Rake::Task[n].}.max result << "latest-prerequisite time: #{latest_prereq}\n" result << "................................\n\n" return result end |
#invoke ⇒ Object
Invoke the task if it is needed. Prerequites are invoked first.
349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/rake.rb', line 349 def invoke @lock.synchronize do if application..trace puts "** Invoke #{name} #{format_trace_flags}" end return if @already_invoked @already_invoked = true invoke_prerequisites execute if needed? end end |
#invoke_prerequisites ⇒ Object
Invoke all the prerequisites of a task.
362 363 364 365 366 |
# File 'lib/rake.rb', line 362 def invoke_prerequisites @prerequisites.each { |n| application[n, @scope].invoke } end |
#name ⇒ Object
Name of the task, including any namespace qualifiers.
344 345 346 |
# File 'lib/rake.rb', line 344 def name @name.to_s end |
#needed? ⇒ Boolean
Is this task needed?
391 392 393 |
# File 'lib/rake.rb', line 391 def needed? true end |
#source ⇒ Object
First source from a rule (nil if no sources)
319 320 321 |
# File 'lib/rake.rb', line 319 def source @sources.first if defined?(@sources) end |
#timestamp ⇒ Object
Timestamp for this task. Basic tasks return the current time for their time stamp. Other tasks can be more sophisticated.
397 398 399 |
# File 'lib/rake.rb', line 397 def @prerequisites.collect { |p| Rake::Task[p]. }.max || Time.now end |
#to_s ⇒ Object
Return task name
308 309 310 |
# File 'lib/rake.rb', line 308 def to_s name end |