config_file_generator 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/config_file_generator.rb +50 -50
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdf04ec67c26d7e8a7752c2d9ef5401f4bad492f
|
4
|
+
data.tar.gz: c0e1a90ed153e57a729e19433b58d4c7dfcbd516
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0f581ac3ae740d7a097f1aca21deaebae79d11c973bb5eae3ffde84e611f722871be0c9de981525e1d6e3c6f6b09ba84172e751c9a49f4977bc8423aadcabb4
|
7
|
+
data.tar.gz: 59103c6aa983d4f4f3685f0385964ce2ba1b9d3fb71af0e12808b3805b54ab18018df6b7f09b82c2b888d11d40854f324cbef9b279e277349060524e3c453153
|
@@ -3,29 +3,56 @@ require 'fileutils'
|
|
3
3
|
require 'ostruct'
|
4
4
|
require 'yaml'
|
5
5
|
|
6
|
+
|
6
7
|
class ConfigFileGenerator
|
7
8
|
include ERB::Util
|
8
9
|
attr_accessor :environment, :template, :date
|
9
10
|
|
10
11
|
# Set up our instance variables so that we can pass them to the template.
|
11
12
|
def initialize(template_path: 'deployment/templates', vars: File.join(template_path, 'vars.yml'))
|
12
|
-
@template_vars = load_vars(vars)
|
13
13
|
@templates = find_templates(template_path)
|
14
|
+
@template_vars = load_vars(vars)
|
14
15
|
end
|
15
16
|
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
# Parse the templates given to the class.
|
18
|
+
# If an output directory is given to this function, write out the parsed templates to files within that
|
19
|
+
# directory with the same name, without the ".erb"
|
20
|
+
# Directory structure is also preserved.
|
21
|
+
# If an output directory isn't given, just print out the parsed templates to stdout.
|
22
|
+
# Basically, this allows for a "dry run" which is useful for testing that your templates are looking solid.
|
23
|
+
def generate(environment, output_directory: File.join('deployment', environment), dry_run: false)
|
24
|
+
if dry_run == true or dry_run.to_s.downcase == 'true'
|
25
|
+
puts "Running generate() in dry_run. Printing parsed templates to stdout instead of to a file:"
|
26
|
+
for template in @templates
|
27
|
+
# Fail early if the template uses variables that weren't passed in.
|
28
|
+
check_required_vars(template, environment)
|
29
|
+
puts "\n~~~~~~~~~~ #{template} ~~~~~~~~~~\n\n"
|
30
|
+
puts render(template, environment)
|
31
|
+
end
|
23
32
|
else
|
24
|
-
|
25
|
-
|
33
|
+
for template in @templates
|
34
|
+
# Fail early if the template uses variables that weren't passed in.
|
35
|
+
check_required_vars(template, environment)
|
36
|
+
begin
|
37
|
+
file_name = File.join(output_directory, File.basename(template, '.erb'))
|
38
|
+
File.open(file_name, 'w+') do |f|
|
39
|
+
f.write(render(template, environment))
|
40
|
+
end
|
41
|
+
rescue Errno::ENOENT # if directory path doesn't exist, create it
|
42
|
+
FileUtils.mkdir_p(File.dirname(file_name))
|
43
|
+
retry
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
26
47
|
end
|
27
48
|
end
|
28
49
|
|
50
|
+
|
51
|
+
#####################################################
|
52
|
+
# Private Methods, currently listed in call-order #
|
53
|
+
#####################################################
|
54
|
+
private
|
55
|
+
|
29
56
|
# Returns an array of templates.
|
30
57
|
# If a directory is passed in, then it returns all files within that directory (recursively).
|
31
58
|
# If a file is passed in, then just that file is returned.
|
@@ -42,14 +69,23 @@ class ConfigFileGenerator
|
|
42
69
|
end
|
43
70
|
end
|
44
71
|
|
45
|
-
|
46
|
-
|
72
|
+
# Load variables for use within the templates.
|
73
|
+
# Accepts either a file path or a Hash.
|
74
|
+
def load_vars(vars)
|
75
|
+
if File.file?(vars)
|
76
|
+
YAML.load(File.read(vars))
|
77
|
+
elsif vars.is_a?(Hash)
|
78
|
+
vars
|
79
|
+
else
|
80
|
+
raise "Either a Hash or a file path is required to load template variables from. " + \
|
81
|
+
"Instead received: #{vars}"
|
82
|
+
end
|
47
83
|
end
|
48
84
|
|
49
85
|
# Get all of the variables used within the template so that we can let the user know which
|
50
86
|
# variables they need to be passing in.
|
51
87
|
def get_vars_used_in_template(template)
|
52
|
-
read(template).scan(/<%= ?([a-z]+[0-9a-z_]*)/i).uniq.flatten
|
88
|
+
File.read(template).scan(/<%= ?([a-z]+[0-9a-z_]*)/i).uniq.flatten
|
53
89
|
end
|
54
90
|
|
55
91
|
# Check that the caller passed in one of every variable that is needed by the template. If not, raise.
|
@@ -71,43 +107,7 @@ class ConfigFileGenerator
|
|
71
107
|
# must pass render() your class's private binding() instance method (which Ruby provides) as done here.
|
72
108
|
def render(template, environment)
|
73
109
|
vars = OpenStruct.new(@template_vars[environment]).instance_eval { binding }
|
74
|
-
ERB.new(read(template)).result(vars)
|
75
|
-
end
|
76
|
-
|
77
|
-
# Parse the templates given to the class.
|
78
|
-
# If an output directory is given to this function, write out the parsed templates to files within that
|
79
|
-
# directory with the same name, without the ".erb"
|
80
|
-
# Directory structure is also preserved.
|
81
|
-
# If an output directory isn't given, just print out the parsed templates to stdout.
|
82
|
-
# Basically, this allows for a "dry run" which is useful for testing that your templates are looking solid.
|
83
|
-
def generate(environment, output_directory: File.join('deployment', environment), dry_run: false)
|
84
|
-
|
85
|
-
if dry_run
|
86
|
-
puts "Running generate() in dry_run. Printing parsed templates to stdout instead of to a file:"
|
87
|
-
for template in @templates
|
88
|
-
# Fail early if the template uses variables that weren't passed in.
|
89
|
-
check_required_vars(template, environment)
|
90
|
-
puts "\n~~~~~~~~~~ #{template} ~~~~~~~~~~\n\n"
|
91
|
-
puts render(template, environment)
|
92
|
-
end
|
93
|
-
else
|
94
|
-
for template in @templates
|
95
|
-
# Fail early if the template uses variables that weren't passed in.
|
96
|
-
check_required_vars(template, environment)
|
97
|
-
begin
|
98
|
-
file_name = File.join(output_directory, File.basename(template, '.erb'))
|
99
|
-
File.open(file_name, 'w+') do |f|
|
100
|
-
f.write(render(template, environment))
|
101
|
-
end
|
102
|
-
rescue Errno::ENOENT # if directory path doesn't exist, create it
|
103
|
-
FileUtils.mkdir_p(File.dirname(file_name))
|
104
|
-
retry
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
+
ERB.new(File.read(template)).result(vars)
|
110
111
|
end
|
111
112
|
|
112
|
-
private :check_required_vars, :find_templates, :get_vars_used_in_template, :load_vars, :read, :render
|
113
113
|
end
|