Sha256: 209d44b4a40932f5d800d6bbb440e3139b08a7ba2bd6e9baa3c7787cc5335138

Contents?: true

Size: 963 Bytes

Versions: 8

Compression:

Stored size: 963 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  # This class handles collecting the options for regenerating a TODO file.
  # @api private
  class ConfigRegeneration
    AUTO_GENERATED_FILE = RuboCop::CLI::Command::AutoGenerateConfig::AUTO_GENERATED_FILE
    COMMAND_REGEX = /(?<=`rubocop )(.*?)(?=`)/.freeze
    DEFAULT_OPTIONS = { auto_gen_config: true }.freeze

    # Get options from the comment in the TODO file, and parse them as options
    def options
      # If there's no existing TODO file, generate one
      return DEFAULT_OPTIONS unless todo_exists?

      match = generation_command.match(COMMAND_REGEX)
      return DEFAULT_OPTIONS unless match

      options = match[1].split(' ')
      Options.new.parse(options).first
    end

    private

    def todo_exists?
      File.exist?(AUTO_GENERATED_FILE) && !File.empty?(AUTO_GENERATED_FILE)
    end

    def generation_command
      File.foreach(AUTO_GENERATED_FILE).take(2).last
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-1.3.1 lib/rubocop/config_regeneration.rb
rubocop-1.3.0 lib/rubocop/config_regeneration.rb
rubocop-1.2.0 lib/rubocop/config_regeneration.rb
rubocop-1.1.0 lib/rubocop/config_regeneration.rb
rubocop-1.0.0 lib/rubocop/config_regeneration.rb
rubocop-0.93.1 lib/rubocop/config_regeneration.rb
rubocop-0.93.0 lib/rubocop/config_regeneration.rb
rubocop-0.92.0 lib/rubocop/config_regeneration.rb