使用 SonarQube 可視化 iOS 代碼質(zhì)量

文檔更新說明

  • 2017-09-17 v1.0 初稿
  • 2017-09-28 v1.1 增加單元測試相關內(nèi)容
  • 2019-01-02 fix error

項目在研發(fā)過程中需要時刻關注代碼質(zhì)量,我們通常會使用 OCLint 工具來生成靜態(tài)代碼分析報告,使用 Slather 生成單元測試覆蓋率報告,但這些報告相對來說都太過簡陋,而 Sonar Qube 以一種可視化的方法來展示代碼中存在的問題,本文將介紹如何使用 Sonar Qube 展示 OCLint 掃描后的報告。

安裝軟件

這里以 Ubuntu 16.04 為例介紹如何安裝 SonarQube,SonarQube 運行依賴幾個東西:JDK 和 MySQL,所以在安裝 SonarQube 之前需要先安裝這兩個東西。

安裝 JDK 和 MySQL

安裝 JDK

  • sudo add-apt-repository ppa:webupd8team/java
  • sudo apt-get update
  • sudo apt-get install oracle-java8-installer

執(zhí)行上述命令后,在終端里面輸入 java -version,如能正常顯示 JDK 版本則說明安裝成功。

安裝 MySQL

  • sudo apt-get update
  • sudo apt-get install mysql-server
  • sudo mysql_secure_installation

安裝完畢后,在終端中輸入 systemctl status mysql.service,如能有如下輸出則說明安裝成功:

安裝 SonarQube

解壓安裝包至安裝目錄

安裝包下載地址:https://www.sonarqube.org/downloads/

創(chuàng)建數(shù)據(jù)庫及相應用戶

新建數(shù)據(jù)庫

  • CREATE DATABASE sonar;

創(chuàng)建用戶并賦予權限

  • CREATE USER 'sonar' IDENTIFIED BY 'sonar’;
  • GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar’;
  • GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar’;
  • FLUSH PRIVILEGES;

配置數(shù)據(jù)庫訪問參數(shù)

編輯 <install_directory>/conf/sonar.properties,根據(jù)使用的數(shù)據(jù)庫類型配置相應的參數(shù),這里選用了 MySQL 數(shù)據(jù)庫,因而需要配置其用戶名、密碼以及 jdbc url。

image.png

配置 Web Server

同樣編輯 <install_directory>/conf/sonar.properties,設置 Web Server host 主機的 ip、端口以及訪問的上下文。

sonar.web.host=192.0.0.1
sonar.web.port=80
sonar.web.context=/sonar

上述配置完成后即可啟動 SonarQube:

  • On Linux/Mac OS: bin/<YOUR OS>/sonar.sh start
  • On Windows: bin/windows-x86-XX/StartSonar.bat

在實際使用中會出現(xiàn)MySQL 數(shù)據(jù)庫無法訪問的情況,通常有如下幾種問題:
1.用戶沒有權限,加權限即可

mysql>GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'%' IDENTIFIED BY 'sonar' WITH GRANT OPTION;

2.默認3306端口只允許本地訪問,修改 /etc/mysql/mysql.conf.d/mysqld.cnf
注掉 bind-address = 127.0.0.1

上述軟件安裝完畢后,SonarQube 就搭建完成,在瀏覽器中輸入 http://192.0.0.1/sonar 即可使用SonarQube 來檢查代碼質(zhì)量。

SonarQube 默認并不支持對 Objective-C 代碼質(zhì)量掃描,官方提供的插件每年需要3000歐,對于普通開發(fā)者來說太貴,幸好 Github 上有開源的插件,下載好源碼后使用 Maven 編譯后將 jar 包放置 <install_directory>/extensions/plugins 目錄中,重啟 SonarQube后在系統(tǒng)控制面板中如能看到如下內(nèi)容則說明安裝成功。

image.png

編寫腳本生成 OCLint 報告并上傳至 SonarQube

軟件安裝

OCLint 所需軟件請翻閱 OCLint 代碼靜態(tài)分析 ,OCLint 生成的 pmd 文件需要通過 sonar-scanner 上傳至 SonarQube,下載地址:https://github.com/SonarSource/sonar-scanner-cli/releases

配置

sonar-project.properties

在項目根目錄添加 sonar-project.properties,配置內(nèi)容如下:

##########################
# Required configuration #
##########################

sonar.projectKey=tztHuaTaiZLMobile
sonar.projectName=tztHuaTaiZLMobile
sonar.projectVersion=4.3.0
sonar.language=objc

# Project description
sonar.projectDescription=tztHuaTaiZLMobile

# Path to source directories 
sonar.sources=tztHuaTaiZLMobile
# Path to test directories (comment if no test)
# sonar.tests=testSrcDir
 
# Destination Simulator to run tests
# As string expected in destination argument of xcodebuild command
# Example = sonar.swift.simulator=platform=iOS Simulator,name=iPhone 6,OS=9.2
sonar.objectivec.simulator=iOS Simulator,name=iPhone 7,OS=10.3.1

