Class: Deal::Command
- Inherits:
-
CLAide::Command
- Object
- CLAide::Command
- Deal::Command
show all
- Includes:
- DealUtils
- Defined in:
- lib/deal/command.rb
Direct Known Subclasses
Text
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from DealUtils
#logC, #logE, #logInner, #logN, #logW, #print_console, #process, #run_shell, #set_progress
Constructor Details
#initialize(argv) ⇒ Command
Returns a new instance of Command.
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
|
# File 'lib/deal/command.rb', line 42
def initialize(argv)
super
configs = argv.option('config')
@signs = argv.option('signs',"").split('|')
@include_paths = argv.option('include_paths',"" ).split('|')
@exclude_paths = argv.option('exclude_paths',"" ).split('|')
@judge_types = argv.option('judge_types',"text" ).split('|')
@replace = argv.option('replace','')
@judge_action = argv.option('judge_action','abort')
@configs = []
if configs
if FileTest.exist? configs
configs = File.read(configs)
begin
configs = JSON.parse(configs)
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
for config in configs
config = DealRule.new(config)
@configs.push config
end
else
logE "file:#{configs} not exist"
return
end
else
config = {}
config["signs"] = @signs
config["include_paths"] = @include_paths
config["exclude_paths"] = @exclude_paths
config["judge_types"] = @judge_types
config["replace"] = @replace
config["judge_action"] = @judge_action
config = DealRule.new(config)
@configs.push config
end
@judge_count = 0
@total_count = 0
@progress = 0.0
@jump_file = {}
@results = []
inputs = argv.option('inputs',"").split('|')
@inputs = []
for input in inputs
ls = Dir.glob(input,File::FNM_DOTMATCH|File::FNM_PATHNAME)
ls = ls.map { |item|
item = Pathname.new item
if item.basename.to_s == '.' || item.basename.to_s == '..'
item = item.dirname
end
item.to_s
}
@inputs = @inputs.concat(ls)
end
if @inputs.length > 1
i = 0
while i<@inputs.length
base = @inputs[i]
j = 0
while j<@inputs.length
if @inputs[i]!= @inputs[j] && @inputs[j].index(@inputs[i]) == 0
@inputs.delete_at j
j-=1
end
j+=1
end
i += 1
end
end
end
|
Class Method Details
.options ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/deal/command.rb', line 30
def self.options
[
['--config', 'json or input files.'],
['--inputs', 'text,file or dir'],
['--signs', 'search items'],
['--judge_types', 'file|dir|text|mach-o,default is text'],
['--judge_action', 'which actions to match word."remove|replace|abort|report",default is abort'],
['--replace', 'replace word'],
['--include_paths', 'include search paths'],
['--exclude_paths', 'exclude search paths']
].concat(super)
end
|
Instance Method Details
#deal_item(configs, input) ⇒ Object
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/deal/command.rb', line 186
def deal_item(configs,input)
@judge_count += 1
progress = @judge_count*1.0/@total_count
if progress - @progress > 0.01
@progress = progress
logW "处理进度:#{format("%.2f",progress*100).to_f}%"
end
configs = []+configs
line_configs = []
multi_configs = []
directory = File.directory?(input)
for config in configs
if directory
if !config.judge_dir_path(input)
configs.delete config
end
else
config.judge_file_path(input)
end
if config.judge_text_line?
line_configs.push config
end
if config.judge_text_multi?
multi_configs.push config
end
end
if !directory
if line_configs.length > 0
judeg_file_line(line_configs,input)
end
else
Dir.entries(input).each do |sub|
if sub != '.' && sub != '..'
deal_item(configs,"#{input}/#{sub}")
else
@judge_count += 0.5
end
end
end
end
|
#judeg_file_line(configs, path) ⇒ Object
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
|
# File 'lib/deal/command.rb', line 143
def judeg_file_line(configs,path)
line_configs = []
for config in configs
line_configs.push config if config.judge_text_line?
end
if line_configs.length > 0
lines = []
File.open(path, "r") do |aFile|
is_binary = false
while line=aFile.gets
need_replace = false
for i in 0...(line.length)
begin
if line[i].ord == 0
is_binary = true
break
end
rescue Exception =>e
is_binary = true
break
end
end
if is_binary
break
end
for config in line_configs
replace = config.judge_line path,line,lines.length
if replace && line != replace
need_replace
end
end
if !need_replace
lines.push line
end
end
end
end
end
|
#run ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/deal/command.rb', line 230
def run
logN "--inputs glob:\n "+@inputs.join("\n")
logN '--configs'
for config in @configs
logN config.to_s verbose?
end
for input in @inputs
if FileTest.directory? input
match = Pathname.new(input).join('**/*').to_s
@total_count += Dir.glob(match,File::FNM_DOTMATCH).size
else
@total_count += 1
end
end
logW "开始扫描,总共#{@total_count}个文件"
for input in @inputs
if FileTest.exist?(input)
deal_item @configs, input
else
logE input+' file does not exist'
end
end
progress = @judge_count*1.0/@total_count
logW "处理进度:100%"
logN '=============================='
logN "共进行了#{@judge_count}次匹配"
for config in @configs
logW "config:#{config.to_s verbose?}"
if config.results.length == 0
logN '没有匹配到结果'
else
logN "匹配到 #{config.results.length} 个结果,如下:"
for result in config.results
logN result.to_s verbose?
end
end
logN '=============================='
end
end
|
#validate! ⇒ Object
120
121
122
123
124
125
|
# File 'lib/deal/command.rb', line 120
def validate!
super
help! 'Please specify an config' unless @configs.length > 0
help! 'Please specify an input' unless @inputs
end
|