-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.rb
More file actions
executable file
·44 lines (37 loc) · 1.3 KB
/
process.rb
File metadata and controls
executable file
·44 lines (37 loc) · 1.3 KB
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
#!/usr/bin/env ruby
require 'yaml'
require 'fileutils'
require 'erb'
def extract_content_and_config(file)
content, config_string = File.read(file).split(/^__CONFIG__$/, 2)
config = YAML.safe_load(ERB.new(config_string).result, permitted_classes: [String, Symbol, Integer, Hash, Array, TrueClass, FalseClass], symbolize_names: true)
[content.strip, config]
end
def generate_file(content, vars, output_path)
env = vars[:env][:default]
output = content.gsub(/\$\w+\b/) do |match|
key = match[1..].to_sym
(vars[key][env.to_sym] || vars[key][:default]).to_s.gsub(/\$env\b/, env)
end
FileUtils.mkdir_p(File.dirname(output_path))
File.write(output_path, output)
# puts output
puts "Generated: #{output_path}"
end
def process_template(template_file, output_dir = 'build')
content, config = extract_content_and_config(template_file)
config[:env].each do |env|
vars = config[:vars].merge(:env => { :default => env })
output_path = File.join(output_dir, env, File.basename(template_file, '.*'))
generate_file(content, vars, output_path)
end
end
# Main execution
if $PROGRAM_NAME == __FILE__
template_file, output_dir = ARGV
unless template_file
$stderr.puts "Usage: #{$PROGRAM_NAME} <template_file> [output_dir]"
exit 1
end
process_template(template_file, output_dir)
end