Sha256: e91d6341c1336246d2e92835b324750a66583223226f15b37d60556881576ab6

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# typed: strict
# frozen_string_literal: true

require 'yaml'

module Tapioca
  class ConfigBuilder
    class << self
      extend(T::Sig)

      sig { params(options: T::Hash[String, T.untyped]).returns(Config) }
      def from_options(options)
        Config.from_hash(
          merge_options(default_options, config_options, options)
        )
      end

      private

      sig { returns(T::Hash[String, T.untyped]) }
      def config_options
        if File.exist?(Config::CONFIG_FILE_PATH)
          YAML.load_file(Config::CONFIG_FILE_PATH, fallback: {})
        else
          {}
        end
      end

      sig { returns(T::Hash[String, T.untyped]) }
      def default_options
        DEFAULT_OPTIONS
      end

      sig { returns(String) }
      def default_command
        command = File.basename($PROGRAM_NAME)
        args = ARGV.join(" ")

        "#{command} #{args}".strip
      end

      sig { params(options: T::Hash[String, T.untyped]).returns(T::Hash[String, T.untyped]) }
      def merge_options(*options)
        options.each_with_object({}) do |option, result|
          result.merge!(option) do |_, this_val, other_val|
            if this_val.is_a?(Hash) && other_val.is_a?(Hash)
              this_val.merge(other_val)
            else
              other_val
            end
          end
        end
      end
    end

    DEFAULT_OPTIONS = T.let({
      "postrequire" => Config::DEFAULT_POSTREQUIRE,
      "outdir" => Config::DEFAULT_OUTDIR,
      "generate_command" => default_command,
      "exclude" => [],
      "typed_overrides" => Config::DEFAULT_OVERRIDES,
      "todos_path" => Config::DEFAULT_TODOSPATH,
    }.freeze, T::Hash[String, T.untyped])
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tapioca-0.4.0 lib/tapioca/config_builder.rb