From 8a695dc293556e9dff366db7196e7446f0210fe7 Mon Sep 17 00:00:00 2001
From: Julien Girard <julien.girard2@cea.fr>
Date: Tue, 2 Jul 2024 17:15:00 +0200
Subject: [PATCH] [wip] A bit cleaner version update script

---
 .gitlab-ci.yml    |  2 +-
 Makefile          |  6 ++----
 doc/conf.py       |  2 +-
 update_version.sh | 26 +++++++++++++++++++++-----
 4 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 239321d..63a9404 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,5 +1,5 @@
 variables:
-  CAISAR_VERSION: "2.0.0"
+  CAISAR_VERSION: "2.0"
   TAG: "2.0.0"
 
 default:
diff --git a/Makefile b/Makefile
index 6d27436..ec4358d 100644
--- a/Makefile
+++ b/Makefile
@@ -36,21 +36,19 @@ view-doc: doc
 ###################################
 
 PROJECT_ID=1082
-TAG=$(shell dune-release tag -f --yes > /dev/null 2>&1 && git describe --tags --abbrev=0)
 PKG="caisar-$(TAG).tbz"
 REG_VERSION=$(cat VERSION)
 PACKAGE_URL="https://git.frama-c.com/api/v4/projects/$(PROJECT_ID)/packages/generic/caisar/$(TAG)/$(PKG)"
 DESCRIPTION="$(shell sed -n -e "p;n;:next;/^##/Q;p;n;b next" CHANGES.md | perl -pe 's/\n/\\n/')"
 
 release:
-	@echo "Are git tag $(TAG) and VERSION file $(REG_VERSION) the same? (y/n)?"
+	@echo "Read $(REG_VERSION) from VERSION file. Proceed to release CAISAR with this version? (y/n)?"
 	@read yesno; test "$$yesno" = y
 	./update_version.sh
 	@echo -n $(DESCRIPTION)
 	@echo "Is the CHANGES.md correct for $(TAG) (y/n)?"
 	@read yesno; test "$$yesno" = y
-	@echo $(TAG) > VERSION
-	dune-release tag $(TAG)
+	dune-release tag $(REG_VERSION)
 	dune-release distrib --skip-build --skip-lint
 	curl --header "PRIVATE-TOKEN: $(GITLAB_TOKEN)" \
 	--upload-file _build/$(PKG) \
diff --git a/doc/conf.py b/doc/conf.py
index 44356ae..acb529a 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -29,7 +29,7 @@ author = 'The CAISAR Development Team'
 # built documents.
 #
 # The short X.Y version.
-version = '2.0.0'
+version = '2.0'
 # The full version, including alpha/beta/rc tags.
 release = '2.0.0'
 
diff --git a/update_version.sh b/update_version.sh
index a5e376e..9c26d3d 100755
--- a/update_version.sh
+++ b/update_version.sh
@@ -1,19 +1,35 @@
 #!/usr/bin/env bash
+# set -xe
 
-set -xe
+if ! test -f VERSION; then
+  echo "This script must be run from CAISAR root directory."
+  exit 2
+fi
 
 # Update headers in source files to match current year
 NEW_DATE=$(date "+%Y")
 OLD_DATE=$(date -d '1 year ago' "+%Y")
 find . \( -type d -name .git -prune \) \( -type d -name ._build -prune \) -o -type f -print0 | xargs -0 sed -i 's/(C) '${OLD_DATE}'/(C) '${NEW_DATE}'/g'
 
-# Update version in manual, ci and main.ml according to the VERSION file
+# Update version in manual, ci and dune-project according to the VERSION file
 VERSION=$(cat VERSION)
-sed -E -i 's/let version = "([0-9]\.)+[0-9]" in/let version = "2.0.0" in/g' src/main.ml
-sed -E -i 's/version = \x27([0-9]\.)+[0-9]\x27/version = \x27'${VERSION}'\x27/g' doc/conf.py
+MAJOR=$(echo "$VERSION" | awk -F '.' '{print $1}')
+MINOR=$(echo "$VERSION" | awk -F '.' '{print $2}')
+REV=$(echo "$VERSION" | awk -F '.' '{print $3}')
+MAJOR_MINOR=${MAJOR}.${MINOR}
+
+echo "Updating CAISAR version number to $VERSION".
+
+sed -E -i 's/let version = "([0-9]\.)+[0-9]" in/let version = "'${VERSION}'" in/g' src/main.ml
+echo "Changing version in CAISAR's manual."
+sed -E -i 's/version = \x27([0-9]\.)+[0-9]\x27/version = \x27'${MAJOR_MINOR}'\x27/g' doc/conf.py
 sed -E -i 's/release = \x27([0-9]\.)+[0-9]\x27/release = \x27'${VERSION}'\x27/g' doc/conf.py
-sed -E -i 's/CAISAR_VERSION: "([0-9]\.)+[0-9]"/CAISAR_VERSION: "'${VERSION}'"/g' .gitlab-ci.yml
+echo "Changing version in CAISAR's CI."
+sed -E -i 's/CAISAR_VERSION: "([0-9]\.)+[0-9]"/CAISAR_VERSION: "'${MAJOR_MINOR}'"/g' .gitlab-ci.yml
 sed -E -i 's/TAG: "([0-9]\.)+[0-9]"/TAG: "'${VERSION}'"/g' .gitlab-ci.yml
+echo "Changing version in dune-project."
+sed -E -i 's/version ([0-9]\.)+[0-9]/version '${MAJOR_MINOR}'/g' dune-project
 
 # Update version in tests
+echo "Changing version in dune tests."
 sed -E -i 's/([0-9]\.)+[0-9]/'${VERSION}'/g' tests/help.t
-- 
GitLab