diff --git a/Makefile b/Makefile index 913a4f5..68d3e38 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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 diff --git a/ancestors.sh b/ancestors.sh new file mode 100755 index 0000000..fe84a65 --- /dev/null +++ b/ancestors.sh @@ -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 \ No newline at end of file diff --git a/get-revision.sh b/get-revision.sh index 90e3155..73ab3ed 100755 --- a/get-revision.sh +++ b/get-revision.sh @@ -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}" diff --git a/get-version.sh b/get-version.sh new file mode 100755 index 0000000..88de079 --- /dev/null +++ b/get-version.sh @@ -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} diff --git a/max-version.pl b/max-version.pl new file mode 100755 index 0000000..2fe8e0c --- /dev/null +++ b/max-version.pl @@ -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 = ) { + if ($row =~ /^[vV]?(?\d+)(?:\.(?\d+))?(?:\.(?\d+))?(?:-(?\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");