-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_and_replace.rb
executable file
·64 lines (46 loc) · 1.28 KB
/
search_and_replace.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
require 'ptools'
if ARGV.length != 3
puts '''
Usage : ./search_and_replace DIR FIND REPLACE
Example : ./search_and_replace ~/Documents/workspace "foo" "bar"
'''
exit -1
end
class Scanner
attr_accessor :directory, :search_pattern, :replace_string, :extensions
def initialize
@directory = nil
@extensions = ['.h', '.m']
end
def start
scan(@directory)
end
def scan(dir)
Dir.foreach(dir) do |item|
next if item == '.' or item == '..'
full_path = File.join(dir, item)
if File.directory?(full_path)
self.scan(full_path)
else
if !File.binary?(full_path) && @extensions.include?(File.extname(full_path))
process_file(full_path) unless File.binary?(full_path)
end
end
end
end
def process_file(path)
print "Processing File = #{path}\n"
file_content = File.read(path)
file_processed_content = file_content.gsub(@search_pattern, @replace_string)
if file_content != file_processed_content
system "p4 edit \"#{path}\""
File.write(path, file_processed_content)
end
end
end
scanner = Scanner.new
scanner.directory = ARGV[0]
scanner.search_pattern = ARGV[1]
scanner.replace_string = ARGV[2]
scanner.start()