上文介绍了如何安装OClint,这次简单介绍下如何使用OClint。
- 先确定要扫描的项目(本文使用了一个很久很久用来学习的demo)
- 通过CD命令进入到项目目录
cd /Users/XXX/Documents/MyXcodeDemo/Demo/UiDemo
“/Users/XXX/Documents/MyXcodeDemo/Demo/UiDemo”是项目目录
3. 逐步输入扫描脚本
1、
# Cleanup before building
rm -f compile_commands.json
xctool -project UiDemo.xcodeproj -scheme UiDemo clean
2、
# Build Project
xctool build \
-project UiDemo.xcodeproj -scheme UiDemo \
-reporter json-compilation-database:compile_commands.json
3、
# Analyze Project
oclint-json-compilation-database -e Pods -- \
-max-priority-1=100000 \
-max-priority-2=100000 -max-priority-3=100000 \
-disable-rule=InvertedLogic \
-disable-rule=CollapsibleIfStatements \
-disable-rule=UnusedMethodParameter \
-disable-rule=LongLine \
-disable-rule=LongVariableName \
-disable-rule=ShortVariableName \
-disable-rule=UselessParentheses \
-disable-rule=IvarAssignmentOutsideAccessorsOrInit | sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/'
输出结果:
OCLint Report
Summary: TotalFiles=10 FilesWithViolations=1 P1=0 P2=0 P3=1
/Users/river/Documents/MyXcodeDemo/Demo/UiDemo/UiDemo/YAViewController.m:30:1: warning: long method [size|P3] Method with 53 lines exceeds limit of 50
[OCLint (http://oclint.org) v0.10.3]
问题分析:
/Users/river/Documents/MyXcodeDemo/Demo/UiDemo/UiDemo/YAViewController.m 是违反规则的文件
30是违反规则的代码位置
long method是违反的规则名称
ethod with 53 lines exceeds limit of 50是违反的规则描述
具体查阅官方文档
http://docs.oclint.org/en/stable/rules/index.html
最后清理扫描结果文件
# Final cleanup
rm -f compile_commands.json
为了方便我们统计分析,我们可以通过html来输出结果,其脚本如下:
oclint-json-compilation-database -v \
> -e Pods \
> oclint_args -- -report-type html -o oclintReport.html \
> -disable-rule ObjCAssignIvarOutsideAccessors \
> -max-priority-1=100000 \
> -max-priority-2=100000 -max-priority-3=100000 \
> -disable-rule=InvertedLogic \
> -disable-rule=CollapsibleIfStatements \
> -disable-rule=UnusedMethodParameter \
> -disable-rule=LongLine \
> -disable-rule=LongVariableName \
> -disable-rule=ShortVariableName \
> -disable-rule=UselessParentheses \
> -disable-rule=IvarAssignmentOutsideAccessorsOrInit
执行后会在项目目录输出oclintReport.html,通过oclintReport.html可以分析。
File - 违反规则的文件
Location - 违反规则的代码位置
Rule Name - 违反的规则名称
Rule Category - 违反的规则类别
Priority - 违反的规则优先级
Message - 违反的规则描述
大概功能简单介绍下,具体问题具体分析~~