summaryrefslogtreecommitdiffhomepage
path: root/version/mkversion.sh
blob: f9bc79b8465ec5697de8fa4d6cc27782359f7f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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