Skip to content

Commit e55fd2d

Browse files
committed
Makefile: automatically determine the bundle server version
Add a 'VERSION-FILE' target that generates a value for the Makefile 'VERSION' using 'git describe'. The process for generating this file is based off of the 'GIT-VERSION-FILE' target in the Git project's Makefile, with some minor changes for the sake of efficiency. If the 'VERSION' is set by the caller (e.g. with 'make VERSION=1.0.0'), the 'VERSION-FILE' target is a .PHONY (i.e., non-file, always run) target. Conversely, if 'VERSION' is not set, 'VERSION-FILE' is instead a real file target and is loaded into the Makefile with '-include VERSION-FILE'. Its recipe runs the script 'generate-version.sh' to determine a version and write 'VERSION := <string>' to 'VERSION-FILE' *if and only if* its contents change. Finally, the 'VERSION-FILE' target depends on a .PHONY 'FORCE' target, ensuring it is always re-run. If the contents of 'VERSION-FILE' change as a result of 'generate-version.sh', the full 'Makefile' is re-evaluated. This ensures that 'VERSION' is up-to-date with the contents of 'VERSION-FILE'. Finally, compile the 'VERSION' into 'git-bundle-server' by setting the appropriate '-ldflags'. Signed-off-by: Victoria Dye <[email protected]>
1 parent c7f5edd commit e55fd2d

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/git-bundle-server
22
/git-bundle-web-server
3+
/VERSION-FILE
34
/bin/
45
/_dist/
56
/_docs/

Makefile

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Default target
22
build:
33

4-
# Project metadata (note: to package, VERSION *must* be set by the caller)
4+
# Project metadata
5+
# By default, the project version is automatically determined using
6+
# 'git describe'. If you would like to set the version manually, set the
7+
# 'VERSION' variable to the desired version string.
58
NAME := git-bundle-server
6-
VERSION :=
7-
PACKAGE_REVISION := 1
89

910
# Installation information
1011
INSTALL_ROOT := /
@@ -22,6 +23,7 @@ GOARCH := $(shell go env GOARCH)
2223
# Packaging information
2324
SUPPORTED_PACKAGE_GOARCHES := amd64 arm64
2425
PACKAGE_ARCH := $(GOARCH)
26+
PACKAGE_REVISION := 1
2527

2628
# Guard against environment variables
2729
APPLE_APP_IDENTITY =
@@ -30,12 +32,29 @@ APPLE_KEYCHAIN_PROFILE =
3032
E2E_FLAGS=
3133
INTEGRATION_FLAGS=
3234

35+
# General targets
36+
.PHONY: FORCE
37+
38+
ifdef VERSION
39+
# If the version is set by the user, don't bother with regenerating the version
40+
# file.
41+
.PHONY: VERSION-FILE
42+
else
43+
# If the version is not set by the user, we need to generate the version file
44+
# and load it.
45+
VERSION-FILE: FORCE
46+
@scripts/generate-version.sh --version-file="$@"
47+
-include VERSION-FILE
48+
endif
49+
3350
# Build targets
51+
LDFLAGS += -X '$(shell go list -m)/cmd/utils.Version=$(VERSION)'
52+
3453
.PHONY: build
3554
build:
3655
$(RM) -r $(BINDIR)
3756
@mkdir -p $(BINDIR)
38-
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) ./...
57+
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) -ldflags "$(LDFLAGS)" ./...
3958

4059
.PHONY: doc
4160
doc:
@@ -197,6 +216,7 @@ endif
197216
.PHONY: clean
198217
clean:
199218
go clean ./...
219+
$(RM) -r VERSION-FILE
200220
$(RM) -r $(BINDIR)
201221
$(RM) -r $(DISTDIR)
202222
$(RM) -r $(DOCDIR)

scripts/generate-version.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
die () {
3+
echo "$*" >&2
4+
exit 1
5+
}
6+
7+
write_version () {
8+
if [ -n "$1" ]; then
9+
echo "Setting VERSION to $1"
10+
else
11+
echo "No version found!"
12+
fi
13+
echo "VERSION := $1" >"$2"
14+
}
15+
16+
# Parse script arguments
17+
for i in "$@"
18+
do
19+
case "$i" in
20+
--version-file=*)
21+
VERSION_FILE="${i#*=}"
22+
shift # past argument=value
23+
;;
24+
*)
25+
die "unknown option '$i'"
26+
;;
27+
esac
28+
done
29+
30+
if [ -z "$VERSION_FILE" ]; then
31+
die "error: missing version file path"
32+
fi
33+
34+
set -e
35+
36+
# The pattern match used below is *less* restrictive than it should be (i.e.
37+
# "v<number>.<number>.<number>[-<optional string>]"), but we're limited by the
38+
# glob patterns used in 'git describe'.
39+
#
40+
# That said, we're explicitly specifying 'VERSION' for formal releases(based on
41+
# the more precise pattern match in 'release.yml', so it's not super important
42+
# to be strict here.
43+
VERSION_RAW="$(git describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" 2>/dev/null || echo "")"
44+
VERSION="${VERSION_RAW:1}"
45+
46+
if [ -r "$VERSION_FILE" ]
47+
then
48+
CACHE_VERSION=$(sed -e 's/^VERSION := //' <"$VERSION_FILE")
49+
if [ "$VERSION" != "$CACHE_VERSION" ]; then
50+
write_version "$VERSION" "$VERSION_FILE"
51+
fi
52+
else
53+
# If the file doesn't exist, write unconditionally
54+
write_version "$VERSION" "$VERSION_FILE"
55+
fi

0 commit comments

Comments
 (0)