iOS持续集成(CI)——OCLint静态代码分析

时间:2022-06-22
本文章向大家介绍iOS持续集成(CI)——OCLint静态代码分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

安装oclint

 brew install oclint

安装 xcpretty

用于对xcodebuild的输出进行格式化

$ 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;

脚本内容分析

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

通过 -e 选项来忽略Cocoapods 来pod文件,通过--来分割 oclint-json-compilation-database 的参数与 oclint_args。oclint_args 就是 oclint 命令的参数。 -report-type 文件类型

oclint-json-compilation-database -e Pods -- -report-type pmd -o oclint_result.pmd

Jenkins集成静态代码分析

安装插件PMD Plugin

项目配置

分析结果