Sha256: 3238afddb6c9f2bf5bcc3458d751040e42d2c35213d6c354412d9be060cd80ab

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

require 'yaml'
require 'erb'
require 'ostruct'

class MonoRepoDeps::Config::Loader
  include MonoRepoDeps::Mixins

  DEFAULT_CONFIG_FILENAME = 'default.yml'

  sig do
    params(
      config_name: T.any(String, Symbol),
    )
    .returns(MonoRepoDeps::Config)
  end
  def call(config_name)
    config_name = config_name.to_s
    config = MonoRepoDeps::Config.new

    default_config_filename = File.join(
      MonoRepoDeps.current_project.root_path,
      MonoRepoDeps.current_project.configs_dir,
      config_name,
      DEFAULT_CONFIG_FILENAME
    )

    unless File.exist?(default_config_filename)
      raise StandardError.new("Config '#{config_name}' was not found. Add it to '#{default_config_filename}'")
    end

    hash =
      begin
        YAML.load( ERB.new(File.read(default_config_filename)).result )
      rescue => e
        puts ERB.new(File.read(default_config_filename)).result.inspect
      end

    hash_to_config(
      hash: hash,
      config: config
    )

    env_config_filename = File.join(
      MonoRepoDeps.current_project.root_path,
      MonoRepoDeps.current_project.configs_dir,
      config_name,
      "#{MonoRepoDeps.current_project.env}.yml"
    )

    hash_to_config(
      hash: YAML.load( ERB.new(File.read(env_config_filename)).result ),
      config: config
    ) if File.exist?(env_config_filename)

    config.send(config_name)
  end

  private

  def hash_to_config(hash:, config:)
    hash.each do |key, value|
      if value.is_a?(Hash)
        nested_config = config.read_attribute(key)
        if nested_config.nil? || !nested_config.is_a?(MonoRepoDeps::Config)
          nested_config = MonoRepoDeps::Config.new
        end
        hash_to_config(hash: value, config: nested_config)
        config.write_attribute(key, nested_config)
      else
        config.write_attribute(key, value)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mono_repo_deps-0.2.1 lib/mono_repo_deps/config/loader.rb
mono_repo_deps-0.2.0 lib/mono_repo_deps/config/loader.rb
mono_repo_deps-0.1.14 lib/mono_repo_deps/config/loader.rb
mono_repo_deps-0.1.13 lib/mono_repo_deps/config/loader.rb