-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabs2spaces.rb
44 lines (39 loc) · 1.71 KB
/
tabs2spaces.rb
1
2
3
4
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
#!/usr/bin/env ruby
# Call it via the command line, where the first (and only) parameter is the directory where
# the source code files exist
require 'find'
# Path & executables vars
source_dir = (!ARGV.first.empty? && File.directory?(ARGV.first)) ? ARGV.first : "/tmp"
tmp_dir = '/tmp/'
expand_path = '/usr/bin/expand'
cat_path = '/bin/cat'
rm_path = '/bin/rm'
# How many spaces & what to (and not to) convert
how_many_spaces = 2
valid_extensions = ['.rake', '.css', '.rhtml', '.rb', '.erb', '.php', '.js', '.haml', '.yml', '.sql', '.conf', '.builder']
valid_extensionless_files = ['.htaccess']
invalid_dirs = ['.svn', '.git', 'changes', 'cache', 'create', 'data', 'php', 'vendor', 'test', 'tmp', 'log', 'doc', 'gems']
invalid_files = ['jquery.js', 'jquery.corner.js', 'jquery-ui.js', 'prototype.js', 'controls.js', 'dragdrop.js', 'effects.js', 'dispatch.fcgi', 'dispatch.rb', 'merb.fcgi', 'dispatch.cgi']
total_files = 0
Find.find(source_dir) do |path|
if FileTest.directory?(path)
# This is a directory
if invalid_dirs.include?(File.basename(path.downcase))
Find.prune
else
# This is an OK directory
next
end
else
# here are files
extension = File.extname(path.downcase)
if valid_extensionless_files.include?(File.basename(path.downcase)) || (valid_extensions.include?(extension) && !invalid_files.include?(File.basename(path.downcase)))
puts " -> Converting file: #{path}\n"
tmp_file = "#{tmp_dir}~#{File.basename(path.downcase)}"
full_cmd = "#{expand_path} -t#{how_many_spaces} #{path} > #{tmp_file}; #{cat_path} #{tmp_file} > #{path}; #{rm_path} #{tmp_file}"
system(full_cmd)
total_files += 1
end
end
end
puts "Total files modified: #{total_files}"