Class: Deal::DealRule

Inherits:
Object
  • Object
show all
Includes:
DealUtils
Defined in:
lib/deal/utils/deal_config.rb

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.



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
# File 'lib/deal/utils/deal_config.rb', line 52

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])
    logN to_s
end

Instance Method Details

#action(sign, line) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/deal/utils/deal_config.rb', line 134

def action(sign,line)
    if @judge_action == JUDEG_ACTION[:REPLACE]
        return @replace
    elsif @judge_action == JUDEG_ACTION[:REMOVE]
        return nil
    elsif @judge_action == JUDEG_ACTION[:ABORT]
        raise "
                find a error, do ABORT action:
                match key:#{sign.source}
                match item:#{line}
            "
    elsif @judge_action == JUDEG_ACTION[:REPORT]
        return line
    end
end

#check_typeObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/deal/utils/deal_config.rb', line 93

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 [#{type}] is invalid"
        pass = false
    end
    if !pass
        raise 'param is error'
        exit 1
    end
end

#include_path(path) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/deal/utils/deal_config.rb', line 110

def include_path(path)
    #0> File.fnmatch('**/.git',path+"/.git",File::FNM_PATHNAME | File::FNM_DOTMATCH)
    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_item(path) ⇒ Object



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
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
# File 'lib/deal/utils/deal_config.rb', line 149

def judge_item(path)
    if  !include_path(path)
        logW 'jump 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
                  # logE "file prase error:#{e.to_s},path:#{path}"
                  break
                end

            end
            if is_binary
                break
            end
            for sign in @signs
                if sign.match? line
                    action_tip = ''
                    ret = action sign,line
                    result.push ret
                    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)
                    ret.set_line_n lines.length
                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

    # run_shell "file #{path}",true ,2 do |out ,err,status|
    #     if status == 0 && out.length > 0
    #         type = out[0]
    #         # if out[0].include? 'text'
    #         #     result.concat judge_text path
    #         # elsif out[0].include? 'Mach-O'
    #         #     result.concat judge_mach_o path
    #         # else
    #         #     logW "jump file type:#{out[0]}, path:#{path}"
    #         # end
    #     end
    # end
    # if include_type type
    #     if type.include? 'text' || type.include?('JSON')
    #         process do
    #             result.concat judge_text path
    #             print path
    #         end
    #     elsif type.include? 'Mach-O'
    #         result.concat judge_mach_o path
    #     else
    #         logW "jump file type:#{type}, path:#{path}"
    #     end
    # end
    # return result

end

#judge_mach_o(line) ⇒ Object



285
286
287
288
289
290
291
292
293
# File 'lib/deal/utils/deal_config.rb', line 285

def judge_mach_o(line)
    # for match in @match_binary
    #     if match.match? path
    #         return DealResult.new(DEAL_TYPE_BINARY,line,match.to_s,@replace)
    #     end
    # end
    raise '暂未实现'
    return []
end

#judge_path(path) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/deal/utils/deal_config.rb', line 262

def judge_path(path)
    results = []
    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
            return results.push DealResult.new('path',path,sign.source,action_tip)
        end
    end
    return results
end

#to_sObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/deal/utils/deal_config.rb', line 80

def to_s
    ls = []
    ls.push "{"
    ls.push "signs:#{@signs.join("\n")}" if @signs.length > 0
    ls.push "exclude_paths:#{@exclude_paths.join("\n")}" if @exclude_paths.length > 0
    ls.push "include_paths:#{@include_paths.join("\n")}" if @include_paths.length > 0
    ls.push "judge_types:#{@judge_types.join("\n")}" if @judge_types.length > 0
    ls.push "judge_action:#{@judge_action}"
    ls.push "replace:#{@replace}" if @replace
    ls.push "}"
    return ls.join("\n")

end