5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/white.rb', line 5
def self.run args
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/**', '**/node_modules/**/*']
verbose = false
dry_run = false
opts = OptionParser.new do |opts|
opts.banner = "Usage: white [options]"
opts.separator ""
opts.separator "Tab expansion:"
opts.on("-t", "--tabsize=SIZE", "Expand tabs using the given tab size") do |size|
default_tab_size = size.to_i
end
opts.separator ""
opts.separator "Picking files to process:"
opts.on("-e", "--ext=EXT", "Include files with the given extension (e.g. -e inc)") do |v|
extensions << v
end
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
puts opts
exit
end
end
opts.parse!(args)
mask = "{" + (names + extensions.collect { |ext| "*.#{ext}" }).join(",") + "}"
files = Dir["**/#{mask}"].to_a
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
errors = []
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 }
errors << "tabs"
end
end
if text =~ /\t/
if default_tab_size == nil
files_with_tabs << file unless files_with_tabs.include?(file)
elsif default_tab_size > 0
text.gsub!("\t", tab)
errors << "tabs inside source"
end
end
if text != original
puts "#{file} (#{errors.join(', ')})"
File.open(file, 'w') { |f| f.write text } unless dry_run
else
puts "#{file} OK" if verbose
end
end
unless files_with_tabs.empty?
$stderr.puts "\nThe following file(s) contain tabs. To expand them, please use -t4 option."
files_with_tabs.each { |f| $stderr.puts " - #{f}" }
end
end
|