Sha256: 3cb3955cfe194a318b3ca11956ce3d3431ea2b32371736ac7571542f76bc9da6

Contents?: true

Size: 1.21 KB

Versions: 33

Compression:

Stored size: 1.21 KB

Contents

require 'tempfile'
require 'yaml'

module Govuk
  module Lint
    class ConfigFile
      CONFIG_PATH = File.expand_path("../../../../configs", __FILE__)
      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)

        config = merged_global_and_local_configs
        file = create_tempfile_for_configs(config)
        file.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 create_tempfile_for_configs(config)
        file = Tempfile.new('tmp-rubocop-all.yml')
        file.write(config.to_yaml)
        file.close
        file
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
govuk-lint-3.11.2 lib/govuk/lint/config_file.rb
govuk-lint-3.11.1 lib/govuk/lint/config_file.rb
govuk-lint-3.11.0 lib/govuk/lint/config_file.rb
govuk-lint-3.10.0 lib/govuk/lint/config_file.rb
govuk-lint-3.9.0 lib/govuk/lint/config_file.rb
govuk-lint-3.8.0 lib/govuk/lint/config_file.rb
govuk-lint-3.7.0 lib/govuk/lint/config_file.rb
govuk-lint-3.6.0 lib/govuk/lint/config_file.rb
govuk-lint-3.5.0 lib/govuk/lint/config_file.rb
govuk-lint-3.4.0 lib/govuk/lint/config_file.rb
govuk-lint-3.3.1 lib/govuk/lint/config_file.rb
govuk-lint-3.3.0 lib/govuk/lint/config_file.rb
govuk-lint-3.2.0 lib/govuk/lint/config_file.rb
govuk-lint-3.1.0 lib/govuk/lint/config_file.rb
govuk-lint-3.0.0 lib/govuk/lint/config_file.rb
govuk-lint-2.1.0 lib/govuk/lint/config_file.rb
govuk-lint-2.0.0 lib/govuk/lint/config_file.rb
govuk-lint-1.2.1 lib/govuk/lint/config_file.rb
govuk-lint-1.2.0 lib/govuk/lint/config_file.rb
govuk-lint-1.1.0 lib/govuk/lint/config_file.rb