Class: YJCocoa::UnusedMethod

Inherits:
Unused show all
Defined in:
lib/yjcocoa/unused/unused_method.rb

Overview

Usage

Constant Summary

Constants inherited from Command

Command::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Unused

#ignore, #match, #matcho, #output

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unused

#check_ignore, #check_match, #initialize

Methods inherited from Command

#askWithAnswers

Constructor Details

This class inherits a constructor from YJCocoa::Unused

Class Method Details

.optionsObject



21
22
23
24
25
26
# File 'lib/yjcocoa/unused/unused_method.rb', line 21

def self.options
    [['--match-o', 'Match O 文件地址'],
    ['--match', '指定的字符串匹配,多字符串用 "," 分隔'],
    ['--ignore', '忽略的字符串匹配, 多字符串用 "," 分隔'],
    ['--output', '日志存放路径,默认当前路径']] + super
end

Instance Method Details

#implemented_methodsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yjcocoa/unused/unused_method.rb', line 58

def implemented_methods
    lines = `otool -oV #{self.matcho}`.split("\n")
    methods = Hash.new
    lines.each {|line|
        line_scan = line.scan(/\s*imp 0x\w+ ([+|-]\[.+\s(.+)\])/)
        if line_scan.count > 0
            imp = line_scan[0][0]
            sel = line_scan[0][1]
            if methods.include?(sel)
                methods[sel] << imp
            else
                methods[sel] = [imp]
            end
        end
    }
    return methods
end

#referenced_selectorsObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yjcocoa/unused/unused_method.rb', line 76

def referenced_selectors
    lines = `otool -v -s __DATA __objc_selrefs #{self.matcho}`.split("\n")
    refs = []
    lines.each { |line|
        line_scan = line.scan(/__TEXT:__objc_methname:(.+)/)
        if line_scan.count > 0
            refs << line_scan[0][0]
        end
    }
    return refs
end

#runObject



37
38
39
40
41
42
43
# File 'lib/yjcocoa/unused/unused_method.rb', line 37

def run
    unused_methods = self.unreferenced_methods
    fetch_methods = unused_methods.reject {|m| !(check_match(m) && !check_ignore(m))}
    puts "分析完毕,打开日志:#{self.output}".green
    File.write(self.output, fetch_methods.join("\n"))
    `open #{self.output}`
end

#unreferenced_methodsObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yjcocoa/unused/unused_method.rb', line 45

def unreferenced_methods
    methods = self.implemented_methods
    unless methods.count
        puts "#{self.matcho} can't find implemented methods".red
        self.banner!
    end
    sels = self.referenced_selectors
    sels.each {|sel| methods.delete(sel)}
    result = []
    methods.values.each {|value| result += value}
    return result.sort
end

#validate!Object

businrss



29
30
31
32
33
34
35
# File 'lib/yjcocoa/unused/unused_method.rb', line 29

def validate!
    super
    unless self.matcho && self.matcho.length > 0
        puts "Match O 文件地址为空".red
        self.banner!
    end
end