-
Notifications
You must be signed in to change notification settings - Fork 1
/
refactor_script.rb
65 lines (54 loc) · 1.68 KB
/
refactor_script.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
61
62
require 'shellwords'
roots = %w(
src
tests
examples
benches
)
exclude_dirs = []
underscore_translations = {
}
other_translations = {
/EvolveReporter/ => 'StrategyReporter',
/HillClimbReporter/ => 'StrategyReporter',
/PermutateReporter/ => 'StrategyReporter',
}
#check:
#rg -i -o --no-line-number --no-filename '\w*context\w*' | sort | uniq -c
refactor_code = true
refactor_filenames = true
translations = underscore_translations.merge(other_translations)
translations.each do |from, to|
roots.each do |dirname|
filenames = Dir.glob(File.join(dirname, '**', '*'))
filenames.each do |filename|
p filename
next if File.directory?(filename)
next if exclude_dirs.detect { |exclude_dir| filename[exclude_dir] }
body = File.read(filename)
new_body = body.gsub(from, to)
if body != new_body
puts "replace #{from} to #{to} in #{filename}"
File.write(filename, new_body) if refactor_code
end
end
end
end
underscore_translations.each do |from, to|
roots.each do |dirname|
filenames = Dir.glob(File.join(dirname, '**', '*'))
filenames.each do |filename|
next if File.directory?(filename)
next if exclude_dirs.detect { |exclude_dir| filename[exclude_dir] }
next unless filename =~ from
new_filename = filename.gsub(from,to)
new_directory = File.dirname(new_filename)
mkdir_p_command = "mkdir -p #{Shellwords.escape(new_directory)}"
puts mkdir_p_command
%x[#{mkdir_p_command}] if refactor_filenames
mv_command = "mv #{Shellwords.escape(filename)} #{Shellwords.escape(new_filename)}"
puts mv_command
%x[#{mv_command}] if refactor_filenames
end
end
end