iOS代碼靜態(tài)分析平臺(tái)搭建(四)之sonarqube平臺(tái)搭建

之前的三篇文章我們集成了OCLint以及Infer這兩個(gè)檢測工具,也順利的收集到了相關(guān)的掃描日志,但是這些日志看起來都不夠直觀,這就需要借助一個(gè)平臺(tái)對我們的掃描日志進(jìn)行一個(gè)清晰化的展示,也就是這一篇要說的sonarqube平臺(tái)。

sonarqube環(huán)境搭建

sonarqube是一個(gè)開源的靜態(tài)代碼掃描分析平臺(tái),它分成了四個(gè)不同的版本社區(qū)版,開發(fā)者版本,企業(yè)版本數(shù)據(jù)中心版,其中社區(qū)版是免費(fèi)的,其他版本都要收費(fèi),并且價(jià)格不菲,大部分情況下社區(qū)版就足夠開發(fā)者使用了,它支持分析19種語言,可惜不包含Objective-C以及Swift,這就需要我們下載相關(guān)的開源插件進(jìn)行支持,幸運(yùn)的是有人針對這個(gè)問題開源了相關(guān)的插件sonar-swift,接下來就一步步的搭建sonarqube環(huán)境。

下載sonarqube

看了很多網(wǎng)站的文章都說直接去官網(wǎng)下載sonarqube,或者直接brew install sonarqube,如果你需要檢測的不是iOS的項(xiàng)目,這么做沒問題,但是我們主要是為了檢測iOS的項(xiàng)目,就不能這么做,因?yàn)閺墓倬W(wǎng)或者h(yuǎn)omebrew下載下來的都是最新的版本,而sonar-swift這個(gè)插件如果集成到最新的版本上是無法運(yùn)行的,如果在最新版本使用此插件,會(huì)走很多的彎路,因此我推薦下載穩(wěn)定的SonarQube 8.9.7版本。
??SonarQube 8.9.7下載地址

由于sonarqube的運(yùn)行環(huán)境是需要Java支持,因此下載后需要先安裝Java環(huán)境,建議安裝Java 11

brew install openjdk@11

安裝完畢執(zhí)行java --version驗(yàn)證一下,如下所示即安裝成功。

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環(huán)境安裝完畢就可以解壓下載好的sonarqube,解壓后的文件放到你想放置的目錄下,進(jìn)入bin/macosx-universal-64目錄下,執(zhí)行sh sonar.sh start啟動(dòng)sonarqube環(huán)境,也可以使用sh sonar.sh console命令啟動(dòng),這樣可以打印出控制臺(tái)的日志,如果啟動(dòng)失敗方便我們查看問題??刂婆_(tái)打印出如下內(nèi)容說明我們的sonarqube啟動(dòng)成功。

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

此時(shí)打開瀏覽器輸入http://localhost:9000會(huì)出現(xiàn)登錄界面,初始帳號(hào)密碼為admin。


登錄成功后會(huì)看到如下警告:

這個(gè)警告提示我們需要配置數(shù)據(jù)庫,默認(rèn)的存儲(chǔ)僅僅支持演示模式,如果需要持久化數(shù)據(jù)則需要配置數(shù)據(jù)庫。

配置數(shù)據(jù)庫

使用homebrew安裝PostgreSQL

brew install postgresql

安裝成功之后執(zhí)行brew services start postgresql@14啟動(dòng)服務(wù),依次執(zhí)行以下命令:

createdb                                //創(chuàng)建數(shù)據(jù)庫
psql                                    //登錄控制臺(tái)

數(shù)據(jù)庫創(chuàng)建成功之后需要提供一個(gè)帳號(hào)和密碼以供sonarqube來使用,依次執(zhí)行以下命令:

//?? ;符號(hào)不可省略??
CREATE USER sonarqube WITH PASSWORD 'sonarqube';//創(chuàng)建用戶
CREATE DATABASE sonar OWNER sonarqube;//創(chuàng)建屬于該用戶的數(shù)據(jù)庫

完成之后輸入\q退出編輯即可。然后進(jìn)入sonarqubesonarqube/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

重新啟動(dòng)sonarqube即可消除警報(bào)。

sonarqube漢化包安裝

??漢化包下載地址
根據(jù)我們的sonarqube版本選擇對應(yīng)的漢化包下載,下載完成放到/extensions/plugins目錄下。


