JAY站

Valar Morghulis Valar Dohaeris

静下心来,用心观察 处处都透着生活的美.


OCLint 静态代码分析

环境

  • Homebrew 终端安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • oclint 安装 brew install oclint
  • xcpretty 安装 gem install xcpretty

脚本 关键点

  • 明确项目是否依赖CocoaPods
  • 使用xcodebuild -list, 明确 scheme target configuration

  • xcodebuild clean
  • xcodebuild -target xxx -scheme xxx 或者 xcodebuild -workspace xxx -scheme xxx
  • xcodebuild |xcpretty -r json-compilation-databasebuild/reports中得到文件compilation_db.json

compilation_db.json

  • compilation_db.json 移到与sh脚本同路径下 cp ./build/reports/compilation_db.json ./compile_commands.json

  • oclint-json-compilation-database -- -report-type html -o oclint_result.html 生成 oclint_result.html文件

  • 使用-e忽略不需要分析的目录, 比如CocoaPos生成的第三方库 Pods 文件, 命令 -e Pods

  • oclint分析错误报 ”too many errors emitted, stopping now” 是因为项目中错误太多, 超过了oclint的默认上限, 所以oclint停止了工作.

设置-max-priority-1 -max-priority-2 -max-priority-3的值, 提高上限,

例:oclint-json-compilation-database -- -max-priority-1 10000 -max-priority-2 10000 -max-priority-3 10000 -rc LONG_LINE=150 -report-type pmd -o oclint.xml
  • oclint升级到0.11版本以上

  • 注意脚本的执行环境路径配置 ~/.bash_profile 或者 /etc/profile

脚本的全文

#! /bin/sh
#或者source ~/.bash_profile
source /etc/profile

if which oclint 2>/dev/null; then
echo 'oclint exist'
else
brew tap oclint/formulae
brew install oclint
fi
if which xcpretty 2>/dev/null; then
echo 'xcpretty exist'
else
gem install xcpretty
fi

# workspace 和 target 只能使用一种. 
#myworkspace=XXX.xcworkspace # 替换workspace的名字
#myscheme=XXX # 替换scheme的名字

targetname=HttpManager
myscheme=HttpManager

xcodebuild -target $targetname -scheme $myscheme clean&&
xcodebuild -target $targetname -scheme $myscheme \
-configuration Debug \
| xcpretty -r json-compilation-database

cp ./build/reports/compilation_db.json ./compile_commands.json

if [ -f ./compile_commands.json ]; then echo "compile_commands.json 文件存在";
else echo "-----compile_commands.json文件不存在-----"; fi

oclint-json-compilation-database  -- \
-report-type html -o oclint_result.html \
-rc LONG_LINE=200 \
-rc=NCSS_METHOD=100 \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000; \
rm compile_commands.json;
if [ -f ./oclint_result.html ]; then echo '-----分析完毕-----'
else echo "-----分析失败-----"; fi

结果

oclint-report.png

  • 也可以 设置 结果为 -report-type xcode

oclint-report-01

oclint-report-02

#! /bin/sh
#或者source ~/.bash_profile
source /etc/profile
cd ${SRCROOT}

if which oclint 2>/dev/null; then
echo 'oclint exist'
else
brew tap oclint/formulae
brew install oclint
fi
if which xcpretty 2>/dev/null; then
echo 'xcpretty exist'
else
gem install xcpretty
fi

xcodebuild  clean&&
xcodebuild | xcpretty -r json-compilation-database

cp ./build/reports/compilation_db.json ./compile_commands.json

if [ -f ./compile_commands.json ]; then echo "compile_commands.json 文件存在";
else echo "-----compile_commands.json文件不存在-----"; fi


oclint-json-compilation-database  -- \
-report-type xcode \
-rc LONG_LINE=200 \
-rc=NCSS_METHOD=100 \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000; \
rm compile_commands.json;

问题

  • 随意创建的项目测试没有问题, 不过我公司的项目测试的时候,发现使用-report-type xcode 法忽略某些第三方库 -e [filePath] 也没用作用. 但是-report-type html 却可以.

相关链接

最近的文章

iOS组件化(基于Cocoapods)-简单使用

注册 trunkpod trunk register xxxx@gmail.com manajay创建本地私有库 创建 pod lib create xxx Class 中添加代码文件 Assets 中添加图片等资源 : 注意获取资源使用 [NSBundle bundleForClass:self], 注意图片要全名, 如果使用了资源,那么 podfile文件中添加描述use_frameworks!更改本次的版本描述 podspec文件 注意 version 一定要对应 git...…

iOS组件化继续阅读
更早的文章

ngrok

概念用途ngrok是非常流行的反向代理服务,可以进行内网穿透,支持80端口以及自定义tcp端口转发.这样你就可以运行本地的程序,而让别人通过公网访问了.安装1、官网下载地址下载, 解压2、将可执行文件ngrok移到系统的bin文件夹内(或者将快捷链接移动到bin目录下),注意是系统目录,不是用户目录.3、只有这样,才可以在命令行执行ngrok,而不会得到command not found的错误信息.使用说明1、 If you don’t know what port your web se...…

网络继续阅读