From 3ffada528b1ca9f062534bf09dd9772d1b2d4673 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 11 Jun 2022 08:28:29 +0200 Subject: [PATCH] Add a script to sync sonar properties MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .gitignore | 1 + package.json | 1 + sonar-project.properties | 2 +- updateSonarProps.sh | 49 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100755 updateSonarProps.sh diff --git a/.gitignore b/.gitignore index 8ddca4be..8cf99a8d 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ build/config.gypi .history/ .scannerwork lib +*.bak diff --git a/package.json b/package.json index 5e921e8c..5012ac2a 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "lint": "eslint . --cache", "lint:fix": "eslint . --cache --fix", "typedoc": "typedoc", + "sonar:properties": "./updateSonarProps.sh", "prepublishOnly": "npm run build:prod" }, "repository": { diff --git a/sonar-project.properties b/sonar-project.properties index fbb18042..4907570e 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -2,7 +2,7 @@ sonar.projectKey=pioardi_poolifier sonar.organization=pioardi sonar.javascript.lcov.reportPaths=coverage/lcov.info sonar.projectName=poolifier -sonar.projectVersion=2.0.0 +sonar.projectVersion=2.2.0 sonar.host.url=https://sonarcloud.io sonar.sources=src sonar.tests=tests diff --git a/updateSonarProps.sh b/updateSonarProps.sh new file mode 100755 index 00000000..d29ed6ab --- /dev/null +++ b/updateSonarProps.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +#title : updateSonarProps.sh +#description : +# This script parses the project's name and version from its package.json and automagically +# updates the version and package name in the SonarQube configuration properties file. +# It can be used as a pre step before running the sonar-scanner command +# +#prerequisites : NodeJS based project with package.json, sonar*.properties file in the cwd +#author : Christian-André Giehl +#modified by : Daniel Duarte +#date : 20180220 +#version : 1.1 +#usage : sh updateSonarProps.sh +#============================================================================== +echo "Updating the SonarQube properties..." + +# Get the project name from package.json +PACKAGE_NAME=$(cat package.json \ + | grep name \ + | head -1 \ + | awk -F: '{ print $2 }' \ + | sed 's/[",]//g' \ + | tr -d '[[:space:]]') +echo "Project: ${PACKAGE_NAME}" + +# Get the version from package.json +PACKAGE_VERSION=$(cat package.json \ + | grep version \ + | head -1 \ + | awk -F: '{ print $2 }' \ + | sed 's/[",]//g' \ + | tr -d '[[:space:]]') +echo "Version: ${PACKAGE_VERSION}" + +# Get the Sonar properties file +SONAR_FILE=$(find . -iname sonar*.properties -type f) +echo "Sonar file: ${SONAR_FILE}" + +SED_EXTRA_OPTS="-i.bak" + +# Update the version +REPLACE='^sonar.projectVersion=.*$' +WITH="sonar.projectVersion=${PACKAGE_VERSION}" +sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE} + +# Update the project name +REPLACE='^sonar.projectName=.*$' +WITH="sonar.projectName=${PACKAGE_NAME}" +sed $SED_EXTRA_OPTS -e "s/${REPLACE}/${WITH}/g" ${SONAR_FILE} -- 2.34.1