class NdrDevSupport::Rubocop::Executor

This class filters the Rubocop report of a file to only the given lines.

Public Class Methods

new(filenames) click to toggle source
# File lib/ndr_dev_support/rubocop/executor.rb, line 17
def initialize(filenames)
  @filenames = Executor.target_files & filenames

  check_ruby_syntax
end
target_files() click to toggle source

Use RuboCop to produce a list of all files that should be scanned.

# File lib/ndr_dev_support/rubocop/executor.rb, line 12
def target_files
  @target_files ||= `rubocop -L 2>/dev/null`.each_line.map(&:strip)
end

Public Instance Methods

offenses_by_file() click to toggle source
# File lib/ndr_dev_support/rubocop/executor.rb, line 23
def offenses_by_file
  return [] if @filenames.empty?

  output = JSON.parse(`rubocop --format json #{escaped_paths.join(' ')} 2>/dev/null`)

  output['files'].each_with_object({}) do |file_output, result|
    result[file_output['path']] = file_output['offenses']
  end
end

Private Instance Methods

check_ruby_syntax() click to toggle source
# File lib/ndr_dev_support/rubocop/executor.rb, line 39
def check_ruby_syntax
  escaped_paths.each do |path|
    stdout_and_err_str, status = Open3.capture2e("ruby -c #{path}")
    next if status.exitstatus.zero?

    raise stdout_and_err_str
  end
end
escaped_paths() click to toggle source
# File lib/ndr_dev_support/rubocop/executor.rb, line 35
def escaped_paths
  @escaped_paths ||= @filenames.map { |path| Shellwords.escape(path) }
end