biran 0.1.13.1 → 0.1.14
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 +4 -4
- data/config/app_config.yml +13 -0
- data/lib/biran/configurinator.rb +7 -1
- data/lib/biran/configurinator.rb.test_changes +155 -0
- data/lib/biran/hash_refinement.rb +19 -0
- data/lib/biran/version.rb +1 -1
- metadata +6 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 724bb696cb3fefd277f0d8c892eab6a3bb247fe2cf8015fc721c9d79f5c3fc78
         | 
| 4 | 
            +
              data.tar.gz: 00af99015b69fa0d49f5ebd4f67fa237729416a1ae9aacbf81bb0bd1aab54482
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 4613fff5623383163a13cb4592c4b869efbdd0ac08b81494f0e6db4e80ef8356d44ff538dcd03b09df873bff1484010fbd093f045f436d4c19d9e6ec6e4806eb
         | 
| 7 | 
            +
              data.tar.gz: 45744b7d551dbd3f3df0d841fc9fcf8de8cda94fdafae3c20a05545e3b708acde2cb9378fbf69dc39ad149dcc2369c174e53834947c7c55088cec4d9f6a75f17
         | 
    
        data/config/app_config.yml
    CHANGED
    
    | @@ -5,9 +5,22 @@ defaults: &defaults | |
| 5 5 | 
             
                files_to_generate:
         | 
| 6 6 | 
             
                  my_config:
         | 
| 7 7 | 
             
                    extension: '.yml'
         | 
| 8 | 
            +
              my_first_value: 1
         | 
| 9 | 
            +
              my_second_value: 2
         | 
| 10 | 
            +
              nested_values: &nested_defaults
         | 
| 11 | 
            +
                my_third_value: 3
         | 
| 12 | 
            +
                my_fourth_value: 4
         | 
| 8 13 |  | 
| 9 14 | 
             
            test:
         | 
| 10 15 | 
             
              <<: *defaults
         | 
| 16 | 
            +
              my_first_value: 11
         | 
| 17 | 
            +
              nested_values:
         | 
| 18 | 
            +
                <<: *nested_defaults
         | 
| 19 | 
            +
                my_third_value: 13
         | 
| 11 20 |  | 
| 12 21 | 
             
            development:
         | 
| 13 22 | 
             
              <<: *defaults
         | 
| 23 | 
            +
              my_first_value: 11
         | 
| 24 | 
            +
              nested_values:
         | 
| 25 | 
            +
                <<: *nested_defaults
         | 
| 26 | 
            +
                my_third_value: 13
         | 
    
        data/lib/biran/configurinator.rb
    CHANGED
    
    | @@ -87,7 +87,7 @@ module Biran | |
| 87 87 | 
             
                def process_config_file(config_file)
         | 
| 88 88 | 
             
                  config_file_contents = File.read(config_file)
         | 
| 89 89 | 
             
                  config_file_contents = ERB.new(config_file_contents).result
         | 
| 90 | 
            -
                  config_file_contents =  | 
| 90 | 
            +
                  config_file_contents = yaml_load(config_file_contents)
         | 
| 91 91 | 
             
                  config_file_contents[env].deep_symbolize_keys!
         | 
| 92 92 | 
             
                rescue Errno::ENOENT
         | 
| 93 93 | 
             
                  raise "Missing config file: #{config_file}"
         | 
| @@ -122,6 +122,12 @@ module Biran | |
| 122 122 | 
             
                  end
         | 
| 123 123 | 
             
                end
         | 
| 124 124 |  | 
| 125 | 
            +
                def yaml_load data_content
         | 
| 126 | 
            +
                  # Ruby 3.1 with Psych 4 allows yaml-aliases only in direct manner
         | 
| 127 | 
            +
                  return YAML.safe_load(data_content, [], [], true) if defined?(Psych::VERSION) && Gem::Version.new(Psych::VERSION) < Gem::Version.new('4.0')
         | 
| 128 | 
            +
                  YAML.safe_load(data_content, aliases: true)
         | 
| 129 | 
            +
                end
         | 
| 130 | 
            +
             | 
| 125 131 | 
             
                def filtered_config
         | 
| 126 132 | 
             
                  @filtered_config ||= config.except(*configuration.app_setup_blocks)
         | 
| 127 133 | 
             
                end
         | 
| @@ -0,0 +1,155 @@ | |
| 1 | 
            +
            require 'biran/hash_refinement'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Biran
         | 
| 4 | 
            +
              class Configurinator
         | 
| 5 | 
            +
                include ConfigDefaults
         | 
| 6 | 
            +
                using HashRefinement
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                DEFAULT_ENV = 'development'
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                attr_reader :config, :db_config, :env
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                class << self
         | 
| 13 | 
            +
                  attr_accessor :config
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  def configure
         | 
| 16 | 
            +
                    self.config ||= Config.instance
         | 
| 17 | 
            +
                    yield config
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                def initialize(env: nil)
         | 
| 22 | 
            +
                  @env = env || app_env
         | 
| 23 | 
            +
                  @config = build_app_config
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                def file_tasks
         | 
| 27 | 
            +
                  files_to_generate.keys
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                def files_to_generate
         | 
| 31 | 
            +
                  @files_to_generate ||= config.fetch(:app, {})
         | 
| 32 | 
            +
                    .fetch(:files_to_generate, configuration.files_to_generate)
         | 
| 33 | 
            +
                    .tap { |files_list| files_list.each(&sanitize_config_files(files_list)) }
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                def create(name:, extension:, output_dir: nil, output_name: nil)
         | 
| 37 | 
            +
                  output_dir ||= config_dir
         | 
| 38 | 
            +
                  output_name ||= name
         | 
| 39 | 
            +
                  generated_file = ERBConfig.new(filtered_config, name, extension, config_dir, output_dir, output_name)
         | 
| 40 | 
            +
                  generated_file.bindings = bindings
         | 
| 41 | 
            +
                  generated_file.save!
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                private
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                def build_app_config
         | 
| 47 | 
            +
                  raise 'Environment not set to build the application config' unless @env
         | 
| 48 | 
            +
                  app_config = {
         | 
| 49 | 
            +
                    app_root_dir: app_root,
         | 
| 50 | 
            +
                    app_shared_dir: app_shared_dir,
         | 
| 51 | 
            +
                    app_base_dir: app_base,
         | 
| 52 | 
            +
                    env: env,
         | 
| 53 | 
            +
                    local_config_file: local_config_file,
         | 
| 54 | 
            +
                    secrets_file_path: secrets_file,
         | 
| 55 | 
            +
                    vhost: config_vhost_dirs
         | 
| 56 | 
            +
                  }
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                  app_config.deep_merge! app_config_defaults
         | 
| 59 | 
            +
                  app_config[:secrets].deep_merge! get_secret_contents(app_config)
         | 
| 60 | 
            +
                  app_config[:db_config] = build_db_config
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  app_config.deep_merge! local_config_file_contents
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                  puts "app config final is #{app_config.inspect}"
         | 
| 65 | 
            +
                  app_config
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                def build_db_config
         | 
| 69 | 
            +
                  default_db_config = base_db_config
         | 
| 70 | 
            +
                  return default_db_config unless File.exist? db_config_override_file
         | 
| 71 | 
            +
                  default_db_config.deep_merge! process_config_file(db_config_override_file)
         | 
| 72 | 
            +
                end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                def base_db_config
         | 
| 75 | 
            +
                  return @base_db_config if @base_db_config
         | 
| 76 | 
            +
                  return @base_db_config = {} unless File.exists? default_db_config_file
         | 
| 77 | 
            +
                  @base_db_config ||= process_config_file(default_db_config_file)
         | 
| 78 | 
            +
                end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                def app_config_defaults
         | 
| 81 | 
            +
                  return @app_config_defaults if @app_config_defaults
         | 
| 82 | 
            +
                  app_config_file = File.join(configuration.config_dirname, configuration.config_filename)
         | 
| 83 | 
            +
                  app_defaults = app_defaults_init.dup
         | 
| 84 | 
            +
                  config_properties = process_config_file(app_config_file)
         | 
| 85 | 
            +
                  @app_config_defaults = app_defaults.deep_merge! config_properties
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                def process_config_file(config_file)
         | 
| 89 | 
            +
                  config_file_contents = File.read(config_file)
         | 
| 90 | 
            +
                  config_file_contents = ERB.new(config_file_contents).result
         | 
| 91 | 
            +
                  config_file_contents = YAML.safe_load(config_file_contents, [], [], true)
         | 
| 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!
         | 
