Sonarqube Download For Mac



Sonarqube Download For Mac

Automatically reviews code style, security, duplication, complexity, and coverage on every. What I didn’t like though, is the fact that Visual Studio for Mac is still missing SonarQube integration. In order to run the sonar, I had to open the terminal and run it from there. After all, I’ve decided to write a script that can run Sonar automatically.

If you have landed up here, I assume you have already made your choice of picking SonarQube as your code analysis tool. And what a choice I must say! Nevertheless, to strengthen your faith in SonarQube even further, here are some key code quality check points provided by SonarQube, which make it a desirable essential of a swift project:

  • Architecture & Design
  • Complexity
  • Duplications
  • Coding Rules
  • Potential Bugs
  • Unit Tests

SonarQube addresses not just bugs but also the above six axes of quality which are neatly projected on a dashboard.

For more insights, visit https://www.sonarqube.org/ if you haven’t already!

We are going to bring the awesomeness of SonarQube in our swift projects with the help of a plugin called sonar-swift. Sonar-swift is an open source initiative for Swift language support in SonarQube and its structure is based on the sonar-objective-c plugin [2].

We are going to try our hand at integrating this with a swift project through this blog.

So brace yourselves, or not, because it is way simpler than it seems! Let’s go.

PREREQUISITES

  1. A Mac with Xcode 7 or +
  2. JDK
  3. A swift project for code analysis
  4. Swift project should have a test target. Even if it is empty and has no tests.
    Add Target -> iOS Unit Testing Target -> Scheme setup -> Done

Sonarqube Download For Eclipse

SETUP

If the above prerequisites are in place, we can begin our adventure with the following guide:

1. SonarQube server setup

Sonarqube scanner download

Before integrating SonarQube in our Swift project, we need to make sure our SonarQube server is up and running locally. Follow the below steps for this [3] :

a. Download and unzip the sonar distribution from here.

NOTE: Make sure your unzipped folder is in the same path as your swift project.

b. Open terminal and run the following command to start the Sonar server:
/path/to/sonar/distribution/directory/bin/macosx-universal-64/sonar.sh start

NOTE: To give execute permissions to the above script, open Terminal and type chmod 755 /path/to/script . Instead of typing the full path, you can drag the script onto the Terminal window from Finder. Once this is done, you will be able to execute the script.

c. Log in to http://localhost:9000 with System Administrator credentials (admin/admin) and generate a project key for your first project. You can give any name you like.

ACHIEVEMENT: Sonar server is up and running and you can now move forward with the setup.

2. SonarQube Scanner download

      1. Download the SonarQube Scanner for Mac OS X 64 bit from the here. [4]
      2. Expand the downloaded file into the directory where you added your sonar distribution folder. Let’s call it – install directory
      3. Update the global settings to point to your SonarQube server by editing /path/to/install/directory/conf/sonar-scanner.properties:

        #—– Default SonarQube server
        #sonar.host.url=http://localhost:9000

        NOTE: You can open the .properties file using xcode itself.

      4. Add the /path/to/install/directory/bin directory to your system paths. To do that, open a new Terminal window and type the following [5] :
        1. sudo nano /etc/paths
        2. Enter your password, when prompted.
        3. Go to the bottom of the file, and enter the path you wish to add.
        4. Hit control-x to quit.
        5. Enter “Y” to save the paths.
      5. You can verify your installation by opening a new terminal window and executing the command sonar-scanner -h. You should get the help menu for sonar-scanner.

ACHIEVEMENT: Sonar-scanner setup successfully done. Now you have an engine which will scan your code for the quality check.

3. Xcpretty installation

Open a new terminal and do the following to install xcpretty with a required fix [2]:

      1. git clone https://github.com/Backelite/xcpretty.git
      2. cd xcpretty
      3. git checkout fix/duration_of_failed_tests_workaround
      4. gem build xcpretty.gemspec
      5. sudo gem install –both xcpretty-0.2.2.gem

ACHIEVEMENT: You have just made a provision to have a meaningful output for your build because xcpretty is a tool designed to format xcodebuild’s output, and make it human readable.

4. Install SwiftLint – Version 0.3.0 or above

SwiftLint is a tool supported by the team of Realm.io to lint your Swift code, verifying that it conforms to a set of rules syntactic rules defined by you [7]. Run the following command in Terminal to install swift lint.

brew install swiftlint

Sonarqube plugin download

NOTE: If homebrew is not already installed. Do the following to install homebrew:

ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

