Skip to content

Instantly share code, notes, and snippets.

@wynst
Created September 14, 2012 10:45
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 wynst/3721256 to your computer and use it in GitHub Desktop.
Save wynst/3721256 to your computer and use it in GitHub Desktop.
rake task to cleanup codebase from devilish whitespace (taken from diaspora project)
# rake task to cleanup codebase from devilish whitespace
# works on OSX, on linux the :new_line_at_eof task should be `sed '$a\'`
#
# at the end of the day, 'cleanup whitespace' commit that cleans up all the whitespace
# wouldn't worth to be included in the repo. this is because
# `git blame` will fail with many 'cleanup whitespace' commits
#
# ref:
# https://www.chiliproject.org/issues/436
# https://github.com/diaspora/diaspora/commits/master/lib/tasks/whitespace.rake
namespace :whitespace do
desc 'Removes trailing whitespace'
task :cleanup do
sh %{for f in `find . -type f | grep -v -e '.git/' -e 'tmp/' -e 'app/assets/images' -e 'public/' -e 'db/' -e 'doc/'`;
do cat $f | sed 's/[ \t]*$//' > tempo; cp tempo $f; rm tempo; echo -n .;
done}
end
desc 'Converts hard-tabs into two-space soft-tabs'
task :retab do
sh %{for f in `find . -type f | grep -v -e '.git/' -e 'tmp/' -e 'app/assets/images' -e 'public/' -e 'db/' -e 'doc/'`;
do cat $f | sed 's/\t/ /g' > tempo; cp tempo $f; rm tempo; echo -n .;
done}
end
desc 'Remove consecutive blank lines'
task :scrub_gratuitous_newlines do
sh %{for f in `find . -type f | grep -v -e '.git/' -e 'tmp/' -e 'app/assets/images' -e 'public/' -e 'db/' -e 'doc/'`;
do cat $f | sed '/./,/^$/!d' > tempo; cp tempo $f; rm tempo; echo -n .;
done}
end
desc 'Add new line at the end of the file'
task :new_line_at_eof do
sh %{for f in `find . -type f | grep -v -e '.git/' -e 'tmp/' -e 'app/assets/images' -e 'public/' -e 'db/' -e 'doc/'`;
do cat $f | sed '$a\\' > tempo; cp tempo $f; rm tempo; echo -n .;
done}
end
task :all => [:new_line_at_eof, :cleanup, :retab, :scrub_gratuitous_newlines] do
desc "clean whitespace"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment