Skip to content

Instantly share code, notes, and snippets.

@pope
Created August 12, 2008 15:47
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 pope/5062 to your computer and use it in GitHub Desktop.
Save pope/5062 to your computer and use it in GitHub Desktop.
#taken and modified from http://github.com/dkubb/dm-dev/tree/master
desc 'Strip trailing whitespace from source files'
task :strip do
require 'pathname'
require 'zlib'
# files and extensions to process
files = %w[ capfile CHANGELOG MIT-LICENSE README README_FOR_APP RUNNING_UNIT_TESTS Rakefile TODO USAGE .autotest .gitignore ].freeze
extensions = %w[ builder cgi conf css deploy erb example fcgi haml htc htm html js java key opts php properties rake ratom rb rcsv rdf rhtml rjs rpdf rxml sake sass sh sql txt vcf xml yml ].freeze
Pathname.getwd.find do |path|
# skip unreadable, unwritable, .git and .svn directories
Find.prune if (path.directory? && (!path.readable? || !path.writable?)) || %w[ .git .svn ].include?(path.basename.to_s)
# skip non-files, zero-sized files, files not matching specific names, or files without the matching extensions
next unless path.file? && path.size? && (files.include?(path.basename.to_s) || extensions.include?(path.extname[1..-1]))
# replace leading whitespace (including tabs) with spaces
# replace trailing whitespace with a newline
document = path.open('r') do |f|
f.collect { |line| line.gsub(/\G\s/, ' ').rstrip + "\n" }.join.rstrip
end + "\n"
# skip the file if it was not modified
next if Zlib.crc32(document) == Zlib.crc32(path.read)
puts "Stripping whitepsace from #{path}"
path.open('w') { |f| f.write document }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment