From 2195cfce131ee599c71d0c426a7eea70ed52345f Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Wed, 23 Oct 2024 16:01:36 +0200
Subject: Inject version with embed instead of build flags

The build flags made the basic build and debug calls quite complex and
bound this strictly to Git repositories which is a problem if the source
code is passed around as tar archive.

Embedding this via Go's embed package and a VERSION.txt file makes this
way easier. A simple `go build` command is sufficient to build ceres.

To make sure the version string is extended by a `-devel` marker and
that this marker is removed before a release a RELEASE.md file was added
as a checklist for releases.
---
 Makefile    | 6 ++----
 RELEASE.md  | 8 ++++++++
 VERSION.txt | 1 +
 version.go  | 4 +++-
 4 files changed, 14 insertions(+), 5 deletions(-)
 create mode 100644 RELEASE.md
 create mode 100644 VERSION.txt

diff --git a/Makefile b/Makefile
index b9d5d51..0d8853e 100644
--- a/Makefile
+++ b/Makefile
@@ -2,16 +2,14 @@ TARGET_EXEC := ceres
 BUILD_DIR := ./build
 MODULE_NAME := xengineering.eu/ceres
 
-VERSION := $(shell git describe --dirty --always)
-
 all: $(BUILD_DIR)/$(TARGET_EXEC)
 
 $(BUILD_DIR)/$(TARGET_EXEC):
-	go build -ldflags "-X main.version=$(VERSION)" -o $@ $(MODULE_NAME)
+	go build -o $@ $(MODULE_NAME)
 
 .PHONY: debug
 debug:
-	go run -race -ldflags "-X main.version=$(VERSION)" $(MODULE_NAME) -e
+	go run -race $(MODULE_NAME) -e
 
 .PHONY: tests
 tests:
diff --git a/RELEASE.md b/RELEASE.md
new file mode 100644
index 0000000..b403a31
--- /dev/null
+++ b/RELEASE.md
@@ -0,0 +1,8 @@
+# Release checklist
+
+- remove `-devel` from VERSION.txt
+- update CHANGELOG.md
+- `go test ./...` must succeed
+- release with `git tag -u <user> -m 'Release version <version>' 'v<version>'
+  with version as <major>.<minor>.<patch> (e.g. 0.2.3)
+- remove `-devel` from VERSION.txt
diff --git a/VERSION.txt b/VERSION.txt
new file mode 100644
index 0000000..9e39250
--- /dev/null
+++ b/VERSION.txt
@@ -0,0 +1 @@
+0.4.0-devel
diff --git a/version.go b/version.go
index f9a997e..087666e 100644
--- a/version.go
+++ b/version.go
@@ -2,12 +2,14 @@ package main
 
 import (
 	"log"
+	_ "embed"
 )
 
+//go:embed VERSION.txt
 var version string
 
 func init() {
 	if version == "" {
-		log.Fatal("No version information passed to this via build flags")
+		log.Fatal("Bad build: No version string embedded.")
 	}
 }
-- 
cgit v1.2.3-70-g09d2