2 #title : updateSonarProps.sh
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
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>
13 #usage : sh updateSonarProps.sh
14 #==============================================================================
15 echo "Updating the SonarQube properties..."
17 # Get the project name from package.json
18 PACKAGE_NAME
=$
(cat package.json \
21 |
awk -F: '{ print $2 }' \
23 |
tr -d '[[:space:]]')
24 echo "Project: ${PACKAGE_NAME}"
26 # Get the version from package.json
27 PACKAGE_VERSION
=$
(cat package.json \
30 |
awk -F: '{ print $2 }' \
32 |
tr -d '[[:space:]]')
33 echo "Version: ${PACKAGE_VERSION}"
35 # Get the Sonar properties file
36 SONAR_FILE
=$
(find .
-iname sonar
*.properties
-type f
)
37 echo "Sonar file: ${SONAR_FILE}"
39 SED_EXTRA_OPTS
="-i.bak"
42 REPLACE
='^sonar.projectVersion=.*$'
43 WITH
="sonar.projectVersion=${PACKAGE_VERSION}"
44 sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE}
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}