底层探索--CodeReview值OCLint

2021-10-18  本文已影响0人  永断阎罗

安装

规则

OCLint是根据一系列定制的规则进行静态分析的,
OCLint所有规则

命令解释

OCLint in 控制台

//控制台编译:如果你没有开发者证书,可加-sdk iphonesimulator编译
xcodebuild -workspace 项目名.xcworkspace -scheme 项目名 -sdk iphonesimulator -configuration Debug

//编译,增强可读性
xcodebuild -workspace 项目名.xcworkspace -scheme 项目名 -sdk iphonesimulator -configuration Debug | xcpretty -r json-compilation-database -o compile_commands.json

OCLint in Xcode 使用--重点

封装成脚本

实例1:

#! /bin/sh
#source ~/.bash_profile
export LC_ALL=en_US.UTF-8
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
rm compile_commands.json;
xcodebuild clean
xcodebuild -workspace 自己的项目名.xcworkspace -scheme 自己的项目名Scheme \
-configuration Debug \
| xcpretty -r json-compilation-database -o compile_commands.json
oclint-json-compilation-database  -v \
-e Pods \
-- -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; \

实例2:

#!/bin/bash

# 指定编码
export LANG="zh_CN.UTF-8"
export LC_COLLATE="zh_CN.UTF-8"
export LC_CTYPE="zh_CN.UTF-8"
export LC_MESSAGES="zh_CN.UTF-8"
export LC_MONETARY="zh_CN.UTF-8"
export LC_NUMERIC="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"


function checkDepend () {
command -v xcpretty >/dev/null 2>&1 || {
echo >&2 "I require xcpretty but it's not installed.  Install:gem install xcpretty";
exit
}
command -v oclint-json-compilation-database >/dev/null 2>&1 || {
echo >&2 "I require oclint-json-compilation-database but it's not installed.  Install:brew install oclint";
exit
}
}

function oclintForProject () {

# 检测依赖
checkDepend

projectName=$1
scheme=$2
reportType=$3

REPORT_PMD="pmd"
REPORT_XCODE="Xcode"

myworkspace=${projectName}
myscheme=${scheme}
echo "myworkspace是:${myworkspace}"
echo "myscheme是:${myscheme}"
echo "reportType为:${reportType}"

# 清除上次编译数据
if [ -d ./build/derivedData ]; then
echo '-----清除上次编译数据derivedData-----'
rm -rf ./build/derivedData
fi

# xcodebuild -workspace $myworkspace -scheme $myscheme clean
xcodebuild clean

echo '-----开始编译-----'

# 生成编译数据
xcodebuild -workspace ${myworkspace} -scheme ${myscheme} -UseModernBuildSystem=NO -derivedDataPath ./build/derivedData -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO | xcpretty -r json-compilation-database -o compile_commands.json


if [ -f ./compile_commands.json ]
then
echo '-----编译数据生成完毕-----'
else
echo "-----生成编译数据失败-----"
return -1
fi

echo '-----分析中-----'

# 自定义排除警告的目录,将目录字符串加到数组里面
# 转化为:-e Debug.m -e Port.m -e Test
exclude_files=("Pods")

exclude=""
for i in ${exclude_files[@]}; do
exclude=${exclude}"-e "${i}" "
done
echo "排除目录:${exclude}"

# 分析reportType =~判断子字符串包含关系
if [[ ${reportType} =~ ${REPORT_PMD} ]]
then
nowReportType="-report-type html -o pmd.html"
else
nowReportType="-report-type Xcode"
fi
# 自定义report 如:
# nowReportType="-report-type html -o oclint_result.html"

echo "**************${nowReportType}"

# 生成报表
oclint-json-compilation-database ${exclude} -- \
${nowReportType} \
-rc=LONG_CLASS=1500 \
-rc=NESTED_BLOCK_DEPTH=5 \
-rc=LONG_VARIABLE_NAME=80 \
-rc=LONG_METHOD=200 \
-rc=LONG_LINE=300 \
-disable-rule ShortVariableName \
-disable-rule ObjCAssignIvarOutsideAccessors \
-disable-rule AssignIvarOutsideAccessors \
-disable-rule TooManyMethods \
-disable-rule BitwiseOperatorInConditional \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000

#rm compile_commands.json
if [[ ${reportType} =~ ${REPORT_PMD} ]] && [ ! -f ./pmd.html ]
then
echo "-----分析失败-----"
return -1
else
echo '-----分析完毕-----'
return 0
fi
}

# 替换workspace的名字
myworkspace="Project.xcworkspace"
# 替换scheme的名字
myscheme="Project"
# 输出方式 Xcode/pmdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
reportType="pmd"

oclintForProject ${myworkspace} ${myscheme} ${reportType}

保存oclint.sh文件在同项目的顶级目录里面,然后控制台执行:sh oclint.sh or ./oclint.sh

参照

参考文章

参考文章1

参考文章2

上一篇 下一篇

猜你喜欢

热点阅读