Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. require "yaml"
  2. require "json"
  3. require "tempfile"
  4.  
  5. module RuboCopAutoCollrect
  6. class << self
  7. def run_all!(targets, *cop_names, commit_log_format:, fixup_commit_log_format:)
  8. plan(targets, *cop_names).each do |cop_name|
  9. run!(targets, cop_name, commit_log_format: commit_log_format)
  10. end
  11. run!(targets, *cop_names, commit_log_format: fixup_commit_log_format)
  12. end
  13.  
  14. def run!(targets, *cop_names, commit_log_format:)
  15. cop_list = cop_names.join(",") unless cop_names.empty?
  16. print "\n*** auto-correct by #{cop_list || 'AllCops'}...\n\n"
  17.  
  18. only_cops = cop_list ? ["--only", cop_list] : []
  19. begin
  20. rubocop_auto_correct("--format", "autogenconf", *only_cops, *targets)
  21. rescue
  22. print "\n** try to detect errors...\n\n"
  23. rubocop_check(*only_cops, *targets)
  24. abort
  25. end
  26. return unless git_diff?
  27.  
  28. git("add", ".")
  29. git("commit", "-q", "-m", format(commit_log_format, cop_list: cop_list))
  30.  
  31. print "\n** commit changes\n\n"
  32. end
  33.  
  34. def plan(targets, *cop_names)
  35. print "*** calculate auto-correct plan...\n\n"
  36.  
  37. only_cops = cop_names.empty? ? [] : ["--only", cop_names.join(",")]
  38. begin
  39. output = Tempfile.open("auto_correct_plan") do |tf|
  40. rubocop_auto_correct(
  41. *only_cops,
  42. "--display-cop-names",
  43. "--format", "fuubar",
  44. "--format", "json",
  45. "--out", tf.path,
  46. *targets,
  47. )
  48. tf.read
  49. end
  50. rescue
  51. print "\n** try to detect errors...\n\n"
  52. rubocop_check(*only_cops, *targets)
  53. abort
  54. end
  55.  
  56. git("restore", ".")
  57.  
  58. categories = cop_names.map {|cop_name| cop_name[%r{\A[^/]*}] }.uniq
  59. corrected_cops = corrected_cops(JSON.parse(output))
  60. corrected_cops.keys.sort_by do |cop_name|
  61. count = corrected_cops[cop_name]
  62. idx = categories.index {|category| cop_name.start_with?(category) }
  63. [idx || categories.size, -count]
  64. end
  65. end
  66.  
  67. private
  68.  
  69. def corrected_cops(rubocop_output)
  70. rubocop_output["files"]
  71. .map {|f| f["offenses"].select {|o| o["corrected"] } }
  72. .flatten
  73. .group_by {|o| o["cop_name"] }
  74. .transform_values(&:size)
  75. end
  76.  
  77. def rubocop(*args, on_failure: :raise)
  78. command("rubocop", "--safe", *args)
  79. rescue => e
  80. warn "\n#{e}"
  81. abort if on_failure == :abort
  82. raise
  83. end
  84.  
  85. def rubocop_auto_correct(*args)
  86. rubocop(
  87. "--fail-level", "E",
  88. "--safe-auto-correct",
  89. *args,
  90. )
  91. end
  92.  
  93. def rubocop_check(*args)
  94. rubocop(
  95. "--fail-level", "E",
  96. "--display-only-fail-level-offenses",
  97. *args,
  98. on_failure: :abort,
  99. )
  100. end
  101.  
  102. def git(*args)
  103. command("git", *args)
  104. rescue => e
  105. warn e
  106. abort
  107. end
  108.  
  109. def git_diff?
  110. command("git", "diff", "--quiet")
  111. false
  112. rescue
  113. true
  114. end
  115.  
  116. def command(*cmd)
  117. system(*cmd)
  118. raise "failed: #{cmd.join(' ')}" unless $?.success?
  119. end
  120. end
  121. end
  122.  
  123. sep_idx = ARGV.index("--") || 0
  124. targets = ARGV[(sep_idx + 1)..-1]
  125. commit_log_prefix, = ARGV[0, sep_idx]
  126. commit_log_format = "#{commit_log_prefix}auto-correct by RuboCop %<cop_list>s"
  127.  
  128. cop_names = %w(Layout Style)
  129. RuboCopAutoCollrect.run_all!(
  130. targets, *cop_names,
  131. commit_log_format: commit_log_format,
  132. fixup_commit_log_format: "#{commit_log_format} (fix-up)",
  133. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement