forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest_extensions.rb
80 lines (64 loc) · 2.42 KB
/
suggest_extensions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
module RuboCop
class CLI
module Command
# Run all the selected cops and report the result.
# @api private
class SuggestExtensions < Base
# Combination of short and long formatter names.
INCLUDED_FORMATTERS = %w[p progress fu fuubar pa pacman].freeze
self.command_name = :suggest_extensions
def self.dependent_gems
return [] unless defined?(Bundler)
# This only includes gems in Gemfile, not in lockfile
Bundler.load.dependencies.map(&:name)
rescue Bundler::GemfileNotFound
[]
end
def run
return if skip? || extensions.none?
puts
puts 'Tip: Based on detected gems, the following '\
'RuboCop extension libraries might be helpful:'
extensions.sort.each do |extension|
puts " * #{extension} (http://github.com/rubocop-hq/#{extension})"
end
puts
puts 'You can opt out of this message by adding the following to your config '\
'(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions '\
'for more options):'
puts ' AllCops:'
puts ' SuggestExtensions: false'
puts if @options[:display_time]
end
private
def skip?
# Disable outputting the notification:
# 1. On CI
# 2. When given RuboCop options that it doesn't make sense for
# 3. For all formatters except specified in `INCLUDED_FORMATTERS'`
ENV['CI'] ||
@options[:only] || @options[:debug] || @options[:list_target_files] || @options[:out] ||
!INCLUDED_FORMATTERS.include?(current_formatter)
end
def current_formatter
@options[:format] || @config_store.for_pwd.for_all_cops['DefaultFormatter'] || 'p'
end
def extensions
return [] unless dependent_gems.any?
@extensions ||= begin
extensions = @config_store.for_pwd.for_all_cops['SuggestExtensions'] || {}
extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys - dependent_gems
end
end
def puts(*args)
output = (@options[:stderr] ? $stderr : $stdout)
output.puts(*args)
end
def dependent_gems
@dependent_gems ||= self.class.dependent_gems
end
end
end
end
end