Class: Build::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/build/rule.rb

Overview

A rule is a function with a specific set of input and output parameters, which can match against a given set of specific arguments. For example, there might be several rules for compiling, but the specific rules depend on the language being compiled.

Defined Under Namespace

Classes: Parameter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(process_name, type) ⇒ Rule

Initialize a rule with a process name and type.



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/build/rule.rb', line 125

def initialize(process_name, type)
	@name = process_name + "." + type
	@full_name = @name.gsub(/[^\w]/, "_")
	
	@process_name = process_name.gsub("-", "_").to_sym
	@type = type
	
	@apply = nil
	
	@parameters = []
	@primary_output = nil
end

Instance Attribute Details

#full_nameObject (readonly)

compile_cpp



147
148
149
# File 'lib/build/rule.rb', line 147

def full_name
  @full_name
end

#nameObject (readonly)

compile.cpp



139
140
141
# File 'lib/build/rule.rb', line 139

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



141
142
143
# File 'lib/build/rule.rb', line 141

def parameters
  @parameters
end

#primary_outputObject (readonly)

Returns the value of attribute primary_output.



149
150
151
# File 'lib/build/rule.rb', line 149

def primary_output
  @primary_output
end

#process_nameObject (readonly)

compile



144
145
146
# File 'lib/build/rule.rb', line 144

def process_name
  @process_name
end

Class Method Details

.build(name, &block) ⇒ Object

Build a frozen rule from a name and a definition block.



12
13
14
15
16
17
18
# File 'lib/build/rule.rb', line 12

def self.build(name, &block)
	rule = self.new(*name.split(".", 2))
	
	rule.instance_eval(&block)
	
	return rule.freeze
end

Instance Method Details

#<<(parameter) ⇒ Object

Append a parameter to the rule.



191
192
193
194
195
196
197
# File 'lib/build/rule.rb', line 191

def << parameter
	@parameters << parameter
	
	if parameter.output?
		@primary_output ||= parameter
	end
end

#applicable?(arguments) ⇒ Boolean

Check if this rule can process these parameters

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
208
# File 'lib/build/rule.rb', line 200

def applicable?(arguments)
	@parameters.each do |parameter|
		next if parameter.implicit?
		
		return false unless parameter.applicable?(arguments)
	end
	
	return true
end

#apply(&block) ⇒ Object

Set the apply block that is executed when the rule is invoked.



244
245
246
# File 'lib/build/rule.rb', line 244

def apply(&block)
	@apply = Proc.new(&block)
end

#apply!(scope, arguments) ⇒ Object

Apply the rule in the given scope with the provided arguments.



251
252
253
# File 'lib/build/rule.rb', line 251

def apply!(scope, arguments)
	scope.instance_exec(arguments, &@apply) if @apply
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/build/rule.rb', line 268

def eql?(other)
	other.kind_of?(self.class) and @name.eql?(other.name) and @parameters.eql?(other.parameters)
end

#files(arguments) ⇒ Object

Derive the input and output file lists from the given arguments.



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

def files(arguments)
	input_files = []
	output_files = []
	
	@parameters.each do |parameter|
		# This could probably be improved a bit, we are assuming all parameters are file based:
		value = arguments[parameter.name]
		
		next unless value
		
		case parameter.direction
		when :input
			input_files << value
		when :output
			output_files << value
		end
	end
	
	return Build::Files::Composite.new(input_files), Build::Files::Composite.new(output_files)
end

#freezeObject

Freeze the rule and all its components.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/build/rule.rb', line 153

def freeze
	return self if frozen?
	
	@name.freeze
	@full_name.freeze
	@process_name.freeze
	@type.freeze
	
	@apply.freeze
	@parameters.freeze
	@primary_output.freeze
	
	super
end

#hashObject



263
264
265
# File 'lib/build/rule.rb', line 263

def hash
	[self.class, @name, @parameters].hash
end

#input(name, options = {}, &block) ⇒ Object

Add an input parameter to the rule.



171
172
173
# File 'lib/build/rule.rb', line 171

def input(name, options = {}, &block)
	self << Parameter.new(:input, name, options, &block)
end

#normalize(arguments, scope) ⇒ Object

The scope is the context in which the dynamic rule computation is done, usually an instance of Task.



211
212
213
214
215
216
217
# File 'lib/build/rule.rb', line 211

def normalize(arguments, scope)
	Hash[
		@parameters.collect do |parameter|
			[parameter.name, parameter.compute(arguments, scope)]
		end
	]
end

#output(name, options = {}, &block) ⇒ Object

Add an output parameter to the rule.



185
186
187
# File 'lib/build/rule.rb', line 185

def output(name, options = {}, &block)
	self << Parameter.new(:output, name, options, &block)
end

#parameter(name, options = {}, &block) ⇒ Object

Add a generic argument parameter to the rule.



178
179
180
# File 'lib/build/rule.rb', line 178

def parameter(name, options = {}, &block)
	self << Parameter.new(:argument, name, options, &block)
end

#result(arguments) ⇒ Object



256
257
258
259
260
# File 'lib/build/rule.rb', line 256

def result(arguments)
	if @primary_output
		arguments[@primary_output.name]
	end
end

#to_sObject



273
274
275
# File 'lib/build/rule.rb', line 273

def to_s
	"#<#{self.class} #{@name.dump}>"
end