class Deal::Text

Public Class Methods

new(argv) click to toggle source
Calls superclass method Deal::Command::new
# File lib/deal/command/text.rb, line 31
def initialize(argv)
    super
    configs = argv.option('config')
    @include = argv.option('include',"")
    @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',"" ).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 = []

    @input = argv.option('input')
    @input = Dir.glob(@input,)


end
options() click to toggle source
Calls superclass method Deal::Command::options
# File lib/deal/command/text.rb, line 19
def self.options
    [
      ['--config', 'json or input files.'],
      ['--input', 'text,file or dir'],
      ['--signs', 'search items'],
      ['--judge_types', 'file|dir|text|mach-o'],
      ['--judge_action', 'which actions to match word."remove|replace|abort",default is abort'],
      ['--replace', 'replace word'],
      ['--include_paths', 'include search paths'],
      ['--exclude_paths', 'exclude search paths']
    ].concat(super)
end

Public Instance Methods

deal_item(config,input) 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/text.rb, line 109
def deal_item(config,input)
    if items = config.judge_item(input)
        @results = @results.concat items
    else
        return
    end

    @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

    if not File.directory?(input)
        return
    end

    Dir.entries(input).each do |sub|
        if sub != '.' && sub != '..'
            deal_item(config,"#{input}/#{sub}")
        end
    end
end
run() click to toggle source
# File lib/deal/command/text.rb, line 134
def run
    for config in @configs
        if FileTest.exist?(@input)
            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
            deal_item config, @input
        else
            logE input+' file does not exist'
        end
    end
    logN '=============================='
    logN "共进行了#{@judge_count}次匹配"
    if @results.length == 0
        logN '没有匹配到结果'
    else
        logN '匹配到结果如下:'
        for result in @results
            logN result.to_s
        end
    end
    logN '=============================='
end
validate!() click to toggle source
Calls superclass method Deal::Command#validate!
# File lib/deal/command/text.rb, line 86
def validate!
    super
    help! 'Please specify an config' unless @configs.length > 0
    help! 'Please specify an input' unless @input

end