version: support major.minor.patch tags without breaking Apple builds.

Signed-off-by: David Anderson <danderson@tailscale.com>
reviewable/pr253/r3
David Anderson 5 years ago committed by Dave Anderson
parent 984a699219
commit 5aae6b734d

@ -1,3 +1,4 @@
describe.txt
long.txt long.txt
short.txt short.txt
version.h version.h

@ -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

@ -0,0 +1,4 @@
describe=$(cd ../.. && git describe)
echo "$describe" >$3
redo-always
redo-stamp <$3

@ -1,10 +1,4 @@
ver=$(cd ../.. && git describe | sed 's/^v//') redo-ifchange mkversion.sh describe.txt
if [ "$ver" = "${ver%-*}" ]; then read -r describe <describe.txt
# no sub-version. ie. it's 0.05 and not 0.05-341 ver=$(./mkversion.sh long "$describe")
# so add a sub-version.
ver=$ver-0
fi
echo "$ver" >$3 echo "$ver" >$3
redo-always
redo-stamp <$3

@ -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

@ -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)
}
}
}

@ -1,18 +1,4 @@
redo-ifchange long.txt redo-ifchange mkversion.sh describe.txt
read -r LONGVER junk <long.txt read -r describe <describe.txt
ver=$(./mkversion.sh short "$describe")
# Convert a version like "0.92-98-g123456" into "0.92-98". echo "$ver" >$3
# Sometimes the version is just "0.92-0", in which case we leave it as is.
case $LONGVER in
*-*-*)
echo "${LONGVER%-*}" >$3
;;
*-*)
echo "$LONGVER" >$3
;;
*)
echo "Fatal: long version in invalid format." >&2
exit 44
esac
redo-stamp <$3

@ -1,17 +1,4 @@
redo-ifchange short.txt redo-ifchange mkversion.sh describe.txt
read -r ver <short.txt read -r describe <describe.txt
ver=$(./mkversion.sh xcode "$describe")
# get it into "major.minor.patch" format echo "$ver" >$3
ver=$(echo "$ver" | sed -e 's/-/./')
# CFBundleShortVersionString: the "short name" used in the App Store.
# eg. 0.92.98
echo "VERSION_NAME = $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

Loading…
Cancel
Save