Sha256: ae4cf98c88dce6965d50382d8ec40987eeacbb73cee4f877faec4d35d8002270

Contents?: true

Size: 1.96 KB

Versions: 17

Compression:

Stored size: 1.96 KB

Contents

# typed: strict
# frozen_string_literal: true

require 'yaml'

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

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

      private

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

      sig { params(command: Symbol).returns(T::Hash[String, T.untyped]) }
      def default_options(command)
        default_outdir = case command
        when :sync, :generate
          Config::DEFAULT_GEMDIR
        when :dsl
          Config::DEFAULT_DSLDIR
        else
          Config::SORBET_PATH
        end

        DEFAULT_OPTIONS.merge("outdir" => default_outdir)
      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" => nil,
      "generate_command" => default_command,
      "exclude" => [],
      "typed_overrides" => Config::DEFAULT_OVERRIDES,
      "todos_path" => Config::DEFAULT_TODOSPATH,
      "generators" => [],
    }.freeze, T::Hash[String, T.untyped])
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
tapioca-0.4.17 lib/tapioca/config_builder.rb
tapioca-0.4.16 lib/tapioca/config_builder.rb
tapioca-0.4.15 lib/tapioca/config_builder.rb
tapioca-0.4.14 lib/tapioca/config_builder.rb
tapioca-0.4.13 lib/tapioca/config_builder.rb
tapioca-0.4.12 lib/tapioca/config_builder.rb
tapioca-0.4.11 lib/tapioca/config_builder.rb
tapioca-0.4.10 lib/tapioca/config_builder.rb
tapioca-0.4.9 lib/tapioca/config_builder.rb
tapioca-0.4.8 lib/tapioca/config_builder.rb
tapioca-0.4.7 lib/tapioca/config_builder.rb
tapioca-0.4.6 lib/tapioca/config_builder.rb
tapioca-0.4.5 lib/tapioca/config_builder.rb
tapioca-0.4.4 lib/tapioca/config_builder.rb
tapioca-0.4.3 lib/tapioca/config_builder.rb
tapioca-0.4.2 lib/tapioca/config_builder.rb
tapioca-0.4.1 lib/tapioca/config_builder.rb