Module: Gergich::Capture

Defined in:
lib/gergich/capture.rb,
lib/gergich/capture/tsc_capture.rb,
lib/gergich/capture/eslint_capture.rb,
lib/gergich/capture/flake8_capture.rb,
lib/gergich/capture/rubocop_capture.rb,
lib/gergich/capture/brakeman_capture.rb,
lib/gergich/capture/yamllint_capture.rb,
lib/gergich/capture/i18nliner_capture.rb,
lib/gergich/capture/stylelint_capture.rb,
lib/gergich/capture/swiftlint_capture.rb,
lib/gergich/capture/shellcheck_capture.rb,
lib/gergich/capture/androidlint_capture.rb

Defined Under Namespace

Classes: AndroidlintCapture, BaseCapture, BrakemanCapture, EslintCapture, Flake8Capture, I18nlinerCapture, RubocopCapture, ShellcheckCapture, StylelintCapture, SwiftlintCapture, TscCapture, YamllintCapture

Class Method Summary collapse

Class Method Details

.base_pathObject



56
57
58
# File 'lib/gergich/capture.rb', line 56

def base_path
  @base_path ||= "#{File.expand_path(GERGICH_GIT_PATH)}/"
end

.captorsObject



112
113
114
# File 'lib/gergich/capture.rb', line 112

def captors
  @captors ||= {}
end

.load_captor(format) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



60
61
62
# File 'lib/gergich/capture.rb', line 60

def relativize(path)
  path.sub(base_path, "")
end

.run(format, command, add_comments: true, suppress_output: false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
83
84
85
86
# 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