Skip to content

Instantly share code, notes, and snippets.

@senorprogrammer
Created August 30, 2011 21:06
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 senorprogrammer/1182045 to your computer and use it in GitHub Desktop.
Save senorprogrammer/1182045 to your computer and use it in GitHub Desktop.
Rake task to strip whitespace
require 'extlib/pathname'
require 'zlib'
desc 'Strip whitespace from source files'
task :strip do
# files and extensions to process
FILES = %w[ capfile CHANGELOG LICENSE Manifest MIT-LICENSE README QUICKLINKS README_FOR_APP RUNNING_UNIT_TESTS Rakefile SPECS TODO USAGE .autotest .gitignore .htaccess ].freeze
EXTENSIONS = %w[ builder cgi conf css deploy erb example fcgi feature gemspec haml htc htm html js key markdown opts php rake ratom rb rcsv rdf rhtml rjs rpdf ru rxml sake sass sh sql thor txt vcf xml yml ].freeze
paths = Pathname.glob(Rails.root / '*') - [ Rails.root / 'vendor' ]
paths.each do |path_in|
start_path = path_in.directory? ? path_in + '**/*' : path_in
Pathname.glob((start_path).to_s).each do |path|
unless path.file? && path.size? && path.readable? && path.writable? && (FILES.include?(path.basename.to_s) || EXTENSIONS.include?(path.extname[1..-1]))
# puts "Skipping #{path}" if path.file?
next
end
# 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 it if the file was not modified
next if Zlib.crc32(document) == Zlib.crc32(path.read)
puts "Modifying #{path}"
path.open('w') { |f| f.write document }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment