Class: 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

Constant Summary collapse

TASKS =
Hash.new
RULES =
Array.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name) ⇒ Task

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



72
73
74
75
76
77
78
# File 'lib/rake.rb', line 72

def initialize(task_name)
  @name = task_name
  @prerequisites = []
  @actions = []
  @already_invoked = false
  @comment = nil
end

Instance Attribute Details

#commentObject

Comment for this task.



64
65
66
# File 'lib/rake.rb', line 64

def comment
  @comment
end

#prerequisitesObject (readonly)

List of prerequisites for a task.



61
62
63
# File 'lib/rake.rb', line 61

def prerequisites
  @prerequisites
end

#sourceObject

Source dependency for rule synthesized tasks. Nil if task was not sythesized from a rule.



68
69
70
# File 'lib/rake.rb', line 68

def source
  @source
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.



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rake.rb', line 170

def [](task_name)
  task_name = task_name.to_s
  if task = TASKS[task_name]
	return task
  end
  if task = enhance_with_matching_rule(task_name)
	return task
  end
  if File.exist?(task_name)
	return FileTask.define_task(task_name)
  end
  fail "Don't know how to build task '#{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.)



156
157
158
159
# File 'lib/rake.rb', line 156

def clear
  TASKS.clear
  RULES.clear
end

.create_rule(args, &block) ⇒ Object

Define a rule for synthesizing tasks.



203
204
205
206
207
208
# File 'lib/rake.rb', line 203

def create_rule(args, &block)
  pattern, deps = resolve_args(args)
  fail "Too many dependents specified in rule #{pattern}: #{deps.inspect}" if deps.size > 1
  pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
  RULES << [pattern, deps, 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.



193
194
195
196
197
198
199
200
# File 'lib/rake.rb', line 193

def define_task(args, &block)
  task_name, deps = resolve_args(args)
  deps = [deps] if (Symbol === deps) || (String === deps)
  deps = deps.collect {|d| d.to_s }
  t = lookup(task_name)
  t.add_comment($last_comment)
  t.enhance(deps, &block)
end

.enhance_with_matching_rule(task_name) ⇒ Object

If a rule can be found that matches the task name, enhance the task with the prerequisites and actions from the rule. Set the source attribute of the task appropriately for the rule. Return the enhanced task or nil of no rule was found.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rake.rb', line 222

def enhance_with_matching_rule(task_name)
  RULES.each do |pattern, extensions, block|
	if md = pattern.match(task_name)
	  ext = extensions.first
	  case ext
	  when String
 source = task_name.sub(/\.[^.]*$/, ext)
	  when Proc
 source = ext.call(task_name)
	  else
 fail "Don't know how to handle rule dependent: #{ext.inspect}"
	  end
	  if File.exist?(source)
 task = FileTask.define_task({task_name => [source]}, &block)
 task.source = source
 return task
	  end
	end
  end
  nil
end

.lookup(task_name) ⇒ Object

Lookup a task. Return an existing task if found, otherwise create a task of the current type.



213
214
215
216
# File 'lib/rake.rb', line 213

def lookup(task_name)
  name = task_name.to_s
  TASKS[name] ||= self.new(task_name)
end

.task_defined?(task_name) ⇒ Boolean

TRUE if the task name is already defined.

Returns:

  • (Boolean)


185
186
187
188
# File 'lib/rake.rb', line 185

def task_defined?(task_name)
  task_name = task_name.to_s
  TASKS[task_name]
end

.tasksObject

List of all defined tasks.



162
163
164
# File 'lib/rake.rb', line 162

def tasks
  TASKS.keys.sort.collect { |tn| Task[tn] }
end

Instance Method Details

#add_comment(comment) ⇒ Object

Add a comment to the task. If a comment alread exists, separate the new comment with “ / ”.



138
139
140
141
142
143
144
145
146
147
# File 'lib/rake.rb', line 138

def add_comment(comment)
  return if ! $last_comment
  if @comment 
    @comment << " / "
  else
    @comment = ''
  end
  @comment << $last_comment
  $last_comment = nil
end

#enhance(deps = nil, &block) ⇒ Object

Enhance a task with prerequisites or actions. Returns self.



81
82
83
84
85
# File 'lib/rake.rb', line 81

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

#executeObject

Execute the actions associated with this task.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rake.rb', line 113

def execute
  if $dryrun
    puts "** Execute (dry run) #{name}"
    return
  end
  if $trace
    puts "** Execute #{name}"
  end
  self.class.enhance_with_matching_rule(name) if @actions.empty?
  @actions.each { |act| result = act.call(self) }
end

#invokeObject

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



93
94
95
96
97
98
99
100
101
# File 'lib/rake.rb', line 93

def invoke
  if $trace
    puts "** Invoke #{name} #{format_trace_flags}"
  end
  return if @already_invoked
  @already_invoked = true
  @prerequisites.each { |n| Task[n].invoke }
  execute if needed?
end

#nameObject

Name of the task.



88
89
90
# File 'lib/rake.rb', line 88

def name
  @name.to_s
end

#needed?Boolean

Is this task needed?

Returns:

  • (Boolean)


126
127
128
# File 'lib/rake.rb', line 126

def needed?
  true
end

#timestampObject

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



132
133
134
# File 'lib/rake.rb', line 132

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