Sha256: 0df8209fe2d74a0da92d6aa64772e4e394665d13622f0a29e93657e1d57dd031

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

require "tempfile"
require "yaml"

module Govuk
  module Lint
    class ConfigFile
      CONFIG_PATH = File.expand_path("../../../configs", __dir__)
      BASE_CONFIG_FILE = File.join(CONFIG_PATH, "rubocop/all.yml")

      def config_file_path
        return BASE_CONFIG_FILE unless File.exist?(local_config_file_path)

        tempfile_for_configs.path
      end

    private

      def merged_global_and_local_configs
        config = load_global_config
        config["inherit_from"] = absolutize_paths(config)
        config["inherit_from"] << local_config_file_path
        config
      end

      def load_global_config
        YAML.load_file(BASE_CONFIG_FILE)
      end

      def absolutize_paths(config)
        config["inherit_from"].map do |filename|
          File.join(CONFIG_PATH, "rubocop/#{filename}")
        end
      end

      def local_config_file_path
        @local_config_file_path ||= File.join(Dir.pwd, ".rubocop.yml")
      end

      def tempfile_for_configs
        @tempfile_for_configs ||= begin
                                   file = Tempfile.new("tmp-rubocop-all.yml")
                                   file.write(merged_global_and_local_configs.to_yaml)
                                   file.close
                                   file
                                 end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
govuk-lint-4.3.0 lib/govuk/lint/config_file.rb
govuk-lint-4.2.0 lib/govuk/lint/config_file.rb
govuk-lint-4.1.0 lib/govuk/lint/config_file.rb
govuk-lint-4.0.1 lib/govuk/lint/config_file.rb