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
63
64
65
66
|
# File 'lib/refactor.rb', line 7
def run(from, to)
from_camelized = from.camelize
from_dashed = from.dasherize
from_humanized = from.humanize
from_underscored = from.underscore
to_camelized = to.camelize
to_dashed = to.dasherize
to_underscored = to.underscore
camelized_regex = /(?<=\b|_)#{Regexp.quote(from_camelized)}(?=\b|_)/
dashed_regex = /(?<=\b|_)#{Regexp.quote(from_dashed)}(?=\b|_)/
underscored_regex = /(?<=\b|_)#{Regexp.quote(from_underscored)}(?=\b|_)/
Dir.glob('**/*') do |old_path|
unless old_path =~ %r{\A(coverage|pkg|tmp|vendor)(\z|/)}
old_basename = File.basename(old_path)
new_basename = old_basename.dup
new_basename.gsub!(dashed_regex, to_dashed)
new_basename.gsub!(underscored_regex, to_underscored)
if new_basename == old_basename
new_path = old_path
else
new_path = File.join(File.dirname(old_path), new_basename)
puts "#{old_path} –> #{new_path}"
FileUtils.mv(old_path, new_path)
end
if File.file?(new_path)
old_text = File.read(new_path)
new_text = old_text.dup
new_text.gsub!(camelized_regex, to_camelized)
new_text.gsub!(dashed_regex, to_dashed)
new_text.gsub!(underscored_regex, to_underscored)
unless new_text == old_text
File.write(new_path, new_text)
end
line_num = 0
new_text.each_line do |old_line|
new_line = old_line.gsub(/#{Regexp.quote(from_humanized)}/i, "\e[33m\\0\e[0m")
unless new_line == old_line
puts "\e[36m#{new_path}:#{line_num}\e[0m #{new_line}"
end
line_num += 1
end
end
end
end
end
|