The last part of the “Continuous Integration for iOS” series covers the configuration of code metrics. Running metrics are a great tool to analyse the quality of the code on continuous level. And this helps to keep the project clean, maintainable and working. This post covers not all of the available metrics, but at the end I linked a few more static analysis tools that can be integrated in the workflow. It’s up to you how much data you want to collect and what you really need. Please always consider that every metric will take its time. Maybe run the most relevant metrics as a new commit is detected and all metrics only once a day at night.
Let’s see how to configure code coverage, lines of code counting, a warnings detector and code duplication.
Add code coverage
- Install Cobertura-Plugin to show cobertura code coverage reports.
- Install gcovr with Homebrew to generate cobertura reports.
$ brew install gcovr
- Generate the code coverage files
xctool -project ${PROJECT_NAME}.xcodeproj \ -scheme JenkinsTest \ -sdk iphonesimulator \ -configuration Debug \ -derivedDataPath $BUILD_DIRECTORY\ test \ GCC_GENERATE_DEBUGGING_SYMBOLS=YES \ GCC_GENERATE_TEST_COVERAGE_FILES=YES \ GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES
- Generate the code coverage report.
BUILD_DIRECTORY=~/Desktop/Jenkins_Build COVERAGE_DIRECTORY=$BUILD_DIRECTORY/Build/Intermediates/JenkinsTest.build/Debug-iphonesimulator/JenkinsTest.build/Objects-normal/i386 cd $COVERAGE_DIRECTORY gcovr —object-directory . —exclude ‘.*Tests.*’ —xml > ${WORKSPACE}/reports/ios-code-coverage.xml
- Add post-build action, choose Publish Cobertura Coverage Report and add
**/reports/ios-code-coverage.xml
.
Add LOC (lines of code) counting
- Install SLOCCount Plugin.
- Install cloc with Homebrew.
$brew install cloc
- Add Build-Step and choose Execute shell.
cloc $PROJECT_NAME —by-file —skip-uniqueness —xml —out=${WORKSPACE}/reports/cloc.xml
- Add post-build action, choose Publish SLOCCount analysis results and add
**/reports/cloc.xml
.
Add warnings detector
- Install warnings detector.
- Add post-build action, choose Scan for compiler warnings and use the “Clang (LLVM based)” parser.
Add CPD (code duplication detection)
- Download PMD in version 4.2.5 — the version is important, version 5.1. did not work for me.
- Install Objective-C language definition package for PMD.
- Install DRY plugin.
- Generate CPD output.
java -Xmx512m \ -classpath /usr/local/Cellar/pmd/4.2.5/lib/pmd-4.2.5.jar:/usr/local/Cellar/pmd/ObjCLanguage-0.0.7-SNAPSHOT.jar net.sourceforge.pmd.cpd.CPD \ -minimum-tokens 1 \ -files JenkinsTest \ -language ObjectiveC \ -encoding UTF-8 \ -format net.sourceforge.pmd.cpd.XMLRenderer > ${WORKSPACE}/reports/duplicated-code.xml
-
Add post-build action, choose Publish duplicate code analysis results and add
**/reports/duplicated-code.xml
.
After all the setup and configuration hopefully everything runs great and this post was somehow helpful. Especially the metrics stuff needs a lot of fine tuning to be really meaningful. But very often the graph alone gives an idea in which direction the code/project is developing.
In the end here are some paths, links and resources that may help the one or the other.
Paths
- Jenkins WAR file:
/usr/local/jenkins
or/usr/local/Cellar//jenkins/libexec
- Jenkins home directory:
~/.jenkins
Links & Resources
- Apple Developer Video – Building from the command line
- Xcode Build Setting Reference
- Jenkins Setup on Os X server
- Optimise code coverage
- Clang Static Analyzer
- OCLint
- Facebook Infer – Static analysis tool