How to use Sonarqube in Swift projects
Issue #216
Install Sonarqube
https://docs.sonarqube.org/latest/setup/get-started-2-minutes/
- Download Sonarqube for macOS https://www.sonarqube.org/downloads/
- Put it in
~/sonarqube
- Run localhost server
~/sonarqube/bin/macosx-universal-64/sonar.sh console
- Login
http://localhost:9000
withadmin/admin
- Create new project
Install Sonar scanner
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
- Download for macOS 64 bit
- Put it in
~/sonarscanner
- export
PATH=$PATH:/Users/khoa/sonarscanner/bin
- Go to project, create
sonar-project.properties
1 | # must be unique in a given SonarQube instance |
- Run
sonar-scanner
Install swift plugin
https://github.com/Backelite/sonar-swift
- Download jar file https://github.com/Backelite/sonar-swift/releases
- Follow https://docs.sonarqube.org/latest/setup/install-plugin/
- Put jar file into
~/sonarqube/extensions/plugins
- Restart server
- Copy run-sonar-swift.sh https://github.com/Backelite/sonar-swift/blob/develop/sonar-swift-plugin/src/main/shell/run-sonar-swift.sh to project folder
- Go to Quality Profile http://localhost:9000/profiles?language=swift
Skip some tools
Modify run-sonar-swift.sh
1 | vflag="" |
Git ignore
1 | .scannerwork/ |
run-sonar-swift.sh
- Update sonar-project.properties
1 | sonar.swift.appScheme=MyApp Staging |
- Run
chmod +x run-sonar-swift.sh
- Run
./run-sonar-swift.sh
Troubleshooting
failed with error code: 64 https://github.com/Backelite/sonar-swift/issues/222
When run ./run-sonar-swift.sh
1 | xcodebuild: error: ''MyApp.xcodeproj'' does not exist. |
👉 Remove quotes in sonar-project.properties
👉 Modify run-sonar-swift.sh, add these before Check for mandatory parameters
section
Surround by double quotes
1 | projectFile="\"$projectFile\"" |
😢 Does not work
👉 Need to create a scheme name without space
Error code 65
👉Specify team in Xcode project
destination
Need double quotes
destinationSimulator="\"$destinationSimulator\""
Use equal sign =
1 | -destination="$destinationSimulator" |
instead of space ‘ ‘
1 | -destination "$destinationSimulator" |
Metric ‘files’ should not be computed by a Sensor
When run sonar-scanner
https://github.com/Backelite/sonar-swift/issues/212
1 | 11:01:14.406 INFO: Sensor JaCoCo XML Report Importer [jacoco] |
slather coverage –input-format profdata –cobertura-xml –output-directory sonar-reports –workspace MyApp.xcworkspace –scheme MyAppStaging MyApp.xcodeproj
1 |
|
14:53:23.251 ERROR: Error during SonarQube Scanner execution
org.sonarsource.scanner.api.internal.ScannerException: Unable to execute SonarQube
at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.lambda$createLauncher$0(IsolatedLauncherFactory.java:85)
1 |
|
Error during SonarQube Scanner execution
java.lang.ClassCastException: com.backelite.sonarqube.swift.complexity.LizardReportParser$SwiftFunction cannot be cast to org.sonar.api.batch.fs.internal.DefaultInputComponent
1 |
|
lizard –xml sonar-reports/lizard-report.xml
```