SonarQube Integration with iOS — Swift
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"
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.
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/
Lizard
sudo pip install lizard
Setting up SonarQube
Step 1. Setup SonarQube & SonarScanner
- 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.
- Unzip downloaded file.
- Move downloaded folder under /Applications.
Step 2. Updating .bash_profile with new path
- 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
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
- Download sonar-project.properties file from this URL and put this file in your project root directory.
- Update the project, schema & folder details as per your project.
- Download the run-sonar-swift.sh from this url and put this file anywhere(like Documents , desktop)
- Now open terminal and scanner using script
/User/Documents/run-sonar-swift.sh -v
‘EXECUTION SUCCESS’ shows that all the tools run successful and report is uploaded on the server.
Step 5. See Sonar report
- Open the url http://localhost:9000/projects in any browser.
- Login using username: admin & password: admin
- You will see your project here.
Note:
- Before applying the sonar on main project run it on Demo first. It will insures you that your sonar set is completely fine.
- 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