biran 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5889906b3c4456f766a2c5204bb4441e3add107cba9db0b1189ba12adb9a55ef
4
- data.tar.gz: 41799ef23de8827c738b3e9cb0f584782fbab60759e2f742f32327e686fc09f9
3
+ metadata.gz: 4d14217aa3698d0cf75e5ca704c0601debc6c49768668251af03fa552130666f
4
+ data.tar.gz: b99edeb48b785ba764c9cb9ab67a2fb403f3216b20b8c95d315592249a6fd417
5
5
  SHA512:
6
- metadata.gz: 7e06d5684a5c05f46fab522863f68f352f27b4eb176e26feaf5dc7fc31a696498103cb92f861fed51454fc269d04049ae2fabc986043bd9e7d4277852c429885
7
- data.tar.gz: 02ae8db4a54fb74ccdbb17d1464a56ab6ec5180f5c8c0fd1d34ea6cc5e06aa9a461374d574b84d0185c4c922f965ab5496bbe1ea3fd238ac7699da3c4aae1b8e
6
+ metadata.gz: 9d660b2a71857448b2cc1508ba8ee250b850a27f5a14c54b47f0c3fc4d9e200d3ddd6546efd134def3c895e3a010a12389a5cb4a004f539ab13983b75386eb5c
7
+ data.tar.gz: 6abba1cd95045a1fb5923edb0d7b360728109d1a3e46ea3736fba9153e8a76234806983803a9cd722ab28669f5bbedd0874d49a55b5940ed4bdf5696172623b7
data/README.md CHANGED
@@ -302,7 +302,39 @@ app:
302
302
  extension: ‘.yml’
303
303
  output_dir: ‘/srv/app/current/reports’
304
304
  output_name: ‘user_report’
305
+ ```
306
+
307
+ In a more advanced example, you can generate multiple files from the same template by setting the `config_index_list` option. If you set this as a list of indexes, then a file will be generated for each index. The index number
308
+ will get passed to the template as an instance variable (`@config_index`) and can be used to look up a specific version of values from the config file. The file that is generated will respect the normal naming, including using the `output_name` option, however, it will append the
309
+ index number to the end of the file name. If the name was going to be special_file, and you add an index list of `[1,2]`, two files will get generated named `special_file-1` and `special_file-2`.
310
+ Advanced example from `config/app:.yml`:
311
+ ```
312
+ app:
313
+ files_to_generate:
314
+ reports:
315
+ extension: '.yml'
316
+ config_index_list:
317
+ - one
318
+ - two
319
+ reports:
320
+ default: &reports_default_values
321
+ some_value: 'some_value'
322
+ other_value: 'other_value'
323
+ one:
324
+ <<: *reports_default_values
325
+ two:
326
+ <<: *reports_default_values
327
+ some_value: 'my_value'
328
+ ```
329
+ Then in your template file you can use the index to get the proper config for each generated file. This allows you to reuse the same template to generate multiple files, each with different content. This is useful for generating service config files, like for sidekiq workers.
330
+ The config above would generate two files: `config/reports-one.yml` and `config/reports-two.yml`, using the content from each index block as specified in the following simple template example.
305
331
  ```
