forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_evaluation_context_rb.go
155 lines (123 loc) · 3.08 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
package erbrenderer
const templateEvaluationContextRb = `
# Based on common/properties/template_evaluation_context.rb
require "rubygems"
require "ostruct"
require "json"
require "erb"
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"]
@properties = openstruct(spec["properties"] || {})
@raw_properties = spec["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
private
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(name)
@name = name
super("Can't find property '#{name}'")
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
`