Class: Deal::Text

Inherits:
Command
  • Object
show all
Includes:
DealUtils
Defined in:
lib/deal/command/text.rb

Class Method 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(argv) ⇒ Text

Returns a new instance of Text.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
79
80
81
82
83
84
# 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

Class Method Details

.optionsObject



19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#deal_item(config, input) ⇒ Object



109
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/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

#runObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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!Object



86
87
88
89
90
91
# 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