Sha256: 355581d6b1ecff9c5c15d1afbf58d5252f6f6f9dcfcb8fbc17f47efae8cd41d7

Contents?: true

Size: 1.82 KB

Versions: 97

Compression:

Stored size: 1.82 KB

Contents

module CC
  class Config
    class YAMLAdapter
      DEFAULT_PATH = ".codeclimate.yml".freeze

      attr_reader :config

      def self.load(path = DEFAULT_PATH)
        new(::YAML.safe_load(File.read(path)))
      end

      def initialize(yaml = {})
        @config = yaml || {}

        upconvert_legacy_yaml!
      end

      private

      def coerce_engine(data)
        if [true, false].include?(data)
          { "enabled" => data }
        elsif data.is_a?(Hash)
          data
        else
          {}
        end
      end

      # Many of our plugins still expect:
      #
      #   { config: PATH }
      #
      # But we document, and hope to eventually move to:
      #
      #   { config: { file: PATH } }
      #
      # We need to munge from the latter to the former when/if we encounter it
      def convert_to_legacy_file_config(config)
        if config.is_a?(Hash) && config.keys.one? && config.key?("file")
          config["file"]
        else
          config
        end
      end

      def upconvert_legacy_yaml!
        config.delete("ratings")

        if config.key?("engines")
          config["plugins"] ||= config.delete("engines")
        end

        plugins = config.fetch("plugins", {})
        plugins.each do |engine, data|
          plugins[engine] = coerce_engine(data)
          if plugins.fetch(engine)["exclude_paths"]
            plugins.fetch(engine)["exclude_patterns"] ||= Array(plugins.fetch(engine).delete("exclude_paths"))
          end
          if plugins.fetch(engine)["config"]
            plugins.fetch(engine)["config"] = convert_to_legacy_file_config(
              plugins.fetch(engine).fetch("config"),
            )
          end
        end

        if config.key?("exclude_paths")
          config["exclude_patterns"] ||= Array(config.delete("exclude_paths"))
        end
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 2 rubygems

Version Path
codeclimate-0.85.27 lib/cc/config/yaml_adapter.rb
codeclimate-0.85.26 lib/cc/config/yaml_adapter.rb
codeclimate-0.85.25 lib/cc/config/yaml_adapter.rb
codeclimate-0.85.24 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.54 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.53 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.52 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.51 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.50 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.49 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.48 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.47 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.46 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.45 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.44 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.43 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.42 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.41 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.40 lib/cc/config/yaml_adapter.rb
codeclimate-fede-0.85.39 lib/cc/config/yaml_adapter.rb