| 111 | 
            +
                rescue Errno::ENOENT
         | 
| 112 | 
            +
                  raise "Missing config file: #{config_file}"
         | 
| 113 | 
            +
                end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                def config_vhost_dirs
         | 
| 116 | 
            +
                  {
         | 
| 117 | 
            +
                    public_dir: File.join(app_root, vhost_public_dirname),
         | 
| 118 | 
            +
                    shared_dir: app_shared_dir.to_s,
         | 
| 119 | 
            +
                    log_dir: File.join(app_root, 'log'),
         | 
| 120 | 
            +
                    pids_dir: File.join(app_root, 'tmp', 'pids')
         | 
| 121 | 
            +
                  }
         | 
| 122 | 
            +
                end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                def local_config_file_contents
         | 
| 125 | 
            +
                  return @local_config_contents if @local_config_contents
         | 
| 126 | 
            +
                  return @local_config_conents = {} unless File.exists? local_config_file
         | 
| 127 | 
            +
                  @local_config_contents = process_config_file(local_config_file)
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                def get_secret_contents(app_config)
         | 
| 131 | 
            +
                  secrets_file_contents = {}
         | 
| 132 | 
            +
                  if File.exist? app_config[:secrets_file_path]
         | 
| 133 | 
            +
                    secrets_file_contents = process_config_file app_config[:secrets_file_path]
         | 
| 134 | 
            +
                  end
         | 
| 135 | 
            +
                  secrets_file_contents
         | 
| 136 | 
            +
                end
         | 
| 137 | 
            +
             | 
| 138 | 
            +
                def sanitize_config_files files_list
         | 
| 139 | 
            +
                  lambda do |file, _|
         | 
| 140 | 
            +
                    files_list[file] ||=  {extension: ''}
         | 
| 141 | 
            +
                    ext = files_list[file].fetch(:extension, '').strip
         | 
| 142 | 
            +
                    ext.prepend('.') unless ext.start_with?('.') || ext.empty?
         | 
| 143 | 
            +
                    files_list[file][:extension] = ext
         | 
| 144 | 
            +
                  end
         | 
| 145 | 
            +
                end
         | 
| 146 | 
            +
             | 
| 147 | 
            +
                def filtered_config
         | 
| 148 | 
            +
                  @filtered_config ||= config.except(*configuration.app_setup_blocks)
         | 
| 149 | 
            +
                end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
                def use_capistrano?
         | 
| 152 | 
            +
                  app_config_defaults[:app][:use_capistrano]
         | 
| 153 | 
            +
                end
         | 
| 154 | 
            +
              end
         | 
| 155 | 
            +
            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
         | 
    
        data/lib/biran/version.rb
    CHANGED
    
    
    
        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. | 
| 4 | 
            +
              version: 0.1.14
         | 
| 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:  | 
| 13 | 
            +
            date: 2022-09-01 00:00:00.000000000 Z
         | 
| 14 14 | 
             
            dependencies:
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 16 16 | 
             
              name: railties
         | 
| @@ -133,15 +133,18 @@ files: | |
| 133 133 | 
             
            - lib/biran/config.rb
         | 
| 134 134 | 
             
            - lib/biran/config_defaults.rb
         | 
| 135 135 | 
             
            - lib/biran/configurinator.rb
         | 
| 136 | 
            +
            - lib/biran/configurinator.rb.test_changes
         | 
| 136 137 | 
             
            - lib/biran/erb_config.rb
         | 
| 137 138 | 
             
            - lib/biran/exceptions.rb
         | 
| 139 | 
            +
            - lib/biran/hash_refinement.rb
         | 
| 138 140 | 
             
            - lib/biran/railtie.rb
         | 
| 139 141 | 
             
            - lib/biran/version.rb
         | 
| 140 142 | 
             
            - lib/tasks/biran_tasks.rake
         | 
| 141 143 | 
             
            homepage: https://github.com/amco/biran
         | 
| 142 144 | 
             
            licenses:
         | 
| 143 145 | 
             
            - MIT
         | 
| 144 | 
            -
            metadata: | 
| 146 | 
            +
            metadata:
         | 
| 147 | 
            +
              rubygems_mfa_required: 'true'
         | 
| 145 148 | 
             
            post_install_message:
         | 
| 146 149 | 
             
            rdoc_options: []
         | 
| 147 150 | 
             
            require_paths:
         |