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

[] add_comment clear create_rule define_task enhance execute investigation invoke invoke_prerequisites name needed? new scope_name source sources task_defined? tasks timestamp to_s

### 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

[](task_name)

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 458
    458:       def [](task_name)
    459:         Rake.application[task_name]
    460:       end

clear()

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 445
    445:       def clear
    446:         Rake.application.clear
    447:       end

create_rule(args, &block)

Define a rule for synthesizing tasks.

[Source]

         # File lib/rake.rb, line 475
    475:       def create_rule(args, &block)
    476:         Rake.application.create_rule(args, &block)
    477:       end

define_task(args, &block)

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 470
    470:       def define_task(args, &block)
    471:         Rake.application.define_task(self, args, &block)
    472:       end

new(task_name, app)

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

[Source]

         # File lib/rake.rb, line 330
    330:     def initialize(task_name, app)
    331:       @name = task_name.to_s
    332:       @prerequisites = FileList[]
    333:       @actions = []
    334:       @already_invoked = false
    335:       @comment = nil
    336:       @lock = Mutex.new
    337:       @application = app
    338:       @scope = app.current_scope
    339:     end

scope_name(scope, task_name)

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 482
    482:       def scope_name(scope, task_name)
    483:         (scope + [task_name]).join(':')
    484:       end

task_defined?(task_name)

TRUE if the task name is already defined.

[Source]

         # File lib/rake.rb, line 463
    463:       def task_defined?(task_name)
    464:         Rake.application.lookup(task_name) != nil
    465:       end

tasks()

List of all defined tasks.

[Source]

         # File lib/rake.rb, line 450
    450:       def tasks
    451:         Rake.application.tasks
    452:       end

### Public Instance methods

add_comment(comment)

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

[Source]

         # File lib/rake.rb, line 408
    408:     def add_comment(comment)
    409:       return if ! comment
    410:       if @comment
    411:         @comment << " / "
    412:       else
    413:         @comment = ''
    414:       end
    415:       @comment << comment
    416:     end

enhance(deps=nil, &block)

Enhance a task with prerequisites or actions. Returns self.

[Source]

         # File lib/rake.rb, line 342
    342:     def enhance(deps=nil, &block)
    343:       @prerequisites |= deps if deps
    344:       @actions << block if block_given?
    345:       self
    346:     end

execute()

Execute the actions associated with this task.

[Source]

         # File lib/rake.rb, line 383
    383:     def execute
    384:       if application.options.dryrun
    385:         puts "** Execute (dry run) #{name}"
    386:         return
    387:       end
    388:       if application.options.trace
    389:         puts "** Execute #{name}"
    390:       end
    391:       application.enhance_with_matching_rule(name) if @actions.empty?
    392:       @actions.each { |act| result = act.call(self) }
    393:     end

investigation()

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

[Source]

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

invoke()

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

[Source]

         # File lib/rake.rb, line 354
    354:     def invoke
    355:       @lock.synchronize do
    356:         if application.options.trace
    357:           puts "** Invoke #{name} #{format_trace_flags}"
    358:         end
    359:         return if @already_invoked
    360:         @already_invoked = true
    361:         invoke_prerequisites
    362:         execute if needed?
    363:       end
    364:     end

invoke_prerequisites()

Invoke all the prerequisites of a task.

[Source]

         # File lib/rake.rb, line 367
    367:     def invoke_prerequisites
    368:       @prerequisites.each { |n|
    369:         application[n, @scope].invoke
    370:       }
    371:     end

name()

Name of the task, including any namespace qualifiers.

[Source]

         # File lib/rake.rb, line 349
    349:     def name
    350:       @name.to_s
    351:     end

needed?()

Is this task needed?

[Source]

         # File lib/rake.rb, line 396
    396:     def needed?
    397:       true
    398:     end

source()

First source from a rule (nil if no sources)

[Source]

         # File lib/rake.rb, line 324
    324:     def source
    325:       @sources.first if defined?(@sources)
    326:     end

sources()

[Source]

         # File lib/rake.rb, line 319
    319:     def sources
    320:       @sources ||= []
    321:     end

timestamp()

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 402
    402:     def timestamp
    403:       @prerequisites.collect { |p| application[p].timestamp }.max || Time.now
    404:     end

to_s()

Return task name

[Source]

         # File lib/rake.rb, line 313
    313:     def to_s
    314:       name
    315:     end

[Validate]