Skip to content

Instantly share code, notes, and snippets.

@nownabe
Last active March 23, 2018 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nownabe/2da122de910a58fdf113bb2ec291a12e to your computer and use it in GitHub Desktop.
Save nownabe/2da122de910a58fdf113bb2ec291a12e to your computer and use it in GitHub Desktop.
Renamer
# frozen_string_literal: true
require "color_echo"
patterns = {
/oldname/ => "newname",
}
skip_files = [
/^\.git\//,
/^vendor\//,
]
Dir.glob("**/*", File::FNM_DOTMATCH).each do |filename|
next unless File.file?(filename)
next if skip_files.any? { |p| p =~ filename }
flag = false
lines = File.read(filename).lines
catch(:next_file) do
lines.each_with_index do |line, i|
patterns.each do |pattern, replace|
if pattern =~ line
unless flag
flag = true
puts "\n******** #{filename} ********"
end
CE.fg(:gray)
puts lines[([0, i - 5].max)...i].map { |l| " " + l }
CE.fg(:red)
puts "-" + line
CE.fg(:green)
puts "+" + line.gsub(pattern, replace)
CE.fg(:gray)
puts lines[(i + 1)...([i + 5, lines.length].min)].map { |l| " " + l }
CE.fg(:h_white)
puts
print "Replace? [Y/n/s] "
ans = gets.chomp
case ans.downcase
when "n"
when "s"
throw(:next_file)
else
lines[i] = line = line.gsub(pattern, replace)
end
puts "=" * 32
end
rescue
throw(:next_file)
end
end
end
File.write(filename, lines.join("")) if flag
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment