From 5aae6b734d0a618f98ee0bcacc681c58820224d6 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 1 Jun 2020 20:42:34 +0000 Subject: [PATCH] version: support major.minor.patch tags without breaking Apple builds. Signed-off-by: David Anderson --- version/.gitignore | 1 + version/clean.do | 2 +- version/describe.txt.do | 4 ++ version/long.txt.do | 12 ++--- version/mkversion.sh | 93 +++++++++++++++++++++++++++++++++++++ version/mkversion_test.go | 57 +++++++++++++++++++++++ version/short.txt.do | 22 ++------- version/version.xcconfig.do | 21 ++------- 8 files changed, 167 insertions(+), 45 deletions(-) create mode 100644 version/describe.txt.do create mode 100755 version/mkversion.sh create mode 100644 version/mkversion_test.go diff --git a/version/.gitignore b/version/.gitignore index e6b904135..47d66053b 100644 --- a/version/.gitignore +++ b/version/.gitignore @@ -1,3 +1,4 @@ +describe.txt long.txt short.txt version.h diff --git a/version/clean.do b/version/clean.do index 5ddcb4f31..312b54f94 100644 --- a/version/clean.do +++ b/version/clean.do @@ -1 +1 @@ -rm -f *~ .*~ long.txt short.txt version.xcconfig ver.go version.h version +rm -f *~ .*~ describe.txt long.txt short.txt version.xcconfig ver.go version.h version diff --git a/version/describe.txt.do b/version/describe.txt.do new file mode 100644 index 000000000..baef23ea6 --- /dev/null +++ b/version/describe.txt.do @@ -0,0 +1,4 @@ +describe=$(cd ../.. && git describe) +echo "$describe" >$3 +redo-always +redo-stamp <$3 diff --git a/version/long.txt.do b/version/long.txt.do index af4b859f5..f88bcf7ff 100644 --- a/version/long.txt.do +++ b/version/long.txt.do @@ -1,10 +1,4 @@ -ver=$(cd ../.. && git describe | sed 's/^v//') -if [ "$ver" = "${ver%-*}" ]; then - # no sub-version. ie. it's 0.05 and not 0.05-341 - # so add a sub-version. - ver=$ver-0 -fi +redo-ifchange mkversion.sh describe.txt +read -r describe $3 - -redo-always -redo-stamp <$3 diff --git a/version/mkversion.sh b/version/mkversion.sh new file mode 100755 index 000000000..f9bc79b84 --- /dev/null +++ b/version/mkversion.sh @@ -0,0 +1,93 @@ +#!/bin/sh + +set -eu + +mode=$1 +describe=$2 + +long() { + ver="${describe#v}" + stem="${ver%%-*}" + case "$stem" in + *.*.*) + # Full SemVer, nothing to do. + semver="${stem}" + ;; + *.*) + # Old style major.minor, add a .0 + semver="${stem}.0" + ;; + *) + echo "Unparseable version $stem" >&2 + exit 1 + ;; + esac + suffix="${ver#$stem}" + case "$suffix" in + -*-*) + # Has a change count in addition to the commit hash. + ;; + -*) + # Missing change count, add one. + suffix="-0${suffix}" + ;; + *) + echo "Unexpected version suffix" >&2 + exit 1 + esac + echo "${semver}${suffix}" +} + +short() { + ver="$(long)" + case "$ver" in + *-*-*) + echo "${ver%-*}" + ;; + *-*) + echo "$ver" + ;; + *) + echo "Long version in invalid format" >&2 + exit 1 + ;; + esac +} + +xcode() { + ver=$(short | sed -e 's/-/./') + major=$(echo "$ver" | cut -f1 -d.) + minor=$(echo "$ver" | cut -f2 -d.) + patch=$(echo "$ver" | cut -f3 -d.) + changecount=$(echo "$ver" | cut -f4 -d.) + + # Apple version numbers must be major.minor.patch. We have 4 fields + # because we need major.minor.patch for go module compatibility, and + # changecount for automatic version numbering of unstable builds. To + # resolve this, for Apple builds, we combine changecount into patch: + patch=$((patch*10000 + changecount)) + + # CFBundleShortVersionString: the "short name" used in the App Store. + # eg. 0.92.98 + echo "VERSION_NAME = $major.$minor.$patch" + # CFBundleVersion: the build number. Needs to be 3 numeric sections + # that increment for each release according to SemVer rules. + # + # We start counting at 100 because we submitted using raw build + # numbers before, and Apple doesn't let you start over. + # e.g. 0.98.3-123 -> 100.98.3123 + major=$((major + 100)) + echo "VERSION_ID = $major.$minor.$patch" +} + +case "$mode" in + long) + long + ;; + short) + short + ;; + xcode) + xcode + ;; +esac diff --git a/version/mkversion_test.go b/version/mkversion_test.go new file mode 100644 index 000000000..c69a0a167 --- /dev/null +++ b/version/mkversion_test.go @@ -0,0 +1,57 @@ +// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package version + +import ( + "fmt" + "os/exec" + "strings" + "testing" +) + +func xcode(short, long string) string { + return fmt.Sprintf("VERSION_NAME = %s\nVERSION_ID = %s", short, long) +} + +func mkversion(t *testing.T, mode, in string) string { + t.Helper() + bs, err := exec.Command("./mkversion.sh", mode, in).CombinedOutput() + if err != nil { + t.Logf("mkversion.sh output: %s", string(bs)) + t.Fatalf("mkversion.sh %s %s: %v", mode, in, err) + } + return strings.TrimSpace(string(bs)) +} + +func TestMkversion(t *testing.T) { + tests := []struct { + in string + long string + short string + xcode string + }{ + {"v0.98-abcdef", "0.98.0-0-abcdef", "0.98.0-0", xcode("0.98.0", "100.98.0")}, + {"v0.98-123-abcdef", "0.98.0-123-abcdef", "0.98.0-123", xcode("0.98.123", "100.98.123")}, + {"v0.99.5-123-abcdef", "0.99.5-123-abcdef", "0.99.5-123", xcode("0.99.50123", "100.99.50123")}, + {"v0.99.5-123-abcdef", "0.99.5-123-abcdef", "0.99.5-123", xcode("0.99.50123", "100.99.50123")}, + {"v2.3-0-abcdef", "2.3.0-0-abcdef", "2.3.0-0", xcode("2.3.0", "102.3.0")}, + {"1.2.3-4-abcdef", "1.2.3-4-abcdef", "1.2.3-4", xcode("1.2.30004", "101.2.30004")}, + } + + for _, test := range tests { + gotlong := mkversion(t, "long", test.in) + gotshort := mkversion(t, "short", test.in) + gotxcode := mkversion(t, "xcode", test.in) + if gotlong != test.long { + t.Errorf("mkversion.sh long %q: got %q, want %q", test.in, gotlong, test.long) + } + if gotshort != test.short { + t.Errorf("mkversion.sh short %q: got %q, want %q", test.in, gotshort, test.short) + } + if gotxcode != test.xcode { + t.Errorf("mkversion.sh xcode %q: got %q, want %q", test.in, gotxcode, test.xcode) + } + } +} diff --git a/version/short.txt.do b/version/short.txt.do index 54540d3ec..8a189f638 100644 --- a/version/short.txt.do +++ b/version/short.txt.do @@ -1,18 +1,4 @@ -redo-ifchange long.txt -read -r LONGVER junk $3 - ;; - *-*) - echo "$LONGVER" >$3 - ;; - *) - echo "Fatal: long version in invalid format." >&2 - exit 44 -esac - -redo-stamp <$3 +redo-ifchange mkversion.sh describe.txt +read -r describe $3 diff --git a/version/version.xcconfig.do b/version/version.xcconfig.do index 7ba1a854d..cda65b06a 100644 --- a/version/version.xcconfig.do +++ b/version/version.xcconfig.do @@ -1,17 +1,4 @@ -redo-ifchange short.txt -read -r ver $3 -# CFBundleVersion: the build number. Needs to increment each release. -# start counting at 100 because we submitted using raw build numbers -# before (and Apple doesn't let you start over). -# eg. 100.92.98 - -major=$((${ver%%.*} + 100)) -minor=${ver#*.} -echo "VERSION_ID = $major.$minor" >>$3 +redo-ifchange mkversion.sh describe.txt +read -r describe $3