Class: Guard::Rubocop::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/rubocop/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner

Returns a new instance of Runner.



8
9
10
# File 'lib/guard/rubocop/runner.rb', line 8

def initialize(options)
  @options = options
end

Instance Method Details

#build_command(paths) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/guard/rubocop/runner.rb', line 26

def build_command(paths)
  command = ['rubocop']
  command.concat(%w(--format progress)) # Keep default formatter for console.
  command.concat(['--format', 'json', '--out', json_file_path])
  command.concat(['--list-cops']) if @options[:cops]
  command.concat(paths)
end

#failed_pathsObject



68
69
70
71
72
73
74
75
# File 'lib/guard/rubocop/runner.rb', line 68

def failed_paths
  failed_files = result[:files].reject do |file|
    file[:offences].empty?
  end
  failed_files.map do |file|
    file[:path]
  end
end

#json_file_pathObject



34
35
36
37
38
39
40
41
42
# File 'lib/guard/rubocop/runner.rb', line 34

def json_file_path
  @tempfile_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



53
54
55
56
# File 'lib/guard/rubocop/runner.rb', line 53

def notify(passed)
  image = passed ? :success : :failed
  Notifier.notify(summary_text, title: 'RuboCop results', image: image)
end

#pluralize(number, thing, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/guard/rubocop/runner.rb', line 77

def pluralize(number, thing, options = {})
  text = ''

  if number == 0 && options[:no_for_zero]
    text = 'no'
  else
    text << number.to_s
  end

  text << " #{thing}"
  text << 's' unless number == 1

  text
end

#resultObject



44
45
46
47
48
49
50
51
# File 'lib/guard/rubocop/runner.rb', line 44

def result
  @result ||= begin
    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
end

#run(paths = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/guard/rubocop/runner.rb', line 12

def run(paths = [])
  command = build_command(paths)
  passed = system(*command)

  case @options[:notification]
  when :failed
    notify(passed) unless passed
  when true
    notify(passed)
  end

  passed
end

#summary_textObject



58
59
60
61
62
63
64
65
66
# File 'lib/guard/rubocop/runner.rb', line 58

def summary_text
  summary = result[:summary]

  text = pluralize(summary[:inspected_file_count], 'file')
  text << ' inspected, '

  text << pluralize(summary[:offence_count], 'offence', no_for_zero: true)
  text << ' detected'
end