Skip to content

About

SonarQube plugin for Swift / Objective-C

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

CI

Future Mind fork of insideapp-fr/sonar-apple (LGPL-3.0). It targets current SonarQube releases and merges fixes from other forks and open upstream PRs. See CHANGELOG.md for what differs from upstream 0.5.1.

SonarQube plugin for Swift / Objective-C

A plugin to enable analysis of Swift and Objective-C code quality and security.

Let us know if you want to get involved.

Features

The plugin is designed to support Swift 5 syntax. Swift 6 code is analysed too: highlighting, size and duplication work, but complexity can be off for newer syntax (typed throws, ~Copyable, macros...).

Feature Tool(s) Availability
Tests Xcode Swift, Objective-C
Coverage Xcode Swift, Objective-C
Complexity SonarQube Swift, Objective-C
Dead code Periphery Swift
Size SonarQube Swift, Objective-C
Syntax highlighting SonarQube Swift, Objective-C
Issues SwiftLint, OCLint Swift, Objective-C
Security mobsfscan Swift, Objective-C

Installation

Server-side

SonarQube Community Build 26.6+ (or another edition with plugin API 13.7+) is required. The scanner needs Java 17+. Scanners only download the plugin for projects that contain Swift or Objective-C files.

  • Download the plugin binary into the $SONARQUBE_HOME/extensions/plugins directory.
  • Restart the server.
  • Activate the rules in your Quality Profiles.

Client-side

Xcode 13+ and SonarScanner are required. The following tools are optional:

Sonar configuration

Create a sonar-project.properties file at the root with this content:

# Project identification
sonar.projectKey=ios_app
sonar.projectName=iOS App
sonar.projectVersion=1.0
	
# Source code location.
# Path is relative to the sonar-project.properties file. Defaults to .
# Use commas to specify more than one folder.
sonar.sources=iOSApp
# Tests source code location.
# Path is relative to the sonar-project.properties file. Defaults to empty.
# Use commas to specify more than one folder.
sonar.tests=iOSAppTests

## Coverage & Tests ##

# Path to the Xcode result bundle file. 
# The path is relative to the project base directory.
# Defaults to build/result.xcresult
#sonar.apple.resultBundlePath=custom/path/to/file.xcresult

## Periphery ##

# Index Store folder path.
# This matches the parameter "-derivedDataPath" in xcodebuild (see below).
# Warning: starting Xcode 14 the folder "Index" is renamed "Index.noindex".
sonar.apple.periphery.indexStorePath=derivedData/Index.noindex/DataStore

# Path to the Xcode project or workspace used to create the Index Store.
sonar.apple.periphery.projectPath=MyApp.xcworkspace

# One or more comma-separated Xcode schemes included in the Index Store.
sonar.apple.periphery.schemes=MyApp

# Set to true to disable the Periphery sensor (e.g. Periphery is not installed on the runner).
#sonar.apple.periphery.skip=true

## OCLint ##

# Path to the JSON Compilation Database folder
# The path is relative to the project base directory.
# Defaults to build/json_compilation_database
# sonar.apple.jsonCompilationDatabasePath=custom/path/to/folder

## Misc ##

# Encoding of the source code. Default is default system encoding.
sonar.sourceEncoding=UTF-8

For a complete list of available options, please refer to the SonarQube documentation.

Run analysis

Use the following commands from the root folder to start an analysis:

# Don't forget to add -workspace to the build command if your project is part of a workspace
# Don't forget to activate 'Gather coverage' option in the app scheme or add '-enableCodeCoverage YES' to the following command

# Run tests 
xcrun xcodebuild \
  -project MyApp.xcodeproj \
  -scheme MyApp \
  -sdk iphonesimulator \
  -destination 'platform=iOS Simulator,name=iPhone 11 Pro' \
  -derivedDataPath ./derivedData \
  -resultBundlePath build/result.xcresult \
  OTHER_CFLAGS="\$(inherited) -gen-cdb-fragment-path build/compilation_database" \
  -quiet \
  clean test

# Run the analysis and publish to the SonarQube server
# Don't forget to specify `sonar.host.url` and `sonar.token` in `sonar-project.properties` or supply it to the following command.
sonar-scanner

Advanced configuration

Importing pre-generated reports

By default the plugin runs each tool itself during the analysis. If your pipeline already ran a tool, point the plugin at its JSON output instead (comma-separated paths, relative to the project base directory). The tool is then not run again.

Property Expected content
sonar.apple.swiftLint.reportPaths swiftlint lint --reporter json
sonar.apple.periphery.reportPaths periphery scan --format json
sonar.apple.mobsfscan.reportPaths mobsfscan --json <sources>
sonar.apple.coverage.reportPaths xcrun xccov view --archive --json <bundle>.xcresult
sonar.apple.warnings.reportPaths xcrun xcresulttool get --legacy --format json --path <bundle>.xcresult

Missing report files are logged and skipped, they do not fail the analysis.

Troubleshooting

When a tool cannot run, the analysis log says why, e.g. Could not start 'periphery'. Is it installed and on the PATH of the scanner?, a timeout, or the tool's exit code and stderr. Run the scanner with -X for stack traces.

Periphery

The plugin can pass the project or workspace and schemes directly to Periphery through sonar.apple.periphery.projectPath and sonar.apple.periphery.schemes. Alternatively, you can keep this information in the Periphery configuration file. When both are present, the scanner properties are passed as command-line options and take precedence.

workspace: path/to/workspace.xcworkspace # optional
project: path/to/project.xcodeproj
schemes:
  - MyScheme
targets:
  - MyTarget

OCLint

On macOS, the system will block usage of OCLint. In order to get rid of the manual verification of each of them, use the following commands:

sudo xattr -dr com.apple.quarantine /usr/local/lib/oclint/rules/lib*
sudo xattr -dr com.apple.quarantine /usr/local/lib/oclint/reporters/lib*

Sonar Scanner

If you have trouble running the Sonar Scanner, you can run it in verbose mode, to get more logs and information. You can either:

  • add sonar.verbose=true to your sonar-project.properties
  • add the option X to the command, like so: sonar-scanner -X ...

Contributing

Any help is welcome, and PRs will be greatly appreciated!

Have a look at the developer guide to get started.

License

This plugin is released under the GNU LGPL v3 license. See the LICENSE file for more information.

About

SonarQube plugin for Swift / Objective-C

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages