代码分析脚本

iOS开发过程中,随着版本迭代的更新,会有很多无用代码及文件产生,今天主要是参考网上的一些内容,并进行思路分析,给需要的人提供一个参考。

主要参考Finding unused files in Xcode 里面有很多脚本,因为自己对shell脚本也不是很熟悉,边看边学习。

第一个脚本https://github.com/arun80/xcodeutils

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
#!/bin/sh
#print usual html headers
echo "<html>"
echo "<h2> Unused resources </h2>"
#usual disclaimer
#<i> 标签:斜体,b 粗体
echo "<i> <b>Note:</b> This scans all the xib, nib files for the images available. Please look for splash screens or other images carefully in the below list which are used in the project definition (pbxproj file).<br>In order for links to work the report file must be in the same folder as project.</i>"
unusedfiles="";
#initialize the counter
let count=0;
let totalsize=0;
# collect the files needs to be introspected
#find命令 -o(or) 默认为 -a(and)
project=`find $1 -name '*.?ib' -o -name '*.[mh]' -o -name '*.storyboard'`
for i in `find $1 -name '*.gif' -o -name '*.jpg' -o -name '*.png' -o -name '*.jpeg'`; do
file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x | xargs basename -s @3x`
if ! grep -q $file $project; then
filesize=`stat -f "%z" $i`;
filesizekb=`echo "$filesize 1024.0" | awk '{printf "%.2f", $1 / $2}'`
unusedfiles="$unusedfiles <br> <a href=\"$i\">$i</a> ($filesizekb kb)";
let "count += 1";
let "totalsize += $filesize"
fi
done
#construct body
totalsizekb=`echo "$totalsize 1024.0" | awk '{printf "%.2f", $1 / $2}'`
echo "<body>"
echo "<h3>"
echo "There are $count unused images (total size: $totalsizekb kb)"
echo "</h3>"
echo "<pre>"
#generate body content if there are unused files.
if [ $count > 0 ]; then
echo $unusedfiles;
fi
echo "</pre>"
#---------------------------------------------------------------------------------------
# Experimental util to find the source files which are not defined in pbxproj definition.
#---------------------------------------------------------------------------------------
count=0;
unusedfiles="";
project=`find $1 -name '*.pbxproj'`
for i in `find $1 -name "*.[hmca]" -o -name "*.cpp"`; do
file=`basename "$i"`
if ! grep -q $file $project; then
unusedfiles="$unusedfiles <br> $i";
let "count = count + 1";
fi
done
echo "<i> <b>Note:</b> This scans all source files (*.h, *.m, *.c, *.a, *.cpp) references in all pbxproj definitions. Once it is added into project definitions, it is considered being used.</i> <br>"
echo "<h3>"
echo "There are $count unused files"
echo "</h3>"
echo "<pre>"
#generate body content if there are unused files.
if [ $count > 0 ]; then
echo $unusedfiles;
fi
echo "</pre>"
echo "</body>"
echo "</html>"
#thats it!

自己需要学习的脚本命令有:
Linux下xargs命令详解

1
2
3
4
xargs命令,例如:
find /sbin -perm +700 |ls -l 这个命令是错误的
find /sbin -perm +700 |xargs ls -l 这样才是正确的
find ./ -name *.[mh] |xargs basename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
grep -q用于if逻辑判断
突然发现grep -q 用于if 逻辑判断很好用。
-q 参数,本意是 Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. 中文意思为,安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0。
小应用
# cat a.txt
nihao
nihaooo
hello
# if grep -q hello a.txt ; then echo yes;else echo no; fi
yes
# if grep -q word a.txt; then echo yes; else echo no; fi
no
1
2
stat -f "%z" README.MD 显示readme.md的大小
-f : 显示文件所在的文件系统的状态

分析:此脚本首先是是扫描图片文件,jpg,png,jpeg,gif,然后从xib,nib,stroyboard,.[mh]总查找,看是否存在,如果不存在则记录名称及大小。然后判断判断 .[hmca|cpp]等文件,是否在 *.pbxproj中,并输出。