Code Review
代碼評審,代碼靜態檢查,Objective-C代碼靜態檢查工具——OCLint
Github: https://github.com/bobwongs/BWCodeReview
Contents
- Overview
- OCLint
- 給Xcode項目集成OCLint
- 1、安裝
- 2、配置
- 3、靜態代碼分析
- OCLint已有規則的自定義
- 給Xcode項目集成OCLint
- Summary
- Next
- Reference
- Follow Me
Overview
- 編寫符合項目團隊規范的代碼
- 檢查和提高項目代碼質量
- 提高項目的可維護性
- 彌補由Xcode原本的代碼檢查所不能檢查到的問題代碼的不足,讓代碼保持更高質量
OCLint
對C、C++、Objective-C進行靜態代碼檢查的工具
給Xcode項目集成OCLint
1、安裝
必要的工具:OCLint、xcpretty(美化xcodebuild的命令行輸出日志工具)
選擇性安裝:Homebrew(The missing package manager for macOS,Mac OS的包管理器,可以便捷地安裝很多常用的工具)
資源下載地址
OCLint:https://github.com/oclint/oclint/releases,選擇最新的版本進行下載
xcpretty:https://github.com/supermarin/xcpretty,安裝命令:gem install xcpretty,首先要安裝有gem
Homebrew:https://brew.sh/,安裝命令:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
OCLint的安裝
Reference:http://docs.oclint.org/en/stable/intro/installation.html
1、解壓OCLint安裝包,打開終端,切換到解壓出來的OCLint安裝包目錄
2、執行腳本
OCLINT_HOME=/path/to/oclint-release
export PATH=$OCLINT_HOME/bin:$PATH
cp bin/oclint* /usr/local/bin/
cp -rp lib/* /usr/local/lib/
3、安裝結果的驗證,終端輸入oclint,輸出如下結果為安裝成功
$ oclint
oclint: Not enough positional command line arguments specified!
Must specify at least 1 positional arguments: See: oclint -help
另外,也可以用Homebrew進行安裝和更新
安裝
brew tap oclint/formulae
brew install oclint
更新
brew update
brew upgrade oclint
2、配置
在項目根目錄下創建用于分析的Shell腳本文件analysis.sh
以下為analysis.sh文件內的腳本
# 移除原有的生成文件
xcodebuild clean | xcpretty
rm -r build
rm -r compile_commands.json
# Build和把日志寫到目標文件
xcodebuild | xcpretty -r json-compilation-database --output compile_commands.json
Reference
http://docs.oclint.org/en/stable/guide/xcode.html
https://gist.github.com/ryuichis/755e6297aec13c900cdf
Xcode -> Build Phases -> New Run Script Phase -> Run Script,配置生成代碼不規范相關警告的Shell腳本
oclint-json-compilation-database -- -report-type xcode
說明:如果要在項目源碼中進行代碼檢查,那么在檢查的時候才開啟上述Xcode里面配置的腳本,不需要進行檢查,打包測試,提交到Git上的時候記得把檢查腳本注釋掉,不然在Build之前都會先執行分析腳本,看具體情況對這段代碼進行注釋或者開啟
3、靜態代碼分析
在命令行切換到項目根目錄,運行analysis.sh,生成build目錄和用于生成警告的compile_commands.json文件
如果項目中有.xcworkspace,則在使用xcodebuild命令,沒有指定相應的參數時是會失敗的,但是沒關系,只要能確保命令行的輸出里面看到有compile的字眼就OK,因為OCLint的原理就是從compile的源文件中去檢查的
如下圖所示,即使是Build Failed,也是沒問題的,也同樣能生成供OCLint進行靜態代碼檢查的文件
對Xcode項目進行Command+B操作,等oclint命令對compile_commands.json文件分析完成,就能查看到由于編碼不規范生成的警告
OCLint已有規則的自定義
Reference
OCLint查看幫助命令:oclint -help
OCLint代碼靜態檢查規則的查看命令:oclint rt -list-enabled-rules
OCLint configuration file:http://docs.oclint.org/en/stable/howto/rcfile.html
Project Configuration File
僅針對單個項目的已有規則的自定義,在項目根目錄下創建.oclint文件,可以使用Shell命令快速創建
touch .oclint # 創建.oclint文件
.oclint文件自定義已有規則
示例一
關閉已有的一些檢查規則
查看已有檢查規則的命令:oclint rt -list-enabled-rules
disable-rules:
- LongLine
- LongVariableName
- UnusedMethodParameter
示例二
自定義已有規則的限值,例如單行字數的上限
Reference:http://docs.oclint.org/en/stable/howto/thresholds.html
disable-rules:
- LongLine
rulePaths:
- /etc/rules
rule-configurations:
- key: CYCLOMATIC_COMPLEXITY
value: 15
- key: LONG_LINE
value: 200
output: oclint.xml
report-type: xml
max-priority-1: 20
max-priority-2: 40
max-priority-3: 60
enable-clang-static-analyzer: false
Summary
- 對項目代碼質量進行周期性檢查
- 及時修改不符合規范的代碼
- 讓項目問題杜絕在代碼編寫的層面
Next
- 自定義OCLint的檢查規則
- Code Review的規范
- Objective-C編碼規范
- Swift編碼規范
Reference
OCLint
- http://oclint.org/
- http://docs.oclint.org/en/stable/
- http://docs.oclint.org/en/stable/guide/xcode.html
Follow Me
Github: https://github.com/bobwongs
Sina: http://weibo.com/bobwongs
歡迎打賞!