Class: Deal::DealRule
- Inherits:
-
Object
show all
- Includes:
- DealUtils
- Defined in:
- lib/deal/utils/deal_config.rb
Instance Attribute 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(config) ⇒ DealRule
Returns a new instance of DealRule.
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
|
# File 'lib/deal/utils/deal_config.rb', line 61
def initialize(config)
config['signs'] = [] if not config['signs']
config['exclude_paths'] = [] if not config['exclude_paths']
config['include_paths'] = [] if not config['include_paths']
config['judge_types'] = [JUDEG_TYPE[:TEXT]] if not config['judge_types']
config['judge_action'] = JUDEG_ACTION[:ABORT] if not config['judge_action']
config['replace'] = '' if not config['replace']
@judge_types = config['judge_types']
@judge_action = config['judge_action']
@replace = config['replace']
@signs = config['signs'].map do |item|
Regexp.new item.strip
end
@exclude_paths = config['exclude_paths'].map do |item|
Regexp.new item.strip
end
@include_paths = config['include_paths'].map do |item|
Regexp.new item.strip
end
@judge_types = config['judge_types']
check_type
@need_content = @judge_types.include?(JUDEG_TYPE[:TEXT]) || @judge_types.include?(JUDEG_TYPE[:MACH])
@results = []
end
|
Instance Attribute Details
#results ⇒ Object
Returns the value of attribute results.
60
61
62
|
# File 'lib/deal/utils/deal_config.rb', line 60
def results
@results
end
|
Instance Method Details
#_judge_path(path) ⇒ Object
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
# File 'lib/deal/utils/deal_config.rb', line 335
def _judge_path(path)
for sign in @signs
if sign.match? path.to_s
action_tip = ''
ret = action sign,path
if ret
if ret != path
item = Pathname.new path
target = item.dirname + ret
FileUtils.mv(path,target.to_s)
action_tip='rename to '+target
end
else
action_tip='remove files'
FileUtils.rm_rf path
end
@results.push DealResult.new('path',nil ,sign.source,action_tip,path)
break
end
end
end
|
#action(sign, line, path = nil) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/deal/utils/deal_config.rb', line 147
def action(sign,line,path=nil )
if @judge_action == JUDEG_ACTION[:REPLACE]
return @replace
elsif @judge_action == JUDEG_ACTION[:REMOVE]
return nil
elsif @judge_action == JUDEG_ACTION[:ABORT]
error =[]
error.push "find a error, do ABORT action:"
error.push"match key:#{sign.source}"
error.push "match item:#{line}"
error.push "match path:#{path}"
raise error.join("\n")
elsif @judge_action == JUDEG_ACTION[:REPORT]
return line
end
end
|
#check_type ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/deal/utils/deal_config.rb', line 106
def check_type
pass = true
for type in @judge_types
if !JUDEG_TYPE.values.include? type
logE "judge_types values [#{type}] is invalid"
pass = false
end
end
if !JUDEG_ACTION.values.include? @judge_action
logE "judge_action value [#{@judge_action}] is invalid"
pass = false
end
if !pass
raise 'param is error'
exit 1
end
end
|
#include_path(path) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/deal/utils/deal_config.rb', line 123
def include_path(path)
if @include_paths.length > 0
match = false
for include in @include_paths
if include.match? path
match = true
end
end
if !match
return false
end
end
if @exclude_paths.length > 0
for exclude in @exclude_paths
if exclude.match? path
return false
end
end
end
return true
end
|
#judge_context(path) ⇒ Object
331
332
333
|
# File 'lib/deal/utils/deal_config.rb', line 331
def judge_context(path)
end
|
#judge_dir? ⇒ Boolean
163
164
165
|
# File 'lib/deal/utils/deal_config.rb', line 163
def judge_dir?
return @judge_types.include?(JUDEG_TYPE[:DIR])
end
|
#judge_dir_path(path) ⇒ Object
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/deal/utils/deal_config.rb', line 302
def judge_dir_path(path)
if !include_path(path)
logW 'jump path :'+path
return
end
if @judge_types.include?(JUDEG_TYPE[:DIR])
_judge_path(path)
end
end
|
#judge_file? ⇒ Boolean
167
168
169
|
# File 'lib/deal/utils/deal_config.rb', line 167
def judge_file?
return @judge_types.include?(JUDEG_TYPE[:FILE])
end
|
#judge_file_path(path) ⇒ Object
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/deal/utils/deal_config.rb', line 291
def judge_file_path(path)
if !include_path(path)
logW 'jump path :'+path
return
end
if @judge_types.include?(JUDEG_TYPE[:FILE])
_judge_path(path)
end
end
|
#judge_item(path) ⇒ Object
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/deal/utils/deal_config.rb', line 179
def judge_item(path)
if !include_path(path)
logW 'jump path :'+path
return
end
result = []
if FileTest.directory?(path)
if @judge_types.include?(JUDEG_TYPE[:DIR])
result = result.concat judge_path(path)
end
return result
else
if @judge_types.include?(JUDEG_TYPE[:FILE])
result = result.concat judge_path(path)
end
end
if !@need_content
return result
end
lines = []
need_rewrite = false
if path.include? '.mm'
puts path
end
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 sign in @signs
if sign.match? line
action_tip = ''
ret = action sign,line,path
need_replace = true
if ret
lines.push ret
if ret != line
need_rewrite = true
action_tip = "rename to #{ret}"
end
else
action_tip = 'remove'
end
ret = DealResult.new('text',line,sign.source,action_tip,path)
ret.set_line_n lines.length
result.push ret
end
end
if !need_replace
lines.push line
end
end
end
if need_rewrite
begin
File.open(path, "w") do |aFile|
lines.each { |line|
aFile.puts line
}
end
rescue Exception =>e
logE "faile write open #{path}"
end
end
return result
end
|
#judge_line(path, line, line_n) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/deal/utils/deal_config.rb', line 313
def judge_line(path,line,line_n)
for sign in @signs
if sign.match? line
action_tip = ''
ret = action sign,line,path
if ret
if ret != line
action_tip = "rename to #{ret}"
end
else
action_tip = 'remove'
end
@results.push DealResult.new('text',line,sign.source,action_tip,path)
break
end
end
end
|
#judge_mach_o(line) ⇒ Object
357
358
359
360
361
362
363
364
365
|
# File 'lib/deal/utils/deal_config.rb', line 357
def judge_mach_o(line)
raise '暂未实现'
return []
end
|
#judge_text_line? ⇒ Boolean
171
172
173
|
# File 'lib/deal/utils/deal_config.rb', line 171
def judge_text_line?
return @judge_types.include?(JUDEG_TYPE[:TEXT])
end
|
#judge_text_multi? ⇒ Boolean
175
176
177
|
# File 'lib/deal/utils/deal_config.rb', line 175
def judge_text_multi?
return @judge_types.include?(JUDEG_TYPE[:TEXT])
end
|
#to_s(verbose = false) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/deal/utils/deal_config.rb', line 89
def to_s(verbose = false )
ls = []
ls.push "{"
ls.push "signs:#{@signs.map{|item| if item.respond_to?(:source)
item.source
else
item
end
}.join(",")}" if @signs.length > 0
ls.push "exclude_paths:#{@exclude_paths.map{|item|item.source}.join("|")}" if @exclude_paths.length > 0
ls.push "include_paths:#{@include_paths.map{|item|item.source}.join("|")}" if @include_paths.length > 0
ls.push "judge_types:#{@judge_types.join("|")}" if @judge_types.length > 0
ls.push "judge_action:#{@judge_action}"
ls.push "replace:#{@replace}" if @replace
ls.push "}"
return ls.join("\n")
end
|