Class: Nucop::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/nucop/cli.rb

Instance Method Summary collapse

Instance Method Details

#diffObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nucop/cli.rb', line 24

def diff
  puts "Running on files changed relative to '#{options[:"commit-spec"]}' (specify using the 'commit-spec' option)"
  diff_filter = options[:"added-only"] ? "A" : "d"
  diff_base = capture_std_out("git merge-base HEAD #{options[:"commit-spec"]}").chomp

  files, diff_status = Open3.capture2("git diff #{diff_base} --diff-filter=#{diff_filter} --name-only | grep \"\\.rb$\"")

  if diff_status != 0
    if options[:exit]
      puts "There are no rb files present in diff. Exiting."
      exit 0
    else
      puts "There are no rb files present in diff."
      return true
    end
  end

  if options[:ignore] && File.exist?(options[:diffignore_file]) && !File.zero?(options[:diffignore_file])
    files, non_ignored_diff_status = Open3.capture2("grep -v -f #{options[:diffignore_file]}", stdin_data: files)

    if non_ignored_diff_status != 0
      if options[:exit]
        puts "There are no non-ignored rb files present in diff. Exiting."
        exit 0
      else
        puts "There are no non-ignored rb files present in diff."
        return true
      end
    end
  end

  no_violations_detected = invoke :rubocop, [multi_line_to_single_line(files)], options

  exit 1 unless no_violations_detected
  return true unless options[:exit]
  exit 0
end

#diff_enforcedObject



13
14
15
# File 'lib/nucop/cli.rb', line 13

def diff_enforced
  invoke :diff, nil, options.merge(only: cops_to_enforce.join(","))
end

#modified_linesObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nucop/cli.rb', line 95

def modified_lines
  diff_files, diff_status = Open3.capture2("git diff #{options[:'commit-spec']} --diff-filter=d --name-only | grep \"\\.rb$\"")

  exit 1 unless diff_status.exitstatus.zero?

  command = [
    "bundle exec rubocop",
    "--parallel",
    "--format Nucop::Formatters::GitDiffFormatter",
    "--config #{options[:rubocop_todo_config_file]}",
    multi_line_to_single_line(diff_files).to_s
  ].join(" ")

  # HACK: use ENVVAR to parameterize GitDiffFormatter
  system({ "RUBOCOP_COMMIT_SPEC" => options[:"commit-spec"] }, command)
end

#ready_for_promotionObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nucop/cli.rb', line 114

def ready_for_promotion
  finder = Helpers::NextCopForPromotion.new(options[:rubocop_todo_file])
  todo_config = YAML.load_file(options[:rubocop_todo_file])

  puts "The following cop(s) are ready to be promoted to enforced. Good luck!"
  puts "Remember to run `nucop:regen_backlog` to capture your hard work."
  puts
  finder.find(options["n"].to_i).each do |todo|
    puts "#{todo.name} with #{todo.offenses} offenses:"
    puts

    files = todo_config.fetch(todo.name, {}).fetch("Exclude", [])

    system("bundle exec rubocop --parallel --config #{options[:rubocop_todo_config_file]} --only #{todo.name} #{files.join(' ')}")
    puts("*" * 100) if options["n"] > 1
    puts
  end
end

#regen_backlogObject



83
84
85
86
# File 'lib/nucop/cli.rb', line 83

def regen_backlog
  regenerate_rubocop_todos
  update_enforced_cops
end

#rubocop(files = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nucop/cli.rb', line 66

def rubocop(files = nil)
  print_cops_being_run(options[:only])
  config_file = options[:"exclude-backlog"] ? RUBOCOP_DEFAULT_CONFIG_FILE : options[:rubocop_todo_config_file]
  junit_report_path = options[:"junit_report"]
  junit_report_options = junit_report_path.to_s.empty? ? "" : "--format Nucop::Formatters::JUnitFormatter --out #{junit_report_path} --format progress"

  rubocop_requires = [
    "--require rubocop-rspec",
    "--require rubocop-performance",
    "--require rubocop-rails"
  ]

  system("bundle exec rubocop --parallel #{rubocop_requires.join(' ')} #{junit_report_options} --force-exclusion --config #{config_file} #{pass_through_option(options, 'auto-correct')} #{pass_through_flag(options, 'only')} #{files}")
end

#update_enforcedObject



89
90
91
# File 'lib/nucop/cli.rb', line 89

def update_enforced
  update_enforced_cops
end