#!/usr/bin/env ruby # frozen_string_literal: true require "rfix" require "rubocop" require "rfix/formatter" require "cli/ui" should_list_files = false limit_files = false options = RuboCop::Options.new display_help = ARGV.include?("--help") is_dry = ARGV.include?("--dry") config_path = false clear_cache = false extend Rfix::Log Rfix.init! CLI::UI::StdoutRouter.enable if ARGV.include?("--debug") Rfix.debug! end say "Using RuboCop {{yellow:#{RuboCop::Version.version}}}" say_debug "Current directory {{italic:#{Dir.pwd}}}" say_debug "[Ruby] {{italic:#{RbConfig.ruby}}}" options.on("--untracked", "Include untracked files") do Rfix.load_untracked! end options.on("--dry", "No auto correct") do say "Will run rfix in {{red:read-only}} mode" Rfix.no_auto_correct! end options.on("--list-files", "List files found by git") do should_list_files = true end options.on("--clear-cache", "Clear RuboCop cache") do clear_cache = true end options.on("--limit-files NUM", Integer, "Limit number of files") do |limit| limit_files = limit end options.on("--config PATH", "A configuration file") do |path| config_path = path end case cmd = ARGV.shift when "info" say "Using Rfix {{yellow:#{Rfix::VERSION}}}" say "Using OS {{yellow:#{Rfix.current_os}}}" say "Using Git {{yellow:#{Rfix.git_version}}}" say "Using Ruby {{yellow:#{Rfix.ruby_version}}}" say "Current Git branch {{yellow:#{Rfix.current_branch}}}" say "Number of commits {{yellow:#{Rfix.number_of_commits_since}}}" exit 0 when "lint" Rfix.lint_mode! reference = Rfix.ref_since_push when "local" reference = Rfix.ref_since_push when "origin" reference = Rfix.ref_since_origin when "all" reference = false Rfix.global_enable! when "branch" abort_message = "No branch passed as second argument" unless reference = ARGV.shift say_abort abort_message end # Catches: # NOT OK: branch --help # OK branch master --help say_abort abort_message if reference.start_with?("--") # Check if branch exists say_abort "Branch {{yellow:#{reference}}} does not exist" unless Rfix.has_branch?(reference) else unless display_help say_error "Valid rfix commands are:" say_error Rfix.help exit 1 end ARGV.unshift(cmd) end begin options, paths = options.parse(ARGV) rescue OptionParser::MissingArgument => e say_abort "[Parser] #{e}" end if path = config_path Rfix.load_config do |config| say_debug "[Config:Option] Load from #{path}" config.options_config = path end else Rfix.load_config do |config| say_debug "[Config:PWD] Load from #{Dir.pwd}" config.for(Dir.pwd) end end if clear_cache say "Clearing cache" Rfix.clear_cache! end if ref = reference say "Compare against {{yellow:#{ref}}}" Rfix.load_tracked!(ref) paths = Rfix.paths if paths.empty? say_exit "No files to run against" if paths.empty? end if limit = limit_files say "Limit number of files to {{yellow:#{limit}}}" paths = paths.take(limit) end if should_list_files say "Loaded {{yellow:#{paths.count}}} paths" paths.each do |path| say "\t" + Rfix.to_relative(path: path) end end say_debug "[Config:Dump] #{Rfix.store}" env = RuboCop::CLI::Environment.new( Rfix.config.merge(options), Rfix.store, paths ) begin exit RuboCop::CLI::Command::ExecuteRunner.new(env).run rescue RuboCop::Error => e say_abort e.message end