class Deal::DealRule

Attributes

results[R]

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

_judge_path(path) click to toggle source
# 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 ) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# File lib/deal/utils/deal_config.rb, line 123
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_context(path) click to toggle source
# File lib/deal/utils/deal_config.rb, line 331
def judge_context(path)

end
judge_dir?() click to toggle source
# File lib/deal/utils/deal_config.rb, line 163
def judge_dir?
    return @judge_types.include?(JUDEG_TYPE[:DIR])
end
judge_dir_path(path) click to toggle source
# 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?() click to toggle source
# File lib/deal/utils/deal_config.rb, line 167
def judge_file?
    return @judge_types.include?(JUDEG_TYPE[:FILE])
end
judge_file_path(path) click to toggle source
# 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) click to toggle source
# 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
                  # 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,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

    # 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_line(path,line,line_n) click to toggle source
# 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) click to toggle source
# File lib/deal/utils/deal_config.rb, line 357
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_text_line?() click to toggle source
# File lib/deal/utils/deal_config.rb, line 171
def judge_text_line?
    return @judge_types.include?(JUDEG_TYPE[:TEXT])
end
judge_text_multi?() click to toggle source
# 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 ) click to toggle source
# 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