Created
August 19, 2010 03:48
-
-
Save jpignata/536972 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# script/whitespace | |
# | |
# Strips whitespace from any files modified in git | |
# Also: | |
# - converts tabs to spaces | |
# - ensures a single newline at the end | |
class WhitespaceProcessor | |
def self.process(code) | |
result = [] | |
code.each do |line| | |
line.gsub!(/(\s+)$/, "\n") | |
line.gsub!(/\t/, ' ') | |
result << line | |
end | |
while result.last =~ /^$/ | |
result.pop | |
end | |
unless result.last =~ /\n$/ | |
result << "\n" | |
end | |
code = result.join | |
code.gsub!(/\A\n*/, '') | |
code | |
end | |
end | |
extensions = "\.(rb|js|haml|html|css|sass|md)" | |
if ARGV.include?('--all') | |
files = `find . -type file | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep "#{extensions}"`.split(/\n/) | |
puts "* Stripping whitespace from all project files" | |
else | |
files = `git status`.split("\n").select do | |
|file| file =~ /^#\t(modified|new file|renamed):/ && | |
!(file =~ /\(new commits\)/) && | |
file =~ /#{extensions}/ | |
end | |
puts "* Stripping whitespace from modified files." | |
end | |
files.each do |line| | |
line = line.split(/[\t\s]+/).last | |
puts " processing #{line}..." | |
code = File.read(line) | |
File.open(line, 'w+') { |f| f << (WhitespaceProcessor.process(code)) } | |
end | |
puts "* DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment