lib/white.rb in white-0.1.0 vs lib/white.rb in white-0.2.1
- old
+ new
@@ -1,17 +1,18 @@
require 'optparse'
class White
def self.run args
- extensions = %w/rb java xml md coffee js html css less yml php info install module h m txt/
+ extensions = %w/rb java xml md markdown coffee js eco html haml css less styl yml php info install module h m c txt/
names = %w/Rakefile Cakefile/
default_tab_size = nil
tab_sizes = {
}
- exclusions = ['**/.git/**']
+ exclusions = ['**/.git/**', '**/node_modules/**/*']
verbose = false
+ dry_run = false
opts = OptionParser.new do |opts|
opts.banner = "Usage: white [options]"
opts.separator ""
@@ -26,17 +27,21 @@
opts.on("-e", "--ext=EXT", "Include files with the given extension (e.g. -e inc)") do |v|
extensions << v
end
- opts.on("-n", "--name=NAME", "Include files with the given name (e.g. -n Cakefile)") do |v|
+ opts.on("-i", "--name=NAME", "Include files with the given name (e.g. -n Cakefile)") do |v|
names << v
end
opts.separator ""
opts.separator "Common options:"
+ opts.on("-n", "--dry-run", "Don't actually change anything on disk") do
+ dry_run = true
+ end
+
opts.on("-v", "--verbose", "Show a full list of processed files") do
verbose = true
end
opts.on_tail("-h", "--help", "Show this message") do
@@ -52,10 +57,12 @@
exclusions.each do |exclusion|
files -= Dir[exclusion]
end
+ files -= Dir['**/.keepwhitespace'].map { |f| Dir[File.join(File.dirname(f), '**/*')] }.flatten
+
files_with_tabs = []
files.each do |file|
tab_size = tab_sizes[File.extname(file)] || default_tab_size
tab = ' ' * tab_size if default_tab_size
@@ -64,13 +71,21 @@
text = File.read(file)
original = text.dup
prev = text.dup
+ text.gsub! /\r/, ''
+ errors << "Windows line breaks" if text != prev
+
+ prev = text.dup
text.gsub! /[ \t]+$/, ''
errors << "trailing whitespace" if text != prev
+ prev = text.dup
+ text.gsub! /\r/, ''
+ errors << "Windows line breaks" if text != prev
+
if text =~ /^ *?\t[\t ]*/
if default_tab_size == nil
files_with_tabs << file unless files_with_tabs.include?(file)
elsif default_tab_size > 0
text.gsub!(/^ *?\t[\t ]*/) { |indent| indent.gsub "\t", tab }
@@ -87,10 +102,10 @@
end
end
if text != original
puts "#{file} (#{errors.join(', ')})"
- File.open(file, 'w') { |f| f.write text }
+ File.open(file, 'w') { |f| f.write text } unless dry_run
else
puts "#{file} OK" if verbose
end
end