重新啟動(dòng)sonarqube服務(wù)即可看到漢化后的界面。

sonar-swift集成

登錄成功之后可以進(jìn)入到質(zhì)量配置選項(xiàng)查看所支持的檢測語言。


默認(rèn)是不支持Objective-C以及Swift的檢測的,因此需要下載對應(yīng)檢測插件,我推薦下載sonar-swift 1.6.1版本。
??sonar-swift下載地址
下載完成依舊放到/extensions/plugins目錄下重啟sonarqube服務(wù)。再次進(jìn)入質(zhì)量配置界面,現(xiàn)在sonarqube就具備掃描Objective-C以及Swift的能力了。

sonar-scanner

到此為止,我們有了OCLint和Infer的掃描報(bào)告,也有了可視化平臺(tái),現(xiàn)在就缺一個(gè)中間角色來分析報(bào)告并且上傳平臺(tái),sonar-scanner就是這樣的一個(gè)角色。

//使用homebrew安裝即可
brew install sonar-scanner

距離大功告成還剩下最后一步配置文件,在項(xiàng)目工程目錄下新增一個(gè)sonar-project.properties文件,加入如下內(nèi)容:

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文件單獨(dú)放到一個(gè)sonar-reports文件夾中,這樣才可以正常檢測到這份報(bào)告,在項(xiàng)目目錄下直接執(zhí)行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網(wǎng)頁就可以看到項(xiàng)目已經(jīng)完成了分析并且上傳了分析報(bào)告,代碼的問題一目了然。

腳本更新

#!/bin/bash

COLOR_ERR="\033[1;31m"    #出錯(cuò)提示
COLOR_SUCC="\033[0;32m"  #成功提示
COLOR_QS="\033[1;37m"  #問題顏色
COLOR_AW="\033[0;37m"  #答案提示
COLOR_END="\033[1;34m"     #顏色結(jié)束符



# 尋找項(xiàng)目的 ProjectName
function searchProjectName () {
    # maxdepth 查找文件夾的深度
    find . -maxdepth 1 -name "*.xcodeproj"
}

function oclintForProject () {
    # 預(yù)先檢測所需的安裝包是否存在
    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}
    # 字符串替換函數(shù)。//表示全局替換 /表示匹配到的第一個(gè)結(jié)果替換。
    path=${path//.\//}  # ./BridgeLabiPhone.xcodeproj -> BridgeLabiPhone.xcodeproj
    path=${path//.xcodeproj/} # BridgeLabiPhone.xcodeproj -> BridgeLabiPhone
    
    myworkspace=$path".xcworkspace" # workspace名字
    myscheme=$path  # scheme名字
    
    # 清除上次編譯數(shù)據(jù)
    DIR=~/Library/Developer/Xcode/DerivedData/
    echo -e $COLOR_SUCC'??????????清除上次編譯數(shù)據(jù)??????????'$COLOR_SUCC
    rm -r -- "$DIR"*

    # # 生成編譯數(shù)據(jù)
    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編譯數(shù)據(jù)生成完畢??????????'$COLOR_SUCC
    else
    echo -e $COLOR_ERR'???xcpretty編譯數(shù)據(jù)生成失敗???'$COLOR_ERR
    return -1
    fi

    echo -e $COLOR_SUCC'??????????OCLint代碼分析開始??????????'$COLOR_SUCC
    # 生成報(bào)表
    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分析數(shù)據(jù)生成完畢??????????'$COLOR_SUCC
    mv oclint.xml sonar-reports/
    echo -e $COLOR_SUCC'??????????移動(dòng)至sonar-reports完畢??????????'$COLOR_SUCC
    else
    echo -e $COLOR_ERR'???OCLint分析數(shù)據(jù)生成失敗???'$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分析數(shù)據(jù)生成完畢??????????'$COLOR_SUCC
    else
    echo -e $COLOR_ERR'???Infer分析數(shù)據(jù)生成失敗???'$COLOR_ERR
    return -1
    fi
    
    echo -e $COLOR_SUCC'??????????開始進(jìn)行數(shù)據(jù)分析??????????'$COLOR_SUCC
    sonar-scanner
    echo -e $COLOR_SUCC'??????????數(shù)據(jù)分析完成??????????'$COLOR_SUCC
}

oclintForProject $1

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容