kokonfig 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c36e71947f5454fda84c7be1b81debeb45ec5c4f5213d9c9b22cedb6a26fd0e2
4
+ data.tar.gz: 994c030f2e127b777e0f20309eef204dbc982f0b454a12a9405d0eba825ddfae
5
+ SHA512:
6
+ metadata.gz: e6608f59a0afc5dbd0f79bb43adf50e2b2504c072da8f43166119473ceee80f5900296a54d5b61b029bd6ef0e5437895484b67735740d28734291faf8df8d92d
7
+ data.tar.gz: 44872212048b1ac1d5a3a360bc2d045444ad9e6cacdb2f6c644ec92516ab8901fe9d923995a8520dc04bc95805d7b8d832044f6c220ff11f34d1ba4980cf0eaf
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /config/
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in kokonfig.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kokonfig (0.1.0)
5
+ erb (~> 2.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.4.4)
11
+ erb (2.2.0)
12
+ rake (12.3.3)
13
+ rspec (3.10.0)
14
+ rspec-core (~> 3.10.0)
15
+ rspec-expectations (~> 3.10.0)
16
+ rspec-mocks (~> 3.10.0)
17
+ rspec-core (3.10.0)
18
+ rspec-support (~> 3.10.0)
19
+ rspec-expectations (3.10.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.10.0)
22
+ rspec-mocks (3.10.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.10.0)
25
+ rspec-support (3.10.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ kokonfig!
32
+ rake (~> 12.0)
33
+ rspec (~> 3.0)
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Elcio Nakashima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # Kokonfig
2
+
3
+ Kokonfig is a command line utility that generates multiple versions of files based on ERB templates and data YAML files.
4
+
5
+ Kokonfig can be used to generate config files for each environment in your application when each config mostly contains the same structure but values depend on the environment. It can also be useful when config files contain several repeated values.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kokonfig'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kokonfig
22
+
23
+ ## Usage
24
+
25
+ For each set of output files, you need to create an input YAML data file and an ERB template. For each version defined by these two files, Kokonfig will output one file. You just need to put the input files in default directories and run the following command:
26
+
27
+ ```
28
+ kokonfig
29
+ ```
30
+
31
+ ### Config files
32
+
33
+ The data file must be a YAML file containing a mapping and its root level keys will be the name of each output version. Inner keys will be values that can be used in the ERB template file.
34
+
35
+ The data file name will define what will be the template file and output file names. Everything before the *.yml* extension is the base file name. The template file has to be named with that base file name concatenated with the extension *.erb*. The output files generated will be named with the base file name with each version string before the file extension. The example below will make this clear.
36
+
37
+ Data files are read from *./config/kokonfig/data* and template files directory is at *./config/kokonfig/templates*. Output files are created in the current working directory.
38
+
39
+ ### Example
40
+
41
+ Create a data file *./config/kokonfig/data/result.txt.yml*:
42
+
43
+ ```
44
+ production:
45
+ country: Australia
46
+ language: english
47
+ development:
48
+ country: Brazil
49
+ language: portuguese
50
+ ```
51
+
52
+ Create a template file *./config/kokonfig/templates/result.txt.erb*:
53
+
54
+ ```
55
+ In <%= country %> people speak <%= language %>!
56
+ ```
57
+
58
+ After runnning *kokonfig* you should see two files in your current working directory named *result.production.txt* and *result.development.txt*. The file *result.production.txt* should contain:
59
+
60
+ ```
61
+ In Australia people speak english!
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/elciok/kokonfig.
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kokonfig"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kokonfig'
4
+ Kokonfig::CLI.start
@@ -0,0 +1,39 @@
1
+ require_relative 'lib/kokonfig/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "kokonfig"
5
+ spec.version = Kokonfig::VERSION
6
+ spec.authors = ["Elcio Nakashima"]
7
+ spec.email = ["elciok@gmail.com"]
8
+
9
+ spec.summary = %q{Kokonfig is a command line utility that generates multiple versions of files based on ERB templates and data YAML files.}
10
+ spec.description = <<~DESCRIPTION
11
+ Kokonfig is a command line utility that generates multiple versions of files based on ERB templates and data YAML files.
12
+
13
+ Kokonfig can be used to generate config files for each environment in your application, when each config mostly contains the same structure but values depend on the environment. It can also be useful when config files contain several repeated values.
14
+ DESCRIPTION
15
+ spec.homepage = "https://github.com/elciok/kokonfig"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
18
+
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/elciok/kokonfig"
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+
25
+ spec.add_dependency "erb", "~> 2.2"
26
+ # spec.add_dependency "fileutils", "~> 1.4"
27
+ # spec.add_dependency "ostruct", "~> 0.3"
28
+ # spec.add_dependency "pathname", "~> 0.1"
29
+ # spec.add_dependency "yaml", "~> 0.1"
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = "exe"
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ["lib"]
39
+ end
@@ -0,0 +1,10 @@
1
+ require "kokonfig/version"
2
+ require "kokonfig/objectifier"
3
+ require "kokonfig/template"
4
+ require "kokonfig/data_file"
5
+ require "kokonfig/cli"
6
+
7
+ module Kokonfig
8
+ class Error < StandardError; end
9
+
10
+ end
@@ -0,0 +1,19 @@
1
+ module Kokonfig
2
+ CONFIG_DIR = "config/kokonfig"
3
+ DATA_DIR = "data"
4
+ TEMPLATES_DIR = "templates"
5
+
6
+ module CLI
7
+ def self.start
8
+ current_dir = Rails.root rescue Dir.pwd
9
+ data_dir = File.join(current_dir, CONFIG_DIR, DATA_DIR)
10
+ templates_dir = File.join(current_dir, CONFIG_DIR, TEMPLATES_DIR)
11
+ output_dir = current_dir
12
+
13
+ Dir.glob("#{data_dir}/**/*.yml").each do |data_file_path|
14
+ data_file = Kokonfig::DataFile.new(data_file_path, data_dir, templates_dir)
15
+ data_file.generate_files(output_dir)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,63 @@
1
+ require 'yaml'
2
+ require 'pathname'
3
+ require 'fileutils'
4
+
5
+ module Kokonfig
6
+ class DataFile
7
+ def initialize(data_file_path, data_dir, templates_dir)
8
+ @path = data_file_path
9
+ @data_dir = data_dir
10
+ @templates_dir = templates_dir
11
+ end
12
+
13
+ def template_path
14
+ path_parts = []
15
+ path_parts << @templates_dir
16
+
17
+ path_parts << base_result_relative_path if base_result_relative_path.to_s != "."
18
+
19
+ template_file_name = "#{base_result_file}.erb"
20
+ path_parts << template_file_name
21
+
22
+ File.join(*path_parts)
23
+ end
24
+
25
+ def result_file(version)
26
+ path_parts = []
27
+ path_parts << base_result_relative_path if base_result_relative_path.to_s != "."
28
+
29
+ result_base_file_extension = File.extname(base_result_file)
30
+ result_base_file_name = File.basename(base_result_file, result_base_file_extension)
31
+ result_file_name = "#{result_base_file_name}.#{version}#{result_base_file_extension}"
32
+ path_parts << result_file_name
33
+
34
+ File.join(*path_parts)
35
+ end
36
+
37
+ def base_result_file
38
+ @base_result_file or @base_result_file = File.basename(@path, ".yml")
39
+ end
40
+
41
+ def base_result_relative_path
42
+ @base_result_relative_path or @base_result_relative_path = Pathname.new(@path).relative_path_from(@data_dir).dirname
43
+ end
44
+
45
+ def generate_files(output_dir)
46
+ template = Kokonfig::Template.from_file(self.template_path)
47
+
48
+ data_per_version = YAML.load_file(@path)
49
+ data_per_version.each do |version, data|
50
+ data = data.merge({version: version})
51
+ rendered_data = template.apply(data)
52
+
53
+ FileUtils.mkdir_p(File.join(output_dir, self.base_result_relative_path))
54
+
55
+ output_path = File.join(output_dir, self.result_file(version))
56
+ File.open(output_path, 'w+') do |result_file|
57
+ result_file.write(rendered_data)
58
+ end
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ require "ostruct"
2
+
3
+ module Kokonfig::Objectifier
4
+ def self.objectify(input)
5
+ result = input
6
+ if input.is_a? Hash
7
+ input.each do |key, value|
8
+ input[key] = Kokonfig::Objectifier.objectify(value)
9
+ end
10
+ result = OpenStruct.new(input)
11
+ elsif input.is_a? Array
12
+ input.each_with_index do |value, index|
13
+ input[index] = Kokonfig::Objectifier.objectify(value)
14
+ end
15
+ end
16
+ result
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require "erb"
2
+
3
+ module Kokonfig
4
+ class Template
5
+
6
+ def initialize(templateString)
7
+ @template = templateString
8
+ end
9
+
10
+ def self.from_file(path)
11
+ template = File.read(path)
12
+ Kokonfig::Template.new(template)
13
+ end
14
+
15
+ def apply(data)
16
+ object = Kokonfig::Objectifier.objectify(data)
17
+ ERB.new(@template, nil, '-').result(object.instance_eval { binding })
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Kokonfig
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kokonfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elcio Nakashima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: erb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ description: |
28
+ Kokonfig is a command line utility that generates multiple versions of files based on ERB templates and data YAML files.
29
+
30
+ Kokonfig can be used to generate config files for each environment in your application, when each config mostly contains the same structure but values depend on the environment. It can also be useful when config files contain several repeated values.
31
+ email:
32
+ - elciok@gmail.com
33
+ executables:
34
+ - kokonfig
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ".gitignore"
39
+ - ".rspec"
40
+ - ".travis.yml"
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - bin/console
47
+ - bin/setup
48
+ - exe/kokonfig
49
+ - kokonfig.gemspec
50
+ - lib/kokonfig.rb
51
+ - lib/kokonfig/cli.rb
52
+ - lib/kokonfig/data_file.rb
53
+ - lib/kokonfig/objectifier.rb
54
+ - lib/kokonfig/template.rb
55
+ - lib/kokonfig/version.rb
56
+ homepage: https://github.com/elciok/kokonfig
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/elciok/kokonfig
61
+ source_code_uri: https://github.com/elciok/kokonfig
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.3.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.1.4
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Kokonfig is a command line utility that generates multiple versions of files
81
+ based on ERB templates and data YAML files.
82
+ test_files: []