Class: Build::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/build/environment/base.rb,
lib/build/environment/system.rb,
lib/build/environment/flatten.rb,
lib/build/environment/version.rb,
lib/build/environment/evaluator.rb,
lib/build/environment/constructor.rb

Overview

This is the basic environment data structure which is essentially a linked list of hashes. It is primarily used for organising build configurations across a wide range of different sub-systems, e.g. platform configuration, target configuration, local project configuration, etc.

Defined Under Namespace

Modules: System Classes: Constructor, Default, Define, Evaluator, Replace

Constant Summary collapse

VERSION =
"1.13.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, values = nil, name: nil, &block) ⇒ Environment

Returns a new instance of Environment.



24
25
26
27
28
29
30
# File 'lib/build/environment/base.rb', line 24

def initialize(parent = nil, values = nil, name: nil, &block)
	@parent = parent
	@values = (values || {}).to_h
	@update = block
	
	@name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



52
53
54
# File 'lib/build/environment/base.rb', line 52

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



49
50
51
# File 'lib/build/environment/base.rb', line 49

def parent
  @parent
end

#updateObject (readonly)

Returns the value of attribute update.



51
52
53
# File 'lib/build/environment/base.rb', line 51

def update
  @update
end

#valuesObject (readonly)

Returns the value of attribute values.



50
51
52
# File 'lib/build/environment/base.rb', line 50

def values
  @values
end

Class Method Details

.combine(*environments) ⇒ Object

Flatten the list of environments.



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/build/environment/constructor.rb', line 129

def self.combine(*environments)
	ordered = []
	
	environments.each do |environment|
		environment.flatten_to_array(ordered)
	end
	
	ordered.inject(nil) do |parent, environment|
		environment.dup(parent: parent)
	end
end

.system_environment(env = ENV, **options) ⇒ Object

Construct an environment from a given system environment:



64
65
66
# File 'lib/build/environment/system.rb', line 64

def self.system_environment(env = ENV, **options)
	self.new(nil, Hash[env.map{|key, value| [key.downcase.to_sym, value]}], **options)
end

Instance Method Details

#==(other) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/build/environment/base.rb', line 32

def == other
	self.equal?(other) or
		self.class == other.class and
		@parent == other.parent and
		@values == other.values and
		@update == other.update and
		@name == other.name
end

#[](key) ⇒ Object



100
101
102
103
104
# File 'lib/build/environment/base.rb', line 100

def [](key)
	environment = lookup(key)
	
	environment ? environment.values[key] : nil
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
	@values[key] = value
end

#checksum(digester: Digest::SHA1.new) ⇒ Object



60
61
62
63
64
# File 'lib/build/environment/flatten.rb', line 60

def checksum(digester: Digest::SHA1.new)
	checksum_recursively(digester)
	
	return digester.hexdigest
end

#construct!(proxy, *arguments, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/build/environment/constructor.rb', line 38

def construct!(proxy, *arguments, &block)
	constructor = Constructor.new(self, proxy)
	
	if block_given?
		constructor.instance_exec(*arguments, &block)
	end
	
	return self
end

#definedObject



56
57
58
# File 'lib/build/environment/flatten.rb', line 56

def defined
	@values.select{|name,value| Define === value}
end

#dup(parent: @parent, values: @values, update: @update, name: @name) ⇒ Object



54
55
56
# File 'lib/build/environment/base.rb', line 54

def dup(parent: @parent, values: @values, update: @update, name: @name)
	self.class.new(parent, values.dup, name: name, &update)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/build/environment/base.rb', line 41

def eql?(other)
	self == other
end

#evaluate(**options) ⇒ Object



42
43
44
# File 'lib/build/environment/flatten.rb', line 42

def evaluate(**options)
	self.class.new(nil, self.to_h, **options)
end

#evaluatorObject



38
39
40
# File 'lib/build/environment/flatten.rb', line 38

def evaluator
	Evaluator.new(self)
end

#exportObject

Make a hash appropriate for a process environment



69
70
71
# File 'lib/build/environment/system.rb', line 69

def export
	System.convert_to_shell(self)
end

#fetch(key, *default, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/build/environment/base.rb', line 88

def fetch(key, *default, &block)
	if environment = lookup(key)
		return environment.values[key]
	elsif block_given?
		yield(key, *default)
	elsif !default.empty?
		return default.first
	else
		raise KeyError.new("Environment missing #{key}")
	end
end

#flatten(**options) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/build/environment/flatten.rb', line 46

def flatten(**options)
	hash = {}
	
	flatten_to_hash(hash)
	
	options[:name] ||= self.name
	
	return self.class.new(nil, hash, **options)
end

#flatten_to_array(array) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/build/environment/constructor.rb', line 154

def flatten_to_array(array)
	if @parent
		@parent.flatten_to_array(array)
	end
	
	array << self
end

#freezeObject



58
59
60
61
62
63
64
65
66
# File 'lib/build/environment/base.rb', line 58

def freeze
	return self if frozen?
	
	@parent.freeze
	@values.freeze
	@update.freeze
	
	super
end

#hashObject



45
46
47
# File 'lib/build/environment/base.rb', line 45

def hash
	@parent.hash ^ @values.hash ^ @update.hash ^ @name.hash
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/build/environment/base.rb', line 76

def include?(key)
	if @values.include?(key)
		true
	elsif @parent
		@parent.include?(key)
	end
end

#lookup(name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/build/environment/base.rb', line 68

def lookup(name)
	if @values.include? name
		self
	elsif @parent
		@parent.lookup(name)
	end
end

#merge(**options, &block) ⇒ Object



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

def merge(**options, &block)
	self.class.new(self, **options, &block)
end

#sizeObject



84
85
86
# File 'lib/build/environment/base.rb', line 84

def size
	@values.size + (@parent ? @parent.size : 0)
end

#to_aObject

Convert the hierarchy of environments to an array where the parent comes before the child.



146
147
148
149
150
151
152
# File 'lib/build/environment/constructor.rb', line 146

def to_a
	flat = []
	
	flatten_to_array(flat)
	
	return flat
end

#to_hObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/build/environment/flatten.rb', line 25

def to_h
	hash = {}
	
	# Flatten this chain of environments:
	flatten_to_hash(hash)
	
	# Evaluate all items to their respective object value:
	evaluator = Evaluator.new(hash)
	
	# Evaluate all the individual environment values so that they are flat:
	Hash[hash.map{|key, value| [key, evaluator.object_value(value)]}]
end

#to_sObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/build/environment/base.rb', line 110

def to_s
	buffer = String.new("\#<#{self.class} ")
	
	if @name
		buffer << @name.inspect << ' '
	end
	
	if @update
		buffer << @update.source_location.join(':') << ' '
	end
	
	buffer << @values.to_s << '>'
	
	return buffer
end