Class: Guard::RuboCop::Runner
- Inherits:
-
Object
- Object
- Guard::RuboCop::Runner
- Defined in:
- lib/guard/rubocop/runner.rb
Overview
This class runs rubocop
command, retrieves result and notifies.
An instance of this class is intended to invoke rubocop
only once in its lifetime.
Instance Method Summary collapse
- #args_specified_by_user ⇒ Object
- #build_command(paths) ⇒ Object
- #failed_paths ⇒ Object
- #include_formatter_for_console?(cli_args) ⇒ Boolean
-
#initialize(options) ⇒ Runner
constructor
A new instance of Runner.
- #json_file_path ⇒ Object
- #notify(passed) ⇒ Object
- #open_launchy_if_needed ⇒ Object
- #pluralize(number, thing, options = {}) ⇒ Object
- #result ⇒ Object
- #run(paths = []) ⇒ Object
- #should_add_default_formatter_for_console? ⇒ Boolean
- #summary_text ⇒ Object
Constructor Details
#initialize(options) ⇒ Runner
Returns a new instance of Runner.
10 11 12 |
# File 'lib/guard/rubocop/runner.rb', line 10 def initialize() @options = end |
Instance Method Details
#args_specified_by_user ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/guard/rubocop/runner.rb', line 47 def args_specified_by_user @args_specified_by_user ||= begin args = @options[:cli] case args when Array then args when String then args.shellsplit when NilClass then [] else raise ArgumentError, ':cli option must be either an array or string' end end end |
#build_command(paths) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/guard/rubocop/runner.rb', line 30 def build_command(paths) command = [@options[:cmd] || 'rubocop'] if should_add_default_formatter_for_console? command.concat(%w[--format progress]) # Keep default formatter for console. end command.concat(['--format', 'json', '--out', json_file_path]) command << '--force-exclusion' command.concat(args_specified_by_user) command.concat(paths) end |
#failed_paths ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/guard/rubocop/runner.rb', line 105 def failed_paths failed_files = result[:files].reject do |file| offenses = file[:offenses] || file[:offences] offenses.empty? end failed_files.map do |file| file[:path] end end |
#include_formatter_for_console?(cli_args) ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/guard/rubocop/runner.rb', line 59 def include_formatter_for_console?(cli_args) index = -1 formatter_args = cli_args.group_by do |arg| index += 1 if arg == '--format' || arg.start_with?('-f') index end formatter_args.delete(-1) formatter_args.each_value.any? do |args| args.none? { |a| a == '--out' || a.start_with?('-o') } end end |
#json_file_path ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/guard/rubocop/runner.rb', line 72 def json_file_path @json_file_path ||= begin # Just generate random tempfile path. basename = self.class.name.downcase.gsub('::', '_') tempfile = Tempfile.new(basename) tempfile.close tempfile.path end end |
#notify(passed) ⇒ Object
89 90 91 92 |
# File 'lib/guard/rubocop/runner.rb', line 89 def notify(passed) image = passed ? :success : :failed Notifier.notify(summary_text, title: 'RuboCop results', image: image) end |
#open_launchy_if_needed ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/guard/rubocop/runner.rb', line 129 def open_launchy_if_needed return unless (output_path = @options[:launchy]) return unless File.exist?(output_path) require 'launchy' ::Launchy.open(output_path) end |
#pluralize(number, thing, options = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/guard/rubocop/runner.rb', line 115 def pluralize(number, thing, = {}) text = if number.zero? && [:no_for_zero] +'no' else number.to_s end text << " #{thing}" text << 's' unless number == 1 text end |
#result ⇒ Object
82 83 84 85 86 87 |
# File 'lib/guard/rubocop/runner.rb', line 82 def result @result ||= File.open(json_file_path) do |file| # Rubinius 2.0.0.rc1 does not support `JSON.load` with 3 args. JSON.parse(file.read, symbolize_names: true) end end |
#run(paths = []) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/guard/rubocop/runner.rb', line 14 def run(paths = []) command = build_command(paths) passed = system(*command) case @options[:notification] when :failed notify(passed) unless passed when true notify(passed) end open_launchy_if_needed passed end |
#should_add_default_formatter_for_console? ⇒ Boolean
43 44 45 |
# File 'lib/guard/rubocop/runner.rb', line 43 def should_add_default_formatter_for_console? !@options[:hide_stdout] && !include_formatter_for_console?(args_specified_by_user) end |
#summary_text ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/guard/rubocop/runner.rb', line 94 def summary_text summary = result[:summary] text = pluralize(summary[:inspected_file_count], 'file') text << ' inspected, ' offense_count = summary[:offense_count] || summary[:offence_count] text << pluralize(offense_count, 'offense', no_for_zero: true) text << ' detected' end |