Skip to content

Instantly share code, notes, and snippets.

@sue445
Last active April 17, 2018 07:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sue445/fcefbe7ef11b89f126ae22b6f9e597d4 to your computer and use it in GitHub Desktop.
Save sue445/fcefbe7ef11b89f126ae22b6f9e597d4 to your computer and use it in GitHub Desktop.
`rubocop --auto-correct --only COP` and `git commit`
# `rubocop --auto-correct --only COP` and `git commit`
# Usage: ruby rubocop_auto_correct_commit.rb
require "json"
DEFAULT_ORDER = 100
# --auto-correctする時の実行順(数字が小さい方が先に実行される)
COP_ORDERS = {
# Style/UnneededPercentQはStyle/StringLiteralsのようにシングルクオーテーション優先かダブルクオーテーション優先のようなconfigがない
# https://github.com/bbatsov/rubocop/blob/v0.49.1/lib/rubocop/cop/style/unneeded_percent_q.rb
# Style/StringLiteralsでダブルクオーテーションにした後でStyle/UnneededPercentQでシングルクオーテーション
# になると再度Style/StringLiteralsをかける必要があり二度手間になってしまうので先にStyle/UnneededPercentQを実行した方がいい
"Style/UnneededPercentQ" => 50,
}.tap { |hash| hash.default = DEFAULT_ORDER }
# Depending on the execution order, there is a possibility that the offences may increase.
# So it is executed twice
2.times do
rubocop_result = JSON.parse(`rubocop --format=json`)
cop_names = []
rubocop_result["files"].each do |file|
cop_names += file["offenses"].map { |offense| offense["cop_name"] }
end
# 「第1ソートキー:COP_ORDERSに登録されてる実行順、第2ソートキー:cop名」の順でソートする
cop_names = cop_names.uniq.sort_by { |cop_name| [COP_ORDERS[cop_name], cop_name] }
cop_names.each do |cop_name|
command = "rubocop --auto-correct --only #{cop_name}"
ret = system command
system "git commit -am ':cop: #{command}'" if ret
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment