biran 0.1.7 → 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: a67e7f787cbdfa09727e1214e5668cfc20ca239a6765c538b46884f70436cb10
4
- data.tar.gz: c48aaf4d46a5a7a1130a14191113ed9db8ec9e8cbac2d71b6e1f74548fd5db32
3
+ metadata.gz: 4d14217aa3698d0cf75e5ca704c0601debc6c49768668251af03fa552130666f
4
+ data.tar.gz: b99edeb48b785ba764c9cb9ab67a2fb403f3216b20b8c95d315592249a6fd417
5
5
  SHA512:
6
- metadata.gz: c878c531325e65bf3d0ec59b7d5b9b2f41921165d4390839c24c5aa78b94b654ab9af45934e0d3242a5031bc19ba7a9afc7e3be294acb14b141e69c438a01c8a
7
- data.tar.gz: 185b07e2b09bd9fd2d0f666b814a9bb6ba8f5bd0c1e8943031d98cd8bb1fae588d12e59edcba4879df95c120e62a0f208153370621b54e2c0a11c07698ef5f2c
6
+ metadata.gz: 9d660b2a71857448b2cc1508ba8ee250b850a27f5a14c54b47f0c3fc4d9e200d3ddd6546efd134def3c895e3a010a12389a5cb4a004f539ab13983b75386eb5c
7
+ data.tar.gz: 6abba1cd95045a1fb5923edb0d7b360728109d1a3e46ea3736fba9153e8a76234806983803a9cd722ab28669f5bbedd0874d49a55b5940ed4bdf5696172623b7
data/README.md CHANGED
@@ -231,7 +231,7 @@ Availble in: environment, initializer, instance**
231
231
 
232
232
  Generally not needed to specify unless you are not using rails or do not want to use `Rails.env` for lookups in config blocks.
233
233
  You can set the app_env during instance creation by passing an environment string.
234
- The following example will use the default or any of the built in environment vairables(BIRAN_APP_ENV, RACK_ENV, RAILS_ENV) or fall back to default.
234
+ The following example will use the value of one of the built in environment variable found, checked in the following order: BIRAN_APP_ENV, RAILS_ENV, RACK_ENV. If one of the environment variables is not found, the default value will be used.
235
235
  ```
236
236
  config = Biran::Configurinator.new
237
237
  ```
@@ -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
@@ -11,16 +11,16 @@ module Biran
11
11
  shared_dir: configuration.shared_dir,
12
12
  base_dir: configuration.base_dir,
13
13
  use_capistrano: configuration.use_capistrano,
14
- db_config: configuration.db_config,
15
- secrets: configuration.secrets,
16
14
  bindings: configuration.bindings,
17
15
  vhost_public_dirname: configuration.vhost_public_dirname
18
- }
16
+ },
17
+ db_config: configuration.db_config,
18
+ secrets: configuration.secrets,
19
19
  }
20
20
  end
21
21
 
22
22
  def app_env
