From 584bec1694824abb05dc4803cd9a5cf7780fe876 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 10 Jun 2022 21:32:01 +0200 Subject: [PATCH] Add script to sync sonar properties with package.json MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .gitignore | 1 + .release-it.json | 2 +- package.json | 1 + sonar-project.properties | 2 +- src/scripts/updateSonarProps.sh | 49 +++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100755 src/scripts/updateSonarProps.sh diff --git a/.gitignore b/.gitignore index b1263465..271df212 100644 --- a/.gitignore +++ b/.gitignore @@ -93,6 +93,7 @@ Thumbs.db *.mta mta_archives/ +*.bak .eslintcache temp/ *.tar.gz diff --git a/.release-it.json b/.release-it.json index 05c40080..3ffb0521 100644 --- a/.release-it.json +++ b/.release-it.json @@ -13,6 +13,6 @@ "releaseName": "Version ${version}" }, "hooks": { - "after:bump": "npx auto-changelog -p -u" + "after:bump": "npx auto-changelog -p -u && npm run sonar:properties" } } diff --git a/package.json b/package.json index 056fc72c..60f08e96 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "git:sdiff": "git diff && git submodule foreach 'git diff'", "git:supdate": "git submodule update --remote --recursive --merge", "git:spush": "git push --recurse-submodules=on-demand", + "sonar:properties": "src/scripts/updateSonarProps.sh", "release": "release-it" }, "dependencies": { diff --git a/sonar-project.properties b/sonar-project.properties index e4c1dc98..6a39fbde 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -3,7 +3,7 @@ sonar.organization=jerome-benoit # This is the name and version displayed in the SonarCloud UI. sonar.projectName=e-mobility-charging-stations-simulator -sonar.projectVersion=1.0.0 +sonar.projectVersion=1.1.61 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. #sonar.sources=. diff --git a/src/scripts/updateSonarProps.sh b/src/scripts/updateSonarProps.sh new file mode 100755 index 00000000..d29ed6ab --- /dev/null +++ b/src/scripts/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