Class
Rake::Task
In: lib/rake/task.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.

Methods

[], add_description, arg_names, clear, clear, clear_actions, clear_prerequisites, comment=, create_rule, define_task, enhance, execute, inspect, investigation, invoke, name, needed?, new, prerequisite_tasks, reenable, scope_name, set_arg_names, source, sources, task_defined?, tasks, timestamp, to_s,
Attributes

 [R]  actions List of actions attached to a task.
 [RW]  application Application owning this task.
 [R]  comment Comment for this task. Restricted to a single line of no more than 50 characters.
 [R]  full_comment Full text of the (possibly multi-line) comment.
 [R]  locations File/Line locations of each of the task definitions for this task (only valid if the task was defined with the detect location option set).
 [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/task.rb, line 297
297:       def [](task_name)
298:         Rake.application[task_name]
299:       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/task.rb, line 284
284:       def clear
285:         Rake.application.clear
286:       end
create_rule(*args, &block)

Define a rule for synthesizing tasks.

     # File lib/rake/task.rb, line 314
314:       def create_rule(*args, &block)
315:         Rake.application.create_rule(*args, &block)
316:       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/task.rb, line 309
309:       def define_task(*args, &block)
310:         Rake.application.define_task(self, *args, &block)
311:       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/task.rb, line 71
71:     def initialize(task_name, app)
72:       @name = task_name.to_s
73:       @prerequisites = []
74:       @actions = []
75:       @already_invoked = false
76:       @full_comment = nil
77:       @comment = nil
78:       @lock = Monitor.new
79:       @application = app
80:       @scope = app.current_scope
81:       @arg_names = nil
82:       @locations = []
83:     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/task.rb, line 321
321:       def scope_name(scope, task_name)
322:         (scope + [task_name]).join(':')
323:       end
task_defined?(task_name)

TRUE if the task name is already defined.

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

List of all defined tasks.

     # File lib/rake/task.rb, line 289
289:       def tasks
290:         Rake.application.tasks
291:       end
Public Instance methods
add_description(description)

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

     # File lib/rake/task.rb, line 223
223:     def add_description(description)
224:       return if ! description
225:       comment = description.strip
226:       add_comment(comment) if comment && ! comment.empty?
227:     end
arg_names()

Name of arguments for this task.

     # File lib/rake/task.rb, line 112
112:     def arg_names
113:       @arg_names || []
114:     end
clear()

Clear the existing prerequisites and actions of a rake task.

     # File lib/rake/task.rb, line 123
123:     def clear
124:       clear_prerequisites
125:       clear_actions
126:       self
127:     end
clear_actions()

Clear the existing actions on a rake task.

     # File lib/rake/task.rb, line 136
136:     def clear_actions
137:       actions.clear
138:       self
139:     end
clear_prerequisites()

Clear the existing prerequisites of a rake task.

     # File lib/rake/task.rb, line 130
130:     def clear_prerequisites
131:       prerequisites.clear
132:       self
133:     end
comment=(description)

Writing to the comment attribute is the same as adding a description.

     # File lib/rake/task.rb, line 230
230:     def comment=(description)
231:       add_description(description)
232:     end
enhance(deps=nil, &block)

Enhance a task with prerequisites or actions. Returns self.

    # File lib/rake/task.rb, line 86
86:     def enhance(deps=nil, &block)
87:       @prerequisites |= deps if deps
88:       @actions << block if block_given?
89:       self
90:     end
execute(args=nil)

Execute the actions associated with this task.

     # File lib/rake/task.rb, line 190
190:     def execute(args=nil)
191:       args ||= EMPTY_TASK_ARGS
192:       if application.options.dryrun
193:         puts "** Execute (dry run) #{name}"
194:         return
195:       end
196:       if application.options.trace
197:         puts "** Execute #{name}"
198:       end
199:       application.enhance_with_matching_rule(name) if @actions.empty?
200:       @actions.each do |act|
201:         case act.arity
202:         when 1
203:           act.call(self)
204:         else
205:           act.call(self, args)
206:         end
207:       end
208:     end
inspect()
    # File lib/rake/task.rb, line 44
44:     def inspect
45:       "<#{self.class} #{name} => [#{prerequisites.join(', ')}]>"
46:     end
investigation()

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

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

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

     # File lib/rake/task.rb, line 142
142:     def invoke(*args)
143:       task_args = TaskArguments.new(arg_names, args)
144:       invoke_with_call_chain(task_args, InvocationChain::EMPTY)
145:     end
name()

Name of the task, including any namespace qualifiers.

    # File lib/rake/task.rb, line 93
93:     def name
94:       @name.to_s
95:     end
needed?()

Is this task needed?

     # File lib/rake/task.rb, line 211
211:     def needed?
212:       true
213:     end
prerequisite_tasks()

List of prerequisite tasks

    # File lib/rake/task.rb, line 55
55:     def prerequisite_tasks
56:       prerequisites.collect { |pre| lookup_prerequisite(pre) }
57:     end
reenable()

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

     # File lib/rake/task.rb, line 118
118:     def reenable
119:       @already_invoked = false
120:     end
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.

     # File lib/rake/task.rb, line 253
253:     def set_arg_names(args)
254:       @arg_names = args.map { |a| a.to_sym }
255:     end
source()

First source from a rule (nil if no sources)

    # File lib/rake/task.rb, line 65
65:     def source
66:       @sources.first if defined?(@sources)
67:     end
sources()
    # File lib/rake/task.rb, line 50
50:     def sources
51:       @sources ||= []
52:     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/task.rb, line 217
217:     def timestamp
218:       prerequisite_tasks.collect { |pre| pre.timestamp }.max || Time.now
219:     end
to_s()

Return task name

    # File lib/rake/task.rb, line 40
40:     def to_s
41:       name
42:     end