Sha256: c6ecb15dc69494a23c96bad758777a766b307cb79741af869d25cfe46066fe82

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    class Generator
      # A class that injects a require directive into the root RuboCop file.
      # It looks for other directives that require files in the same (cop)
      # namespace and injects the provided one in alpha
      class ConfigurationInjector
        TEMPLATE = <<~YAML
          %<badge>s:
            Description: 'TODO: Write a description of the cop.'
            Enabled: true
            VersionAdded: '%<version_added>s'
        YAML

        def initialize(configuration_file_path:, badge:, version_added:)
          @configuration_file_path = configuration_file_path
          @badge = badge
          @version_added = version_added
          @output = output
        end

        def inject
          target_line = find_target_line
          if target_line
            configuration_entries.insert(target_line,
                                         new_configuration_entry + "\n")
          else
            configuration_entries.push("\n" + new_configuration_entry)
          end

          File.write(configuration_file_path, configuration_entries.join)

          yield if block_given?
        end

        private

        attr_reader :configuration_file_path, :badge, :version_added, :output

        def configuration_entries
          @configuration_entries ||= File.readlines(configuration_file_path)
        end

        def new_configuration_entry
          format(TEMPLATE, badge: badge, version_added: version_added)
        end

        def find_target_line
          configuration_entries.find.with_index do |line, index|
            next unless cop_name_line?(line)

            return index if badge.to_s < line
          end

          nil
        end

        def cop_name_line?(yaml)
          yaml !~ /^[\s#]/
        end
      end
    end
  end
end

Version data entries

6 entries across 5 versions & 2 rubygems

Version Path
rubocop-0.78.0 lib/rubocop/cop/generator/configuration_injector.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/generator/configuration_injector.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/generator/configuration_injector.rb
rubocop-0.77.0 lib/rubocop/cop/generator/configuration_injector.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/generator/configuration_injector.rb
rubocop-0.76.0 lib/rubocop/cop/generator/configuration_injector.rb