Class: RuboCop::CLI
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Exceptions:
| |
Inherits: | Object |
Defined in: | lib/rubocop/cli.rb, lib/rubocop/cli/command.rb, lib/rubocop/cli/environment.rb, lib/rubocop/cli/command/auto_generate_config.rb, lib/rubocop/cli/command/base.rb, lib/rubocop/cli/command/execute_runner.rb, lib/rubocop/cli/command/init_dotfile.rb, lib/rubocop/cli/command/lsp.rb, lib/rubocop/cli/command/show_cops.rb, lib/rubocop/cli/command/show_docs_url.rb, lib/rubocop/cli/command/suggest_extensions.rb, lib/rubocop/cli/command/version.rb |
Overview
The CLI is a class responsible of handling all the command line interface logic.
Constant Summary
-
DEFAULT_PARALLEL_OPTIONS =
# File 'lib/rubocop/cli.rb', line 13%i[ color config debug display_style_guide display_time display_only_fail_level_offenses display_only_failed editor_mode except extra_details fail_level fix_layout format ignore_disable_comments lint only only_guide_cops require safe autocorrect safe_autocorrect autocorrect_all ].freeze
-
STATUS_ERROR =
# File 'lib/rubocop/cli.rb', line 112
-
STATUS_INTERRUPTED =
# File 'lib/rubocop/cli.rb', line 12Signal.list['INT'] + 128
-
STATUS_OFFENSES =
# File 'lib/rubocop/cli.rb', line 101
-
STATUS_SUCCESS =
# File 'lib/rubocop/cli.rb', line 90
Class Method Summary
- .new ⇒ CLI constructor
Instance Attribute Summary
- #config_store readonly
- #options readonly
Instance Method Summary
-
#run(args = ARGV) ⇒ Integer
Entry point for the application logic.
- #act_on_options private
- #apply_default_formatter private
- #execute_runners private
- #handle_editor_mode private
- #handle_exiting_options private
- #parallel_by_default! private
-
#profile_if_needed
private
Metrics/AbcSize.
-
#require_gem(name)
private
Metrics/AbcSize.
- #run_command(name) private
- #set_options_to_config_loader private
- #suggest_extensions private
- #validate_options_vs_config private
Constructor Details
.new ⇒ CLI
# File 'lib/rubocop/cli.rb', line 24
def initialize @options = {} @config_store = ConfigStore.new end
Instance Attribute Details
#config_store (readonly)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 22
attr_reader :, :config_store
#options (readonly)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 22
attr_reader :, :config_store
Instance Method Details
#act_on_options (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 156
def handle_editor_mode @config_store. = @options[:config] if @options[:config] @config_store.force_default_config! if @options[:force_default_config] if @options[:color] # color output explicitly forced on Rainbow.enabled = true elsif @options[:color] == false # color output explicitly forced off Rainbow.enabled = false end end
#apply_default_formatter (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 198
def apply_default_formatter # This must be done after the options have already been processed, # because they can affect how ConfigStore behaves @options[:formatters] ||= begin if @options[:auto_gen_config] formatter = 'autogenconf' else cfg = @config_store.for_pwd.for_all_cops formatter = cfg['DefaultFormatter'] || 'progress' end [[formatter, @options[:output_path]]] end end
#execute_runners (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 125
def execute_runners if @options[:auto_gen_config] run_command(:auto_gen_config) else run_command(:execute_runner).tap { suggest_extensions } end end
#handle_editor_mode (private)
[ GitHub ]#handle_exiting_options (private)
# File 'lib/rubocop/cli.rb', line 187
def return unless Options::EXITING_OPTIONS.any? { |o| @options.key? o } run_command(:version) if @options[:version] || @options[:verbose_version] run_command(:show_cops) if @options[:show_cops] run_command(:show_docs_url) if @options[:show_docs_url] run_command(:lsp) if @options[:lsp] raise Finished end
#parallel_by_default! (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 144
def parallel_by_default! # See https://github.com/rubocop/rubocop/pull/4537 for JRuby and Windows constraints. return if RUBY_ENGINE != 'ruby' || RuboCop::Platform.windows? if (@options.keys - DEFAULT_PARALLEL_OPTIONS).empty? && @config_store.for_pwd.for_all_cops['UseCache'] != false puts 'Use parallel by default.' if @options[:debug] @options[:parallel] = true end end
#profile_if_needed (private)
Metrics/AbcSize
# File 'lib/rubocop/cli.rb', line 80
def profile_if_needed return yield unless @options[:profile] return STATUS_ERROR unless require_gem('stackprof') with_memory = @options[:memory] if with_memory return STATUS_ERROR unless require_gem('memory_profiler') MemoryProfiler.start end tmp_dir = File.join(ConfigFinder.project_root, 'tmp') FileUtils.mkdir_p(tmp_dir) cpu_profile_file = File.join(tmp_dir, 'rubocop-stackprof.dump') status = nil StackProf.run(out: cpu_profile_file) do status = yield end puts "Profile report generated at #{cpu_profile_file}" if with_memory puts 'Building memory report...' report = MemoryProfiler.stop memory_profile_file = File.join(tmp_dir, 'rubocop-memory_profiler.txt') report.pretty_print(to_file: memory_profile_file, scale_bytes: true) puts "Memory report generated at #{memory_profile_file}" end status end
#require_gem(name) (private)
Metrics/AbcSize
# File 'lib/rubocop/cli.rb', line 113
def require_gem(name) require name true rescue LoadError warn("You don't have #{name} installed. Add it to your Gemfile and run `bundle install`") false end
#run(args = ARGV) ⇒ Integer
Entry point for the application logic. Here we do the command line arguments processing and inspect the target files.
# File 'lib/rubocop/cli.rb', line 39
def run(args = ARGV) @options, paths = Options.new.parse(args) @env = Environment.new(@options, @config_store, paths) profile_if_needed do if @options[:init] run_command(:init) else parallel_by_default! apply_default_formatter execute_runners end end rescue ConfigNotFoundError, IncorrectCopNameError, OptionArgumentError => e warn e. STATUS_ERROR rescue RuboCop::Error => e warn Rainbow("Error: #{e.}").red STATUS_ERROR rescue Interrupt warn '' warn 'Exiting...' STATUS_INTERRUPTED rescue Finished STATUS_SUCCESS rescue OptionParser::InvalidOption => e warn e. warn 'For usage information, use --help' STATUS_ERROR rescue StandardError, SyntaxError, LoadError => e warn e. warn e.backtrace STATUS_ERROR end
#run_command(name) (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 121
def run_command(name) @env.run(name) end
#set_options_to_config_loader (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 174
def ConfigLoader.debug = @options[:debug] ConfigLoader.disable_pending_cops = @options[:disable_pending_cops] ConfigLoader.enable_pending_cops = @options[:enable_pending_cops] ConfigLoader.ignore_parent_exclusion = @options[:ignore_parent_exclusion] ConfigLoader.ignore_unrecognized_cops = @options[:ignore_unrecognized_cops] end
#suggest_extensions (private)
[ GitHub ]# File 'lib/rubocop/cli.rb', line 133
def suggest_extensions run_command(:suggest_extensions) end
#validate_options_vs_config (private)
# File 'lib/rubocop/cli.rb', line 137
def return unless @options[:parallel] && !@config_store.for_pwd.for_all_cops['UseCache'] raise OptionArgumentError, '-P/--parallel uses caching to speed up execution, so combining ' \ 'with AllCops: UseCache: false is not allowed.' end