iOS專案冗餘資源掃描指令碼

Coffee_zq發表於2016-06-19

iOS專案冗餘資源掃描指令碼

  

  隨著iOS專案的版本不斷迭代,app中冗餘檔案會越來越多,app size也持續增加,是時候需要對app冗餘資源進行檢測,對app進行瘦身。

 

使用方法:

1. 執行環境為mac, 首先準備好工程原始碼;

2. 新建check.sh,將下面的程式碼貼上到check.sh中並且儲存;(可將txt檔案改成sh字尾)

#!/bin/sh
 
##### several cases that the scripts does not work:
##### 1) there is space or slash in the resources file, such as "aaa .png" 資原始檔名中含有空格或者/
##### 2) reference resources in commented code 資源引用程式碼被註釋了
##### 3) you need to manually checked the resources one by one in the result 對於指令碼檢查結果,最好人工檢查一遍
##### 4) you can add some other types more than png, jpg, gif, wav, m4a 如果需要檢查其他資源,請自行修改指令碼;
##### 5)預設檔案字尾都是如@2x.png格式,如果字尾格式不同,請自行修改指令碼;
 
 
#### set parameters:PrjPath為專案工程所在目錄,包含.m .xib檔案;ResPath為被掃描的資原始檔目錄,包含.png .wav
#### xcodeprojPath為工程xcodeproj位置
PrjPath=/Users/.......
ResPath=/Users/.......
xcodeprojPath=/Users/....../******.xcodeproj
 
 
if [ -f ~/Desktop/resource_san_result.txt ];then
    rm -f ~/Desktop/resource_san_result.txt
fi
 
 
cd $PrjPath
files=$(find . -name "*.m" -o -name "*.xib" -o -name "*.mm" -o -name "*.plist")
 
cd $ResPath
for png in $(find . -name "*.png" -o -name "*.jpg" -o -name "*.gif" -o -name "*.wav" -o -name "*.m4a")
do
 
    basename='basename/'$png
    basename=${basename##*/}
    # echo $basename
 
    if [ "${basename##*.}" == "png" ];then
 
        echo $basename|grep -q @2x.png
 
        if [ $? -eq 0 ];then
            name=${basename%%@2x.png}
        else
            echo $basename|grep -q @3x.png
            if [ $? -eq 0 ];then
                name=${basename%%@3x.png}
            else
                name=${basename%.png}
            fi
        fi
 
    elif [ "${basename##*.}" == "jpg" ];then
        echo $basename|grep -q @2x.jpg
        if [ $? -eq 0 ];then
            name=${basename%%@2x.jpg}
        else
            echo $basename|grep -q @3x.jpg
            if [ $? -eq 0 ];then
                name=${basename%%@3x.jpg}
            else
                name=${basename%%.jpg}
            fi
        fi
 
    elif [ "${basename##*.}" == "gif" ];then
        echo $basename|grep -q @2x.gif
        if [ $? -eq 0 ];then
            name=${basename%%@2x.gif}
        else
            echo $basename|grep -q @3x.gif
            if [ $? -eq 0 ];then
                name=${basename%%@3x.gif}
            else
                name=${basename%%.gif}
            fi
        fi
 
    elif [ "${basename##*.}" == "wav" ];then
        name=${basename%%.wav}
 
    elif [ "${basename##*.}" == "m4a" ]; then
        name=${basename%%.m4a}
 
    else
        name=''
    fi
 
 
    if [ ${#name} -gt 0 ];then
        # # name=${name%%[0-9]*}
 
        cd $PrjPath
        if grep -q $name $files;then
            echo "$png" is used
 
        else
            cd $xcodeprojPath
            if grep -q $name project.pbxproj;then
                echo "$png" is not used >> ~/Desktop/resource_san_result.txt
            else
                echo "$png" is not packaged
            fi
        fi
    else
        echo name is empty
    fi
 
done
 
 
if [ -f ~/Desktop/resource_san_result.txt ]; then
    echo ***************the end of scan. Please see result from resource_san_result.txt
else
    echo ***************the end of scan, everything is OK
fi

 

3. 設定指令碼中引數:

PrjPath為專案工程所在目錄,包含.m .xib檔案;

ResPath為被掃描的資原始檔目錄,包含.png .wav;

xcodeprojPath為工程xcodeproj位置;

例如:

PrjPath=/Users/zhuquan/Documents/secret-develop/Project
ResPath=/Users/zhuquan/Documents/secret-develop/Project/Phoenix/Res
xcodeprojPath=/Users/zhuquan/Documents/secret-develop/Project/Phoenix.xcodeproj

 

4. 執行指令碼check.sh;

5. 最後會出檢測結果,檢測出來的冗餘資源最好人工檢查一遍。

[zhuquandeMacBook-Pro:Desktop zhuquan$ ./check.sh
./1.png is used
./2.png is used
./3.png is used
./4.png is used
./5.png is used
./ajax-loader.gif is not packaged
./運動記錄.png is not packaged
***************the end of scan, everything is OK

 

使用總結:

指令碼使用過程中有一些注意事項如下,

1. 如果資原始檔名中含有空格或者/,比如”aaa .png”,該資源無法正常檢測;

2. 如果資原始檔在程式碼中被引用了,但是該引用程式碼被註釋掉了,也無法成功檢測;

3. 對於最終指令碼輸出的指令碼檢查結果,最好人工檢查一遍,有些資源可能並非是冗餘資源;

4. 目前指令碼中支援的資源型別有.png .jpg .gif .wav .m4a,如果需要檢查其他資源,請自行修改指令碼。

 

相關文章