-
Notifications
You must be signed in to change notification settings - Fork 161
/
template_evaluation_context_rb.go
204 lines (163 loc) · 4.12 KB
/
template_evaluation_context_rb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package erbrenderer
const templateEvaluationContextRb = `
# Based on common/properties/template_evaluation_context.rb
require "rubygems"
require "ostruct"
require "json"
require "erb"
require "yaml"
class Hash
def recursive_merge!(other)
self.merge!(other) do |_, old_value, new_value|
if old_value.class == Hash && new_value.class == Hash
old_value.recursive_merge!(new_value)
else
new_value
end
end
self
end
end
class TemplateEvaluationContext
attr_reader :name, :index
attr_reader :properties, :raw_properties
attr_reader :spec
def initialize(spec)
@name = spec["job"]["name"] if spec["job"].is_a?(Hash)
@index = spec["index"]
if !spec['job_properties'].nil?
properties1 = spec['job_properties']
else
properties1 = spec['global_properties'].recursive_merge!(spec['cluster_properties'])
end
properties = {}
spec['default_properties'].each do |name, value|
copy_property(properties, properties1, name, value)
end
@properties = openstruct(properties)
@raw_properties = properties
@spec = openstruct(spec)
end
def get_binding
binding.taint
end
def p(*args)
names = Array(args[0])
names.each do |name|
result = lookup_property(@raw_properties, name)
return result unless result.nil?
end
return args[1] if args.length == 2
raise UnknownProperty.new(names)
end
def if_p(*names)
values = names.map do |name|
value = lookup_property(@raw_properties, name)
return ActiveElseBlock.new(self) if value.nil?
value
end
yield *values
InactiveElseBlock.new
end
def if_link(name)
false
end
private
def copy_property(dst, src, name, default = nil)
keys = name.split(".")
src_ref = src
dst_ref = dst
keys.each do |key|
src_ref = src_ref[key]
break if src_ref.nil? # no property with this name is src
end
keys[0..-2].each do |key|
dst_ref[key] ||= {}
dst_ref = dst_ref[key]
end
dst_ref[keys[-1]] ||= {}
dst_ref[keys[-1]] = src_ref.nil? ? default : src_ref
end
def openstruct(object)
case object
when Hash
mapped = object.inject({}) { |h, (k,v)| h[k] = openstruct(v); h }
OpenStruct.new(mapped)
when Array
object.map { |item| openstruct(item) }
else
object
end
end
def lookup_property(collection, name)
keys = name.split(".")
ref = collection
keys.each do |key|
ref = ref[key]
return nil if ref.nil?
end
ref
end
class UnknownProperty < StandardError
attr_reader :name
def initialize(names)
@names = names
super("Can't find property '#{names.join("', or '")}'")
end
end
class ActiveElseBlock
def initialize(template)
@context = template
end
def else
yield
end
def else_if_p(*names, &block)
@context.if_p(*names, &block)
end
end
class InactiveElseBlock
def else; end
def else_if_p(*names)
InactiveElseBlock.new
end
end
end
# todo do not use JSON in releases
class << JSON
alias dump_array_or_hash dump
def dump(*args)
arg = args[0]
if arg.is_a?(String) || arg.is_a?(Numeric)
arg.inspect
else
dump_array_or_hash(*args)
end
end
end
class ERBRenderer
def initialize(context)
@context = context
end
def render(src_path, dst_path)
erb = ERB.new(File.read(src_path))
erb.filename = src_path
File.open(dst_path, "w") do |f|
f.write(erb.result(@context.get_binding))
end
rescue Exception => e
name = "#{@context.name}/#{@context.index}"
line_i = e.backtrace.index { |l| l.include?(erb.filename) }
line_num = line_i ? e.backtrace[line_i].split(':')[1] : "unknown"
location = "(line #{line_num}: #{e.inspect})"
raise("Error filling in template '#{src_path}' for #{name} #{location}")
end
end
if $0 == __FILE__
context_path, src_path, dst_path = *ARGV
context_hash = JSON.load(File.read(context_path))
context = TemplateEvaluationContext.new(context_hash)
renderer = ERBRenderer.new(context)
renderer.render(src_path, dst_path)
end
`