之前的三篇文章我们集成了OCLint以及Infer这两个检测工具,也顺利的收集到了相关的扫描日志,但是这些日志看起来都不够直观,这就需要借助一个平台对我们的扫描日志进行一个清晰化的展示,也就是这一篇要说的sonarqube平台。
sonarqube环境搭建
sonarqube是一个开源的静态代码扫描分析平台,它分成了四个不同的版本社区版,开发者版本,企业版本,数据中心版,其中社区版是免费的,其他版本都要收费,并且价格不菲,大部分情况下社区版就足够开发者使用了,它支持分析19种语言,可惜不包含Objective-C
以及Swift
,这就需要我们下载相关的开源插件进行支持,幸运的是有人针对这个问题开源了相关的插件sonar-swift,接下来就一步步的搭建sonarqube环境。
下载sonarqube
看了很多网站的文章都说直接去官网下载sonarqube,或者直接brew install sonarqube
,如果你需要检测的不是iOS的项目,这么做没问题,但是我们主要是为了检测iOS的项目,就不能这么做,因为从官网或者homebrew下载下来的都是最新的版本,而sonar-swift
这个插件如果集成到最新的版本上是无法运行的,如果在最新版本使用此插件,会走很多的弯路,因此我推荐下载稳定的SonarQube 8.9.7
版本。
👉SonarQube 8.9.7下载地址
由于sonarqube的运行环境是需要Java支持,因此下载后需要先安装Java环境,建议安装Java 11。
brew install openjdk@11
安装完毕执行java --version
验证一下,如下所示即安装成功。
openjdk 11.0.20.1 2023-08-24
OpenJDK Runtime Environment Homebrew (build 11.0.20.1+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.20.1+0, mixed mode)
Java环境安装完毕就可以解压下载好的sonarqube,解压后的文件放到你想放置的目录下,进入bin/macosx-universal-64
目录下,执行sh sonar.sh start
启动sonarqube环境,也可以使用sh sonar.sh console
命令启动,这样可以打印出控制台的日志,如果启动失败方便我们查看问题。控制台打印出如下内容说明我们的sonarqube启动成功。
jvm 1 | 2023.10.12 14:49:23 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up
jvm 1 | 2023.10.12 14:49:23 INFO app[][o.s.a.SchedulerImpl] SonarQube is up
此时打开浏览器输入http://localhost:9000
会出现登录界面,初始帐号密码为admin。
登录成功后会看到如下警告:
这个警告提示我们需要配置数据库,默认的存储仅仅支持演示模式,如果需要持久化数据则需要配置数据库。
配置数据库
使用homebrew安装PostgreSQL
brew install postgresql
安装成功之后执行brew services start postgresql@14
启动服务,依次执行以下命令:
createdb //创建数据库
psql //登录控制台
数据库创建成功之后需要提供一个帐号和密码以供sonarqube来使用,依次执行以下命令:
//⚠️ ;符号不可省略⚠️
CREATE USER sonarqube WITH PASSWORD 'sonarqube';//创建用户
CREATE DATABASE sonar OWNER sonarqube;//创建属于该用户的数据库
完成之后输入\q
退出编辑即可。然后进入sonarqube的sonarqube/conf
目录下,编辑sonar.properties
文件。
# IMPORTANT:
# - The embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are Oracle, PostgreSQL and Microsoft SQLServer.
# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.
# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- Oracle 12c/18c/19c
# The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/.
# Only the thin client is supported, and we recommend using the latest Oracle JDBC driver. See
# https://jira.sonarsource.com/browse/SONAR-9758 for more details.
# If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE
#----- PostgreSQL 9.3 or greater
# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
#sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
重新启动sonarqube即可消除警报。
sonarqube汉化包安装
👉汉化包下载地址
根据我们的sonarqube版本选择对应的汉化包下载,下载完成放到/extensions/plugins
目录下。
重新启动sonarqube服务即可看到汉化后的界面。
sonar-swift集成
登录成功之后可以进入到质量配置选项查看所支持的检测语言。
默认是不支持Objective-C以及Swift的检测的,因此需要下载对应检测插件,我推荐下载
sonar-swift 1.6.1
版本。👉sonar-swift下载地址
下载完成依旧放到
/extensions/plugins
目录下重启sonarqube服务。再次进入质量配置界面,现在sonarqube就具备扫描Objective-C以及Swift的能力了。sonar-scanner
到此为止,我们有了OCLint和Infer的扫描报告,也有了可视化平台,现在就缺一个中间角色来分析报告并且上传平台,sonar-scanner就是这样的一个角色。
//使用homebrew安装即可
brew install sonar-scanner
距离大功告成还剩下最后一步配置文件,在项目工程目录下新增一个sonar-project.properties
文件,加入如下内容:
sonar.projectKey=your_project
sonar.projectName=your_project
sonar.language=objc
sonar.objectivec.workspace=your_project.xcworkspace
sonar.objectivec.appScheme=your_scheme
sonar.sourceEncoding=UTF-8
sonar.junit.reportsPath=sonar-reports/
sonar.objectivec.oclint.report=sonar-reports/oclint.xml
sonar.swift.infer.report=infer-out/report.json
需要注意的是我们必须把oclint.xml
文件单独放到一个sonar-reports文件夹中,这样才可以正常检测到这份报告,在项目目录下直接执行sonar-scanner
即可。
INFO: Analysis report uploaded in 165ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=Demo
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=xxxxxxxxxx
INFO: Analysis total time: 1:49.589 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 1:50.191s
INFO: Final Memory: 15M/80M
INFO: ------------------------------------------------------------------------
再去打开sonarqube网页就可以看到项目已经完成了分析并且上传了分析报告,代码的问题一目了然。
脚本更新
#!/bin/bash
COLOR_ERR="\033[1;31m" #出错提示
COLOR_SUCC="\033[0;32m" #成功提示
COLOR_QS="\033[1;37m" #问题颜色
COLOR_AW="\033[0;37m" #答案提示
COLOR_END="\033[1;34m" #颜色结束符
# 寻找项目的 ProjectName
function searchProjectName () {
# maxdepth 查找文件夹的深度
find . -maxdepth 1 -name "*.xcodeproj"
}
function oclintForProject () {
# 预先检测所需的安装包是否存在
if which xcodebuild 2>/dev/null; then
echo 'xcodebuild exist'
else
echo 'xcodebuild 未安装,请安装Xcode'
fi
if which oclint 2>/dev/null; then
echo 'oclint exist'
else
export PATH="/Users/imac0823/Documents/Tools/oclint/bin:$PATH"
source ~/.zshrc
fi
if which xcpretty 2>/dev/null; then
echo 'xcpretty exist'
else
echo 'xcpretty 未安装,请安装xcpretty'
fi
# 指定编码
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"
export xcpretty=/usr/local/bin/xcpretty # xcpretty 的安装位置可以在终端用 which xcpretty找到
searchFunctionName=`searchProjectName`
path=${searchFunctionName}
# 字符串替换函数。//表示全局替换 /表示匹配到的第一个结果替换。
path=${path//.\//} # ./BridgeLabiPhone.xcodeproj -> BridgeLabiPhone.xcodeproj
path=${path//.xcodeproj/} # BridgeLabiPhone.xcodeproj -> BridgeLabiPhone
myworkspace=$path".xcworkspace" # workspace名字
myscheme=$path # scheme名字
# 清除上次编译数据
DIR=~/Library/Developer/Xcode/DerivedData/
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀清除上次编译数据🚀🚀🚀🚀🚀'$COLOR_SUCC
rm -r -- "$DIR"*
# # 生成编译数据
xcodebuild GCC_PRECOMPILE_PREFIX_HEADER=YES COMPILER_INDEX_STORE_ENABLE=NO OTHER_CFLAGS="-DNS_FORMAT_ARGUMENT(A)= -D_Nullable_result=_Nullable" clean build -scheme $myscheme'Q' -workspace $myworkspace -configuration Debug -sdk iphoneos16.2 |tee xcodebuild.log|xcpretty -r json-compilation-database -o compile_commands.json
if [ -f ./compile_commands.json ]; then
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀xcpretty编译数据生成完毕🚀🚀🚀🚀🚀'$COLOR_SUCC
else
echo -e $COLOR_ERR'❌❌❌xcpretty编译数据生成失败❌❌❌'$COLOR_ERR
return -1
fi
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀OCLint代码分析开始🚀🚀🚀🚀🚀'$COLOR_SUCC
# 生成报表
oclint-json-compilation-database -e Pods -e Applications -- -extra-arg=-Wno-everything -report-type pmd -o oclint.xml \-rc=LONG_LINE=200 \-rc=LONG_VARIABLE_NAME=40 \-disable-rule ShortVariableName \-disable-rule UseContainerLiteral \-disable-rule ParameterReassignment \-disable-rule UseObjectSubscripting \-disable-rule AssignIvarOutsideAccessors \-disable-rule UnusedMethodParameter
if [ -f ./oclint.xml ]; then
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀OCLint分析数据生成完毕🚀🚀🚀🚀🚀'$COLOR_SUCC
mv oclint.xml sonar-reports/
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀移动至sonar-reports完毕🚀🚀🚀🚀🚀'$COLOR_SUCC
else
echo -e $COLOR_ERR'❌❌❌OCLint分析数据生成失败❌❌❌'$COLOR_ERR
return -1
fi
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀Infer代码分析开始🚀🚀🚀🚀🚀'$COLOR_SUCC
# --skip-analysis-in-path 是忽略扫描目录
infer run --skip-analysis-in-path Pods --keep-going --compilation-database compile_commands.json
if [ -f ./infer-out/report.json ]; then
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀Infer分析数据生成完毕🚀🚀🚀🚀🚀'$COLOR_SUCC
else
echo -e $COLOR_ERR'❌❌❌Infer分析数据生成失败❌❌❌'$COLOR_ERR
return -1
fi
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀开始进行数据分析🚀🚀🚀🚀🚀'$COLOR_SUCC
sonar-scanner
echo -e $COLOR_SUCC'🚀🚀🚀🚀🚀数据分析完成🚀🚀🚀🚀🚀'$COLOR_SUCC
}
oclintForProject $1