Class: Nucop::Cli
- Inherits:
-
Thor
- Object
- Thor
- Nucop::Cli
- Defined in:
- lib/nucop/cli.rb
Instance Method Summary collapse
- #diff ⇒ Object
- #diff_enforced ⇒ Object
- #diff_enforced_github ⇒ Object
- #diff_github ⇒ Object
- #modified_lines ⇒ Object
- #ready_for_promotion ⇒ Object
- #regen_backlog ⇒ Object
- #rubocop(files = nil) ⇒ Object
- #update_enforced ⇒ Object
Instance Method Details
#diff ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/nucop/cli.rb', line 43 def diff puts "Running on files changed relative to '#{[:"commit-spec"]}' (specify using the 'commit-spec' option)" diff_filter = [:"added-only"] ? "A" : "d" diff_base = capture_std_out("git merge-base HEAD #{[:"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 [: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 [:ignore] && File.exist?([:diffignore_file]) && !File.empty?([:diffignore_file]) files, non_ignored_diff_status = Open3.capture2("grep -v -f #{[:diffignore_file]}", stdin_data: files) if non_ignored_diff_status != 0 if [: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)], exit 1 unless no_violations_detected return true unless [:exit] exit 0 end |
#diff_enforced ⇒ Object
18 19 20 |
# File 'lib/nucop/cli.rb', line 18 def diff_enforced invoke :diff, nil, .merge(only: cops_to_enforce.join(",")) end |
#diff_enforced_github ⇒ Object
30 31 32 |
# File 'lib/nucop/cli.rb', line 30 def diff_enforced_github invoke :diff_github, nil, .merge(only: cops_to_enforce.join(",")) end |
#diff_github ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/nucop/cli.rb', line 91 def diff_github puts "Running on files changed relative to '#{[:"commit-spec"]}' (specify using the 'commit-spec' option)" diff_head = capture_std_out("git rev-parse HEAD").chomp diff_base = [:"commit-spec"] repository = capture_std_out("git remote get-url origin | sed 's/[email protected]://; s/.git//'").chomp uri = URI("https://api.github.com/repos/#{repository}/compare/#{diff_base}...#{diff_head}") request = Net::HTTP::Get.new(uri) request["Accept"] = "application/vnd.github+json" request["Authorization"] = "Bearer #{[:"github-authorization-token"]}" request["X-GitHub-Api-Version"] = "2022-11-28" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) } if response.code != "200" puts "Error fetching data from Github: #{response.code} -- #{response.body}" return true unless [:exit] exit 0 end commit_data = JSON.parse(response.body) diff_filter = [:"added-only"] ? proc { |status| status == "added" } : proc { |status| status != "removed" } files = commit_data["files"] .filter { |file_data| diff_filter.call(file_data["status"]) } .map { |file_data| file_data["filename"] } .filter { |file_name| file_name.include?(".rb") } if files.empty? if [: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 [:ignore] && File.exist?([:diffignore_file]) && !File.empty?([:diffignore_file]) files, non_ignored_diff_status = Open3.capture2("grep -v -f #{[:diffignore_file]}", stdin_data: files.join("\n")) if non_ignored_diff_status != 0 if [: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)], exit 1 unless no_violations_detected return true unless [:exit] exit 0 end |
#modified_lines ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/nucop/cli.rb', line 204 def modified_lines diff_files, diff_status = Open3.capture2("git diff #{[:"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 #{[:rubocop_todo_config_file]}", multi_line_to_single_line(diff_files).to_s ].join(" ") # HACK: use ENVVAR to parameterize GitDiffFormatter system({ "RUBOCOP_COMMIT_SPEC" => [:"commit-spec"] }, command) end |
#ready_for_promotion ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/nucop/cli.rb', line 223 def ready_for_promotion finder = Helpers::NextCopForPromotion.new([:rubocop_todo_file]) todo_config = YAML.load_file([: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(["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 #{[:rubocop_todo_config_file]} --only #{todo.name} #{files.join(' ')}") puts("*" * 100) if ["n"] > 1 puts end end |
#regen_backlog ⇒ Object
192 193 194 195 |
# File 'lib/nucop/cli.rb', line 192 def regen_backlog regenerate_rubocop_todos update_enforced_cops end |
#rubocop(files = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/nucop/cli.rb', line 158 def rubocop(files = nil) print_cops_being_run([:only]) config_file = [:"exclude-backlog"] ? RUBOCOP_DEFAULT_CONFIG_FILE : [:rubocop_todo_config_file] rubocop_requires = [ "--require rubocop-rspec", "--require rubocop-performance", "--require rubocop-rails" ] formatters = [] formatters << "--format Nucop::Formatters::JUnitFormatter --out #{[:junit_report]}" if [:junit_report] formatters << "--format json --out #{[:json]}" if [:json] formatters << "--format progress" if formatters.any? command = [ "bundle exec rubocop", "--no-server", "--parallel", rubocop_requires.join(" "), formatters.join(" "), "--force-exclusion", "--config", config_file, pass_through_option(, "auto-correct"), pass_through_option(, "autocorrect"), pass_through_option(, "autocorrect-all"), pass_through_flag(, "only"), files ].join(" ") system(command) end |
#update_enforced ⇒ Object
198 199 200 |
# File 'lib/nucop/cli.rb', line 198 def update_enforced update_enforced_cops end |