Class: Rake::Task

Inherits:
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.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Task) initialize(task_name, app)

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



509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/rake.rb', line 509

def initialize(task_name, app)
  @name = task_name.to_s
  @prerequisites = []
  @actions = []
  @already_invoked = false
  @full_comment = nil
  @comment = nil
  @lock = Monitor.new
  @application = app
  @scope = app.current_scope
  @arg_names = nil
end

Instance Attribute Details

- (Object) actions (readonly)

List of actions attached to a task.



472
473
474
# File 'lib/rake.rb', line 472

def actions
  @actions
end

- (Object) application

Application owning this task.



475
476
477
# File 'lib/rake.rb', line 475

def application
  @application
end

- (Object) comment

Comment for this task. Restricted to a single line of no more than 50 characters.



479
480
481
# File 'lib/rake.rb', line 479

def comment
  @comment
end

- (Object) full_comment (readonly)

Full text of the (possibly multi-line) comment.



482
483
484
# File 'lib/rake.rb', line 482

def full_comment
  @full_comment
end

- (Object) prerequisites (readonly)

List of prerequisites for a task.



469
470
471
# File 'lib/rake.rb', line 469

def prerequisites
  @prerequisites
end

- (Object) scope (readonly)

Array of nested namespaces names used for task lookup by this task.



485
486
487
# File 'lib/rake.rb', line 485

def scope
  @scope
end

- (Object) sources



498
499
500
# File 'lib/rake.rb', line 498

def sources
  @sources ||= []
end

Class Method Details

+ (Object) [](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.



726
727
728
# File 'lib/rake.rb', line 726

def [](task_name)
  Rake.application[task_name]
end

+ (Object) clear

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



713
714
715
# File 'lib/rake.rb', line 713

def clear
  Rake.application.clear
end

+ (Object) create_rule(*args, &block)

Define a rule for synthesizing tasks.



743
744
745
# File 'lib/rake.rb', line 743

def create_rule(*args, &block)
  Rake.application.create_rule(*args, &block)
end

+ (Object) 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.



738
739
740
# File 'lib/rake.rb', line 738

def define_task(*args, &block)
  Rake.application.define_task(self, *args, &block)
end

+ (Object) 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.



750
751
752
# File 'lib/rake.rb', line 750

def scope_name(scope, task_name)
  (scope + [task_name]).join(':')
end

+ (Boolean) task_defined?(task_name)

TRUE if the task name is already defined.

Returns:

  • (Boolean)


731
732
733
# File 'lib/rake.rb', line 731

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

+ (Object) tasks

List of all defined tasks.



718
719
720
# File 'lib/rake.rb', line 718

def tasks
  Rake.application.tasks
end

Instance Method Details

- (Object) add_description(description)

Add a description to the task. The description can consist of an option argument list (enclosed brackets) and an optional comment.



652
653
654
655
656
# File 'lib/rake.rb', line 652

def add_description(description)
  return if ! description
  comment = description.strip
  add_comment(comment) if comment && ! comment.empty?
end

- (Object) arg_description

Argument description (nil if none).



544
545
546
# File 'lib/rake.rb', line 544

def arg_description # :nodoc:
  @arg_names ? "[#{(arg_names || []).join(',')}]" : nil
end

- (Object) arg_names

Name of arguments for this task.



549
550
551
# File 'lib/rake.rb', line 549

def arg_names
  @arg_names || []
end

- (Object) clear

Clear the existing prerequisites and actions of a rake task.



560
561
562
563
564
# File 'lib/rake.rb', line 560

def clear
  clear_prerequisites
  clear_actions
  self
end

- (Object) clear_actions

Clear the existing actions on a rake task.



573
574
575
576
# File 'lib/rake.rb', line 573

def clear_actions
  actions.clear
  self
end

- (Object) clear_prerequisites

Clear the existing prerequisites of a rake task.



567
568
569
570
# File 'lib/rake.rb', line 567

def clear_prerequisites
  prerequisites.clear
  self
end

- (Object) enhance(deps = nil, &block)

Enhance a task with prerequisites or actions. Returns self.



523
524
525
526
527
# File 'lib/rake.rb', line 523

def enhance(deps=nil, &block)
  @prerequisites |= deps if deps
  @actions << block if block_given?
  self
end

- (Object) execute(args = nil)

Execute the actions associated with this task.



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/rake.rb', line 619

def execute(args=nil)
  args ||= EMPTY_TASK_ARGS
  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 do |act|
    case act.arity
    when 1
      act.call(self)
    else
      act.call(self, args)
    end
  end
end

- (Object) inspect



492
493
494
# File 'lib/rake.rb', line 492

def inspect
  "<#{self.class} #{name} => [#{prerequisites.join(', ')}]>"
end

- (Object) investigation

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



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/rake.rb', line 688

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| application[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| application[n].timestamp}.max
  result <<  "latest-prerequisite time: #{latest_prereq}\n"
  result << "................................\n\n"
  return result
end

- (Object) invoke(*args)

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



579
580
581
582
# File 'lib/rake.rb', line 579

def invoke(*args)
  task_args = TaskArguments.new(arg_names, args)
  invoke_with_call_chain(task_args, InvocationChain::EMPTY)
end

- (Object) invoke_prerequisites(task_args, invocation_chain)

Invoke all the prerequisites of a task.



601
602
603
604
605
606
607
# File 'lib/rake.rb', line 601

def invoke_prerequisites(task_args, invocation_chain) # :nodoc:
  @prerequisites.each { |n|
    prereq = application[n, @scope]
    prereq_args = task_args.new_scope(prereq.arg_names)
    prereq.invoke_with_call_chain(prereq_args, invocation_chain)
  }
end

- (Object) name

Name of the task, including any namespace qualifiers.



530
531
532
# File 'lib/rake.rb', line 530

def name
  @name.to_s
end

- (Object) name_with_args

Name of task with argument list description.



535
536
537
538
539
540
541
# File 'lib/rake.rb', line 535

def name_with_args # :nodoc:
  if arg_description
    "#{name}#{arg_description}"
  else
    name
  end
end

- (Boolean) needed?

Is this task needed?

Returns:

  • (Boolean)


640
641
642
# File 'lib/rake.rb', line 640

def needed?
  true
end

- (Object) reenable

Reenable the task, allowing its tasks to be executed if the task is invoked again.



555
556
557
# File 'lib/rake.rb', line 555

def reenable
  @already_invoked = false
end

- (Object) set_arg_names(args)

Set the names of the arguments for this task. args should be an array of symbols, one for each argument name.



682
683
684
# File 'lib/rake.rb', line 682

def set_arg_names(args)
  @arg_names = args.map { |a| a.to_sym }
end

- (Object) source

First source from a rule (nil if no sources)



503
504
505
# File 'lib/rake.rb', line 503

def source
  @sources.first if defined?(@sources)
end

- (Object) timestamp

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



646
647
648
# File 'lib/rake.rb', line 646

def timestamp
  @prerequisites.collect { |p| application[p].timestamp }.max || Time.now
end

- (Object) to_s

Return task name



488
489
490
# File 'lib/rake.rb', line 488

def to_s
  name
end