class Ruumba::Analyzer

Generates analyzer objects that, when run, delegate to RuboCop for linting (style, correctness, &c).

Attributes

options[R]

Public Class Methods

new(opts = nil) click to toggle source
# File lib/ruumba/analyzer.rb, line 20
def initialize(opts = nil)
  @options = opts || {}
end

Public Instance Methods

run(files_or_dirs = ARGV) click to toggle source

Performs static analysis on the provided directory. @param [Array<String>] dir The directories / files to analyze.

# File lib/ruumba/analyzer.rb, line 26
def run(files_or_dirs = ARGV)
  if options[:tmp_folder]
    analyze(File.expand_path(options[:tmp_folder]), files_or_dirs)
  else
    Dir.mktmpdir do |dir|
      analyze(dir, files_or_dirs)
    end
  end
end

Private Instance Methods

analyze(temp_dir, files_or_dirs) click to toggle source
# File lib/ruumba/analyzer.rb, line 40
def analyze(temp_dir, files_or_dirs)
  temp_dir_path = Pathname.new(temp_dir)

  iterator, corrector =
    if stdin?
      [Iterators::StdinIterator.new(File.expand_path(stdin_filename)), Correctors::StdinCorrector.new(digestor, parser)]
    else
      [Iterators::DirectoryIterator.new(files_or_dirs, temp_dir.to_s), Correctors::FileCorrector.new(digestor, parser)]
    end

  iterator.each do |file, contents|
    code, new_file_name = copy_erb_file(file, contents, temp_dir_path)

    if stdin?
      @stdin_contents = code
      @new_stdin_filename = new_file_name
    end
  end

  stdout, stderr, exit_code = RubocopRunner.new(arguments, pwd, temp_dir_path, @stdin_contents, !disable_rb_extension?).execute

  corrector.correct(stdout, stderr, file_mappings) if auto_correct?

  [[STDOUT, stdout], [STDERR, stderr]].each do |output_stream, output|
    next if output.nil? || output.empty?

    output_stream.puts(output)
  end

  exit_code
end
arguments() click to toggle source
# File lib/ruumba/analyzer.rb, line 88
def arguments
  if stdin?
    options[:arguments] + ['--stdin', @new_stdin_filename]
  else
    options[:arguments]
  end
end
auto_correct?() click to toggle source
# File lib/ruumba/analyzer.rb, line 76
def auto_correct?
  options[:auto_correct]
end
auto_correct_marker() click to toggle source
# File lib/ruumba/analyzer.rb, line 104
def auto_correct_marker
  return @auto_correct_marker if defined?(@auto_correct_marker)

  @auto_correct_marker = auto_correct? ? 'marker_' + SecureRandom.uuid.tr('-', '_') : nil
end
copy_erb_file(file, contents, temp_dir) click to toggle source
# File lib/ruumba/analyzer.rb, line 122
def copy_erb_file(file, contents, temp_dir)
  code = parser.extract(contents)
  new_file = temp_filename_for(file, temp_dir)

  if auto_correct?
    properties = []
    properties << new_file
    properties << digestor.call(code)

    properties <<
      if stdin?
        contents
      else
        -> { File.read(file) }
      end

    file_mappings[file] = properties
  end

  unless stdin?
    FileUtils.mkdir_p(File.dirname(new_file))

    File.open(new_file, 'w+') do |tmp_file|
      tmp_file.write(code)
    end
  end

  [code, new_file]
end
digestor() click to toggle source
# File lib/ruumba/analyzer.rb, line 114
def digestor
  @digestor ||= ->(contents) { Digest::SHA256.base64digest(contents) }
end
disable_rb_extension?() click to toggle source
# File lib/ruumba/analyzer.rb, line 96
def disable_rb_extension?
  options[:disable_rb_extension]
end
extension() click to toggle source
# File lib/ruumba/analyzer.rb, line 72
def extension
  '.rb' unless disable_rb_extension?
end
file_mappings() click to toggle source
# File lib/ruumba/analyzer.rb, line 118
def file_mappings
  @file_mappings ||= {}
end
parser() click to toggle source
# File lib/ruumba/analyzer.rb, line 110
def parser
  @parser ||= Parser.new(auto_correct_marker)
end
pwd() click to toggle source
# File lib/ruumba/analyzer.rb, line 100
def pwd
  @pwd ||= Pathname.new(ENV['PWD'])
end
stdin?() click to toggle source
# File lib/ruumba/analyzer.rb, line 80
def stdin?
  stdin_filename
end
stdin_filename() click to toggle source
# File lib/ruumba/analyzer.rb, line 84
def stdin_filename
  options[:stdin]
end
temp_filename_for(file, temp_dir) click to toggle source
# File lib/ruumba/analyzer.rb, line 152
def temp_filename_for(file, temp_dir)
  name = temp_dir.join(Pathname.new(file).relative_path_from(pwd))

  "#{name}#{extension}"
end