Skip to content

Commit

Permalink
Calculate version stamp at build time. (#7)
Browse files Browse the repository at this point in the history
Inspects git tags to find the largest stamped version that is an ancestor of the current commit. That is used as the version for the build.
  • Loading branch information
marstr authored Feb 7, 2019
1 parent c3fa723 commit 01fd937
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ TEST_SRC = $(shell find . -name '*_test.go' -type f)

# Define the current git revision being packed into each of the build products.
REVISION = $(shell sh ./get-revision.sh)
VERSION = $(shell sh ./get-version.sh)

# Define high-level build targets.
.PHONY: all
all: darwin linux windows docker
all: darwin linux windows test lint

.PHONY: linux
linux: bin/linux/baronial.gz
Expand All @@ -23,19 +24,19 @@ docker: bin/docker/baronial-alpine.tar.gz bin/docker/baronial-debian.tar.gz

# Define specific build targets.
bin/darwin/baronial: ${SRC} .git/HEAD go.sum
GOOS=darwin go build -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION}" -o bin/darwin/baronial
GOOS=darwin go build -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION} -X github.com/marstr/baronial/cmd.version=${VERSION}" -o bin/darwin/baronial

bin/darwin/baronial.gz: bin/darwin/baronial
gzip -kf bin/darwin/baronial

bin/linux/baronial: ${SRC} .git/HEAD go.sum
GOOS=linux go build -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION}" -o bin/linux/baronial
GOOS=linux go build -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION} -X github.com/marstr/baronial/cmd.version=${VERSION}" -o bin/linux/baronial

bin/linux/baronial.gz: bin/linux/baronial
gzip -kf bin/linux/baronial

bin/windows/baronial.exe: ${SRC} .git/HEAD go.sum
GOOS=windows go build -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION}" -o bin/windows/baronial.exe
GOOS=windows go build -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION} -X github.com/marstr/baronial/cmd.version=${VERSION}" -o bin/windows/baronial.exe

bin/docker/baronial-alpine.tar.gz: ${SRC} Dockerfile.alpine
mkdir -p bin/docker
Expand Down Expand Up @@ -66,7 +67,7 @@ lint: ${SRC} ${TEST_SRC}
# Install this build on the local system.
.PHONY: install
install: ${SRC}
go install -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION}"
go install -ldflags "-X github.com/marstr/baronial/cmd.revision=${REVISION} -X github.com/marstr/baronial/cmd.version=${VERSION}"

# Remove all build products from the current system.
.PHONY: clean
Expand Down
7 changes: 7 additions & 0 deletions ancestors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

for tag in $(git tag) ; do
if git merge-base --is-ancestor ${tag} HEAD; then
echo ${tag}
fi
done
4 changes: 2 additions & 2 deletions get-revision.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#! /bin/bash

export revision="$(git rev-parse HEAD)"
revision="$(git rev-parse HEAD)"

if ! [[ -z "$(git status --short)" ]]; then
export revision="${revision}-modified"
revision="${revision}-modified"
fi

echo "${revision}"
11 changes: 11 additions & 0 deletions get-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

version=$(./ancestors.sh | ./max-version.pl)
revision=$(./get-revision.sh)


if [[ $(git rev-parse ${version}) != ${revision} ]]; then
version="${version}-modified"
fi

echo ${version}
76 changes: 76 additions & 0 deletions max-version.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#! /usr/bin/env perl

use strict;
use warnings;

my $maxMajor = 0;
my $maxMinor = 0;
my $maxPatch = 0;
my $maxTag = "";

while(my $row = <STDIN>) {
if ($row =~ /^[vV]?(?<major>\d+)(?:\.(?<minor>\d+))?(?:\.(?<patch>\d+))?(?:-(?<tag>\S+))?$/) {
my $currentMajor = int($+{major});
my $currentMinor = int($+{minor});
my $currentPatch = int($+{patch});
my $currentTag = $+{tag};

if (not defined $currentMinor) {
$currentMinor = 0;
}

if (not defined $currentPatch) {
$currentPatch = 0;
}

if (not defined $currentTag) {
$currentTag = "";
}

if ($currentMajor > $maxMajor) {
$maxMajor = $currentMajor;
$maxMinor = $currentMinor;
$maxPatch = $currentPatch;
$maxTag = $currentTag;
next;
} elsif ($currentMajor < $maxMajor) {
next;
}

if ($currentMinor > $maxMinor) {
$maxMinor = $currentMinor;
$maxPatch = $currentPatch;
$maxTag = $currentTag;
next;
} elsif ($currentMinor < $maxMinor) {
next;
}

if ($currentPatch > $maxPatch) {
$maxPatch = $currentPatch;
$maxTag = $currentTag;
next;
} elsif ($currentPatch < $maxPatch) {
next;
}

if ($currentTag eq "" and $maxTag ne "") {
$maxTag = $currentTag;
next;
} elsif ($maxTag eq "" and $currentTag ne ""){
next;
} elsif ($currentTag gt $maxTag) {
$maxTag = $currentTag;
next
} elsif ($currentTag le $maxTag) {
next;
}
}
}

my $formatted = "v${maxMajor}.${maxMinor}.${maxPatch}";
if ($maxTag ne "") {
$formatted = $formatted . "-${maxTag}";
}

print($formatted . "\n");

0 comments on commit 01fd937

Please sign in to comment.