class RuboCop::CLI::Command::SuggestExtensions
Suggest RuboCop
extensions to install based on Gemfile dependencies. Only primary dependencies are evaluated, so if a dependency depends on a gem with an extension, it is not suggested. However, if an extension is a transitive dependency, it will not be suggested. @api private
Constants
- INCLUDED_FORMATTERS
Combination of short and long formatter names.
Public Instance Methods
run()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 17 def run return if skip? || extensions.none? print_install_suggestions if not_installed_extensions.any? print_load_suggestions if installed_and_not_loaded_extensions.any? print_opt_out_instruction puts if @options[:display_time] end
Private Instance Methods
all_extensions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 73 def all_extensions return [] unless lockfile.dependencies.any? extensions = @config_store.for_pwd.for_all_cops['SuggestExtensions'] case extensions when true extensions = ConfigLoader.default_configuration.for_all_cops['SuggestExtensions'] when false, nil extensions = {} end extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys end
current_formatter()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 69 def current_formatter @options[:format] || @config_store.for_pwd.for_all_cops['DefaultFormatter'] || 'p' end
dependent_gems()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 111 def dependent_gems lockfile.dependencies.map(&:name) end
extensions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 87 def extensions not_installed_extensions + installed_and_not_loaded_extensions end
installed_and_not_loaded_extensions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 103 def installed_and_not_loaded_extensions installed_extensions - loaded_extensions end
installed_extensions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 91 def installed_extensions all_extensions & installed_gems end
installed_gems()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 115 def installed_gems lockfile.gems.map(&:name) end
loaded_extensions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 99 def loaded_extensions @config_store.for_pwd.loaded_features.to_a end
lockfile()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 107 def lockfile @lockfile ||= Lockfile.new end
not_installed_extensions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 95 def not_installed_extensions all_extensions - installed_gems end
print_install_suggestions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 41 def print_install_suggestions puts puts 'Tip: Based on detected gems, the following ' \ 'RuboCop extension libraries might be helpful:' not_installed_extensions.sort.each do |extension| puts " * #{extension} (https://rubygems.org/gems/#{extension})" end end
print_load_suggestions()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 51 def print_load_suggestions puts puts 'The following RuboCop extension libraries are installed but not loaded in config:' installed_and_not_loaded_extensions.sort.each do |extension| puts " * #{extension}" end end
print_opt_out_instruction()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 60 def print_opt_out_instruction 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' end
puts(*args)
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 119 def puts(*args) output = (@options[:stderr] ? $stderr : $stdout) output.puts(*args) end
skip?()
click to toggle source
# File lib/rubocop/cli/command/suggest_extensions.rb, line 30 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.fetch('CI', nil) || @options[:only] || @options[:debug] || @options[:list_target_files] || @options[:out] || @options[:stdin] || !INCLUDED_FORMATTERS.include?(current_formatter) end