# Xcode project configuration (.xcodeproj or .xcworkspace)
# -> If you have a project: configure only sonar.objectivec.project
# -> If you have a workspace: configure sonar.objectivec.workspace and sonar.objectivec.project
# and use the later to specify which project(s) to include in the analysis (comma separated list)
# sonar.objectivec.project=myApplication.xcodeproj 
sonar.objectivec.workspace=tztMobileApp_HTSC.xcworkspace

# Scheme to build your application
sonar.objectivec.appScheme=tztHuaTaiZLMobile
# Scheme to build and run your tests (comment following line of you don't have any tests)
# sonar.objectivec.testScheme=myApplicationTests

##########################
# Optional configuration #
##########################

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# JUnit report generated by run-sonar.sh is stored in sonar-reports/TEST-report.xml
# Change it only if you generate the file on your own
# The XML files have to be prefixed by TEST- otherwise they are not processed 
# sonar.junit.reportsPath=sonar-reports/

# Cobertura report generated by run-sonar.sh is stored in sonar-reports/coverage.xml
# Change it only if you generate the file on your own
# sonar.objectivec.coverage.reportPattern=sonar-reports/coverage*.xml

# OCLint report generated by run-sonar.sh is stored in sonar-reports/oclint.xml
# Change it only if you generate the file on your own
sonar.objectivec.oclint.report=build/sonar-reports/oclint.xml

# FauxPas report generated by run-sonar.sh is stored in sonar-reports/fauxpas.json
# Change it only if you generate the file on your own
# sonar.objectivec.fauxpas.report=sonar-reports/fauxpas.json

# Paths to exclude from coverage report (tests, 3rd party libraries etc.)
# sonar.objectivec.excludedPathsFromCoverage=pattern1,pattern2
sonar.objectivec.excludedPathsFromCoverage=.*Tests.*,.*Specs.*

# Project SCM settings
# sonar.scm.enabled=true
# sonar.scm.url=scm:git:https://...

編寫腳本生成 OCLint 報告并上傳 SonarQube

在項目根目錄添加 scripts 目錄,并在該目錄下新建shell腳本,腳本內(nèi)容如下:

#!/bin/sh

function testIsInstalled() {

    hash $1 2>/dev/null
    if [ $? -eq 1 ]; then
        echo >&2 "ERROR - $1 is not installed or not in your PATH"; exit 1;
    fi
}

echo "change to parent folder"
cd ..

echo "check xcodebuild, oclint installed is installed"
testIsInstalled xcodebuild
testIsInstalled xcpretty
testIsInstalled oclint

echo "xcodebuild clean"
xcodebuild clean -workspace DemoApp.xcworkspace \
-scheme tztHuaTaiZLMobile

echo "xcodebuild analyze | tee xcodebuild.log | xcpretty --report json-compilation-database"
xcodebuild -workspace DemoApp.xcworkspace \
-configuration Debug \
-scheme DemoApp analyze COMPILER_INDEX_STORE_ENABLE=NO  | tee xcodebuild.log | \
xcpretty -r json-compilation-database

echo "mv compilation_db.json compile_commands.json"
mv ./build/reports/compilation_db.json ./compile_commands.json

echo "check folder existence"
if [ ! -d "build/sonar-reports" ]; then
    mkdir -p build/sonar-reports
fi

echo "oclint-json-compilation-database"
oclint-json-compilation-database \
-v \
-- \
-report-type pmd -o build/sonar-reports/oclint.xml \
-max-priority-1=99999 -max-priority-2=99999 -max-priority-3=99999 \
-rc LONG_METHOD=300 \
-rc LONG_VARIABLE_NAME=50 \
-rc LONG_CLASS=3000 \
-rc NCSS_METHOD=300 \
-rc NESTED_BLOCK_DEPTH=8 \

echo "upload generated oclint report to sonar qube server"
sonar-scanner -X

echo "clean up"
rm -rf .scannerwork
rm -rf xcodebuild.log
rm -rf compile_commands.json
rm -rf build/sonar-reports/oclint.xml

結(jié)果展示

報告上傳后,SonarQube 會根據(jù)報告內(nèi)容生成可視化的界面,描述每一項存在的問題,如可能存在的 Bug、代碼缺陷、需要重構的代碼塊等等。

本項目分析完畢后效果如下圖所示:

image.png

fix error

1.oclint: error: one compiler command contains multiple jobs:

運行環(huán)境:MacOS 10.14.2, XCode Version 9.4, OCLint 0.13.

xcodebuild執(zhí)行命令中添加COMPILER_INDEX_STORE_ENABLE=NO參數(shù)選項。

1.cd current project folder
2.xcodebuild -target Target -configuration Debug -scheme Scheme -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' CLANG_ENABLE_MODULE_DEBUGGING=NO CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ENABLE_BITCODE=NO COMPILER_INDEX_STORE_ENABLE=NO | tee xcodebuild.log | xcpretty -r json-compilation-database --output compile_commands.json 
3.oclint-json-compilation-database -- -max-priority-1 '10' -max-priority-2 '2000' -max-priority-3 '5000' -report-type pmd -o pmd.xml

2.pre-commit can not execute fastlane in SourceTree

>>> source ~/.bash_profile
>>> open ~/Applications/SourceTree.app

3.CocoaPods requires your terminal to be using UTF-8 encoding and it aborts to run pod

>>> export LANG=en_US.UTF-8
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容