安裝oclint
brew install oclint
安裝 xcpretty
用于對(duì)xcodebuild的輸出進(jìn)行格式化
$ gem install xcpretty
oclint分析腳本
#!/bin/bash
source $HOME/.bash_profile
export LC_ALL=en_US.UTF-8
myworkspace=PuddingPlus.xcworkspace # 替換workspace的名字
myscheme=PuddingPlus # 替換scheme的名字
xcodebuild -workspace $myworkspace -scheme $myscheme clean&&
xcodebuild -workspace $myworkspace -scheme $myscheme \
-configuration Debug \
| tee xcodebuild.log \
| xcpretty -r json-compilation-database -o compile_commands.json&&
oclint-json-compilation-database -e Pods -- \
-report-type pmd -o oclint_result.pmd \
-rc LONG_LINE=200 \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000; \
rm compile_commands.json;
腳本內(nèi)容分析
xcodebuild 編譯工程
xcodebuild -workspace $myworkspace -scheme $myscheme clean&&
xcodebuild -workspace $myworkspace -scheme $myscheme \
-configuration Debug
獲取xcodebuild.log
tee xcodebuild.log
compile_commands.json
xcpretty -r json-compilation-database -o compile_commands.json
clint-json-compilation-database
$ oclint-json-compilation-database -help
usage: oclint-json-compilation-database [-h] [-v] [-debug] [-i INCLUDES]
[-e EXCLUDES]
[oclint_args [oclint_args ...]]
OCLint for JSON Compilation Database (compile_commands.json)
positional arguments:
oclint_args arguments that are passed to OCLint invocation
optional arguments:
-h, --help show this help message and exit
-v show invocation command with arguments
-debug, --debug invoke OCLint in debug mode
-i INCLUDES, -include INCLUDES, --include INCLUDES
extract files matching pattern
-e EXCLUDES, -exclude EXCLUDES, --exclude EXCLUDES
remove files matching pattern
通過(guò) -e 選項(xiàng)來(lái)忽略Cocoapods 來(lái)pod文件,通過(guò)--來(lái)分割 oclint-json-compilation-database 的參數(shù)與 oclint_args盗誊。oclint_args 就是 oclint 命令的參數(shù)。 -report-type 文件類型
oclint-json-compilation-database -e Pods -- -report-type pmd -o oclint_result.pmd
Jenkins集成靜態(tài)代碼分析
安裝插件PMD Plugin
image.png
項(xiàng)目配置
image.png
分析結(jié)果
image.png