Class Rake::Task
In: lib/rake.rb
Parent: Object

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.

Methods

Attributes

application  [RW]  Application owning this task.
comment  [RW]  Comment for this task.
prerequisites  [R]  List of prerequisites for a task.
scope  [R]  Array of nested namespaces names used for task lookup by this task.
sources  [W]  List of sources for task.

Public Class methods

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.

[Source]

     # File lib/rake.rb, line 466
466:       def [](task_name)
467:         Rake.application[task_name]
468:       end

Clear the task list. This cause rake to immediately forget all the tasks that have been assigned. (Normally used in the unit tests.)

[Source]

     # File lib/rake.rb, line 453
453:       def clear
454:         Rake.application.clear
455:       end

Define a rule for synthesizing tasks.

[Source]

     # File lib/rake.rb, line 483
483:       def create_rule(args, &block)
484:         Rake.application.create_rule(args, &block)
485:       end

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.

[Source]

     # File lib/rake.rb, line 478
478:       def define_task(args, &block)
479:         Rake.application.define_task(self, args, &block)
480:       end

Create a task named task_name with no actions or prerequisites.. use enhance to add actions and prerequisites.

[Source]

     # File lib/rake.rb, line 337
337:     def initialize(task_name, app)
338:       @name = task_name.to_s
339:       @prerequisites = FileList[]
340:       @actions = []
341:       @already_invoked = false
342:       @comment = nil
343:       @lock = Mutex.new
344:       @application = app
345:       @scope = app.current_scope
346:     end

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.

[Source]

     # File lib/rake.rb, line 490
490:       def scope_name(scope, task_name)
491:         (scope + [task_name]).join(':')
492:       end

TRUE if the task name is already defined.

[Source]

     # File lib/rake.rb, line 471
471:       def task_defined?(task_name)
472:         Rake.application.lookup(task_name) != nil
473:       end

List of all defined tasks.

[Source]

     # File lib/rake.rb, line 458
458:       def tasks
459:         Rake.application.tasks
460:       end

Public Instance methods

Add a comment to the task. If a comment alread exists, separate the new comment with " / ".

[Source]

     # File lib/rake.rb, line 415
415:     def add_comment(comment)
416:       return if ! comment
417:       if @comment 
418:         @comment << " / "
419:       else
420:         @comment = ''
421:       end
422:       @comment << comment
423:     end

Enhance a task with prerequisites or actions. Returns self.

[Source]

     # File lib/rake.rb, line 349
349:     def enhance(deps=nil, &block)
350:       @prerequisites |= deps if deps
351:       @actions << block if block_given?
352:       self
353:     end

Execute the actions associated with this task.

[Source]

     # File lib/rake.rb, line 390
390:     def execute
391:       if application.options.dryrun
392:         puts "** Execute (dry run) #{name}"
393:         return
394:       end
395:       if application.options.trace
396:         puts "** Execute #{name}"
397:       end
398:       application.enhance_with_matching_rule(name) if @actions.empty?
399:       @actions.each { |act| result = act.call(self) }
400:     end

Return a string describing the internal state of a task. Useful for debugging.

[Source]

     # File lib/rake.rb, line 427
427:     def investigation
428:       result = "------------------------------\n"
429:       result << "Investigating #{name}\n" 
430:       result << "class: #{self.class}\n"
431:       result <<  "task needed: #{needed?}\n"
432:       result <<  "timestamp: #{timestamp}\n"
433:       result << "pre-requisites: \n"
434:       prereqs = @prerequisites.collect {|name| application[name]}
435:       prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
436:       prereqs.each do |p|
437:         result << "--#{p.name} (#{p.timestamp})\n"
438:       end
439:       latest_prereq = @prerequisites.collect{|n| application[n].timestamp}.max
440:       result <<  "latest-prerequisite time: #{latest_prereq}\n"
441:       result << "................................\n\n"
442:       return result
443:     end

Invoke the task if it is needed. Prerequites are invoked first.

[Source]

     # File lib/rake.rb, line 361
361:     def invoke
362:       @lock.synchronize do
363:         if application.options.trace
364:           puts "** Invoke #{name} #{format_trace_flags}"
365:         end
366:         return if @already_invoked
367:         @already_invoked = true
368:         invoke_prerequisites
369:         execute if needed?
370:       end
371:     end

Invoke all the prerequisites of a task.

[Source]

     # File lib/rake.rb, line 374
374:     def invoke_prerequisites
375:       @prerequisites.each { |n|
376:         application[n, @scope].invoke
377:       }
378:     end

Name of the task, including any namespace qualifiers.

[Source]

     # File lib/rake.rb, line 356
356:     def name
357:       @name.to_s
358:     end

Is this task needed?

[Source]

     # File lib/rake.rb, line 403
403:     def needed?
404:       true
405:     end

First source from a rule (nil if no sources)

[Source]

     # File lib/rake.rb, line 331
331:     def source
332:       @sources.first if defined?(@sources)
333:     end

[Source]

     # File lib/rake.rb, line 326
326:     def sources
327:       @sources ||= []
328:     end

Timestamp for this task. Basic tasks return the current time for their time stamp. Other tasks can be more sophisticated.

[Source]

     # File lib/rake.rb, line 409
409:     def timestamp
410:       @prerequisites.collect { |p| application[p].timestamp }.max || Time.now
411:     end

Return task name

[Source]

     # File lib/rake.rb, line 320
320:     def to_s
321:       name
322:     end

[Validate]