Sha256: fa2a0b1c6bf23689b7e76e9ae964510c68042f29255eb8074bfc26ddc3780f79

Contents?: true

Size: 1.88 KB

Versions: 60

Compression:

Stored size: 1.88 KB

Contents

require "rubocop"

module Standard
  class MergesSettings
    Settings = Struct.new(:runner, :options, :paths)

    def call(argv, standard_yaml)
      standard_argv, rubocop_argv = separate_argv(argv)
      standard_cli_flags = parse_standard_argv(standard_argv)
      rubocop_cli_flags, lint_paths = RuboCop::Options.new.parse(rubocop_argv)

      Settings.new(
        determine_command(standard_argv),
        merge(standard_yaml, standard_cli_flags, without_banned(rubocop_cli_flags)),
        lint_paths
      )
    end

    private

    def separate_argv(argv)
      argv.partition { |flag|
        ["--generate-todo", "--fix", "--no-fix", "--version", "-v", "--help", "-h"].include?(flag)
      }
    end

    def parse_standard_argv(argv)
      argv.each_with_object({}) { |arg, cli_flags|
        if arg == "--fix"
          cli_flags[:auto_correct] = true
          cli_flags[:safe_auto_correct] = true
        elsif arg == "--no-fix"
          cli_flags[:auto_correct] = false
          cli_flags[:safe_auto_correct] = false
        end
      }
    end

    def determine_command(argv)
      if (argv & ["--help", "-h"]).any?
        :help
      elsif (argv & ["--version", "-v"]).any?
        :version
      elsif (argv & ["--generate-todo"]).any?
        :genignore
      else
        :rubocop
      end
    end

    def merge(standard_yaml, standard_cli_flags, rubocop_cli_flags)
      {
        auto_correct: standard_yaml[:fix],
        safe_auto_correct: standard_yaml[:fix],
        formatters: [[standard_yaml[:format] || "Standard::Formatter", nil]],
        parallel: standard_yaml[:parallel],
        todo_file: standard_yaml[:todo_file],
        todo_ignore_files: standard_yaml[:todo_ignore_files]
      }.merge(standard_cli_flags).merge(rubocop_cli_flags)
    end

    def without_banned(rubocop_cli_flags)
      rubocop_cli_flags.tap do |flags|
        flags.delete(:config)
      end
    end
  end
end

Version data entries

60 entries across 60 versions & 3 rubygems

Version Path
standard-1.12.1 lib/standard/merges_settings.rb
standard-1.12.0 lib/standard/merges_settings.rb
standard-1.11.0 lib/standard/merges_settings.rb
standard-1.10.0 lib/standard/merges_settings.rb
standard-1.9.1 lib/standard/merges_settings.rb
standard-1.9.0 lib/standard/merges_settings.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/standard-1.8.0/lib/standard/merges_settings.rb
standard-1.8.0 lib/standard/merges_settings.rb
standard-1.7.3 lib/standard/merges_settings.rb
standard-1.7.2 lib/standard/merges_settings.rb
standard-1.7.1 lib/standard/merges_settings.rb
standard-1.7.0 lib/standard/merges_settings.rb
standard-1.6.0 lib/standard/merges_settings.rb
standard-1.5.0 lib/standard/merges_settings.rb
standard-1.4.0 lib/standard/merges_settings.rb
standard-mkv-1.3.4 lib/standard/merges_settings.rb
standard-mkv-1.3.3 lib/standard/merges_settings.rb
standard-mkv-1.3.2 lib/standard/merges_settings.rb
standard-mkv-1.3.1 lib/standard/merges_settings.rb
standard-mkv-1.3.0 lib/standard/merges_settings.rb