| 1 | #!/usr/bin/env bash |
| 2 | #title : updateSonarProps.sh |
| 3 | #description : |
| 4 | # This script parses the project's name and version from its package.json and automagically |
| 5 | # updates the version and package name in the SonarQube configuration properties file. |
| 6 | # It can be used as a pre step before running the sonar-scanner command |
| 7 | # |
| 8 | #prerequisites : NodeJS based project with package.json, sonar*.properties file in the cwd |
| 9 | #author : Christian-André Giehl <christian@emailbrief.de> |
| 10 | #modified by : Daniel Duarte <danieldd.ar@gmail.com> |
| 11 | #date : 20180220 |
| 12 | #version : 1.1 |
| 13 | #usage : sh updateSonarProps.sh |
| 14 | #============================================================================== |
| 15 | echo "Updating the SonarQube properties..." |
| 16 | |
| 17 | # Get the project name from package.json |
| 18 | PACKAGE_NAME=$(cat package.json \ |
| 19 | | grep name \ |
| 20 | | head -1 \ |
| 21 | | awk -F: '{ print $2 }' \ |
| 22 | | sed 's/[",]//g' \ |
| 23 | | tr -d '[[:space:]]') |
| 24 | echo "Project: ${PACKAGE_NAME}" |
| 25 | |
| 26 | # Get the version from package.json |
| 27 | PACKAGE_VERSION=$(cat package.json \ |
| 28 | | grep version \ |
| 29 | | head -1 \ |
| 30 | | awk -F: '{ print $2 }' \ |
| 31 | | sed 's/[",]//g' \ |
| 32 | | tr -d '[[:space:]]') |
| 33 | echo "Version: ${PACKAGE_VERSION}" |
| 34 | |
| 35 | # Get the Sonar properties file |
| 36 | SONAR_FILE=$(find . -iname sonar*.properties -type f) |
| 37 | echo "Sonar file: ${SONAR_FILE}" |
| 38 | |
| 39 | SED_EXTRA_OPTS="-i.bak" |
| 40 | |
| 41 | # Update the version |
| 42 | REPLACE='^sonar.projectVersion=.*$' |
| 43 | WITH="sonar.projectVersion=${PACKAGE_VERSION}" |
| 44 | sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE} |
| 45 | |
| 46 | # Update the project name |
| 47 | REPLACE='^sonar.projectName=.*$' |
| 48 | WITH="sonar.projectName=${PACKAGE_NAME}" |
| 49 | sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE} |