Sonar-iOS-插件安装及Xcode配置扫描

一、安装SonarQube Community版本的服务器

SonarQube官网 https://www.sonarqube.org ,按照Sonar-iOS-SonarQube服务器安装中一步步开始进行服务器的搭建操作。


二、Sonar-Swift插件的安装

  1. 在Github上的Sonar-Swift,去下载最新的jar包。
  2. 将下载的jar放到SonarQube服务器的extensions/plugins文件夹下。
  3. 重启SonarQube服务器。


三、客户端扫描软件安装

客户端扫描代码只需要安装这部分软件即可

1、安装sonar-scannner

该软件用于上传报告到SonarQube服务器。
下载 sonar-scanner,将下载的文件解压缩到本地目录,并且将目录下的bin目录加到系统的环境变量PATH中,以便在命令行中可以访问该命令。

2、下载run-sonar-swift.sh脚本

该脚本是用于执行扫描的主入口。
下载后将该文件放到固定的目录下,并将该目录加入到系统的环境变量PATH中。

3、安装sonar-swift依赖的所有软件

3.1、xcpretty,需要安装一个修复问题版本的xcpretty
git clone https://github.com/Backelite/xcpretty.git
cd xcpretty
git checkout fix/duration_of_failed_tests_workaround
gem build xcpretty.gemspec
sudo gem install --both xcpretty-0.2.2.gem

如果报如下错误,表示没有写入/usr/bin的权限,修改安装路径即可:

>sudo gem install --both xcpretty-0.2.2.gem
ERROR:  While executing gem ... (Gem::FilePermissionError)
   You don't have write permissions for the /usr/bin directory.

执行如下命令:

>sudo gem install -n /usr/local/bin --both xcpretty-0.2.2.gem 

    在OSX系统中,/usr/bin属于系统级别的保护,任何人不能写入,包括root也一样,是绝对禁止写入的,唯一的办法就是像你上面的那样,安装到/usr/local/bin里面去。
    为什么要这么严格?因为/usr/bin这个目录太敏感了,是任何病毒都想要攻克的地方,所以苹果对此做了严格的禁止。

3.2、SwiftLint,Swift的静态代码分析工具
brew install swiftlint

安装过程如下:

>brew install swiftlint
==> Downloading https://homebrew.bintray.com/bottles/swiftlint-0.38.0.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/a2/a2b39e51661fc24d201788391ce4f9c68007c818857f423161b37bc51f7fcb0b?__gd
######################################################################## 100.0%
==> Pouring swiftlint-0.38.0.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/swiftlint/0.38.0: 6 files, 7.9MB
3.3、Tailor,针对Swift的跨平台静态分析工具
brew install tailor

正常安装过程如下:

>brew install tailor
==> Downloading https://github.com/sleekbyte/tailor/releases/download/v0.12.0/tailor-0.12.0.tar
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/36204628/4d7049d8-1261-11e7-889c-b4
######################################################################## 100.0%
🍺  /usr/local/Cellar/tailor/0.12.0: 41 files, 4.0MB, built in 19 seconds

此工具依赖JDK,如果JDK没有安装,会报如下错误,建议先安装JDK

>brew install tailor
tailor: Java 1.8+ is required to install this formula.
Install AdoptOpenJDK with Homebrew Cask:
  brew cask install adoptopenjdk
Error: An unsatisfied requirement failed this build.
3.4、slather,单元测试覆盖率检测工具
gem install slather

如果提示没有权限,则执行如下命令:
sudo gem install slather

如果还是报没有权限,则执行如下命令:
sudo gem install -n /usr/local/bin slather

安装过程如下:

>sudo gem install -n /usr/local/bin slather
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.3
Fetching: slather-2.4.7.gem (100%)
Successfully installed slather-2.4.7
Parsing documentation for nokogiri-1.10.3
Installing ri documentation for nokogiri-1.10.3
Parsing documentation for slather-2.4.7
Installing ri documentation for slather-2.4.7
Done installing documentation for nokogiri, slather after 9 seconds
2 gems installed
3.5、 lizard,复杂度检测工具

3.5.1 使用pip安装lizard
由于会有权限问题,所以先将工具安装到当前用户下

pip install lizard --user

如果提示如下错误,说明本地还没有安装python,需要先安装

>pip install lizard --user
zsh: command not found: pip

安装pip命令之后,再次执行lizard安装命令

pip是python的包管理工具,在Python2.7的安装包中,easy_install.py是默认安装的,而pip需要我们手动安装

>sudo easy_install pip

>pip install lizard --user

正常安装过程如下:

 >pip install lizard --user
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting lizard
  Downloading https://files.pythonhosted.org/packages/24/7a/7ca04c3d84998d7344caafd3534a1b8a80d293bf32afbd2ef3956282d1c2/lizard-1.16.6-py2.py3-none-any.whl (56kB)
     |████████████████████████████████| 61kB 326kB/s 
Installing collected packages: lizard
  WARNING: The script lizard is installed in '/Users/hushijun/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed lizard-1.16.6

3.5.2 将用户的python安装包目录~/Library/Python/2.7加到系统变量PATH中

3.6、OCLint,Objective-C的静态代码分析工具
brew tap oclint/formulae
brew install oclint

安装oclint报如下错误:

 >brew install oclint
==> Installing oclint from oclint/formulae
Error: Your Xcode (10.1) is too outdated.
Please update to Xcode 10.2.1 (or delete it).
Xcode can be updated from the App Store.

更新brew和oclint
参考homebrew中的oclint文档

$ brew update
$ brew upgrade oclint

查看brew缓存目录:
$ brew --cache

正常安装过程如下:

>brew tap oclint/formulae
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.

==> Tapping oclint/formulae
Cloning into '/usr/local/Homebrew/Library/Taps/oclint/homebrew-formulae'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
Tapped 1 formula (32 files, 29.4KB).

>brew install oclint
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
wordgrinder

==> Installing oclint from oclint/formulae
==> Downloading https://github.com/oclint/oclint/releases/download/v0.13/oclint-0.13-x86_64-darwin-16.7.0.tar.gz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/7159640/07e87f60-9c5f-11e7-8590-f3a
######################################################################## 100.0%
🍺  /usr/local/Cellar/oclint/0.13: 374 files, 86.2MB, built in 50 seconds


四、客户端扫描配置

添加sonar-project.properties文件到项目的根目录下,配置sonar-project.properties

在进行单元测试覆盖率检测的时候,需要对要扫描的target的进行Test相关配置
1、用到Cocoapods的项目,要将项目中依赖的库,都要放到Test的target中去。
2、对选中的Scheme进行编辑(Edit Scheme), 最终做到如下效果。

image.png

3、配置对应Test的Target。

A、在Host Application部分一定要选择你要进行扫描的target。同时下部的复选框要选中Allow testing Host Application API。
如果是私有库framework,则不需要选择,只有真正的APP项目才需要选择。
B、在签名部分要选择对的Team和签名环境。
取消自动签名,手动配置签名。

私有库framework的单元测试,不需要选择Host Application部分,如下图:

image.png

APP项目的单元测试需要选择,如下图:

image.png

4、build setting 配置
sonar 扫描需要把framework的Target做如下配置:

mach-o type 修改成dynamic library
defines module 修改成 yes

如下图所示:


扫描


五、客户端sonar扫描

1、终端进入xcode项目根目录
2、执行命令

run-sonar-swift.sh

如果是本地扫描,需要启动本地数据库和sonar服务器:

//启动mysql数据库
mysql.server start

//启动sonar服务器
sonar.sh start

//开始扫描
run-sonar-swift.sh

//如果需要单独将扫描结果上传到指定服务器
1、修改sonar-project.properties配置中的
sonar.host.url
2、执行命令:
sonar-scanner


六、 参考文档

  1. OCLint: http://docs.oclint.org/en/stable/contents.html
  2. SonarQube & sonar-scanner: https://docs.sonarqube.org/display/SCAN/
  3. slather: https://github.com/SlatherOrg/slather
  4. Lizard: https://github.com/terryyin/lizard
  5. xcpretty: https://github.com/supermarin/xcpretty
  6. SwiftLint: https://github.com/realm/SwiftLint
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341