Class: RuboCop::CLI::Command::AutoGenerateConfig Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
RuboCop::CLI::Command::Base
|
Defined in: | lib/rubocop/cli/command/auto_generate_config.rb |
Overview
Generate a configuration file acting as a TODO list.
Constant Summary
-
AUTO_GENERATED_FILE =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 11'.rubocop_todo.yml'
-
PHASE_1 =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 15'Phase 1 of 2: run Layout/LineLength cop'
-
PHASE_1_DISABLED =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 19'(skipped because Layout/LineLength is disabled)'
-
PHASE_1_OVERRIDDEN =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 18'(skipped because the default Layout/LineLength:Max is overridden)'
-
PHASE_1_SKIPPED_ONLY_COPS =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 20'(skipped because a list of cops is passed to the `--only` flag)'
-
PHASE_1_SKIPPED_ONLY_EXCLUDE =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 22'(skipped because only excludes will be generated due to `--auto-gen-only-exclude` flag)'
-
PHASE_2 =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 16'Phase 2 of 2: run all cops'
-
PLACEHOLDER =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 13'###rubocop:inherit_here'
-
YAML_OPTIONAL_DOC_START =
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 12/\A---(\s+#|\s*\z)/.freeze
Class Attribute Summary
Base
- Inherited
Class Method Summary
Base
- Inherited
Instance Attribute Summary
- #only_exclude? ⇒ Boolean readonly private Internal use only
- #options_has_only_flag? ⇒ Boolean readonly private Internal use only
Base
- Inherited
Instance Method Summary
- #run Internal use only
- #add_formatter private Internal use only
- #add_inheritance_from_auto_generated_file(config_file) private Internal use only
- #execute_runner private Internal use only
- #existing_configuration(config_file) private Internal use only
- #line_length_cop(config) private Internal use only
- #line_length_enabled?(config) ⇒ Boolean private Internal use only
- #max_line_length(config) private Internal use only
- #maybe_run_line_length_cop private Internal use only
- #relative_path_to_todo_from_options_config private Internal use only
- #reset_config_and_auto_gen_file private Internal use only
- #run_all_cops(line_length_contents) private Internal use only
-
#run_line_length_cop
private
Internal use only
Do an initial run with only Layout/LineLength so that cops that depend on Layout/LineLength:Max get the correct value for that parameter.
- #same_max_line_length?(config1, config2) ⇒ Boolean private Internal use only
- #skip_line_length_cop(reason) private Internal use only
- #write_config_file(file_name, file_string, rubocop_yml_contents) private Internal use only
Instance Attribute Details
#only_exclude? ⇒ Boolean
(readonly, private)
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 73
def only_exclude? @options[:auto_gen_only_exclude] end
#options_has_only_flag? ⇒ Boolean
(readonly, private)
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 69
def @options[:only] end
Instance Method Details
#add_formatter (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 107
def add_formatter @options[:formatters] << [Formatter::DisabledConfigFormatter, AUTO_GENERATED_FILE] end
#add_inheritance_from_auto_generated_file(config_file) (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 115
def add_inheritance_from_auto_generated_file(config_file) file_string = " #{}" config_file ||= ConfigFinder::DOTFILE if File.exist?(config_file) files = Array(ConfigLoader.load_yaml_configuration(config_file)['inherit_from']) return if files.include?( ) files.unshift( ) file_string = "\n - #{files.join("\n - ")}" if files.size > 1 rubocop_yml_contents = existing_configuration(config_file) end write_config_file(config_file, file_string, rubocop_yml_contents) puts "Added inheritance from `#{}` " \ "in `#{ConfigFinder::DOTFILE}`." end
#execute_runner (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 111
def execute_runner Environment.new(@options, @config_store, @paths).run(:execute_runner) end
#existing_configuration(config_file) (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 136
def existing_configuration(config_file) File.read(config_file, encoding: Encoding::UTF_8) .sub(/^inherit_from: *[^\n]+/, PLACEHOLDER) .sub(/^inherit_from: *(\n *- *[^\n]+)+/, PLACEHOLDER) end
#line_length_cop(config) (private)
[ GitHub ]
#line_length_enabled?(config) ⇒ Boolean
(private)
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 53
def line_length_enabled?(config) line_length_cop(config)['Enabled'] end
#max_line_length(config) (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 61
def max_line_length(config) line_length_cop(config)['Max'] end
#maybe_run_line_length_cop (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 34
def maybe_run_line_length_cop if only_exclude? skip_line_length_cop(PHASE_1_SKIPPED_ONLY_EXCLUDE) elsif !line_length_enabled?(@config_store.for_pwd) skip_line_length_cop(PHASE_1_DISABLED) elsif !same_max_line_length?(@config_store.for_pwd, ConfigLoader.default_configuration) skip_line_length_cop(PHASE_1_OVERRIDDEN) elsif skip_line_length_cop(PHASE_1_SKIPPED_ONLY_COPS) else run_line_length_cop end end
#relative_path_to_todo_from_options_config (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 153
def return AUTO_GENERATED_FILE unless @options[:config] base = Pathname.new(Dir.pwd) config_dir = Pathname.new(@options[:config]).realpath.dirname # Don't have the path start with `/` return AUTO_GENERATED_FILE if config_dir == base "#{base.relative_path_from(config_dir)}/#{AUTO_GENERATED_FILE}" end
#reset_config_and_auto_gen_file (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 100
def reset_config_and_auto_gen_file @config_store = ConfigStore.new @config_store. = @options[:config] if @options[:config] File.open(AUTO_GENERATED_FILE, 'w') {} # create or truncate if exists add_inheritance_from_auto_generated_file(@options[:config]) end
#run
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 25
def run add_formatter reset_config_and_auto_gen_file line_length_contents = maybe_run_line_length_cop run_all_cops(line_length_contents) end
#run_all_cops(line_length_contents) (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 91
def run_all_cops(line_length_contents) puts Rainbow(PHASE_2).yellow result = execute_runner # This run was made with the current maximum length allowed, so append # the saved setting for LineLength. File.open(AUTO_GENERATED_FILE, 'a') { |f| f.write(line_length_contents) } result end
#run_line_length_cop (private)
Do an initial run with only Layout/LineLength so that cops that depend on Layout/LineLength:Max get the correct value for that parameter.
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 80
def run_line_length_cop puts Rainbow(PHASE_1).yellow @options[:only] = ['Layout/LineLength'] execute_runner @options.delete(:only) @config_store = ConfigStore.new @config_store. = @options[:config] if @options[:config] # Save the todo configuration of the LineLength cop. File.read(AUTO_GENERATED_FILE).lines.drop_while { |line| line.start_with?('#') }.join end
#same_max_line_length?(config1, config2) ⇒ Boolean
(private)
# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 57
def same_max_line_length?(config1, config2) max_line_length(config1) == max_line_length(config2) end
#skip_line_length_cop(reason) (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 48
def skip_line_length_cop(reason) puts Rainbow("#{PHASE_1} #{reason}").yellow '' end
#write_config_file(file_name, file_string, rubocop_yml_contents) (private)
[ GitHub ]# File 'lib/rubocop/cli/command/auto_generate_config.rb', line 142
def write_config_file(file_name, file_string, rubocop_yml_contents) lines = /\S/.match?(rubocop_yml_contents) ? rubocop_yml_contents.split("\n", -1) : [] unless rubocop_yml_contents&.include?(PLACEHOLDER) doc_start_index = lines.index { |line| YAML_OPTIONAL_DOC_START.match?(line) } || -1 lines.insert(doc_start_index + 1, PLACEHOLDER) end File.write(file_name, lines.join("\n") .sub(/#{PLACEHOLDER}\n*/o, "inherit_from:#{file_string}\n\n") .sub(/\n\n+\Z/, "\n")) end