Continuous Integration for iOS – Code metrics

| 0 comments

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

  1. Install Cobertura-Plugin to show cobertura code coverage reports.
  2. Install gcovr with Homebrew to generate cobertura reports.
    $ brew install gcovr
  3. 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
    
  4. 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
    
  5. Add post-build action, choose Publish Cobertura Coverage Report and add **/reports/ios-code-coverage.xml.
Code Coverage Report

Code Coverage Report

 

Add LOC (lines of code) counting

  1. Install SLOCCount Plugin.
  2. Install cloc with Homebrew.
    $brew install cloc
  3. Add Build-Step and choose Execute shell.
    cloc $PROJECT_NAME —by-file —skip-uniqueness —xml —out=${WORKSPACE}/reports/cloc.xml
  4. Add post-build action, choose Publish SLOCCount analysis results and add **/reports/cloc.xml.
Lines of code

Lines of code

 

Add warnings detector

  1. Install warnings detector.
  2. Add post-build action, choose Scan for compiler warnings and use the “Clang (LLVM based)” parser.
Warnings in files

Warnings in files

Warning categories

Warning categories

 

Add CPD (code duplication detection)

  1. Download PMD in version 4.2.5 — the version is important, version 5.1. did not work for me.
  2. Install Objective-C language definition package for PMD.
  3. Install DRY plugin.
  4. 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
    
  5. 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

Leave a Reply

Required fields are marked *.