/
configuration.rb
90 lines (69 loc) · 1.96 KB
/
configuration.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
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
require 'erb'
module Calatrava
class Configuration
include Rake::DSL
@@extras = []
@@env = ENV['CALATRAVA_ENV'] || 'development'
def self.extra(&configurator)
@@extras << configurator
end
def self.env
@@env
end
def initialize
@runtime = {}
end
def config_result_dir
"config/result"
end
def config_yaml
"config/environments.yml"
end
def config_template_dir
"config/templates"
end
def templates
Rake::FileList["#{config_template_dir}/*.erb"]
end
def config_for(environment)
@@extras.each { |e| e.call(self) }
@runtime.merge(environments_yml[environment])
end
def environment_names
environments_yml.keys
end
def environments_yml
@environments_yml ||= YAML::load(File.open(config_yaml))
end
def runtime(key, value)
@runtime[key] = value
end
def evaluate_template(template_path, configuration)
file_path = File.join(config_result_dir, File.basename(template_path).gsub(".erb", ''))
puts "Config: #{File.basename(template_path)} -> #{File.basename(file_path)}"
result = ERB.new(IO.read(template_path)).result(binding)
IO.write(file_path, result)
end
def path(file)
artifact_path(File.join(Configuration.env, file))
end
def install_tasks
directory config_result_dir
puts "CALATRAVA_ENV = '#{Configuration.env}'"
transient :calatrava_env, Configuration.env
environment_names.each do |environment|
desc "Create config files for #{environment} environment"
task environment.to_sym => [:clean, config_result_dir] do
configuration = config_for(environment)
templates.each do |template|
evaluate_template(template, configuration)
end
artifact_dir(config_result_dir, environment)
end
end
task :clean do
rm_rf config_result_dir
end
end
end
end