module Gergich::Capture

Public Class Methods

base_path() click to toggle source
# File lib/gergich/capture.rb, line 56
def base_path
  @base_path ||= "#{File.expand_path(GERGICH_GIT_PATH)}/"
end
captors() click to toggle source
# File lib/gergich/capture.rb, line 112
def captors
  @captors ||= {}
end
load_captor(format) click to toggle source
# File lib/gergich/capture.rb, line 88
def load_captor(format)
  if (match = format.match(/\Acustom:(?<path>.+?):(?<class_name>.+)\z/))
    load_custom_captor(match[:path], match[:class_name])
  else
    captor = captors[format]
    raise GergichError, "Unrecognized format `#{format}`" unless captor

    captor
  end
end
load_custom_captor(path, class_name) click to toggle source
# File lib/gergich/capture.rb, line 99
def load_custom_captor(path, class_name)
  begin
    require path
  rescue LoadError
    raise GergichError, "unable to load custom format from `#{path}`"
  end
  begin
    const_get(class_name)
  rescue NameError
    raise GergichError, "unable to find custom format class `#{class_name}`"
  end
end
relativize(path) click to toggle source
# File lib/gergich/capture.rb, line 60
def relativize(path)
  path.sub(base_path, "")
end
run(format, command, add_comments: true, suppress_output: false) click to toggle source
# File lib/gergich/capture.rb, line 27
def run(format, command, add_comments: true, suppress_output: false)
  captor = load_captor(format)

  exit_code, output = run_command(command, suppress_output: suppress_output)
  comments = captor.new.run(output.gsub(/\e\[\d+m/m, ""))
  comments.each do |comment|
    comment[:path] = relativize(comment[:path])
  end

  draft = Gergich::Draft.new
  skip_paths = (ENV["SKIP_PATHS"] || "").split(",")
  if add_comments
    comments.each do |comment|
      next if skip_paths.any? { |path| comment[:path].start_with?(path) }

      message = +"[#{comment[:source]}] "
      message << "#{comment[:rule]}: " if comment[:rule]
      message << comment[:message]

      draft.add_comment comment[:path],
                        comment[:position],
                        message,
                        comment[:severity]
    end
  end

  [exit_code, comments]
end
run_command(command, suppress_output: false) click to toggle source
# File lib/gergich/capture.rb, line 64
def run_command(command, suppress_output: false)
  exit_code = 0

  if command == "-"
    output = wiretap($stdin, suppress_output)
  else
    IO.popen("#{command} 2>&1", "r+") do |io|
      output = wiretap(io, suppress_output)
    end
    exit_code = $CHILD_STATUS.exitstatus
  end

  [exit_code, output]
end
wiretap(io, suppress_output) click to toggle source
# File lib/gergich/capture.rb, line 79
def wiretap(io, suppress_output)
  output = []
  io.each do |line|
    $stdout.puts line unless suppress_output
    output << line
  end
  output.join
end