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 299
299:       def [](task_name)
300:         Rake.application[task_name]
301:       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 286
286:       def clear
287:         Rake.application.clear
288:       end
create_rule(args, &block)

Define a rule for synthesizing tasks.

     # File lib/rake.rb, line 316
316:       def create_rule(args, &block)
317:         Rake.application.create_rule(args, &block)
318:       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 311
311:       def define_task(args, &block)
312:         Rake.application.define_task(self, args, &block)
313:       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 170
170:     def initialize(task_name, app)
171:       @name = task_name.to_s
172:       @prerequisites = FileList[]
173:       @actions = []
174:       @already_invoked = false
175:       @comment = nil
176:       @lock = Mutex.new
177:       @application = app
178:       @scope = app.current_scope
179:     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 323
323:       def scope_name(scope, task_name)
324:         (scope + [task_name]).join(':')
325:       end
task_defined?(task_name)

TRUE if the task name is already defined.

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

List of all defined tasks.

     # File lib/rake.rb, line 291
291:       def tasks
292:         Rake.application.tasks
293:       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 248
248:     def add_comment(comment)
249:       return if ! comment
250:       if @comment 
251:         @comment << " / "
252:       else
253:         @comment = ''
254:       end
255:       @comment << comment
256:     end
enhance(deps=nil, &block)

Enhance a task with prerequisites or actions. Returns self.

     # File lib/rake.rb, line 182
182:     def enhance(deps=nil, &block)
183:       @prerequisites |= deps if deps
184:       @actions << block if block_given?
185:       self
186:     end
execute()

Execute the actions associated with this task.

     # File lib/rake.rb, line 223
223:     def execute
224:       if application.options.dryrun
225:         puts "** Execute (dry run) #{name}"
226:         return
227:       end
228:       if application.options.trace
229:         puts "** Execute #{name}"
230:       end
231:       application.enhance_with_matching_rule(name) if @actions.empty?
232:       @actions.each { |act| result = act.call(self) }
233:     end
investigation()

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

     # File lib/rake.rb, line 260
260:     def investigation
261:       result = "------------------------------\n"
262:       result << "Investigating #{name}\n" 
263:       result << "class: #{self.class}\n"
264:       result <<  "task needed: #{needed?}\n"
265:       result <<  "timestamp: #{timestamp}\n"
266:       result << "pre-requisites: \n"
267:       prereqs = @prerequisites.collect {|name| Rake::Task[name]}
268:       prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
269:       prereqs.each do |p|
270:         result << "--#{p.name} (#{p.timestamp})\n"
271:       end
272:       latest_prereq = @prerequisites.collect{|n| Rake::Task[n].timestamp}.max
273:       result <<  "latest-prerequisite time: #{latest_prereq}\n"
274:       result << "................................\n\n"
275:       return result
276:     end
invoke()

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

     # File lib/rake.rb, line 194
194:     def invoke
195:       @lock.synchronize do
196:         if application.options.trace
197:           puts "** Invoke #{name} #{format_trace_flags}"
198:         end
199:         return if @already_invoked
200:         @already_invoked = true
201:         invoke_prerequisites
202:         execute if needed?
203:       end
204:     end
invoke_prerequisites()

Invoke all the prerequisites of a task.

     # File lib/rake.rb, line 207
207:     def invoke_prerequisites
208:       @prerequisites.each { |n|
209:         application[n, @scope].invoke
210:       }
211:     end
name()

Name of the task, including any namespace qualifiers.

     # File lib/rake.rb, line 189
189:     def name
190:       @name.to_s
191:     end
needed?()

Is this task needed?

     # File lib/rake.rb, line 236
236:     def needed?
237:       true
238:     end
source()

First source from a rule (nil if no sources)

     # File lib/rake.rb, line 164
164:     def source
165:       @sources.first if defined?(@sources)
166:     end
sources()
     # File lib/rake.rb, line 159
159:     def sources
160:       @sources ||= []
161:     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 242
242:     def timestamp
243:       @prerequisites.collect { |p| Rake::Task[p].timestamp }.max || Time.now
244:     end
to_s()

Return task name

     # File lib/rake.rb, line 153
153:     def to_s
154:       name
155:     end