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 inputs and outputs. 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

Instance Method Summary collapse

Constructor Details

#initialize(process_name, type) ⇒ Rule

Returns a new instance of Rule.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/build/rule.rb', line 91

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 = []
end

Instance Attribute Details

#full_nameObject (readonly)

compile_cpp



112
113
114
# File 'lib/build/rule.rb', line 112

def full_name
  @full_name
end

#nameObject (readonly)

compile.cpp



104
105
106
# File 'lib/build/rule.rb', line 104

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



106
107
108
# File 'lib/build/rule.rb', line 106

def parameters
  @parameters
end

#primary_outputObject (readonly)

Returns the value of attribute primary_output.



114
115
116
# File 'lib/build/rule.rb', line 114

def primary_output
  @primary_output
end

#process_nameObject (readonly)

compile



109
110
111
# File 'lib/build/rule.rb', line 109

def process_name
  @process_name
end

Instance Method Details

#<<(parameter) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/build/rule.rb', line 128

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

#==(other) ⇒ Object



191
192
193
# File 'lib/build/rule.rb', line 191

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

#applicable?(arguments) ⇒ Boolean

Check if this rule can process these parameters

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
# File 'lib/build/rule.rb', line 137

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

#apply(&block) ⇒ Object



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

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

#apply!(scope, arguments) ⇒ Object



181
182
183
# File 'lib/build/rule.rb', line 181

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

#files(arguments) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/build/rule.rb', line 156

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

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



116
117
118
# File 'lib/build/rule.rb', line 116

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.



148
149
150
151
152
153
154
# File 'lib/build/rule.rb', line 148

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

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



124
125
126
# File 'lib/build/rule.rb', line 124

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

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



120
121
122
# File 'lib/build/rule.rb', line 120

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

#result(arguments) ⇒ Object



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

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

#to_sObject



195
196
197
# File 'lib/build/rule.rb', line 195

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