Class: SC::Buildfile::Task

Inherits:
Object
  • Object
show all
Includes:
Cloneable
Defined in:
lib/sproutcore/buildfile/task.rb

Overview

Buildfile tasks are rake tasks with a few extras added to support unique buildfile constraints. Much of this source code is borrowed from Rake 0.8.3

Direct Known Subclasses

BuildTask

Constant Summary collapse

IGNORE =
%w(@lock @application)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cloneable

#clone

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.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sproutcore/buildfile/task.rb', line 81

def initialize(task_name, app)
  @name = task_name.to_s
  @prerequisites = []
  @actions = []
  @full_comment = nil
  @comment = nil
  @lock = Monitor.new
  @application = app
  @scope = app.current_scope
  @arg_names = nil
  @invoke_count = @execute_count = 0
  @task_options = {}
end

Instance Attribute Details

#actionsObject (readonly)

List of actions attached to a task.



21
22
23
# File 'lib/sproutcore/buildfile/task.rb', line 21

def actions
  @actions
end

#applicationObject

Application owning this task.



24
25
26
# File 'lib/sproutcore/buildfile/task.rb', line 24

def application
  @application
end

#commentObject

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



28
29
30
# File 'lib/sproutcore/buildfile/task.rb', line 28

def comment
  @comment
end

#execute_countObject (readonly)

The number of times the task was actually executed. This may differ from the invoke_count if the task was invoked but was not needed.



42
43
44
# File 'lib/sproutcore/buildfile/task.rb', line 42

def execute_count
  @execute_count
end

#full_commentObject (readonly)

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



31
32
33
# File 'lib/sproutcore/buildfile/task.rb', line 31

def full_comment
  @full_comment
end

#invoke_countObject (readonly)

The number of times this task has been invoked. Use to ensure that the task was invoked during some call chain…



38
39
40
# File 'lib/sproutcore/buildfile/task.rb', line 38

def invoke_count
  @invoke_count
end

#prerequisitesObject (readonly)

List of prerequisites for a task.



18
19
20
# File 'lib/sproutcore/buildfile/task.rb', line 18

def prerequisites
  @prerequisites
end

#scopeObject (readonly)

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



34
35
36
# File 'lib/sproutcore/buildfile/task.rb', line 34

def scope
  @scope
end

#sourcesObject



58
59
60
# File 'lib/sproutcore/buildfile/task.rb', line 58

def sources
  @sources ||= []
end

#task_optionsObject (readonly)

Various options you can set on the task to control log level, etc.



45
46
47
# File 'lib/sproutcore/buildfile/task.rb', line 45

def task_options
  @task_options
end

Class Method Details

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



294
295
296
# File 'lib/sproutcore/buildfile/task.rb', line 294

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

Instance Method Details

#add_description(description) ⇒ Object

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



229
230
231
232
233
# File 'lib/sproutcore/buildfile/task.rb', line 229

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

#add_options(task_options) ⇒ Object

Add task options.



236
237
238
239
# File 'lib/sproutcore/buildfile/task.rb', line 236

def add_options(task_options)
  return if !task_options
  @task_options = task_options
end

#arg_descriptionObject

Argument description (nil if none).



117
118
119
# File 'lib/sproutcore/buildfile/task.rb', line 117

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

#arg_namesObject

Name of arguments for this task.



122
123
124
# File 'lib/sproutcore/buildfile/task.rb', line 122

def arg_names
  @arg_names || []
end

#clearObject

Clear the existing prerequisites and actions of a rake task.



127
128
129
130
131
# File 'lib/sproutcore/buildfile/task.rb', line 127

def clear
  clear_prerequisites
  clear_actions
  self
end

#clear_actionsObject

Clear the existing actions on a rake task.



140
141
142
143
# File 'lib/sproutcore/buildfile/task.rb', line 140

def clear_actions
  actions.clear
  self
end

#clear_prerequisitesObject

Clear the existing prerequisites of a rake task.



134
135
136
137
# File 'lib/sproutcore/buildfile/task.rb', line 134

def clear_prerequisites
  prerequisites.clear
  self
end

#dup(app = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/sproutcore/buildfile/task.rb', line 68

def dup(app=nil)
  app = application if app.nil?
  sibling = self.class.new(name, app)
  self.instance_variables.each do |key|
    next if IGNORE.include?(key)
    sibling.instance_variable_set(key, self.instance_variable_get(key))
  end
  sibling.taint if tainted?
  sibling
end

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

Enhance a task with prerequisites or actions. Returns self.



96
97
98
99
100
# File 'lib/sproutcore/buildfile/task.rb', line 96

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

#execute(args = nil) ⇒ Object

Execute the actions associated with this task.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sproutcore/buildfile/task.rb', line 201

def execute(args=nil)
  @execute_count += 1
  args ||= EMPTY_TASK_ARGS
  return if SC.env.dryrun

  @actions.each do |act|
    case act.arity
    when 1
      act.call(self)
    else
      act.call(self, args)
    end
  end
end

#inspectObject



52
53
54
# File 'lib/sproutcore/buildfile/task.rb', line 52

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

#investigationObject

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/sproutcore/buildfile/task.rb', line 271

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

#invoke(*args) ⇒ Object

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



146
147
148
149
# File 'lib/sproutcore/buildfile/task.rb', line 146

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

#invoke_prerequisites(task_args, invocation_chain) ⇒ Object

Invoke all the prerequisites of a task.



182
183
184
185
186
187
188
189
# File 'lib/sproutcore/buildfile/task.rb', line 182

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

#nameObject

Name of the task, including any namespace qualifiers.



103
104
105
# File 'lib/sproutcore/buildfile/task.rb', line 103

def name
  @name.to_s
end

#name_with_argsObject

Name of task with argument list description.



108
109
110
111
112
113
114
# File 'lib/sproutcore/buildfile/task.rb', line 108

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

#needed?Boolean

Is this task needed?

Returns:

  • (Boolean)


217
218
219
# File 'lib/sproutcore/buildfile/task.rb', line 217

def needed?
  true
end

#set_arg_names(args) ⇒ Object

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



265
266
267
# File 'lib/sproutcore/buildfile/task.rb', line 265

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

#sourceObject

First source from a rule (nil if no sources)



63
64
65
# File 'lib/sproutcore/buildfile/task.rb', line 63

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.



223
224
225
# File 'lib/sproutcore/buildfile/task.rb', line 223

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

#to_sObject

Return task name



48
49
50
# File 'lib/sproutcore/buildfile/task.rb', line 48

def to_s
  name
end