Sha256: d2caabc39cfb424dc063c97f255f6570be5ff8f1d8ce365826a1780a387b0cda

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# lib/linter_changes/linter/rubocop/adapter.rb

module LinterChanges
  module Linter
    module RuboCop
      class Adapter < Base
        # By default, anything containing rubocop on the file path will be consider a config file
        DEFAULT_CONFIG_FILES = [/rubocop/].freeze
        DEFAULT_COMMAND = 'bin/rubocop'.freeze

        def list_target_files
          cmd = "#{DEFAULT_COMMAND} --list-target-files"
          Logger.debug "Executing command: #{cmd}"

          stdout, stderr, status = Open3.capture3(cmd)
          unless status.success?
            Logger.error "Error listing RuboCop target files: #{stderr}"
            exit 1
          end

          stdout.strip.split("\n")
        end

        def run(files: [])
          cmd = if files.empty?
                  @command
                else
                  "#{@command} #{files.join(' ')}"
                end
          execute_linter(cmd)
        end

        def config_changed?(changed_files)
          # Check if any of the config files have changed
          return true if super

          # TODO: get the location of Gemfile.lock with bundle command
          # Check if Gemfile.lock has changed and contains something related to rubocop
          if changed_files.include?('Gemfile.lock') && @git_diff.changed_lines_contains?(file: 'Gemfile.lock',
                                                                                        pattern: 'rubocop')
            Logger.debug 'Something related to rubocop gem version changed in Gemfile.lock'
            return true
          end
          false
        end

        private

        def default_config_files
          DEFAULT_CONFIG_FILES
        end

        def default_command
          DEFAULT_COMMAND
        end

        def execute_linter(command)
          Logger.debug "Executing RuboCop command: #{command}"
          system(command)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
linter_changes-0.2.0 lib/linter/rubocop/adapter.rb
linter_changes-0.1.0 lib/linter/rubocop/adapter.rb