332
+ <% index = "{@config_index}" -%>
333
+ defaults:
334
+ :some_value: <%= @app_config[:reports][index.to_sym][:some_value] %>
335
+ :other_value: <%= @app_config[:reports][index.to_sym][:other_value] %>
336
+ ```
337
+
306
338
  ### vhost_public_dirname
307
339
  **Type: string
308
340
  Default: 'public'
@@ -5,6 +5,7 @@ require 'biran/config_defaults'
5
5
  require 'biran/config'
6
6
  require 'biran/erb_config'
7
7
  require 'biran/configurinator'
8
+ require 'biran/exceptions'
8
9
  require 'biran/railtie' if defined?(Rails)
9
10
 
10
11
  module Biran
@@ -30,12 +30,17 @@ module Biran
30
30
  .tap { |files_list| files_list.each(&sanitize_config_files(files_list)) }
31
31
  end
32
32
 
33
- def create(name:, extension:, output_dir: nil, output_name: nil)
33
+ def create(name:, extension:, output_dir: nil, output_name: nil, config_index_list: [])
34
34
  output_dir ||= config_dir
35
35
  output_name ||= name
36
36
  generated_file = ERBConfig.new(filtered_config, name, extension, config_dir, output_dir, output_name)
37
37
  generated_file.bindings = bindings
38
- generated_file.save!
38
+ return generated_file.save! unless config_index_list.any?
39
+ config_index_list.each do |config_index|
40
+ generated_file.output_name = "#{output_name}-#{config_index}"
41
+ generated_file.template_config_index = config_index
42
+ generated_file.save!
43
+ end
39
44
  end
40
45
 
41
46
  private
@@ -1,34 +1,56 @@
1
1
  module Biran
2
2
  class ERBConfig
3
- attr_reader :output_dir, :source_dir, :name, :extension, :config, :output_name
4
- attr_accessor :bindings
3
+ attr_reader :output_dir, :source_dir, :name, :extension, :config, :template_contents
4
+ attr_accessor :bindings, :output_name, :template_config_index
5
+
6
+ DEFAULT_TEMPLATE_CONFIG_INDEX = 1
5
7
 
6
8
  def initialize(config, name, extension, source, output_dir, output_name)
7
- @name = name
8
- @extension = extension
9
- @config = config
10
- @source_dir = source
11
- @output_dir = output_dir
12
- @output_name = output_name
9
+ before_process_erb do
10
+ @name = name
11
+ @extension = extension
12
+ @config = config
13
+ @source_dir = source
14
+ @output_dir = output_dir
15
+ @output_name = output_name
16
+ end
13
17
  end
14
18
 
15
19
  def save!
16
20
  File.open(File.join(output_dir, "#{output_name}#{extension}"), 'w') do |f|
17
- f.print process_erb.result(build_erb_env.call)
21
+ f.print template_contents.result(build_erb_env.call)
18
22
  end
19
23
  end
20
24
 
21
25
  private
22
26
 
27
+ def before_process_erb
28
+ yield
29
+ raise ArgumentError unless process_erb_ready?
30
+ @template_contents = process_erb
31
+ rescue ArgumentError
32
+ e_message = "Settings required to determine template name for #{name} are not configured"
33
+ raise Biran::ConfigSyntaxError, e_message
34
+ end
35
+
36
+ def process_erb_ready?
37
+ @name &&
38
+ @extension &&
39
+ @source_dir
40
+ end
41
+
23
42
  def process_erb
24
43
  config_erb_file = File.join(source_dir, "_#{name}#{extension}.erb")
25
44
  ERB.new(File.read(config_erb_file), nil, '-')
45
+ rescue Errno::ENOENT
46
+ raise Biran::ConfigSyntaxError, "Missing template file for #{name}: #{config_erb_file}"
26
47
  end
27
48
 
28
49
  def build_erb_env
29
50
  proc do
30
51
  @environment = config[:env]
31
52
  @app_config = config
53
+ @config_index = template_config_index || DEFAULT_TEMPLATE_CONFIG_INDEX
32
54
 
33
55
  @bindings.each(&assign_instance_vars) unless @bindings.nil?
34
56
 
@@ -0,0 +1,16 @@
1
+ module Biran
2
+ class ConfigSyntaxError < ::StandardError
3
+ def initialize(msg=nil)
4
+ @msg = msg || 'Missing required argument or bad formatting in config file'
5
+ set_backtrace []
6
+ end
7
+
8
+ def to_s
9
+ @msg
10
+ end
11
+
12
+ def p_warning
13
+ "Warning: #{@msg}"
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Biran
2
- VERSION = '0.1.11'
2
+ VERSION = '0.1.12'
3
3
  end
@@ -1,18 +1,39 @@
1
1
  namespace :config do
2
2
  config = Biran::Configurinator.new
3
3
 
4
- desc 'Generate new config files'
5
- task :generate do
4
+ desc 'Legacy - Generate all new config files'
5
+ task :generate_legacy do
6
6
  Rake::Task['config:generate_with_deps'].enhance config.file_tasks
7
7
  Rake::Task['config:generate_with_deps'].invoke
8
8
  end
9
9
 
10
10
  task :generate_with_deps
11
11
 
12
+ desc 'Generate all new config files'
13
+ task :generate do
14
+ error_count = 0
15
+ config.file_tasks.each do |task|
16
+ Rake::Task["config:#{task}"].invoke
17
+ rescue Biran::ConfigSyntaxError => e
18
+ error_count += 1
19
+ puts e.p_warning
20
+ next
21
+ end
22
+
23
+ abort 'Errors in creating config files' unless error_count == 0
24
+ end
25
+
12
26
  config.files_to_generate.each do |file_name, options|
13
27
  desc %(Generate the #{file_name}#{options.fetch(:extension, '')} config file)
14
28
  task file_name do
15
29
  config.create name: file_name, **options
30
+ rescue ArgumentError => e
31
+ e_message = "Missing required argument or bad formatting in config file for #{file_name}"
32
+ e.set_backtrace([])
33
+ raise Biran::ConfigSyntaxError, e_message
34
+ rescue Biran::ConfigSyntaxError => e
35
+ e.set_backtrace([])
36
+ raise Biran::ConfigSyntaxError, e.message
16
37
  end
17
38
  end
18
39
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biran
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - javierg
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-04-04 00:00:00.000000000 Z
13
+ date: 2020-09-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -127,6 +127,7 @@ files:
127
127
  - lib/biran/configurinator.rb
128
128
  - lib/biran/configurinator.rb.test_changes
129
129
  - lib/biran/erb_config.rb
130
+ - lib/biran/exceptions.rb
130
131
  - lib/biran/hash_refinement.rb
131
132
  - lib/biran/railtie.rb
132
133
  - lib/biran/version.rb