5. Install Tailor – Version 0.11.1 or above

Tailor is a static analysis and lint tool for source code written in Swift programming language which analyzes your code to ensure consistent styling and help avoid bugs [8]. Run the following command to install tailor.

brew install tailor

6. Install slather – Version 2.1.0 or above (2.4 since Xcode 8.3)

Slather is a great Ruby tool that can convert gcc coverage data to various other formats which helps generate test coverage reports for Xcode projects [9]. To install slather, run the following command in Terminal.

gem install slather

7. Install lizard

Lizard is an extensible Cyclomatic Complexity Analyzer for many imperative programming languages including Swift [10]. To install lizard, run the following command in Terminal.

sudo pip install lizard

NOTE: To install pip run the following:

sudo easy_install pip

Sonarqube scanner download for mac

8. Add support for Swift in Sonar

Sonarqube Scanner Download

      1. Download the latest Swift plugin from here.
      2. Then, move the .jar to the plugins folder where the SonarQube distribution server has been installed – /path/to/sonar/distribution/folder/extensions/plugins.

ACHIEVEMENT: You have successfully added the sonar-swift plugin in your sonar distribution.

9. Add the Swift Sonar script to your swift project

Add run-sonar-swift.sh in your swift project path. It can be downloaded from here.

10. Restart Sonar Server

Now restart the Sonar server in order to apply the plugin and enable support for Swift. Run the following in Terminal to do so:

Sonarqube Scanner Download For Mac

/path/to/sonar/distribution/folder/bin/macosx-universal-64/sonar.sh restart

ACHIEVEMENT: We are done with the server setup part. But we still need to configure our project to gather data to feed Sonar for analysis.

11. Configure your Swift project

Download and add sonar-project.properties file beside the .xcodeproj file, in your Xcode project root folder. It can be downloaded from here.

NOTE: To directly download individual files from GitHub repo [6] :

      1. Click the file name in a GitHub repo.
      2. Click Raw to display the file contents.
      3. Copy the URL in your browser.
      4. Open terminal and run: curl -LJO https://URL/from/stepc/

12. Update properties file

Sonarqube Scanner Download For Mac

Open the above file in xcode and update this file to configure it as per your Swift project by setting keys such as sonar.projectKey, sonar.projectName, sonar.sources, sonar.swift.workspace, sonar.swift.appScheme, etc. Basically fill all the required settings. Refer the below screenshot of a sonar-project.properties file.

Sonarqube Download For Mac

Figure 1: Screenshot of a sonar-project.properties file

13. Start your code analysis

Time to run our script run-sonar-swift.sh and see the analysis in motion.

Sonarqube Scanner For Msbuild Download

        1. Open terminal and cd to the swift project folder
        2. /path/to/run-sonar-swift.sh/
        3. Hit enter
        4. The analysis will begin

Sonarqube Download For Windows

ACHIEVEMENT: Once the analysis is complete, it will create a folder called sonar-reports in the project directory where the code analysis reports will be stored.

Sonarqube Java 8

Figure 2: Screenshot of Swift project folder structure

14. Dashboard Analysis

Open localhost:9000 in your browser to see the SonarQube code analysis results on the dashboard. The following screenshots of the dashboard screen show an overview of the quality axes and quality gate result for the project, a graphical analysis of various parameters like security, maintainability, reliability of the code and a list of issues as per their severity level.

Figure 3: Screenshot of the overview of the quality axes of the project – App

  • Figure 4: Screenshot of a list of issues discovered after code analysis

    Figure 5: Screenshot of the graphical analysis of code maintainability

    ACHIEVEMENT: You have successfully analysed your code for bugs, vulnerabilities and code smells, gauging it on parameters like reliability, maintainability, coverage, duplications, security, etc. And all this with the help of sonar-swift plugin for SonarQube.

    Hope this helped !

    References

    [1] http://blog.novoda.com/using-sonar-in-swift/

    [2] https://github.com/Backelite/sonar-swift

    [3] https://docs.sonarqube.org/display/SONAR/Get+Started+in+Two+Minutes

    [4] https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

    [5] https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.WrpCQNNubjF

    [6] https://stackoverflow.com/questions/4604663/download-single-files-from-github

    [7] theiostimes.com/advent-calendar/swiftlint.html

    [8] https://tailor.sh/

    [9] https://github.com/SlatherOrg/slather

    [10] https://github.com/terryyin/lizard