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
[RW] application Application owning this task.
[RW] comment Comment for this task.
[R] prerequisites List of prerequisites for a task.
[R] scope Array of nested namespaces names used for task lookup by this task.
[W] sources 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.

     # File lib/rake.rb, line 454
454:       def [](task_name)
455:         Rake.application[task_name]
456:       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.)

     # File lib/rake.rb, line 441
441:       def clear
442:         Rake.application.clear
443:       end
create_rule(args, &block)

Define a rule for synthesizing tasks.

     # File lib/rake.rb, line 471
471:       def create_rule(args, &block)
472:         Rake.application.create_rule(args, &block)
473:       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.

     # File lib/rake.rb, line 466
466:       def define_task(args, &block)
467:         Rake.application.define_task(self, args, &block)
468:       end
new(task_name, app)

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

     # File lib/rake.rb, line 325
325:     def initialize(task_name, app)
326:       @name = task_name.to_s
327:       @prerequisites = FileList[]
328:       @actions = []
329:       @already_invoked = false
330:       @comment = nil
331:       @lock = Mutex.new
332:       @application = app
333:       @scope = app.current_scope
334:     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.

     # File lib/rake.rb, line 478
478:       def scope_name(scope, task_name)
479:         (scope + [task_name]).join(':')
480:       end
task_defined?(task_name)

TRUE if the task name is already defined.

     # File lib/rake.rb, line 459
459:       def task_defined?(task_name)
460:         Rake.application.lookup(task_name) != nil
461:       end
tasks()

List of all defined tasks.

     # File lib/rake.rb, line 446
446:       def tasks
447:         Rake.application.tasks
448:       end
Public Instance methods
add_comment(comment)

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

     # File lib/rake.rb, line 403
403:     def add_comment(comment)
404:       return if ! comment
405:       if @comment 
406:         @comment << " / "
407:       else
408:         @comment = ''
409:       end
410:       @comment << comment
411:     end
enhance(deps=nil, &block)

Enhance a task with prerequisites or actions. Returns self.

     # File lib/rake.rb, line 337
337:     def enhance(deps=nil, &block)
338:       @prerequisites |= deps if deps
339:       @actions << block if block_given?
340:       self
341:     end
execute()

Execute the actions associated with this task.

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

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

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

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

     # File lib/rake.rb, line 349
349:     def invoke
350:       @lock.synchronize do
351:         if application.options.trace
352:           puts "** Invoke #{name} #{format_trace_flags}"
353:         end
354:         return if @already_invoked
355:         @already_invoked = true
356:         invoke_prerequisites
357:         execute if needed?
358:       end
359:     end
invoke_prerequisites()

Invoke all the prerequisites of a task.

     # File lib/rake.rb, line 362
362:     def invoke_prerequisites
363:       @prerequisites.each { |n|
364:         application[n, @scope].invoke
365:       }
366:     end
name()

Name of the task, including any namespace qualifiers.

     # File lib/rake.rb, line 344
344:     def name
345:       @name.to_s
346:     end
needed?()

Is this task needed?

     # File lib/rake.rb, line 391
391:     def needed?
392:       true
393:     end
source()

First source from a rule (nil if no sources)

     # File lib/rake.rb, line 319
319:     def source
320:       @sources.first if defined?(@sources)
321:     end
sources()
     # File lib/rake.rb, line 314
314:     def sources
315:       @sources ||= []
316:     end
timestamp()

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

     # File lib/rake.rb, line 397
397:     def timestamp
398:       @prerequisites.collect { |p| Rake::Task[p].timestamp }.max || Time.now
399:     end
to_s()

Return task name

     # File lib/rake.rb, line 308
308:     def to_s
309:       name
310:     end