1

With gnatpro 19.1, I'm trying to add gnathub to my project and am wondering how to set dynamically Project_Version as in:

package Dashboard is
   for Project_Version use @git --describe --tags@; -- this needs to be updated.
end Dashboard;

I can't think of any simple way to do it.

A solution would be to use a Makefile that would configure a .gpr.in file but it seems contrived to change my whole buildchain just to add a version to the sonar config.

A simple, not automated solution, is to call the project with another switch:

gnathub -P Foo.gpr --plugins sonar-config,sonar-scanner\
 --targs:sonar-scanner -Dsonar.projectVersion=$(git describe --tags)

But this is not really usable.

Similar question is to add the option -Dsonar.branch.name=$(git branch). AFAICT, the package Dashboard, as per the documentation has no Switch switch.

Is there any solution other than passing the extra arguments or forking gnatdashboard?

Vser
  • 578
  • 4
  • 18
  • 2
    gnathub? gnatdashboard? Are you by any chance a supported customer? if so, best use the (excellent, IME) support from AdaCore that you’re paying for – Simon Wright Jul 09 '20 at 18:49
  • Right, I’ll get in touch with AdaCore on this topic. – Vser Jul 15 '20 at 12:46
  • As per AdaCore advice, using a build tool like Makefile is the recommended solution. Shall I let the question open? Close it, post this comment as an answer? – Vser Jul 23 '20 at 12:41
  • I’d post this comment as an answer (perhaps slightly expanded to hint at _how_ you’ll use the Makefile) – Simon Wright Jul 23 '20 at 16:56

1 Answers1

0

The best solution seems to reside in automating this configuration with a tool like Make.

For example, one can define the following Makefile:

# This target runs all the plugins listed
# in the section Dashboard.plugins of your project’s gpr
# sonar-config and sonar-scanner shall not be listed therein.
analyzes:
    gnathub -P project

# This uses gnathub API to get the object dir where sonar-config file will be generated
OBJECT_DIR = $(shell gnathub -P project --exec object_dir.py 2>/dev/null | tail -n 1)
SONAR_PROPERTIES = $(OBJECT_DIR)/gnathub/sonar/sonar-project.properties

PROJECT_VERSION = $(shell git describe --tags)
BRANCH_NAME = $(shell git rev-parse --abbrev-ref HEAD)
# Uses gnathub to generate sonar properties file.
# Replaces the projectVersion and add branch name
# (notice that, on sonar, the branch name shall not be specified on your "master" branch)
$(SONAR_PROPERTIES): analyzes
    gnathub -P project --plugins sonar-config --incremental
    @sed -i "s/\(sonar.projectVersion = \).*/\1$(PROJECT_VERSION)/" $@
ifneq ($(BRANCH_NAME), master)
    @echo "sonar.branch.name = $(BRANCH_NAME)" >> $@
endif
    
sonar: $(SONAR_PROPERTIES)
    gnathub -P project --plugins sonar-scanner --incremental

.PHONY: sonar analyzes

Where object_dir.py is:

#!/usr/bin/env python
import GNAThub;
print (GNAThub.Project.object_dir());

Then:

$make sonar

Would run the analyzes and update them with the correct version and branch name (if necessary) to the SonarQube server.

Vser
  • 578
  • 4
  • 18