前言
OCLint 是一個靜態(tài)分析代碼的工具砂心,支持自定義規(guī)則懈词,可以很好地幫助我們規(guī)范代碼,提高代碼質(zhì)量计贰,是團隊開發(fā)CodeReview一大利器钦睡,本文主要通過OCLint讓XCode來Code Review,網(wǎng)上的資料挺多的躁倒,(但還是有些坑)記錄下方便自己以后越坑.
OCLint相關(guān)的詳細內(nèi)容也可以到OCLint官網(wǎng)上查看荞怒。
安裝OCLint 和 xcpretty
- 安裝xcpretty
用gem安裝xcpretty
sudo gem install xcpretty
用sudo是防止一些權(quán)限出現(xiàn)問題
安裝OCLint
1)通過安裝包:
從oclint的github項目鏈接下載安裝
OCLINT_HOME=/pathexport PATH=$PATH:OCLINT_HOME/bin
然后source .bashrc即可
2)通過Homebrew
brew tap oclint/formulae
brew install oclint
3)確認安裝成功
運行oclint確認環(huán)境配置正確
$ oclint
如果展示以下內(nèi)容即為安裝成功:
oclint: Not enough positional command line arguments specified!Must specify at least 1 positional arguments: See: oclint -help
4)oclint的版本
oclint --version
which oclint
oclint存放位置
OCLint 三個指令
1)oclint常規(guī)核心指令
oclint [options] <source> -- [compiler flags]
[options]為一些參數(shù)選項,可以是規(guī)則加載選項秧秉、報告形式選項等:
- -R <路徑> : 檢測所用的規(guī)則的路徑褐桌,默認路徑$(/path/to/bin/oclint)/../lib/oclint/rules
- -disable-rule <規(guī)則名>: 讓相對應(yīng)的規(guī)則失效(OCLint 規(guī)則列表)。
- -rc <參數(shù)>=<值> :修改閾值
- -report-type <報告類型>象迎,有"text"荧嵌、“html”、“json”砾淌、“pmd”啦撮、“xcode”幾個類型
- -o <路徑> 報告生成路徑.
egoclint -R /path/to/rules -disable-rule ObjCAssignIvarOutsideAccessors -report-type xcode
2)oclint-json-compilation-database
從編譯好的compile_commands.json 文件中讀取配置信息并執(zhí)行 oclint。
主要用于生成compile_commands.json文件汪厨,現(xiàn)在已經(jīng)不進行維護了赃春,我們可以用xcpretty替代,來生成json文件.
oclint-json-compilation-database 指令
這是我們主要用到的指令劫乱,它是oclint 指令的升級版织中,使用起來相對 oclint 指令簡單方便≈愿辏可以通過 -- 的方式在指令的最后加上oclint 選項狭吼。
oclint-json-compilation-database 指令有過濾文件選項
-i :包含進某些文件
oclint-json-compilation-database -i Pods
-e : 過濾掉某些文件
oclint-json-compilation-database -e Pods
3)oclint-xcodebuild 指令
oclint-xcodebuild 指令不再維護,實際應(yīng)用中用xcpretty代替.
OCLint使用
1)新建工程AddTest添加Aggregate
075F858D-C8F3-4B97-BF59-FC8C8DA95925.png
2)選擇Aggregate
2ACF7D44-CF28-4F6D-BDD2-86416762945D.png
3)點擊next輸入ProductName
圖片.png
4)建立運行腳本
C480D7AC-D0E5-48BB-BB1B-790EC8E3C670.png
5)配置運行腳本
CA419EC0-8487-4F3D-9102-3674258695EC.png
6)運行結(jié)果
87EE9D72-60DB-4A33-B39E-02067599AA53.png
***注意點 ***
1.在根目錄下新建文件oclint殖妇,然后進入oclint文件夾刁笙,touch oclint.sh
2.sh腳本不對,路徑不對都很容易gg,有些運行成功但是不報警告采盒,需要檢查下路徑.
3 oclint.sh腳本
export LANG="zh_CN.UTF-8"
export LC_COLLATE="zh_CN.UTF-8"
export LC_CTYPE="zh_CN.UTF-8"
export LC_MESSAGES="zh_CN.UTF-8"
export LC_MONETARY="zh_CN.UTF-8"
export LC_NUMERIC="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"
export LC_ALL=
function checkDepend () {
command -v xcpretty >/dev/null 2>&1 || {
echo >&2 "I require xcpretty but it's not installed. Install:gem install xcpretty";
exit
}
command -v oclint-json-compilation-database >/dev/null 2>&1 || {
echo >&2 "I require oclint-json-compilation-database but it's not installed. Install:brew install oclint";
exit
}
}
function oclintForProject () {
# 檢測依賴
checkDepend
projectName=$1
scheme=$2
reportType=$3
REPORT_PMD="pmd"
REPORT_XCODE="xcode"
myworkspace=${projectName}
myscheme=${scheme}
echo "myworkspace是:${myworkspace}"
echo "myscheme是:${myscheme}"
echo "reportType為:${reportType}"
# 清除上次編譯數(shù)據(jù)
if [ -d ./build/derivedData ]; then
echo '-----清除上次編譯數(shù)據(jù)derivedData-----'
rm -rf ./build/derivedData
fi
# xcodebuild -workspace $myworkspace -scheme $myscheme clean
xcodebuild clean
echo '-----開始編譯-----'
# 生成編譯數(shù)據(jù)
xcodebuild -workspace ${myworkspace} -scheme ${myscheme} -sdk iphonesimulator -derivedDataPath ./build/derivedData -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO | xcpretty -r json-compilation-database -o compile_commands.json
if [ -f ./compile_commands.json ]
then
echo '-----編譯數(shù)據(jù)生成完畢-----'
else
echo "-----生成編譯數(shù)據(jù)失敗-----"
return -1
fi
echo '-----分析中-----'
# 自定義排除警告的目錄旧乞,將目錄字符串加到數(shù)組里面
# 轉(zhuǎn)化為:-e Debug.m -e Port.m -e Test
exclude_files=("cardloan_js" "Pods")
exclude=""
for i in ${exclude_files[@]}; do
exclude=${exclude}"-e "${i}" "
done
echo "排除目錄:${exclude}"
# 分析reportType
if [[ ${reportType} =~ ${REPORT_PMD} ]]
then
nowReportType="-report-type pmd -o pmd.xml"
else
nowReportType="-report-type xcode"
fi
# 自定義report 如:
# nowReportType="-report-type html -o oclint_result.html"
# 生成報表
oclint-json-compilation-database ${exclude} -- \
${nowReportType} \
-rc LONG_LINE=200 \
# --命名
# 變量名字最長字節(jié)
-rc=LONG_VARIABLE_NAME=20 \
# 變量名字最短字節(jié)
-disable-rule ShortVariableName \
# --size # 圈復(fù)雜度 #
-re=CYCLOMATIC_COMPLEXITY=10 \
# 每個類最行數(shù) #
-rc=LONG_CLASS=700 \
# 每行字節(jié)數(shù)量 #
-rc=LONG_LINE=200 \
# 每個方法行數(shù) #
-rc=LONG_METHOD=80 \
# 忽略注釋后括號后的有效代碼行數(shù) #
-rc=NCSS_METHOD=40 \
# 嵌套深度 #
-rc=NESTED_BLOCK_DEPTH=5 \
# 字段數(shù)量 #
-rc=TOO_MANY_FIELDS=20 \
# 方法數(shù)量 #
-rc=TOO_MANY_METHODS=50 \
# 方法參數(shù) #
-rc=TOO_MANY_PARAMETERS=6
-disable-rule ShortVariableName \
-disable-rule ObjCAssignIvarOutsideAccessors \
-disable-rule AssignIvarOutsideAccessors \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000
rm compile_commands.json
if [[ ${reportType} =~ ${REPORT_PMD} ]] && [ ! -f ./pmd.xml ]
then
echo "-----分析失敗-----"
return -1
else
echo '-----分析完畢-----'
return 0
fi
}
# 替換workspace的名字
myworkspace="AddTest.xcworkspace"
# 替換scheme的名字
myscheme="AddTest"
# 輸出方式 xcode/pmd
reportType="xcode"
oclintForProject ${myworkspace} ${myscheme} ${reportType}
下面是幾個參考鏈接,我獲益良多磅氨,建議深入閱讀尺栖,尤其是里面的例子…
http://oriochan.com/codeReview01.html
http://www.cocoachina.com/ios/20170928/20669.html