iOS 的那些事儿

使用Python3检查Objective-C 中未引用类和未使用

2020-05-05  本文已影响0人  uniapp

已经调整好脚本,使用python3环境:

# -*- coding: UTF-8 -*-
#!/usr/bin/env python
# 使用方法:python py文件 Xcode工程文件目录

import sys
import os
import re

if len(sys.argv) == 1:
    print('请在.py文件后面输入工程路径')
    sys.exit()

projectPath = sys.argv[1]

# projectPath = '/Users/zhudongdong/Desktop/iOS/FileTest'
print('工程路径为%s' % projectPath)

resourcefile = []
totalClass = set([])
unusedFile = []
pbxprojFile = []

def Getallfile(rootDir):
    for lists in os.listdir(rootDir):
        path = os.path.join(rootDir, lists)
        if os.path.isdir(path):
            Getallfile(path)
        else:
            ex = os.path.splitext(path)[1]
            if ex == '.m' or ex == '.mm' or ex == '.h':
                resourcefile.append(path)
            elif ex == '.pbxproj':
                pbxprojFile.append(path)

Getallfile(projectPath)

print('工程中所使用的类列表为:')
for ff in resourcefile:
    print(ff)

for e in pbxprojFile:
    f = open(e, 'r')
    content = f.read()
    array = re.findall(r'\s+([\w,\+]+\.[h,m]{1,2})\s+',content)
    see = set(array)
    totalClass = totalClass|see
    f.close()

print('工程中所引用的.h与.m及.mm文件')
for x in totalClass:
    print(x)
print('--------------------------')
for x in resourcefile:
    ex = os.path.splitext(x)[1]
    if ex == '.h': #.h头文件可以不用检查
        continue
    fileName = os.path.split(x)[1]
    print(fileName)
    if fileName not in totalClass:
        unusedFile.append(x)

for x in unusedFile:
    resourcefile.remove(x)

print('未引用到工程的文件列表为:')

writeFile = []
for unImport in unusedFile:
    ss = '未引用到工程的文件:%s\n' % unImport
    writeFile.append(ss)
    print(unImport)

unusedFile = []

allClassDic = {}
# print("resourcefile2", resourcefile)
for x in resourcefile:
    f = open(x,'r')
    content = f.read()
    array = re.findall(r'@interface\s+([\w,\+]+)\s+:',content)
    for xx in array:
        allClassDic[xx] = x
    f.close()

print('所有类及其路径:')
for x in allClassDic.keys():
    print(x,':',allClassDic[x])

def checkClass(path,className):
    f = open(path,'r')
    content = f.read()
    if os.path.splitext(path)[1] == '.h':
        match = re.search(r':\s+(%s)\s+' % className,content)
    else:
        match = re.search(r'(%s)\s+\w+' % className,content)
    f.close()
    if match:
        return True

ivanyuan = 0
totalIvanyuan = len(allClassDic.keys())

for key in allClassDic.keys():
    path = allClassDic[key]
    
    index = resourcefile.index(path)
    count = len(resourcefile)
    
    used = False
    
    offset = 1
    ivanyuan += 1
    print ('完成',ivanyuan,'共:',totalIvanyuan,'path:%s'%path)    
    # 检查除本文件外的其他文件是否引用该类
    while index+offset < count or index-offset > 0:
        if index+offset < count:
            subPath = resourcefile[index+offset]
            if checkClass(subPath,key):
                used = True
                break
        if index - offset > 0:
            subPath = resourcefile[index-offset]
            if checkClass(subPath,key):
                used = True
                break
        offset += 1
    
    if not used:
        str = '未使用的类:%s 文件路径:%s\n' %(key,path)
        unusedFile.append(str)
        writeFile.append(str)

for p in unusedFile:
    print('未使用的类:%s' % p)

filePath = os.path.split(projectPath)[0]
writePath = '%s/未使用的类.txt' % filePath
f = open(writePath,'w+')
f.writelines(writeFile)
f.close()

使用方式:python3 脚本名.py 工程绝对路径,快试试吧。

上一篇 下一篇

猜你喜欢

热点阅读