|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + class CLI |
| 5 | + module Command |
| 6 | + # Run all the selected cops and report the result. |
| 7 | + # @api private |
| 8 | + class SuggestExtensions < Base |
| 9 | + # Combination of short and long formatter names. |
| 10 | + INCLUDED_FORMATTERS = %w[p progress fu fuubar pa pacman].freeze |
| 11 | + |
| 12 | + self.command_name = :suggest_extensions |
| 13 | + |
| 14 | + def run |
| 15 | + return if skip? || extensions.none? |
| 16 | + |
| 17 | + puts |
| 18 | + puts 'Tip: Based on detected gems, the following '\ |
| 19 | + 'RuboCop extension libraries might be helpful:' |
| 20 | + |
| 21 | + extensions.each do |extension| |
| 22 | + puts " * #{extension} (http://github.com/rubocop-hq/#{extension})" |
| 23 | + end |
| 24 | + |
| 25 | + puts |
| 26 | + puts 'You can opt out of this message by adding the following to your config:' |
| 27 | + puts ' AllCops:' |
| 28 | + puts ' SuggestExtensions: false' |
| 29 | + puts if @options[:display_time] |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + def skip? |
| 35 | + # Disable outputting the notification: |
| 36 | + # 1. On CI |
| 37 | + # 2. When given RuboCop options that it doesn't make sense for |
| 38 | + # 3. For all formatters except specified in `INCLUDED_FORMATTERS'` |
| 39 | + ENV['CI'] || |
| 40 | + @options[:only] || @options[:debug] || @options[:list_target_files] || @options[:out] || |
| 41 | + !INCLUDED_FORMATTERS.include?(current_formatter) |
| 42 | + end |
| 43 | + |
| 44 | + def current_formatter |
| 45 | + @options[:format] || @config_store.for_pwd.for_all_cops['DefaultFormatter'] || 'p' |
| 46 | + end |
| 47 | + |
| 48 | + def extensions |
| 49 | + extensions = @config_store.for_pwd.for_all_cops['SuggestExtensions'] |
| 50 | + return [] unless extensions |
| 51 | + |
| 52 | + extensions.select { |_, v| (v & dependent_gems).any? }.keys - dependent_gems |
| 53 | + end |
| 54 | + |
| 55 | + def dependent_gems |
| 56 | + # This only includes gems in Gemfile, not in lockfile |
| 57 | + Bundler.load.dependencies.map(&:name) |
| 58 | + end |
| 59 | + |
| 60 | + def puts(*args) |
| 61 | + output = (@options[:stderr] ? $stderr : $stdout) |
| 62 | + output.puts(*args) |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments