iOS mac sonar安裝指南

一、安裝工具

1、Java環(huán)境與MySQL安裝

1.1 進(jìn)入jdk1.8安裝界面,下載macos版本
1.2 進(jìn)入MySQL 下載界面难述,下載之后安裝踩身,安裝完成后胀茵,進(jìn)入系統(tǒng)偏好設(shè)置,可看到MySql選項(xiàng)挟阻,代表已安裝成功

2琼娘、SonarQube安裝

下載地址
下載完成解壓之后直接把文件夾放到 /usr/local/ 路徑下

3、其它必備插件的安裝

安裝OCLint

OCLint是Sonar掃描代碼的主要工具附鸽,所以必須安裝脱拼。若電腦中已安裝Homebrew,可打開終端使用下面的命令安裝

brew tap oclint/formulae
brew install oclint

安裝xctool

xctool是用于編譯構(gòu)建和測(cè)試iOS坷备、Mac項(xiàng)目的工具熄浓,打開終端使用下面的命令安裝

brew install xctool

安裝gcovr

gcovr是一個(gè)將單元測(cè)試中的代碼覆蓋率以多種方式(包括列表方式、XML文件方式击你、HTML網(wǎng)頁方式等)展示出來的工具玉组,打開終端使用下面的命令安裝

brew install gcovr

xcpretty安裝

用于對(duì)xcodebuild的輸出進(jìn)行格式化。并包含輸出report功能丁侄。
github 鏈接

如果按照如下方法進(jìn)行安裝惯雳,一般會(huì)報(bào)錯(cuò),

$ gem install xcpretty

You don’t have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

因?yàn)檫@時(shí)你使用的是 Apple 家自帶的 ruby鸿摇,權(quán)限不夠石景,使用以下命令即可安裝完成,

$ sudo gem install xcpretty -n/usr/local/bin

這樣就成功了拙吉!

安裝Sonar-Scanner
Sonar-Scanner是Sonar中真正用于實(shí)現(xiàn)代碼掃描的工具,進(jìn)入 [Sonar-Scanner下載頁面](https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner)潮孽,如下圖所示
image

下載完成同樣需要解壓到/usr/local/文件夾下


二、環(huán)境變量與數(shù)據(jù)庫的配置

配置環(huán)境變量

打開終端筷黔,創(chuàng)建.bash_profile配置文件(已有文件可跳過此步驟)

touch ~/.bash_profile

編輯.bash_profile配置文件

vim ~/.bash_profile

下列代碼為我的配置往史,可直接復(fù)制到.bash_profile中使用,復(fù)制時(shí)注意修改安裝路徑與工具版本號(hào)為自己電腦上的情況

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.

export OCLINT_HOME=/usr/local/oclint-0.13
export PATH=$OCLINT_HOME/bin:$PATH

編輯完成后保存佛舱,并執(zhí)行下面的命令

source ~/.bash_profile

MySql數(shù)據(jù)庫配置
  • 創(chuàng)建sonar數(shù)據(jù)庫和用戶

打開終端椎例,輸入 mysql -u root -p 挨决,再輸入數(shù)據(jù)庫密碼,進(jìn)入MySql订歪,輸入下面sql語句創(chuàng)建sonar所需的數(shù)據(jù)庫脖祈。不知道數(shù)據(jù)庫初始密碼的可參考這篇文章

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; 
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;


三、配置SonarQube

配置SonarQube須在Java環(huán)境與MySql數(shù)據(jù)庫都已創(chuàng)建配置好的情況下進(jìn)行

配置sonar.properties

打開下面路徑的文件(sonar版本不同刷晋,路徑也會(huì)不同)

/usr/local/sonarqube-6.7.5/conf/sonar.properties

修改參數(shù)

sonar.jdbc.username=你的數(shù)據(jù)庫賬號(hào)(我的是root)
sonar.jdbc.password=你的數(shù)據(jù)庫密碼 (123)

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

sonar.jdbc.maxActive=60
sonar.jdbc.maxIdle=5
sonar.jdbc.minIdle=2
sonar.jdbc.maxWait=5000

sonar.jdbc.minEvictableIdleTimeMillis=600000
sonar.jdbc.timeBetweenEvictionRunsMillis=30000

修改完成保存后盖高,在終端中啟動(dòng)sonar

sudo /usr/local//sonarqube-6.7.5/bin/macosx-universal-64/sonar.sh start

完成啟動(dòng)后,打開瀏覽器訪問http://localhost:9000
若能看到下圖頁面則證明配置成功

image

安裝中文插件

SonarQube中的應(yīng)用市場(chǎng)中是有免費(fèi)的中文插件的眼虱,在配置成功后可以去下載喻奥,下載方式如下圖所示,圖中是已經(jīng)完成插件安裝的情況
image
安裝sonar-objective-c-plugin

由于SonarQube官方插件中支持OC語言的插件非常昂貴蒙幻,所以我這里使用的是來自GitHub上的一個(gè)開源插件映凳,將jar包復(fù)制到下面的路徑中,重啟sonar即安裝成功

/usr/local/sonarqube-6.7.5/extensions/plugins 


四邮破、本地項(xiàng)目中的配置

將文件 run-sonar.sh 與 sonar-project.properties 拷貝到待分析的項(xiàng)目目錄下诈豌,如下圖所示

image

配置sonar-project.properties文件
sonar.projectKey=你的項(xiàng)目名
sonar.projectName=你的項(xiàng)目名
sonar.projectVersion=顯示在sonar中的項(xiàng)目版本號(hào)

sonar.sources=代碼存放的文件夾

sonar.objectivec.project=項(xiàng)目名.xcodeproj
sonar.objectivec.workspace=項(xiàng)目名.xcworkspace

sonar.objectivec.appScheme=項(xiàng)目名

到這里本地項(xiàng)目中的配置就完成了。


五抒和、開始使用

在終端中使用cd命令進(jìn)入項(xiàng)目所在目錄矫渔,再執(zhí)行run-sonar.sh

cd /Users/chuqingqin/Desktop/項(xiàng)目文件夾
./run-sonar.sh

oc插件安裝,下載地址自找


image.png

開始執(zhí)行分析摧莽,如下圖所示
image

分析完成后庙洼,打開瀏覽器訪問http://localhost:9000,可看到項(xiàng)目分析報(bào)告

六镊辕、踩過的坑

1油够、不要使用Mysql8.0版本,因?yàn)槠渲械呐渲冒l(fā)生改變征懈,與SonarQube會(huì)有沖突石咬,導(dǎo)致無法啟動(dòng)SonarQube。

2卖哎、如果要更新sonar,請(qǐng)同時(shí)看看sonar-objective-c-plugin是否需要更新鬼悠,如果該插件作者未更新,則最好不要更新sonar亏娜,因?yàn)榭赡軐?dǎo)致OC插件不兼容焕窝。 插件下載地址

https://github.com/culeo/sonar-objective-c

作者的開源代碼中未提供jar包,需要下載下來后自行安裝maven環(huán)境運(yùn)行 build-and-deploy.sh 文件维贺,生成jar包它掂。

附件(run-sonar.sh)

#!/bin/bash
## INSTALLATION: script to copy in your Xcode project in the same directory as the .xcodeproj file
## USAGE: ./run-sonar.sh
## DEBUG: ./run-sonar.sh -v
## WARNING: edit your project parameters in sonar-project.properties rather than modifying this script
#

trap "echo 'Script interrupted by Ctrl+C'; stopProgress; exit 1" SIGHUP SIGINT SIGTERM

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
}

function readParameter() {
    
    variable=$1
    shift
    parameter=$1
    shift

    eval $variable="\"$(sed '/^\#/d' sonar-project.properties | grep $parameter | tail -n 1 | cut -d '=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\""
}

# Run a set of commands with logging and error handling
function runCommand() {

    # 1st arg: redirect stdout 
    # 2nd arg: command to run
    # 3rd..nth arg: args
    redirect=$1
    shift

    command=$1
    shift
    
    if [ "$nflag" = "on" ]; then
        # don't execute command, just echo it
        echo
        if [ "$redirect" = "/dev/stdout" ]; then    
            if [ "$vflag" = "on" ]; then
                echo "+" $command "$@"
            else
                echo "+" $command "$@" "> /dev/null"
            fi
        elif [ "$redirect" != "no" ]; then
            echo "+" $command "$@" "> $redirect"
        else
            echo "+" $command "$@"
        fi
        
    elif [ "$vflag" = "on" ]; then
        echo

        if [ "$redirect" = "/dev/stdout" ]; then    
            set -x #echo on
            $command "$@"
            returnValue=$?  
            set +x #echo off            
        elif [ "$redirect" != "no" ]; then
            set -x #echo on
            $command "$@" > $redirect
            returnValue=$?  
            set +x #echo off            
        else
            set -x #echo on
            $command "$@"
            returnValue=$?  
            set +x #echo off            
        fi
        
        if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
            stopProgress
            echo "ERROR - Command '$command $@' failed with error code: $returnValue"
            exit $returnValue
        fi
    else
        echo "--------------------------------"
        echo $command
        echo "$@"
        if [ "$redirect" = "/dev/stdout" ]; then    
            $command "$@" > /dev/null
        elif [ "$redirect" != "no" ]; then
            $command "$@" > $redirect
        else
            $command "$@"
        fi

        returnValue=$?
        if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
            stopProgress
            echo "ERROR - Command '$command $@' failed with error code: $returnValue"
            exit $?
        fi

    
        echo    
    fi  
}

## COMMAND LINE OPTIONS
vflag=""
nflag=""
oclint="on"
while [ $# -gt 0 ]
do
    case "$1" in
    -v) vflag=on;;
    -n) nflag=on;;
    -nooclint) oclint="";;      
    --) shift; break;;
    -*)
        echo >&2 "Usage: $0 [-v]"
        exit 1;;
    *)  break;;     # terminate while loop
    esac
    shift
done

# Usage OK
echo "Running run-sonar.sh..."

# 檢查依賴是否已經(jīng)安裝 xcpretty and oclint
testIsInstalled xcpretty
testIsInstalled oclint

# 檢查有沒有 sonar-project.properties 文件
if [ ! -f sonar-project.properties ]; then
    echo >&2 "ERROR - No sonar-project.properties in current directory"; exit 1;
fi

# 從 sonar-project.properties 讀出參數(shù)

# .xcworkspace/.xcodeproj filename
workspaceFile=''; readParameter workspaceFile 'sonar.objectivec.workspace'
projectFile=''; readParameter projectFile 'sonar.objectivec.project'
# 源文件
srcDirs=''; readParameter srcDirs 'sonar.sources'
# Scheme
appScheme=''; readParameter appScheme 'sonar.objectivec.appScheme'
# test 
testScheme=''; readParameter testScheme 'sonar.objectivec.testScheme'

if [ "$vflag" = "on" ]; then
    echo "Xcode workspace file is: $workspaceFile"
    echo "Xcode project file is: $projectFile"
    echo "Xcode application scheme is: $appScheme"
    echo "Xcode test scheme is: $testScheme"
fi

# 檢查必須參數(shù)
if [ -z "$projectFile" -o "$projectFile" = " " ]; then
    if [ ! -z "$workspaceFile" -a "$workspaceFile" != " " ]; then
        echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties. You must specify which projects (comma-separated list) are application code within the workspace $workspaceFile."
    else
        echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties (name of your .xcodeproj)"
    fi
    exit 1
fi
if [ -z "$srcDirs" -o "$srcDirs" = " " ]; then
    echo >&2 "ERROR - sonar.sources parameter is missing in sonar-project.properties. You must specify which directories contain your .h/.m source files (comma-separated list)."
    exit 1
fi
if [ -z "$appScheme" -o "$appScheme" = " " ]; then
    echo >&2 "ERROR - sonar.objectivec.appScheme parameter is missing in sonar-project.properties. You must specify which scheme is used to build your application."
    exit 1
fi

## SCRIPT

# Create sonar-reports/ for reports output

if [[ ! (-d "sonar-reports") && ("$nflag" != "on") ]]; then
    if [ "$vflag" = "on" ]; then
        echo 'Creating directory sonar-reports/'
    fi
    mkdir sonar-reports
    if [[ $? != 0 ]] ; then
        stopProgress
        exit $?
    fi
fi

# Extracting project information needed later
echo 'Extracting Xcode project information'
xcodebuild clean

export LC_ALL="en_US.UTF-8"
if [[ "$workspaceFile" != "" ]] ; then
    echo "xcodebuild clean"
    xcodebuild clean -workspace "${workspaceFile}" -scheme "${appScheme}" -sdk iphonesimulator -configuration Release
    echo "xcodebuild analyze"
    xcodebuild -workspace "${workspaceFile}" -scheme "${appScheme}" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' ONLY_ACTIVE_ARCH=NO -configuration build | tee xcodebuild.log | xcpretty -r json-compilation-database --output compile_commands.json
else
    echo "xcodebuild clean"
    xcodebuild clean -project "${projectFile}" -scheme "${appScheme}" -configuration Release
    echo "xcodebuild analyze"
    xcodebuild -project "${projectFile}" -scheme "${appScheme}" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' ONLY_ACTIVE_ARCH=NO -configuration build | tee xcodebuild.log | xcpretty -r json-compilation-database --output compile_commands.json
fi


if [ "$oclint" = "on" ]; then

    # OCLint
    echo -n 'Running OCLint...'

    # Build the --include flags
    currentDirectory=${PWD##*/}
    includedCommandLineFlags=""
    echo "$srcDirs" | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
    while read word; do
        includedCommandLineFlags+=" --include .*/${currentDirectory}/${word}"
    done < tmpFileRunSonarSh
    rm -rf tmpFileRunSonarSh
    if [ "$vflag" = "on" ]; then
        echo
        echo -n "Path included in oclint analysis is:$includedCommandLineFlags"
    fi
    
    # Run OCLint with the right set of compiler options
    maxPriority=10000
    oclint-json-compilation-database -- -max-priority-1 $maxPriority -max-priority-2 $maxPriority -max-priority-3 $maxPriority -rc LONG_LINE=150 -report-type pmd -o sonar-reports/oclint.xml

else
    echo 'Skipping OCLint (test purposes only!)'
fi

# SonarQube
echo 'Running SonarQube using SonarQube Scanner'
sonar-scanner

rm -rf sonar-reports
rm -rf build
rm -rf compile_commands.json
rm -rf xcodebuild.log
rm -rf .scannerwork

exit 0


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市溯泣,隨后出現(xiàn)的幾起案子群发,更是在濱河造成了極大的恐慌晰韵,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熟妓,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡栏尚,警方通過查閱死者的電腦和手機(jī)起愈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來译仗,“玉大人抬虽,你說我怎么就攤上這事∽菥” “怎么了阐污?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長咱圆。 經(jīng)常有香客問我笛辟,道長,這世上最難降的妖魔是什么序苏? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任手幢,我火速辦了婚禮,結(jié)果婚禮上忱详,老公的妹妹穿的比我還像新娘围来。我一直安慰自己,他們只是感情好匈睁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布监透。 她就那樣靜靜地躺著,像睡著了一般航唆。 火紅的嫁衣襯著肌膚如雪胀蛮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天佛点,我揣著相機(jī)與錄音醇滥,去河邊找鬼。 笑死超营,一個(gè)胖子當(dāng)著我的面吹牛鸳玩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播演闭,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼不跟,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了米碰?” 一聲冷哼從身側(cè)響起窝革,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤购城,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后虐译,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瘪板,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年漆诽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了侮攀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡厢拭,死狀恐怖兰英,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情供鸠,我是刑警寧澤畦贸,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站楞捂,受9級(jí)特大地震影響薄坏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜泡一,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一颤殴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鼻忠,春花似錦涵但、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至塑娇,卻和暖如春澈侠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背埋酬。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國打工嵌器, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拼坎,地道東北人芦瘾。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓富纸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親珍特。 傳聞我的和親對(duì)象是個(gè)殘疾皇子祝峻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

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