config_file_generator 1.0
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 +7 -0
- data/lib/config_file_generator.rb +117 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eab0f21815e92439dbae8326cd49e22bc5976891
|
4
|
+
data.tar.gz: f6664a74e2659989e0485424ac790f1d9fd983c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34ec5c85c3dc88f4a63a25d5d95a33700f2261db8a3482f9115893cc911a4c636249dcf97c5f4eeef2cf7eeefcf3590f89b39628cf849ff3fd23d1d7880bd6d7
|
7
|
+
data.tar.gz: 6b90bae6b8b56f9a0985e7d113488928ab08859fb3f47668a5008dbba57e352776ee9503b6e48b8dd43e0ade68e3f26435ffd75e544db7484fb22b4a7e45574e
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
class ConfigFileGenerator
|
7
|
+
include ERB::Util
|
8
|
+
attr_accessor :environment, :template, :date
|
9
|
+
|
10
|
+
# Set up our instance variables so that we can pass them to the template.
|
11
|
+
def initialize(template_path: 'deployment/templates', vars: File.join(template_path, 'vars.yml'))
|
12
|
+
@template_vars = load_vars(vars)
|
13
|
+
@templates = find_templates(template_path)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Load variables for use within the templates.
|
17
|
+
# Accepts either a file path or a Hash.
|
18
|
+
def load_vars(vars)
|
19
|
+
if File.file?(vars)
|
20
|
+
YAML.load(File.read(vars))
|
21
|
+
elsif vars.is_a?(Hash)
|
22
|
+
vars
|
23
|
+
else
|
24
|
+
raise "Either a Hash or a file path is required to load template variables from. " + \
|
25
|
+
"Instead received: #{vars}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns an array of templates.
|
30
|
+
# If a directory is passed in, then it returns all files within that directory (recursively).
|
31
|
+
# If a file is passed in, then just that file is returned.
|
32
|
+
def find_templates(template_path)
|
33
|
+
if File.directory?(template_path)
|
34
|
+
Dir["#{template_path}/**/*"].select do |file|
|
35
|
+
File.file?(file) and ! file.include? 'vars.yml'
|
36
|
+
end
|
37
|
+
elsif File.file?(template_path)
|
38
|
+
[template_path]
|
39
|
+
else
|
40
|
+
raise "Need a valid Template file path. Instead received: '#{template_path}', " + \
|
41
|
+
"which doesn't look to exist."
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def read(file_path)
|
46
|
+
File.read(file_path)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get all of the variables used within the template so that we can let the user know which
|
50
|
+
# variables they need to be passing in.
|
51
|
+
def get_vars_used_in_template(template)
|
52
|
+
read(template).scan(/<%= ?([a-z]+[0-9a-z_]*)/i).uniq.flatten
|
53
|
+
end
|
54
|
+
|
55
|
+
# Check that the caller passed in one of every variable that is needed by the template. If not, raise.
|
56
|
+
def check_required_vars(template, environment)
|
57
|
+
template_vars = get_vars_used_in_template(template).sort
|
58
|
+
passed_vars = @template_vars[environment].keys.sort.map {|x| x.to_s}
|
59
|
+
difference = template_vars - passed_vars
|
60
|
+
unless difference.empty?
|
61
|
+
raise "\n\nNot all required variables were provided for template '#{template}'.\n" + \
|
62
|
+
"Missing variables: #{difference}\n" + \
|
63
|
+
"The variables provided were: #{passed_vars}\n\n"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# ERB passes variables to the template from a Binding, an object that provides access to the instance
|
68
|
+
# methods and variables that are owned by another object.
|
69
|
+
# If you do not specify a Binding, the result() method only passes the Binding to the template for
|
70
|
+
# the top-level object. To pass the template your class's binding (probably what you want), you
|
71
|
+
# must pass render() your class's private binding() instance method (which Ruby provides) as done here.
|
72
|
+
def render(template, environment)
|
73
|
+
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
|
+
end
|
111
|
+
|
112
|
+
private :read, :render
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
deployment_config = ConfigFileGenerator.new
|
117
|
+
deployment_config.generate('staging')
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config_file_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Austin Curtis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple tool to take *.yml.erb files, parse them with Ruby ERB, and
|
14
|
+
generate *.yml files.
|
15
|
+
email: austin.curtis@wpengine.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/config_file_generator.rb
|
21
|
+
homepage: http://rubygems.org/gems/config_file_generator
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.4.5.1
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Generates YAML from ERB
|
45
|
+
test_files: []
|