class Deal::Command

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# 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
options() click to toggle source
Calls superclass method
# 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

Public Instance Methods

deal_item(configs,input) click to toggle source
# 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) click to toggle source

def handle_text(path)

line_n = 0
File.open(path, "r") do |aFile|
    aFile.each do |line|
        for config in @configs
            if result = config.judge_file_line(line)
                result.set_line_n(line_n)
                @results.push result
            end
        end
        line_n+=1
    end
end

end

# 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
                        # logE "file prase error:#{e.to_s},path:#{path}"
                        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() click to toggle source
# 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!() click to toggle source
Calls superclass method
# 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