Class: YJCocoa::UnusedClass

Inherits:
Unused show all
Defined in:
lib/yjcocoa/unused/unused_class.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_class.rb', line 21

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

Instance Method Details

#match_o_sectionObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yjcocoa/unused/unused_class.rb', line 63

def match_o_section
    lines = `otool -oV #{self.matcho}`.split("\n")
    section = Hash.new
    key = nil
    lines.each {|line|
        line = line#.strip
        if line.include?("Contents of (__DATA,")
            key = line
        elsif (key)
            if section.include?(key)
                section[key] << line
            else
                section[key] = [line]
            end
        end
    }
    return section
end

#objc_classlist(classlist) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/yjcocoa/unused/unused_class.rb', line 93

def objc_classlist(classlist)
    result = []
    classlist.each { |line|
        line_scan = line.scan(/_OBJC_CLASS_\$_(.+)/)
        if line_scan.count > 0 && !line.include?("superclass")
            result << line_scan[0][0]
        end
    }
    return result
end

#objc_super_classlist(classlist) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/yjcocoa/unused/unused_class.rb', line 82

def objc_super_classlist(classlist)
    result = []
    classlist.each { |line|
        line_scan = line.scan(/_OBJC_CLASS_\$_(.+)/)
        if line_scan.count > 0 && line.include?("superclass")
            result << line_scan[0][0]
        end
    }
    return result
end

#runObject



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

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

#unreferenced_classObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yjcocoa/unused/unused_class.rb', line 45

def unreferenced_class
    section = match_o_section
    unless section.count
        puts "#{self.matcho} can't find classlist".red
        self.banner!
    end
    classlist = section["Contents of (__DATA,__objc_classlist) section"]
    classrefs = section["Contents of (__DATA,__objc_classrefs) section"]
    superrefs = section["Contents of (__DATA,__objc_superrefs) section"]
    all_classlist = objc_classlist(classlist)
    used_classlist = objc_super_classlist(classlist) + objc_classlist(classrefs) + objc_classlist(superrefs)
    unused_classlist = []
    all_classlist.each {|item|
        unused_classlist << item unless used_classlist.include?(item)
    }
    return unused_classlist.sort
end

#validate!Object

businrss



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

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