Skip to content

Instantly share code, notes, and snippets.

@indirect
Created September 29, 2009 18:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save indirect/197146 to your computer and use it in GitHub Desktop.
Save indirect/197146 to your computer and use it in GitHub Desktop.
rake tasks to fix whitespace
def find_and_replace_in_source_files(find, replace)
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 }
changed = text.gsub!(find, replace)
File.open(file_name, 'w'){|file| file.write(changed)} if changed
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment