| 1 |
require 'yaml' |
| 2 |
|
| 3 |
require 'rubocop/rake_task' |
| 4 |
RuboCop::RakeTask.new |
| 5 |
|
| 6 |
namespace 'rubocop' do |
| 7 |
desc 'mikutter 4.1以降に変更されたファイルを .rubocop.blacklist.yml から取り除く' |
| 8 |
task 'unblacklist' do |
| 9 |
yaml = YAML.load_file('.rubocop.blacklist.yml') |
| 10 |
blacklist = yaml.dig('AllCops', 'Exclude').to_set |
| 11 |
excludes = Set[] |
| 12 |
IO.popen('git log --numstat --no-merges --pretty="" 4.1.0..HEAD', 'r') do |rp| |
| 13 |
rp.lazy.map { _1.chomp.split("\t", 3) }.each do |add, del, fn| |
| 14 |
matched = fn.match(/{(\w*) => (\w*)}/) |
| 15 |
if matched |
| 16 |
after = matched.pre_match + matched[2] + matched.post_match |
| 17 |
if blacklist.include?(after) && target?(add, del, after, 'renamed ') |
| 18 |
excludes << after |
| 19 |
end |
| 20 |
elsif blacklist.include?(fn) && target?(add, del, fn) |
| 21 |
excludes << fn |
| 22 |
end |
| 23 |
end |
| 24 |
end |
| 25 |
yaml['AllCops']['Exclude'] = |
| 26 |
yaml['AllCops']['Exclude'].select(&FileTest.method(:exist?)) - excludes.to_a |
| 27 |
File.open('.rubocop.blacklist.yml', 'w') do |wp| |
| 28 |
YAML.dump(yaml, wp) |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
def target?(add, del, file_name, hint='') |
| 33 |
if FileTest.exist?(file_name) |
| 34 |
siz = File.open(file_name, 'r') { _1.readlines.size } |
| 35 |
rate = add.to_i.fdiv(siz) |
| 36 |
rate >= 0.5 |
| 37 |
else |
| 38 |
true |
| 39 |
end |
| 40 |
end |
| 41 |
end |