-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.rb
executable file
·38 lines (31 loc) · 1.04 KB
/
cmd.rb
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
#!/usr/bin/env ruby
# from https://gist.github.com/bastman/55f1c5a5bb474e472d5e
require 'erb'
require 'json'
require 'pathname'
class ErbRenderer < OpenStruct
# https://stackoverflow.com/questions/1572660/is-there-a-way-to-initialize-an-object-through-a-hash
def initialize(h)
h.each {|k,v| instance_variable_set("@#{k}",v)}
end
# https://stackoverflow.com/questions/8954706/render-an-erb-template-with-values-from-a-hash
def render(template)
ERB.new(template).result(binding)
end
end
params=JSON.parse(File.read('/data'))
renderer = ErbRenderer.new(params)
# https://alvinalexander.com/blog/post/ruby/ruby-how-process-each-file-directory-name-pattern
Dir.glob("/templates/**/*") {|filename|
result_path = filename.sub('/templates', '/result').delete_suffix('.erb')
if File.directory? filename
Dir.mkdir result_path
else
template=File.read(filename)
result = renderer.render(template)
# output to file
output = File.new(result_path, 'w')
output.puts(result)
output.close
end
}