# frozen_string_literal: true require_relative 'utils' class Danger::DangerWCC < Danger::Plugin class Reek include Utils def initialize(plugin, options = {}) @plugin = plugin @options = options end def perform # get the diff of new reek issues diff = run_reek_diff # run reek again to get line numbers reek_lines = run 'bundle exec reek --single-line --no-progress --no-color' reek_lines = reek_lines.lines each_addition_in_diff(diff) do |line| add_reek_warning(line, reek_lines[line.line_number.right - 1]) end end private def run_reek_diff diff = run_and_diff do write_reek_config run('bundle exec reek --single-line --no-progress '\ '--no-color --no-line-numbers') end GitDiff.from_string(diff) end def write_reek_config return if File.exist?('.reek') return unless Dir.glob('*.reek').empty? default = File.join(File.dirname(__FILE__), 'defaults.reek') FileUtils.cp(default, './') end def add_reek_warning(line, with_line_number) return unless warning = line.content.match(/^\+?\s*([^\:]+\:\s+.+)$/i) line_info = with_line_number.match(/^\s*([^\:]+)\:(\d+)\:/i) plugin.warn(format_links_as_markdown(warning.captures[0]), file: line_info.captures[0], line: line_info.captures[1].to_i) end end end