SonarQube Integration with iOS — Swift

MANVENDRA SINGH RATHOR
4 min readOct 3, 2022

--

SonarQube logo

Why SonarQube?

SonarQube® is an automatic code review tool to detect bugs, vulnerabilities, and code smells in your code. It can integrate with your existing workflow to enable continuous code inspection across your project branches and pull requests.

You can checkout the of SonarQube website to get more about it, Here we discuss about the configuration of SonarQube in iOS(Swift) project.

Steps to Integrate

To run the SonarQube we need some other tools like Java, Swift Lint, OCLint.. So we installed them first then Setup the Sonar for swift.

Install Tools

To install some tool we use Home-brew, so we first install it.(Run all these commands on terminal)

Home-brew

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
Enter your system password
Next steps:
Run these two commands to add Homebrew to your PATH:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/xxx/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

JAVA

The first step is to install the Java in your mac.

Download the macOS x64 DMG Installer(jdk-11.0.15_osx-x64_bin.dmg) from the official website of Oracle.

JDK 11.0.15

Open the dmg follow the instruction, if all went well you will land on this screen.

Xcpretty

sudo gem install xcpretty

SwiftLint

brew install SwiftLint

Tailor

brew install tailor

slather

sudo gem install slather

OCLint

brew tap oclint/formulae
brew install oclint

Xcpretty Fix

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

Install Python

https://www.python.org/downloads/release/python-3107/
Install Python

Lizard

sudo pip install lizard

Setting up SonarQube

Step 1. Setup SonarQube & SonarScanner

  1. Download SonarQube from the drive (you can also download from https://www.sonarqube.org/downloads/) but in the drive file I made some changes & also Backlite for SonarSwift is integrated in zip.
  2. Unzip downloaded file.
  3. Move downloaded folder under /Applications.
Application folder

Step 2. Updating .bash_profile with new path

  1. Start Terminal and run following command.
open -e ~/.bash_profile

2. Above commands will open your bash_profile in editor

3. Copy & paste following lines.

# Sonar Setting
export PATH=$PATH:/Applications/SonarScanner/bin
export PATH=$PATH:/Applications/SonarQube/bin

4.Save the file

Step 3. Run Sonar Server

sh /Applications/SonarQube/bin/macosx-universal-64/sonar.sh console
Sonar Server is Up

If you see the “SonarQube is up” then sonar setup done perfectly. Now leave this terminal as it is and open another terminal to run sonar scanner.

Step 4. Run Sonar Scanner

  1. Download sonar-project.properties file from this URL and put this file in your project root directory.
  2. Update the project, schema & folder details as per your project.
  3. Download the run-sonar-swift.sh from this url and put this file anywhere(like Documents , desktop)
  4. Now open terminal and scanner using script
/User/Documents/run-sonar-swift.sh -v
Scanner run successfully

‘EXECUTION SUCCESS’ shows that all the tools run successful and report is uploaded on the server.

Step 5. See Sonar report

  1. Open the url http://localhost:9000/projects in any browser.
  2. Login using username: admin & password: admin
  3. You will see your project here.
Project List
Overview
Issues

Note:

  1. Before applying the sonar on main project run it on Demo first. It will insures you that your sonar set is completely fine.
  2. Depending upon the system and project you may face different errors, Please ping me on Linked in I will try to answer them.

Git Reference

https://github.com/Idean/sonar-swift

--

--