Created
October 7, 2009 17:38
-
-
Save kevmoo/204239 to your computer and use it in GitHub Desktop.
A ruby script for whitespace clean-up
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 | |
# Take a file_name | |
# If there are any "bad" lines: | |
# => Print out the file name | |
# => Print out the offending line numbers | |
# => Overwrite the file with "good" lines | |
# You might want to make sure *everything* is in source control first | |
# It's "good form" to have mass white-space clean-up be a single check-in, too | |
def clean_whitespace(file_name) | |
lines = [] | |
File.open(file_name) do |file| | |
file.each_line do |line| | |
lines << line | |
end | |
end | |
clean = true | |
lines.each_index do |index| | |
line = lines[index] | |
#match one or more spaces/tabs at the end of the line -> nothing | |
new_line = line.gsub(/[ \t]+$/, "") | |
#match all tabs and replace with two spaces | |
new_line = new_line.gsub(/\t/, " ") | |
if(new_line != line) | |
if(clean) | |
puts "Cleaning '#{file_name}'" | |
clean = false | |
end | |
puts "#{index+1}" | |
lines[index] = new_line | |
end | |
end | |
if(!clean) | |
File.open(file_name, 'w') do |file| | |
lines.each { |line| file.write(line) } | |
end | |
end | |
end | |
# All as, mxml, rb, sass, and haml files in this and all subdirectories | |
source_files_pattern = File.join("**", "{*.{as,mxml,rb,sass,haml},Rakefile}") | |
# create an array of all files that patch the above file pattern | |
source_files = Dir.glob(source_files_pattern) | |
# Exclude files in a a directory -> /site/vendor | |
source_files = source_files.select { |file| !(file =~ /site\/vendor\/.*/) } | |
source_files.each { |file| clean_whitespace(file) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment