Class: Rake::Task

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

FileTask, MultiTask

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#applicationObject

Application owning this task.



299
300
301
# File 'lib/rake.rb', line 299

def application
  @application
end

#commentObject

Comment for this task.



302
303
304
# File 'lib/rake.rb', line 302

def comment
  @comment
end

#prerequisitesObject (readonly)

List of prerequisites for a task.



296
297
298
# File 'lib/rake.rb', line 296

def prerequisites
  @prerequisites
end

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

#sourcesObject



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

.clearObject

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.

Returns:

  • (Boolean)


459
460
461
# File 'lib/rake.rb', line 459

def task_defined?(task_name)
	Rake.application.lookup(task_name) != nil
end

.tasksObject

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

#executeObject

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.options.dryrun
	puts "** Execute (dry run) #{name}"
	return
  end
  if application.options.trace
	puts "** Execute #{name}"
  end
  application.enhance_with_matching_rule(name) if @actions.empty?
  @actions.each { |act| result = act.call(self) }
end

#investigationObject

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: #{timestamp}\n"
  result << "pre-requisites: \n"
  prereqs = @prerequisites.collect {|name| Rake::Task[name]}
  prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
  prereqs.each do |p|
	result << "--#{p.name} (#{p.timestamp})\n"
  end
  latest_prereq = @prerequisites.collect{|n| Rake::Task[n].timestamp}.max
  result <<  "latest-prerequisite time: #{latest_prereq}\n"
  result << "................................\n\n"
  return result
end

#invokeObject

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.options.trace
	  puts "** Invoke #{name} #{format_trace_flags}"
	end
	return if @already_invoked
	@already_invoked = true
	invoke_prerequisites
	execute if needed?
  end
end

#invoke_prerequisitesObject

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

#nameObject

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?

Returns:

  • (Boolean)


391
392
393
# File 'lib/rake.rb', line 391

def needed?
  true
end

#sourceObject

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

#timestampObject

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 timestamp
  @prerequisites.collect { |p| Rake::Task[p].timestamp }.max || Time.now
end

#to_sObject

Return task name



308
309
310
# File 'lib/rake.rb', line 308

def to_s
  name
end