| 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.
| actions | [R] | List of actions attached to a task. |
| application | [RW] | Application owning this task. |
| comment | [R] | Comment for this task. Restricted to a single line of no more than 50 characters. |
| full_comment | [R] | Full text of the (possibly multi-line) comment. |
| 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. |
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 709
709: def [](task_name)
710: Rake.application[task_name]
711: end
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 721
721: def define_task(*args, &block)
722: Rake.application.define_task(self, *args, &block)
723: end
Create a task named task_name with no actions or prerequisites. Use enhance to add actions and prerequisites.
# File lib/rake.rb, line 492
492: def initialize(task_name, app)
493: @name = task_name.to_s
494: @prerequisites = []
495: @actions = []
496: @already_invoked = false
497: @full_comment = nil
498: @comment = nil
499: @lock = Monitor.new
500: @application = app
501: @scope = app.current_scope
502: @arg_names = nil
503: end
Add a description to the task. The description can consist of an option argument list (enclosed brackets) and an optional comment.
# File lib/rake.rb, line 635
635: def add_description(description)
636: return if ! description
637: comment = description.strip
638: add_comment(comment) if comment && ! comment.empty?
639: end
Name of arguments for this task.
# File lib/rake.rb, line 532
532: def arg_names
533: @arg_names || []
534: end
Clear the existing prerequisites and actions of a rake task.
# File lib/rake.rb, line 543
543: def clear
544: clear_prerequisites
545: clear_actions
546: self
547: end
Clear the existing actions on a rake task.
# File lib/rake.rb, line 556
556: def clear_actions
557: actions.clear
558: self
559: end
Clear the existing prerequisites of a rake task.
# File lib/rake.rb, line 550
550: def clear_prerequisites
551: prerequisites.clear
552: self
553: end
Writing to the comment attribute is the same as adding a description.
# File lib/rake.rb, line 642
642: def comment=(description)
643: add_description(description)
644: end
Enhance a task with prerequisites or actions. Returns self.
# File lib/rake.rb, line 506
506: def enhance(deps=nil, &block)
507: @prerequisites |= deps if deps
508: @actions << block if block_given?
509: self
510: end
Execute the actions associated with this task.
# File lib/rake.rb, line 602
602: def execute(args=nil)
603: args ||= EMPTY_TASK_ARGS
604: if application.options.dryrun
605: puts "** Execute (dry run) #{name}"
606: return
607: end
608: if application.options.trace
609: puts "** Execute #{name}"
610: end
611: application.enhance_with_matching_rule(name) if @actions.empty?
612: @actions.each do |act|
613: case act.arity
614: when 1
615: act.call(self)
616: else
617: act.call(self, args)
618: end
619: end
620: end
# File lib/rake.rb, line 475
475: def inspect
476: "<#{self.class} #{name} => [#{prerequisites.join(', ')}]>"
477: end
Return a string describing the internal state of a task. Useful for debugging.
# File lib/rake.rb, line 671
671: def investigation
672: result = "------------------------------\n"
673: result << "Investigating #{name}\n"
674: result << "class: #{self.class}\n"
675: result << "task needed: #{needed?}\n"
676: result << "timestamp: #{timestamp}\n"
677: result << "pre-requisites: \n"
678: prereqs = @prerequisites.collect {|name| application[name]}
679: prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
680: prereqs.each do |p|
681: result << "--#{p.name} (#{p.timestamp})\n"
682: end
683: latest_prereq = @prerequisites.collect{|n| application[n].timestamp}.max
684: result << "latest-prerequisite time: #{latest_prereq}\n"
685: result << "................................\n\n"
686: return result
687: end
Invoke the task if it is needed. Prerequites are invoked first.
# File lib/rake.rb, line 562
562: def invoke(*args)
563: task_args = TaskArguments.new(arg_names, args)
564: invoke_with_call_chain(task_args, InvocationChain::EMPTY)
565: end
Name of the task, including any namespace qualifiers.
# File lib/rake.rb, line 513
513: def name
514: @name.to_s
515: end