Add code sonarlint configuration
[poolifier.git] / updateSonarProps.sh
CommitLineData
3ffada52
JB
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#==============================================================================
15echo "Updating the SonarQube properties..."
16
17# Get the project name from package.json
18PACKAGE_NAME=$(cat package.json \
19 | grep name \
20 | head -1 \
21 | awk -F: '{ print $2 }' \
22 | sed 's/[",]//g' \
23 | tr -d '[[:space:]]')
24echo "Project: ${PACKAGE_NAME}"
25
26# Get the version from package.json
27PACKAGE_VERSION=$(cat package.json \
28 | grep version \
29 | head -1 \
30 | awk -F: '{ print $2 }' \
31 | sed 's/[",]//g' \
32 | tr -d '[[:space:]]')
33echo "Version: ${PACKAGE_VERSION}"
34
35# Get the Sonar properties file
36SONAR_FILE=$(find . -iname sonar*.properties -type f)
37echo "Sonar file: ${SONAR_FILE}"
38
39SED_EXTRA_OPTS="-i.bak"
40
41# Update the version
42REPLACE='^sonar.projectVersion=.*$'
43WITH="sonar.projectVersion=${PACKAGE_VERSION}"
44sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE}
45
46# Update the project name
47REPLACE='^sonar.projectName=.*$'
48WITH="sonar.projectName=${PACKAGE_NAME}"
49sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE}