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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/findr/cli.rb', line 37
def execute(stdout, arguments=[])
@stdout = stdout
options = {}
options[:coding] = 'utf-8'
@context_lines = 0
@option_parser = OptionParser.new do |opts|
opts.on('-g', '--glob FILE SEARCH GLOB', 'e.g. "*.{rb,erb}"; glob relative to the current directory') do |glob|
options[:glob] = glob
fail "-g option cannot be combined with -G" if options[:gglob]
end
opts.on('-G', '--global-glob GLOB', 'e.g. "/**/*.{rb,erb}"; glob may escape the current directory') do |glob|
options[:gglob] = glob
fail "-G option cannot be combined with -g" if options[:glob]
end
opts.on('-i', '--ignore-case', 'do a case-insensitive search') do
options[:re_opt_i] = true
end
opts.on('-n', '--number-of-context LINES', 'number of lines in context to print') do |lines|
@context_lines = lines.to_i
end
opts.on('-x', '--execute', 'actually execute the replacement') do
options[:force] = true
end
opts.on('-c', '--coding FILE CODING SYSTEM', 'e.g. "iso-8859-1"') do |coding|
options[:coding] = coding
end
opts.on('-s', '--save', "saves your options to #{CONFIGFILE} for future use") do
options[:save] = true
end
opts.on('-C', '--codings-list', "list available encodings") do
pp Encoder.list
exit
end
opts.on('-v', '--verbose', "verbose output") do
@verbose = true
end
end
@option_parser.banner = self.banner
@option_parser.parse!( arguments )
if !options[:glob] && !options[:gglob]
options[:glob] = '*'
end
if options[:save]
options.delete(:save)
stdout.puts red "Nothing to save." unless options
File.open( CONFIGFILE, 'w' ) { |file| YAML::dump( options, file ) }
stdout.puts green "Saved options to file #{CONFIGFILE}."
exit
else
if File.exists?( CONFIGFILE )
file_options = YAML.load_file(CONFIGFILE)
file_options.delete(:save)
options.merge!( file_options )
stdout.puts green "Using #{CONFIGFILE}."
end
end
options[:gglob] = '**/' + options[:glob] unless options[:gglob]
show_usage if arguments.size == 0
arguments.clone.each do |arg|
if !options[:find]
options[:find] = Regexp.compile( arg, options[:re_opt_i] )
arguments.delete_at(0)
elsif !options[:replace]
options[:replace] = arg
arguments.delete_at(0)
else
show_usage
end
end
show_usage if arguments.size != 0
stdout.puts green "File inclusion glob: " + options[:gglob]
stdout.puts green "Searching for regex " + options[:find].to_s
stats = {}
stats[:total_hits] = stats[:local_hits] = stats[:hit_files] = stats[:total_files] = 0
replacement_done = false
tempfile = nil
coder = Encoder.new( options[:coding] )
verbose "Building file tree..."
files = Pathname.glob("#{options[:gglob]}")
verbose [' ', files.count, ' files found.']
files.each do |current_file|
next unless current_file.file?
verbose ["Reading file ", current_file]
stats[:total_files] += 1
stats[:local_hits] = 0
firstmatch = true
linenumber = 0
tempfile = Tempfile.new( 'current_file.basename' ) and verbose([' Create tempfile ',tempfile.path]) if options[:replace] && options[:force]
clear_context(); print_post_context = 0
current_file.each_line do |l|
begin
l, coding = coder.decode(l)
rescue Encoder::Error
stdout.puts "Skipping file #{current_file} because of error on line #{linenumber}: #{$!.original.class} #{$!.original.message}"
if tempfile
tempfile_path = tempfile.path
tempfile.unlink and verbose([' Delete tempfile ', tempfile_path])
end
break
end
linenumber += 1
if l=~ options[:find]
stats[:local_hits] += 1
if firstmatch
stdout.puts red("#{current_file.cleanpath}:")
end
if @context_lines > 0
pop_context.map do |linenumber, l|
print_line( linenumber, l, coding, false )
end
print_post_context = @context_lines
end
print_line( linenumber, l.gsub( /(#{options[:find]})/, bold('\1') ), coding, :bold )
firstmatch = false
if options[:replace]
l_repl = l.gsub( options[:find], options[:replace] )
tempfile.puts coder.encode(l_repl, coding) if tempfile
replacement_done = true
print_line( linenumber, l_repl, coding, :blue )
end
else
if tempfile
tempfile.puts coder.encode(l, coding)
end
if print_post_context > 0
print_post_context -= 1
print_line( linenumber, l, coding )
else
push_context([linenumber, l])
end
end
end
if tempfile
tempfile.close
if stats[:local_hits] > 0
FileUtils.cp( tempfile.path, current_file )
verbose([' Copy tempfile ', tempfile.path, ' to ', current_file])
end
tempfile_path = tempfile.path
tempfile.unlink and verbose([' Delete tempfile ', tempfile_path])
end
if stats[:local_hits] > 0
stats[:total_hits] += stats[:local_hits]
stats[:hit_files] += 1
end
end
stdout.puts green( "#{stats[:total_hits]} occurences (lines) in #{stats[:hit_files]} of #{stats[:total_files]} files found." )
end
|