|
| 1 | +#!/bin/bash -xe |
| 2 | + |
| 3 | +#------------------------------------------------------------------------------ |
| 4 | +# Sample build script for SonarQube analysis |
| 5 | +# |
| 6 | +# This script should be run from the root of your project (usually along side |
| 7 | +# your *.xcodeproj). It contains commands to produce all the different types |
| 8 | +# of reports that can be imported into SonarQube. |
| 9 | +#------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +TEST_SCHEME_NAME='MyTestScheme' |
| 12 | +APP_TARGET_NAME='MyApplication' |
| 13 | +SOURCES_DIR='MyApplication' |
| 14 | +TEST_SOURCES_DIR='MyApplicationTests' |
| 15 | + |
| 16 | +# Set the version of Xcode to use with xcodebuild |
| 17 | +export DEVELOPER_DIR=/Applications/Xcode7.0.1.app/Contents/Developer |
| 18 | + |
| 19 | +# Corresponding SDK version to build with that is available with the Xcode version you're using |
| 20 | +SDK_VERSION=9.0 |
| 21 | + |
| 22 | +# Allows snapshot's reset_simulators command to work without prompting the user for confirmation |
| 23 | +export SNAPSHOT_FORCE_DELETE=true |
| 24 | + |
| 25 | +# Clean |
| 26 | +rm -rf compile_commands.json build/ DerivedData/ |
| 27 | + |
| 28 | +# Recreate output dirs |
| 29 | +mkdir -p build/logs |
| 30 | +mkdir -p build/reports/cobertura |
| 31 | +mkdir -p build/reports/junit |
| 32 | +mkdir -p build/reports/lizard |
| 33 | +mkdir -p build/reports/oclint |
| 34 | + |
| 35 | +# For informational purposes |
| 36 | +xcodebuild -version |
| 37 | +xcodebuild -showsdks |
| 38 | +xcodebuild -list |
| 39 | + |
| 40 | +# Fresh simulator state for tests |
| 41 | +killall "iOS Simulator" "iPhone Simulator" "Simulator" || true |
| 42 | +snapshot reset_simulators |
| 43 | + |
| 44 | +# Will check xcodebuild for failures when piped into xcpretty |
| 45 | +set -o pipefail |
| 46 | + |
| 47 | +# Run tests |
| 48 | +xcodebuild \ |
| 49 | + -scheme "${TEST_SCHEME_NAME}" \ |
| 50 | + -sdk "iphonesimulator${SDK_VERSION}" \ |
| 51 | + -destination "OS=${SDK_VERSION},name=iPhone 6" \ |
| 52 | + -destination-timeout 600 \ |
| 53 | + -configuration Release \ |
| 54 | + clean test \ |
| 55 | + ONLY_ACTIVE_ARCH=NO \ |
| 56 | + -enableCodeCoverage YES \ |
| 57 | + -derivedDataPath DerivedData \ |
| 58 | + | tee build/logs/xcodebuild-test.log \ |
| 59 | + | xcpretty -r junit -o "build/reports/junit/TESTS-${TEST_SCHEME_NAME}.xml" |
| 60 | + |
| 61 | +# Build and run Clang analysis |
| 62 | +# You may need to add extra arguments such as "CODE_SIGN_IDENTITY=iPhone Distribution: My Cert (ABC123)" |
| 63 | +xcodebuild \ |
| 64 | + -target "${APP_TARGET_NAME}" \ |
| 65 | + -sdk "iphoneos${SDK_VERSION}" \ |
| 66 | + -configuration Release \ |
| 67 | + clean analyze \ |
| 68 | + CONFIGURATION_BUILD_DIR=build \ |
| 69 | + | tee build/logs/xcodebuild-analyze.log \ |
| 70 | + | xcpretty -r json-compilation-database -o compile_commands.json |
| 71 | + |
| 72 | +# Done with xcpretty, so we can disable pipefail |
| 73 | +set +o pipefail |
| 74 | + |
| 75 | +if which lizard |
| 76 | +then |
| 77 | + # Run Lizard report for complexity |
| 78 | + lizard \ |
| 79 | + --xml \ |
| 80 | + "${SOURCES_DIR}" \ |
| 81 | + > 'build/reports/lizard/lizard.xml' |
| 82 | +fi |
| 83 | + |
| 84 | +if which slather |
| 85 | +then |
| 86 | + # Run Slather for coverage with Xcode 7 or later |
| 87 | + slather \ |
| 88 | + coverage \ |
| 89 | + --cobertura-xml \ |
| 90 | + --output-directory build/reports/cobertura \ |
| 91 | + --input-format profdata \ |
| 92 | + --build-directory DerivedData \ |
| 93 | + --source-directory . \ |
| 94 | + --ignore '../**' \ |
| 95 | + --ignore "${TEST_SOURCES_DIR}/**" \ |
| 96 | + MyApplication.xcodeproj |
| 97 | +fi |
| 98 | + |
| 99 | +MAX_PRIORITY=10000 |
| 100 | +oclint-json-compilation-database \ |
| 101 | + --include "${SOURCES_DIR}/.*" \ |
| 102 | + -- \ |
| 103 | + -max-priority-1 "${MAX_PRIORITY}" \ |
| 104 | + -max-priority-2 "${MAX_PRIORITY}" \ |
| 105 | + -max-priority-3 "${MAX_PRIORITY}" \ |
| 106 | + -report-type pmd \ |
| 107 | + -o 'build/reports/oclint/oclint.xml' |
| 108 | + |
| 109 | +cat << _EOF > sonar-project.properties |
| 110 | +sonar.projectName=MyApplication |
| 111 | +sonar.projectKey=com.example:my-application |
| 112 | +sonar.projectVersion=1.0 |
| 113 | +
|
| 114 | +sonar.language=objectivec |
| 115 | +sonar.sourceEncoding=UTF-8 |
| 116 | +
|
| 117 | +sonar.sources=${SOURCES_DIR} |
| 118 | +
|
| 119 | +sonar.tests=${TEST_SOURCES_DIR} |
| 120 | +
|
| 121 | +sonar.objectivec.junit.reportsPath=build/reports/junit |
| 122 | +sonar.objectivec.cobertura.reportPath=build/reports/cobertura/cobertura.xml |
| 123 | +sonar.objectivec.lizard.reportPath=build/reports/lizard/lizard.xml |
| 124 | +sonar.objectivec.clang.reportsPath=$(find build -type d -name StaticAnalyzer)/MyApplication/${APP_TARGET_NAME}/normal/armv7 |
| 125 | +sonar.objectivec.oclint.reportPath=build/reports/oclint/oclint.xml |
| 126 | +_EOF |
| 127 | + |
| 128 | +sonar-runner |
0 commit comments