Class: Deal::Command

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

Direct Known Subclasses

Text

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) ⇒ Command

Returns a new instance of Command.



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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('|')
    logN "--inputs:#{inputs.join("\n")}"
    @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
    logN "input glob:\n "+@inputs.join("\n")


end

Class Method Details

.optionsObject



30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Method Details

#deal_item(config, input) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/deal/command.rb', line 146

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}")
        else
            @judge_count += 0.5
        end
    end
end

#runObject



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
# File 'lib/deal/command.rb', line 173

def run
    for config in @configs
        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 config, input
            else
                logE input+' file does not exist'
            end
        end

    end
    progress = @judge_count*1.0/@total_count
    logW "处理进度:100%"

    logN '=============================='
    logN "共进行了#{@judge_count}次匹配"
    if @results.length == 0
        logN '没有匹配到结果'
    else
        logN "匹配到 #{@results.length} 个结果,如下:"
        for result in @results
            logN result.to_s
        end
    end
    logN '=============================='
end

#validate!Object



123
124
125
126
127
128
# File 'lib/deal/command.rb', line 123

def validate!
    super
    help! 'Please specify an config' unless @configs.length > 0
    help! 'Please specify an input' unless @inputs

end