23
- ENV['BIRAN_APP_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || configuration.app_env
23
+ ENV['BIRAN_APP_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || configuration.app_env
24
24
  end
25
25
 
26
26
  def app_base
@@ -47,7 +47,11 @@ module Biran
47
47
 
48
48
  def local_config_file
49
49
  ENV['BIRAN_LOCAL_CONFIG_FILE'] ||
50
- File.join(app_shared_dir, configuration.config_dirname, configuration.local_config_filename)
50
+ File.join(app_shared_dir, configuration.config_dirname, local_config_filename)
51
+ end
52
+
53
+ def local_config_filename
54
+ ENV['BIRAN_LOCAL_CONFIG_FILENAME'] || app_config_defaults[:app][:local_config_filename] || configuration.local_config_filename
51
55
  end
52
56
 
53
57
  def vhost_public_dirname
@@ -55,15 +59,23 @@ module Biran
55
59
  end
56
60
 
57
61
  def db_config_override_file
58
- File.join(app_shared_dir, configuration.config_dirname, configuration.db_config_filename)
62
+ File.join(app_shared_dir, configuration.config_dirname, db_config_filename)
63
+ end
64
+
65
+ def db_config_filename
66
+ app_config_defaults[:app][:db_config_filename] || configuration.db_config_filename
59
67
  end
60
68
 
61
69
  def secrets_file
62
- File.join(configuration.base_path, configuration.config_dirname, configuration.secrets_filename)
70
+ File.join(configuration.base_path, configuration.config_dirname, secrets_filename)
71
+ end
72
+
73
+ def secrets_filename
74
+ app_config_defaults[:app][:secrets_filename] || configuration.secrets_filename
63
75
  end
64
76
 
65
77
  def default_db_config_file
66
- File.join(config_dir, configuration.db_config_filename)
78
+ File.join(config_dir, db_config_filename)
67
79
  end
68
80
 
69
81
  def use_capistrano?
@@ -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
@@ -53,8 +58,8 @@ module Biran
53
58
  }
54
59
 
55
60
  app_config.deep_merge! app_config_defaults
56
- app_config[:secrets] = get_secret_contents(app_config)
57
- app_config[:db_config] = build_db_config
61
+ app_config[:secrets].deep_merge! get_secrets_content(app_config[:secrets_file_path])
62
+ app_config[:db_config].deep_merge! build_db_config
58
63
 
59
64
  app_config.deep_merge! local_config_file_contents
60
65
  end
@@ -103,12 +108,9 @@ module Biran
103
108
  @local_config_contents = process_config_file(local_config_file)
104
109
  end
105
110
 
106
- def get_secret_contents(app_config)
107
- secrets_file_contents = {}
108
- if File.exist? app_config[:secrets_file_path]
109
- secrets_file_contents = process_config_file app_config[:secrets_file_path]
110
- end
111
- secrets_file_contents
111
+ def get_secrets_content(secrets_file)
112
+ return {} unless File.exists? secrets_file
113
+ process_config_file secrets_file
112
114
  end
113
115
 
114
116
  def sanitize_config_files files_list
@@ -1,10 +1,13 @@
1
+ require 'biran/hash_refinement'
2
+
1
3
  module Biran
2
4
  class Configurinator
3
5
  include ConfigDefaults
6
+ using HashRefinement
4
7
 
5
8
  DEFAULT_ENV = 'development'
6
9
 
7
- attr_reader :config, :db_config
10
+ attr_reader :config, :db_config, :env
8
11
 
9
12
  class << self
10
13
  attr_accessor :config
@@ -13,18 +16,10 @@ module Biran
13
16
  self.config ||= Config.instance
14
17
  yield config
15
18
  end
16
-
17
- def env= env
18
- @env = env
19
- end
20
- end
21
-
22
- def env
23
- return @env if @env
24
- @env = app_env
25
19
  end
26
20
 
27
- def initialize
21
+ def initialize(env: nil)
22
+ @env = env || app_env
28
23
  @config = build_app_config
29
24
  end
30
25
 
@@ -32,23 +27,16 @@ module Biran
32
27
  files_to_generate.keys
33
28
  end
34
29
 
35
- def debug_stuff
36
- puts "app env2 is: #{app_env}"
37
- puts "instance @env is #{@env}"
38
- puts "env from method is #{env}"
39
- puts "rack env is #{ENV['RACK_ENV']}"
40
- end
41
-
42
30
  def files_to_generate
43
31
  @files_to_generate ||= config.fetch(:app, {})
44
32
  .fetch(:files_to_generate, configuration.files_to_generate)
45
33
  .tap { |files_list| files_list.each(&sanitize_config_files(files_list)) }
46
34
  end
47
35
 
48
- def create(name:, extension:, output_dir: nil)
36
+ def create(name:, extension:, output_dir: nil, output_name: nil)
49
37
  output_dir ||= config_dir
50
- #debug_stuff
51
- generated_file = ERBConfig.new(filtered_config, name, extension, config_dir, output_dir)
38
+ output_name ||= name
39
+ generated_file = ERBConfig.new(filtered_config, name, extension, config_dir, output_dir, output_name)
52
40
  generated_file.bindings = bindings
53
41
  generated_file.save!
54
42
  end
@@ -56,6 +44,7 @@ module Biran
56
44
  private
57
45
 
58
46
  def build_app_config
47
+ raise 'Environment not set to build the application config' unless @env
59
48
  app_config = {
60
49
  app_root_dir: app_root,
61
50
  app_shared_dir: app_shared_dir,
@@ -67,10 +56,13 @@ module Biran
67
56
  }
68
57
 
69
58
  app_config.deep_merge! app_config_defaults
70
- app_config[:secrets] = get_secret_contents(app_config)
59
+ app_config[:secrets].deep_merge! get_secret_contents(app_config)
71
60
  app_config[:db_config] = build_db_config
72
61
 
73
62
  app_config.deep_merge! local_config_file_contents
63
+
64
+ puts "app config final is #{app_config.inspect}"
65
+ app_config
74
66
  end
75
67
 
76
68
  def build_db_config
@@ -97,7 +89,25 @@ module Biran
97
89
  config_file_contents = File.read(config_file)
98
90
  config_file_contents = ERB.new(config_file_contents).result
99
91
  config_file_contents = YAML.safe_load(config_file_contents, [], [], true)
100
- config_file_contents[env].deep_symbolize_keys!
92
+ #puts "Config file contents after yaml load before env grab #{config_file_contents.inspect}"
93
+ config_file_contents_for_test = config_file_contents['test']
94
+ config_file_contents_for_env = config_file_contents[env]
95
+ puts "Config file contents for test:"
96
+ puts config_file_contents_for_test.to_yaml
97
+ puts
98
+ puts "Config file contents for #{env}:"
99
+ puts config_file_contents_for_env.to_yaml
100
+ puts
101
+ config_file_diff = config_file_contents_for_test.deep_diff(config_file_contents_for_env)
102
+ puts "diff hash is:"
103
+ puts config_file_diff.to_yaml
104
+ puts
105
+ config_file_diff_r = config_file_contents_for_env.deep_diff(config_file_contents_for_test)
106
+ puts "diff hash reversed is:"
107
+ puts config_file_diff_r.to_yaml
108
+ puts
109
+
110
+ config_file_contents_for_env.deep_symbolize_keys!
101
111
  rescue Errno::ENOENT
102
112
  raise "Missing config file: #{config_file}"
103
113
  end
@@ -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
@@ -0,0 +1,19 @@
1
+ module HashRefinement
2
+
3
+ refine Hash do
4
+ def deep_diff(b)
5
+ a = self
6
+ (a.keys | b.keys).inject({}) do |diff, k|
7
+ if a[k] != b[k]
8
+ if a[k].respond_to?(:deep_diff) && b[k].respond_to?(:deep_diff)
9
+ diff[k] = a[k].deep_diff(b[k])
10
+ else
11
+ #diff[k] = b[k] || a[k]
12
+ diff[k] = b.keys.exclude?(k) ? a[k] : b[k]
13
+ end
14
+ end
15
+ diff
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Biran
2
- VERSION = '0.1.7'
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.7
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - javierg
@@ -10,48 +10,48 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-05-28 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
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - "~>"
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: 5.0.7
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - "~>"
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: 5.0.7
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: activesupport
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - "~>"
33
+ - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: 5.0.7
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - "~>"
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: 5.0.7
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: rails
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - "~>"
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: 5.0.7
50
50
  type: :development
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - "~>"
54
+ - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: 5.0.7
57
57
  - !ruby/object:Gem::Dependency
@@ -125,8 +125,10 @@ files:
125
125
  - lib/biran/config.rb
126
126
  - lib/biran/config_defaults.rb
127
127
  - lib/biran/configurinator.rb
128
- - lib/biran/configurinator.rb.debug
128
+ - lib/biran/configurinator.rb.test_changes
129
129
  - lib/biran/erb_config.rb
130
+ - lib/biran/exceptions.rb
131
+ - lib/biran/hash_refinement.rb
130
132
  - lib/biran/railtie.rb
131
133
  - lib/biran/version.rb
132
134
  - lib/tasks/biran_tasks.rake