傳送門
Demo源碼:https://gitee.com/hcsaaron/csoclint-demo
安裝OCLint及相關(guān)工具
- 安裝oclint:
brew tap oclint/formulae
brew install oclint
- 安裝xcpretty(增加xcodebuild輸出的可讀性)
gem install xcpretty
OCLint命令及使用
1) cd進項目,查看項目信息
cd /Users/xxx/CSOCLintDemo
xcodebuild -list
Command line invocation:
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -list
Information about project "CSOCLintDemo":
Targets:
CSOCLintDemo
CSOCLintDemoTests
CSOCLintDemoUITests
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
CSOCLintDemo
2) 編譯項目
先clean指定項目CSOCLintDemo,使用-project CSOCLintDemo.xcodeproj,然后再Debug編譯項目。最后通過xcpretty,使用-r json-compilation-database可以生成指定格式的數(shù)據(jù)。
編譯成功后,會在項目的文件夾下出現(xiàn) compile_commands.json文件:
xcodebuild -project CSOCLintDemo.xcodeproj -scheme CSOCLintDemo clean \
&& xcodebuild -project CSOCLintDemo.xcodeproj -scheme CSOCLintDemo -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO \
| xcpretty -r json-compilation-database -o compile_commands.json
如果是用pod集成的項目,要使用-workspace CSSphinx.xcworkspace
這里加上COMPILER_INDEX_STORE_ENABLE=NO,是因為報這個錯oclint: error: compilation contains multiple jobs:
如果要使用舊編譯模式,需要加上-UseModernBuildSystem=NO
3) 生成html報告
使用oclint-json-compilation-database命令對上一步生成的json數(shù)據(jù)進行分析,對項目代碼進行分析,最終生成oclintReport.html文件。OCLint目前支持輸出html、json、xml、pmd、Xcode格式文件:
oclint-json-compilation-database -- -report-type html -o oclintReport.html
用瀏覽器打開oclintReport.html展示如下:

4) 分析單個文件
如何工程太大,分析太耗時間,可以單獨分析某個文件或文件夾,替換掉下面的xxxx:
oclint-json-compilation-database -i AppDelegate.m -- -report-type html -o oclintReport-AppDelegate.html
OCLint的規(guī)則
1) 通過-e忽略指定文件,可以指定多個
假如你使用了CocoaPods依賴第三方庫,你可能想忽略對Pods文件夾的分析:
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html
2) 通過-rc改變檢查規(guī)則的默認(rèn)值
比如有一條默認(rèn)規(guī)則:long line [size|P3] Line with 137 characters exceeds limit of 100 ,這表示一個方法里的代碼行數(shù)不能超過100,可以通過-rc改變默認(rèn)100行的限制比如改成200行:
oclint-json-compilation-database -- -report-type html -o oclintReport.html \
-rc=LONG_LINE=200 \
3) 通過-disable-rule可以禁止檢查某一規(guī)則
比如禁止UnusedMethodParameter檢查未使用的方法參數(shù)(一般使用到的系統(tǒng)代理方法返回的參數(shù)都不一定會使用到,建議忽略此規(guī)則):
oclint-json-compilation-database -- -report-type html -o oclintReport.html \
-disable-rule UnusedMethodParameter \
4) 通過-max-priority來設(shè)置允許的違例數(shù)
如果超過違例個數(shù),則失敗,我們這里設(shè)置為9999個:
oclint-json-compilation-database -- -report-type html -o oclintReport.html \
-max-priority-1=9999 \
-max-priority-2=9999 \
-max-priority-3=9999 \
在Xcode中通過腳本集成OCLint
1) 創(chuàng)建Target,命名為OCLint

2) 選中OCLint - Build Phases - New Run Script Phase

3) 編寫腳本
根據(jù)自己的需求修改相關(guān)規(guī)則
source ~/.bash_profile
export LC_ALL="en_US.UTF-8"
PROJECT_NAME="CSOCLintDemo.xcodeproj"
TARGET_NAME="CSOCLintDemo"
cd ${SRCROOT}
rm -rf ./build/derivedData
xcodebuild clean -UseModernBuildSystem=NO
xcodebuild -project ${PROJECT_NAME} -scheme ${TARGET_NAME} -UseModernBuildSystem=YES -derivedDataPath ./build/derivedData -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO | xcpretty -r json-compilation-database -o compile_commands.json
oclint-json-compilation-database -- -report-type Xcode \
-rc=LONG_METHOD=50 \
-rc=LONG_LINE=100 \
-disable-rule ParameterReassignment \
-disable-rule TooFewBranchesInSwitchStatement \
-max-priority-1=9999 \
-max-priority-2=9999 \
-max-priority-3=9999 \
exit
3) Xcode運行OCLint
Xcode中切換Scheme為OCLint,Command+B編譯,由于腳本中-report-type 為xcode,編譯成功后會在Xcode中展示相關(guān)issuse
點擊直接進入對應(yīng)代碼查看問題。

自定義規(guī)則
可以看到,OCLint默認(rèn)規(guī)則有的比較苛刻,我們可以修改腳本,適當(dāng)提高相關(guān)規(guī)則的閾值。了解更多OCLint規(guī)則及使用,請移步《OCLint規(guī)則查詢&簡單理解》