Skip to content

Instantly share code, notes, and snippets.

@eventualbuddha
Forked from indirect/source.rake
Created October 14, 2009 20:54
Show Gist options
  • Save eventualbuddha/210400 to your computer and use it in GitHub Desktop.
Save eventualbuddha/210400 to your computer and use it in GitHub Desktop.
def find_and_replace_in_source_files(find, replace)
puts "Search and replace #{find.inspect} => #{replace.inspect}"
extensions = %w[.rhtml .rxml .erb .builder .rb .css .js .rake]
files = Dir["**/*"]
files.each do |file_name|
next if (file_name =~ /^vendor/) || !extensions.include?(File.extname(file_name))
text = File.open(file_name, 'r'){ |file| file.read }
if text.gsub!(find, replace)
puts "rewriting #{file_name}..."
File.open(file_name, 'w'){|file| file.write(text)}
end
end
end
namespace :source do
desc "Replace all tabs in source code files with two spaces"
task :detab do
find_and_replace_in_source_files("\t", " ")
end
desc "Remove trailing whitespace on the ends of lines"
task :detrail do
find_and_replace_in_source_files(/ +$/, '')
end
desc "Replace all instances of {pattern} with {result}"
task :gsub, :pattern, :result do |t, args|
find_and_replace_in_source_files(Regexp.new(args[:pattern] || ENV['PATTERN']), args[:result] || ENV['RESULT'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment