config_templates 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/autoload.rb +2 -5
- data/lib/config_templates.rb +11 -8
- data/lib/config_templates/config.rb +10 -16
- data/lib/config_templates/contexts/compilation.rb +14 -7
- data/lib/config_templates/contexts/{renderer.rb → rendering.rb} +5 -3
- data/lib/config_templates/filesystem/locator.rb +16 -0
- data/lib/config_templates/models/compilation.rb +8 -6
- data/lib/config_templates/models/component.rb +34 -0
- data/lib/config_templates/models/template.rb +3 -15
- data/lib/config_templates/outputs/filesystem.rb +3 -3
- data/lib/config_templates/outputs/stdout.rb +2 -2
- data/lib/config_templates/repositories/settings.rb +4 -2
- data/lib/config_templates/repositories/templates.rb +6 -3
- data/lib/config_templates/version.rb +1 -1
- data/lib/ioc_config.rb +20 -0
- data/spec/templates_spec.rb +1 -1
- metadata +34 -5
- data/lib/config_templates/file_locator.rb +0 -18
- data/lib/config_templates/models/renderer.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e66c47dca61de3ce9695b3650d08c201bed7f1c1
|
4
|
+
data.tar.gz: 6526fa9950e98b668a134af33f2e49098b2bfd75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac03e9885abf6a5bcc39b18630a7add0913fa4b04924fde186b86a651d970550fa6b65ca936fbabaee7361c0b2a0b27da98ae06c53b8da54b8ef612f9245a35e
|
7
|
+
data.tar.gz: b2c4f27c584a088e5aa110752e5584fc983c9cf9911ebfedcf5cf74c7678704f9831b04e511766cf2b49d3c603ac8b326ab0c800f3299df41e093edb42c16779
|
data/lib/autoload.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
require 'erb'
|
2
|
-
require 'yaml'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
1
|
module ConfigTemplates
|
6
2
|
module Contexts; end
|
7
3
|
module Criteria; end
|
8
4
|
module Engines; end
|
9
|
-
module
|
5
|
+
module Filesystem; end
|
6
|
+
module Templates; end
|
10
7
|
module Outputs; end
|
11
8
|
module Repositories; end
|
12
9
|
module Validators; end
|
data/lib/config_templates.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
-
|
1
|
+
require 'erb'
|
2
|
+
require 'yaml'
|
3
|
+
require 'fileutils'
|
2
4
|
|
3
5
|
module ConfigTemplates
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
6
|
+
def self.configure
|
7
|
+
yield container.resolve(:config)
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
10
|
+
def self.container
|
11
|
+
@container ||= ::Dry::Container.new
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
require_relative 'ioc_config'
|
16
|
+
require_relative 'autoload'
|
@@ -1,15 +1,21 @@
|
|
1
1
|
module ConfigTemplates
|
2
2
|
class Config
|
3
|
+
include ConfigTemplates::Inject[
|
4
|
+
'repositories.validators',
|
5
|
+
'repositories.engines',
|
6
|
+
'repositories.outputs'
|
7
|
+
]
|
8
|
+
|
3
9
|
attr_accessor :templates_path, :destination_path
|
4
10
|
attr_accessor :settings_path, :settings_file_basename
|
5
11
|
attr_accessor :stages
|
6
12
|
attr_reader :stage
|
7
13
|
|
8
|
-
def initialize
|
14
|
+
def initialize(validators, engines, outputs)
|
9
15
|
@stages = []
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@validators =
|
16
|
+
@engines = engines
|
17
|
+
@outputs = outputs
|
18
|
+
@validators = validators
|
13
19
|
end
|
14
20
|
|
15
21
|
def stage=(stage)
|
@@ -20,26 +26,14 @@ module ConfigTemplates
|
|
20
26
|
end
|
21
27
|
end
|
22
28
|
|
23
|
-
def output(name)
|
24
|
-
@outputs.find_by_name name
|
25
|
-
end
|
26
|
-
|
27
29
|
def outputs(outputs)
|
28
30
|
@outputs.add outputs
|
29
31
|
end
|
30
32
|
|
31
|
-
def engine(extension)
|
32
|
-
@engines.find_by_extension extension
|
33
|
-
end
|
34
|
-
|
35
33
|
def engines(engines)
|
36
34
|
@engines.add engines
|
37
35
|
end
|
38
36
|
|
39
|
-
def validator(file_name)
|
40
|
-
@validators.find_by_file_name file_name
|
41
|
-
end
|
42
|
-
|
43
37
|
def validators(validators)
|
44
38
|
@validators.add validators
|
45
39
|
end
|
@@ -1,8 +1,15 @@
|
|
1
1
|
module ConfigTemplates::Contexts
|
2
2
|
class Compilation
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
include ConfigTemplates::Inject[
|
4
|
+
'repositories.templates',
|
5
|
+
'repositories.validators',
|
6
|
+
'repositories.engines'
|
7
|
+
]
|
8
|
+
|
9
|
+
def initialize(templates, validators, engines)
|
10
|
+
@templates = templates
|
11
|
+
@validators = validators
|
12
|
+
@engines = engines
|
6
13
|
@criteria = ::ConfigTemplates::Criteria::Composite.new
|
7
14
|
end
|
8
15
|
|
@@ -10,16 +17,16 @@ module ConfigTemplates::Contexts
|
|
10
17
|
@criteria = ::ConfigTemplates::Criteria::Composite.new @criteria, criteria
|
11
18
|
end
|
12
19
|
|
13
|
-
def
|
20
|
+
def components
|
14
21
|
@templates.find_all_by(@criteria).map do |template|
|
15
|
-
::ConfigTemplates::Models::
|
22
|
+
::ConfigTemplates::Models::Component.new template, context, @validators, @engines
|
16
23
|
end
|
17
24
|
end
|
18
25
|
|
19
26
|
private
|
20
27
|
|
21
|
-
def
|
22
|
-
::ConfigTemplates::Contexts::
|
28
|
+
def context
|
29
|
+
::ConfigTemplates::Contexts::Rendering.new
|
23
30
|
end
|
24
31
|
end
|
25
32
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module ConfigTemplates::Contexts
|
2
|
-
class
|
3
|
-
|
2
|
+
class Rendering
|
3
|
+
include ::ConfigTemplates::Inject['repositories.settings', 'config']
|
4
|
+
|
5
|
+
def initialize(settings, config)
|
4
6
|
@settings = settings
|
5
|
-
@config =
|
7
|
+
@config = config
|
6
8
|
end
|
7
9
|
|
8
10
|
def param(path, default = nil)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ConfigTemplates::Filesystem
|
2
|
+
class Locator
|
3
|
+
include ::ConfigTemplates::Inject['config']
|
4
|
+
|
5
|
+
def templates
|
6
|
+
::Dir[::File.join(config.templates_path, '**', '*')].lazy.select { |path| ::File.file? path }
|
7
|
+
end
|
8
|
+
|
9
|
+
def settings
|
10
|
+
::Dir[
|
11
|
+
::File.join(config.settings_path, "#{config.settings_file_basename}.yml"),
|
12
|
+
::File.join(config.settings_path, "#{config.settings_file_basename}.#{config.stage}.yml")
|
13
|
+
]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,7 +1,9 @@
|
|
1
1
|
module ConfigTemplates::Models
|
2
2
|
class Compilation
|
3
|
-
|
4
|
-
|
3
|
+
include ::ConfigTemplates::Inject['repositories.outputs']
|
4
|
+
|
5
|
+
def initialize(outputs)
|
6
|
+
@outputs = outputs
|
5
7
|
@context = ::ConfigTemplates::Contexts::Compilation.new
|
6
8
|
end
|
7
9
|
|
@@ -16,10 +18,10 @@ module ConfigTemplates::Models
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def send_to(output_name)
|
19
|
-
@context.
|
20
|
-
output =
|
21
|
-
|
22
|
-
|
21
|
+
@context.components.tap do |components|
|
22
|
+
output = outputs.find_by_name output_name
|
23
|
+
components.each &:validate!
|
24
|
+
components.each { |component| output.print component }
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ConfigTemplates::Models
|
2
|
+
class Component
|
3
|
+
def initialize(template, context, validators, engines)
|
4
|
+
@template = template
|
5
|
+
@context = context
|
6
|
+
@validators = validators
|
7
|
+
@engines = engines
|
8
|
+
end
|
9
|
+
|
10
|
+
def destination
|
11
|
+
engine.output_file_name @template.destination
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate!
|
15
|
+
unless validator.valid? render
|
16
|
+
raise ::ConfigTemplates::Errors::InvalidTemplate, @template.path
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def render
|
21
|
+
@render ||= engine.evaluate @template.content, @context
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def validator
|
27
|
+
@validator ||= @validators.find_by_file_name @template.path
|
28
|
+
end
|
29
|
+
|
30
|
+
def engine
|
31
|
+
@engine ||= @engines.find_by_extension @template.extension
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -2,9 +2,9 @@ module ConfigTemplates::Models
|
|
2
2
|
class Template
|
3
3
|
attr_reader :path
|
4
4
|
|
5
|
-
def initialize(path)
|
5
|
+
def initialize(path, config)
|
6
6
|
@path = path
|
7
|
-
@config =
|
7
|
+
@config = config
|
8
8
|
end
|
9
9
|
|
10
10
|
def content
|
@@ -16,19 +16,7 @@ module ConfigTemplates::Models
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def destination
|
19
|
-
|
20
|
-
@config.destination_path,
|
21
|
-
@config.stage.to_s,
|
22
|
-
@path.sub(@config.templates_path, '')
|
23
|
-
)
|
24
|
-
end
|
25
|
-
|
26
|
-
def validator
|
27
|
-
@config.validator @path
|
28
|
-
end
|
29
|
-
|
30
|
-
def engine
|
31
|
-
@config.engine extension
|
19
|
+
::File.join @config.destination_path, @config.stage.to_s, @path.sub(@config.templates_path, '')
|
32
20
|
end
|
33
21
|
end
|
34
22
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module ConfigTemplates::Outputs
|
2
2
|
class Filesystem
|
3
|
-
def print(
|
4
|
-
destination =
|
3
|
+
def print(component)
|
4
|
+
destination = component.destination
|
5
5
|
::FileUtils.mkdir_p ::File.dirname destination
|
6
|
-
::File.write destination,
|
6
|
+
::File.write destination, component.render
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -1,13 +1,16 @@
|
|
1
1
|
module ConfigTemplates::Repositories
|
2
2
|
class Templates
|
3
|
-
|
4
|
-
|
3
|
+
include ::ConfigTemplates::Inject['config', 'files.locator']
|
4
|
+
|
5
|
+
def initialize(config, locator)
|
6
|
+
@config = config
|
7
|
+
@locator = locator
|
5
8
|
end
|
6
9
|
|
7
10
|
def find_all_by(criteria)
|
8
11
|
@locator.templates
|
9
12
|
.select { |filename| criteria.matches? filename }
|
10
|
-
.map { |filename| ::ConfigTemplates::Models::Template.new filename }
|
13
|
+
.map { |filename| ::ConfigTemplates::Models::Template.new filename, @config }
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
data/lib/ioc_config.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'dry-auto_inject'
|
2
|
+
require 'dry-container'
|
3
|
+
|
4
|
+
::ConfigTemplates::Inject = ::Dry::AutoInject(::ConfigTemplates.container).args
|
5
|
+
|
6
|
+
::ConfigTemplates.container.tap do |container|
|
7
|
+
container.register(:config, memoize: true) { ::ConfigTemplates::Config.new }
|
8
|
+
|
9
|
+
container.namespace :files do
|
10
|
+
register(:locator, memoize: true) { ::ConfigTemplates::Filesystem::Locator.new }
|
11
|
+
end
|
12
|
+
|
13
|
+
container.namespace :repositories do
|
14
|
+
register(:engines, memoize: true) { ::ConfigTemplates::Repositories::Engines.new }
|
15
|
+
register(:outputs, memoize: true) { ::ConfigTemplates::Repositories::Outputs.new }
|
16
|
+
register(:settings, memoize: true) { ::ConfigTemplates::Repositories::Settings.new }
|
17
|
+
register(:templates, memoize: true) { ::ConfigTemplates::Repositories::Templates.new }
|
18
|
+
register(:validators, memoize: true) { ::ConfigTemplates::Repositories::Validators.new }
|
19
|
+
end
|
20
|
+
end
|
data/spec/templates_spec.rb
CHANGED
@@ -11,7 +11,7 @@ RSpec.describe ::ConfigTemplates do
|
|
11
11
|
|
12
12
|
it 'renders selected template using configs and environment metadata' do
|
13
13
|
compilation = ::ConfigTemplates::Models::Compilation.new
|
14
|
-
compilation.reject(/.*/).select('kapacitor/config.erb').send_to(:test)
|
14
|
+
compilation.reject(/.*/).select(Regexp.new 'kapacitor/config.erb').send_to(:test)
|
15
15
|
expect(@output_mock.result.strip).to eq('true')
|
16
16
|
end
|
17
17
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_templates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- g.ivanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-auto_inject
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-container
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: rake
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,15 +105,15 @@ files:
|
|
77
105
|
- lib/config_templates.rb
|
78
106
|
- lib/config_templates/config.rb
|
79
107
|
- lib/config_templates/contexts/compilation.rb
|
80
|
-
- lib/config_templates/contexts/
|
108
|
+
- lib/config_templates/contexts/rendering.rb
|
81
109
|
- lib/config_templates/criteria/composite.rb
|
82
110
|
- lib/config_templates/criteria/name.rb
|
83
111
|
- lib/config_templates/engines/erb.rb
|
84
112
|
- lib/config_templates/engines/text.rb
|
85
113
|
- lib/config_templates/errors.rb
|
86
|
-
- lib/config_templates/
|
114
|
+
- lib/config_templates/filesystem/locator.rb
|
87
115
|
- lib/config_templates/models/compilation.rb
|
88
|
-
- lib/config_templates/models/
|
116
|
+
- lib/config_templates/models/component.rb
|
89
117
|
- lib/config_templates/models/template.rb
|
90
118
|
- lib/config_templates/outputs/filesystem.rb
|
91
119
|
- lib/config_templates/outputs/stdout.rb
|
@@ -96,6 +124,7 @@ files:
|
|
96
124
|
- lib/config_templates/repositories/validators.rb
|
97
125
|
- lib/config_templates/validators/composite.rb
|
98
126
|
- lib/config_templates/version.rb
|
127
|
+
- lib/ioc_config.rb
|
99
128
|
- spec/fixtures/dest/production/kapacitor/config
|
100
129
|
- spec/fixtures/settings/settings.production.yml
|
101
130
|
- spec/fixtures/settings/settings.yml
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module ConfigTemplates
|
2
|
-
class FileLocator
|
3
|
-
def initialize
|
4
|
-
@config = ::ConfigTemplates.config
|
5
|
-
end
|
6
|
-
|
7
|
-
def templates
|
8
|
-
::Dir[::File.join(@config.templates_path, '**', '*')].lazy.select { |path| ::File.file? path }
|
9
|
-
end
|
10
|
-
|
11
|
-
def settings
|
12
|
-
::Dir[
|
13
|
-
::File.join(@config.settings_path, "#{@config.settings_file_basename}.yml"),
|
14
|
-
::File.join(@config.settings_path, "#{@config.settings_file_basename}.#{@config.stage}.yml")
|
15
|
-
]
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module ConfigTemplates::Models
|
2
|
-
class Renderer
|
3
|
-
attr_reader :template
|
4
|
-
|
5
|
-
def initialize(template, context)
|
6
|
-
@template = template
|
7
|
-
@context = context
|
8
|
-
end
|
9
|
-
|
10
|
-
def validate!
|
11
|
-
unless @template.validator.valid? render
|
12
|
-
raise ::ConfigTemplates::Errors::InvalidTemplate, @template.path
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def render
|
17
|
-
@render ||= @template.engine.evaluate @template.content, @context
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|