forked from cadence-workflow/cadence-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versioned_go_build.sh
executable file
·176 lines (148 loc) · 4.73 KB
/
versioned_go_build.sh
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
set -euf -o pipefail
# In a nutshell, this script:
# - makes a tempdir and moves to it
# - go gets the requested bin (but does not install it)
# - cds to the repo
# - checks out the requested version
# - maybe runs `glide install`
# - maybe runs `dep ensure`
# - builds the bin and puts it where you told it to.
#
# Since doing that verbatim is a bit noisy, and pinning tools tends to
# cause them to slowly get out of date, it does two additional things:
# - suppresses output unless `--debug` is passed
# - checks for newer commits / tags than the one you passed, and
# prints the newer sha/version (if any) to stderr so it's always visible.
usage () {
echo 'Installs a specific version of a go-gettable bin to the specified location.'
echo ''
echo 'Usage:'
echo ''
echo -e "\\t$0 [--debug] go-gettable-repo version [path-to-bin-in-repo] install-location"
echo ''
echo 'Examples:'
echo ''
echo -e "\\t$0 go.uber.org/thriftrw 1.10.0 somewhere/thriftrw"
echo -e '\t\tInstalls v1.10.0 of go.uber.org/thriftrw to somewhere/thriftrw.'
echo -e '\t\tNotice that go.uber.org/thriftrw is both the repository AND the binary name.'
echo ''
echo -e "\\t$0 golang.org/x/lint SOME_SHA golint somewhere/golint"
echo -e '\t\tInstalls a specific SHA of e.g. "golang.org/x/lint/golint" to "somewhere/golint",'
echo -e '\t\tNotice that the golint bin is in a subfolder of the golang.org/x/lint repository.'
exit 1
}
num_commits_behind () {
head=$1
git rev-list "..$head" | wc -l | tr -dc '0-9'
}
is_versioned () {
# returns truthy if the arg is version-like.
str=$1
if echo "$str" | grep -q -E "$VERSION_TAG_REGEX"; then
return 0
fi
return 1
}
most_recent_version_tag () {
# using xargs because it's safer (for big lists) + it doesn't result in
# a ton of SHAs in debug output like `git describe --tags $(...)` causes.
#
# in brief:
# - get all tagged shas
# - get their tags
# - grep for version-like tags
# - sort by version
# - return the "biggest"
git rev-list --tags \
| xargs git describe --always --tags 2>/dev/null \
| grep -E "$VERSION_TAG_REGEX" \
| sort -Vr \
| head -n 1
}
# matches "v1[.2[.3[.4]]]" exactly or fails.
# this intentionally does not match "v1.2.3-ae83c2" tags, nor "v1.2-pre", etc,
# as these are not likely "real" release versions.
declare -r VERSION_TAG_REGEX='^v?\d+(\.\d+){0,3}$'
# output control vars
declare DEBUG=
declare TO_DEV_NULL=
declare QUIET=
# build control vars
declare -x GOPATH
declare GO_GETTABLE_REPO
declare VERSION
declare GO_GETTABLE_BIN
declare INSTALL_LOCATION
# handle optional debug (and consume the arg)
if [ "$1" == "--debug" ]; then
shift # consume it
DEBUG=1
else
# otherwise, redirect to /dev/null (requires eval)
TO_DEV_NULL='>/dev/null'
# pass quiet flags where needed (do not quote)
QUIET='--quiet'
fi
# must have 3 or 4 args
[ $# -ge 3 ] || usage
[ $# -le 4 ] || usage
[ -z $DEBUG ] || set -x
#
# Set up some variables / the temp folder
#
# set up gopath, and make sure it's cleaned up regardless of how we exit
GOPATH=$(mktemp -d)
trap 'rm -rf $GOPATH' EXIT
GO_GETTABLE_REPO="$1"
VERSION="$2"
if [ $# -eq 4 ]; then
# bin resides in a sub-folder
GO_GETTABLE_BIN="$1/$3"
INSTALL_LOCATION="$4"
elif [ $# -eq 3 ]; then
# repo == bin
GO_GETTABLE_BIN="$1"
INSTALL_LOCATION="$3"
else
# should be unreachable
usage
fi
#
# Pull the repo, set to the correct version
#
go get -d "$GO_GETTABLE_BIN"
# eval so redirection works when quiet
eval "pushd $GOPATH/src/$GO_GETTABLE_REPO $TO_DEV_NULL"
# save for the version check
HEAD="$(git rev-parse HEAD)"
# silently check out, reduces a lot of default spam
git checkout $QUIET "$VERSION"
#
# Check if we're building an old version, warn if so
#
if is_versioned "$VERSION"; then
# versioned, check for newer tags.
LATEST="$(most_recent_version_tag)"
# use sort to check if VERSION >= LATEST, or warn about the newer version
echo -e "$VERSION\\n$LATEST" | sort -Vrc 2>/dev/null || (>&2 echo -e "\\t$GO_GETTABLE_REPO has a newer tag: $LATEST")
else
# not versioned, check for newer commits
BEHIND="$(num_commits_behind "$HEAD")"
# should be zero, or warn about the newer version
[ "$BEHIND" -eq 0 ] || (>&2 echo -e "\\t$GO_GETTABLE_REPO is $BEHIND commits behind the current HEAD: $HEAD")
fi
#
# Build the bin, install to the correct location
#
# only glide install when there is a glide file, or it tries to install
# to the current repo (not in our current folder)
if [ -f glide.lock ]; then
glide $QUIET install
fi
if [ -f Gopkg.lock ]; then
dep init
fi
# eval so redirection works when quiet
eval "popd $TO_DEV_NULL"
go build -o "$INSTALL_LOCATION" "$GO_GETTABLE_BIN"