-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcheckUnusedClassOrMethod.rb
144 lines (114 loc) · 4.2 KB
/
checkUnusedClassOrMethod.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
## usage: ruby checkUnusedClassOrMethod.rb 【optioal】machofilePath [-c][-s](means class or selector)
## need to install snake,please run 'brew install snake' in terminal first
##
require 'json'
def parseUnusedClass
txtDir = __dir__ + "/" + "classUnused.txt"
machOFilePath = ARGV[0]
if !machOFilePath
puts("please specfic a macho file path...")
return
end
unusedClassCmd = 'snake -c -j ' + machOFilePath + ' > ' + txtDir
snakeResult = system(unusedClassCmd)
if !snakeResult
puts("脚本调用失败,不妨试试 brew install snake ?")
return
else
puts("执行结束,开始分析筛选")
end
filtArray = Array.new()
text = File.read(txtDir).gsub("\"","").gsub("[","").gsub("]","")
classArray = text.split(",")
for className in classArray
filtArray.push(className)
end
writeContent = filtArray.join(",\n")
logFilePath = __dir__ + "/classUnusedWithFilter.txt"
if !Dir.exists?(logFilePath)
File.new(logFilePath,'w')
end
File.open(logFilePath, "w") do |f|
f.write(writeContent)
end
File.delete(txtDir) if File.exist?(txtDir)
puts("分析结束,请到:" + logFilePath + " 查看最终分析结果")
end
#针对单个结果分析
def filterClassStructor(classMethodStructMap)
#set方法排除,同样,有get的方法也要排除
setSelectorsArray = Array.new()
normalSelectorArray = Array.new()
allSelectorsArray = classMethodStructMap["selectors"]
#分出set方法和非方法
for functionName in allSelectorsArray
functionActureName = functionName.split(" ")[1].gsub("]","")
#是set方法,当有多个:说明是多个参数,不属于set方法
if functionActureName.start_with?("set") and functionActureName.end_with?(":") and functionActureName.split(":").count == 1
#setAbc: => abc set向get方法转换
getFunctionName = functionActureName.split(":")[0].gsub("set","").downcase
setSelectorsArray.push(getFunctionName)
else
normalSelectorArray.push(functionActureName)
end
end
#相减即为非get也非set方法
normalSelectorArray = normalSelectorArray - setSelectorsArray
finalResultMaps = Hash.new()
if normalSelectorArray.count > 0
className = classMethodStructMap["class"]
finalResultMaps[className] = normalSelectorArray
end
return finalResultMaps
end
def parseUnusedSelector
txtDir = __dir__ + "/" + "methodUnused.txt"
machOFilePath = ARGV[0]
if !machOFilePath
puts("please specfic a macho file path...")
return
end
unusedClassCmd = 'snake -s -j ' + machOFilePath + ' > ' + txtDir
snakeResult = system(unusedClassCmd)
if !snakeResult
puts("脚本调用失败,不妨试试 brew install snake ?")
return
else
puts("执行结束,开始分析筛选")
end
jsonText = File.read(txtDir)
selectorStruct = JSON.parse(jsonText)
finalFilterMap = Array.new()
for methodMap in selectorStruct
filterMap = filterClassStructor(methodMap)
if !filterMap.empty?()
className = filterMap.keys[0]
finalFilterMap.push(filterMap)
end
end
separateResult = Hash.new()
separateResult["ClassUnUsedMethodResult"] = finalFilterMap
jsonDir = __dir__ + "/" + "classMethodUnused.json"
File.write(jsonDir,JSON.pretty_generate(separateResult))
File.delete(txtDir) if File.exist?(txtDir)
puts("分析结果已输出至:" + jsonDir + " 请打开查看")
end
def startParseMachO
machOFilePath = ARGV[0]
parseWay = ARGV[1]
if !machOFilePath or machOFilePath.length == 0
puts("请指定machO文件路径 useage .rb xxxx/xxx/xx")
return
end
if !parseWay or parseWay.length == 0
puts("指定方式 -s为分析无用方法 -c为分析无用类")
return
end
if parseWay == '-s'
parseUnusedSelector()
elsif parseWay == '-c'
parseUnusedClass()
puts("请谨慎删除相关类,特别是在很多类里使用反射方式,通过string拿到class时,mach-O文件内未关联此种引用造成误报")
end
end
startParseMachO