222

让XCode自动CodeReview你的代码-OCLint使用

 6 years ago
source link: http://www.cocoachina.com/ios/20170928/20669.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

让XCode自动CodeReview你的代码-OCLint使用

user.png 枣泥布丁· 2017-09-28 阅读数 10628
本文来自 alanwangmodify ,作者 枣泥布丁

前言

OCLint 是一个静态分析代码的工具,支持自定义规则,可以很好地帮助我们规范代码,这篇文章主要介绍通过OCLint让XCode来帮你Code Review,规范代码。

OCLint相关的详细内容也可以到OCLint官网上查看。

安装OCLint 和 xcpretty

这里需要安装OCLint 和 xcpretty 工具

1、 安装xcpretty

用gem安装xcpretty

sudo gem install xcpretty复制代码

用sudo是防止一些权限出现问题

2、安装OCLint

安装OCLint有两种常用的方法:

1)通过安装包:

从oclint的github项目链接中下载安装包oclint-0.10.3-x86_64-darwin-15.5.0.tar.gz.

解压后在当前用户的.bashrc中配置环境变量

OCLINT_HOME=/pathexport PATH=$PATH:OCLINT_HOME/bin复制代码

然后source .bashrc即可

2)通过Homebrew

brew tap oclint/formulae

brew install oclint

3)确认安装成功

运行oclint确认环境配置正确

$ oclint复制代码

如果展示以下内容即为安装成功:

oclint: Not enough positional command line arguments specified!Must specify at least 1 positional arguments: See: oclint -help复制代码

OCLint 的使用

这里介绍一些简单常用的用法,足够应对很多场景,其他详细用法可以到OCLint Manual上查看。

1240

OCLint 有三个指令:oclint、oclint-json-compilation-database、oclint-xcodebuild。

  1. oclint:常规核心指令。

  2. oclint-json-compilation-database:从编译好的compile_commands.json 文件中读取配置信息并执行 oclint。

  3. oclint-xcodebuild:主要用于生成compile_commands.json文件,现在已经不进行维护了,我们可以用xcpretty替代,来生成json文件。

oclint指令

oclint [options]  -- [compiler flags]复制代码

[options]为一些参数选项,可以是规则加载选项、报告形式选项等:

  1. -R <路径> : 检测所用的规则的路径,默认路径$(/path/to/bin/oclint)/../lib/oclint/rules

  2. -disable-rule <规则名>: 让相对应的规则失效(OCLint 规则列表)。

  3. -rc <参数>=<值> :修改阈值

  4. -report-type <报告类型>,有"text"、“html”、“json”、“pmd”、“xcode”几个类型

  5. -o <路径> 报告生成路径。

oclint  -R  /path/to/rules   -disable-rule ObjCAssignIvarOutsideAccessors -report-type xcode复制代码

oclint-json-compilation-database 指令

这是我们主要用到的指令,它是oclint 指令的升级版,使用起来相对 oclint 指令简单方便。

可以通过 -- 的方式在指令的最后加上oclint 选项。

oclint-json-compilation-database 指令有过滤文件选项

  • -i :包含进某些文件

  • -e : 过滤掉某些文件

# 排除Pods 里的文件
oclint-json-compilation-database -e Pods 
# 包含进Pods里的文件
oclint-json-compilation-database -i Pods复制代码

oclint-xcodebuild 指令。

因为oclint-xcodebuild 指令已经不再维护,实际应用中用xcpretty代替,所以这里就不进行介绍。

其他命令工具:xcodebuild 和 xcpretty

xcodebuild

xcodebuild是苹果发布自动构建的工具,可以通过命令行脚本的方式编译、打包Xcode 工程。

可以在终端输入man xcodebuild来查询相关用法

1240

xcpretty

用xcpretty生成OClint 解析用的json 文件

这里用xcpretty是因为oclint-xcodebuild不再维护了,在XCode 8之后采用xcpretty来生成。

用XCode检测代码

首先在targets点添加按钮:

1240

在Cross-platform选项中,选“Aggregate”,然后命名为"OCLint":

1240

接着选中OCLint Target,进入Build Phases选项,点左上角的加号,选择“New Run Script Phase”

1240

然后输入脚本即可:

1240

示例脚本:

source ~/.bash_profile
cd ${SRCROOT}
xcodebuild clean
xcodebuild  -workspace demo.xcworkspace -scheme demo | xcpretty -r json-compilation-database --output compile_commands.json
oclint-json-compilation-database -v \n-e Pods \n-e MGLivenessDetection \n-e MGBaseKit \n-e MGIDCard \noclint_args -- -report-type xcode \n-disable-rule ObjCAssignIvarOutsideAccessors \n-disable-rule ShortVariableName \n-rc=MINIMUM_CASES_IN_SWITCH=3 \n-rc=CYCLOMATIC_COMPLEXITY=10 \n-rc=LONG_CLASS=700 \n-rc=LONG_LINE=200 \n-rc=NCSS_METHOD=40 \n-rc=NESTED_BLOCK_DEPTH=5 \n-rc=TOO_MANY_FIELDS=20 \n-rc=TOO_MANY_METHODS=30 \n-rc=TOO_MANY_PARAMETERS=6
exit复制代码

接着选择OCLint Scheme,Command+B就可以执行了

在执行前最好删除drivedata里面的数据缓存,以保证不会解析到旧的编译内容。

编译成功后Xcode上会在不符合规则的代码上显示Warning:

1240

参考链接:

iOS 工程自动化 - OCLint 使用详解

用OCLint给iOS代码做静态分析


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK