-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·213 lines (182 loc) · 5.01 KB
/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env bash
set -e
cd $(dirname "$0")
CWD=$(pwd)
# Use this to ensure that we have all the tools required to do a build.
export CGO_ENABLED=0
export GO111MODULE=on
export GOFLAGS="-mod=vendor"
export PATH=$CWD/bin:$PATH
MISSING=()
check() {
local X=$1
set +e
command -v $X >/dev/null 2>&1
local RESULT=$?
set -e
if [ $RESULT != 0 ]; then
MISSING+=($X)
fi
}
check git
check go
check jq
check protoc
check zip
if ! [ ${#MISSING[@]} -eq 0 ]; then
echo "Missing prerequisites:"
for X in $MISSING; do
echo " $X"
done
exit 1
fi
echo "Prerequisites present"
GIT_COMMIT=$(git rev-list -1 HEAD)
VERSION=$(git describe --tags)
if ! [ -z "$(git status --porcelain)" ]; then
# There are untracked or unstaged changes
GIT_COMMIT="DIRTY-${GIT_COMMIT}"
VERSION="WIP-${VERSION}"
fi
echo "Build version '$VERSION', git SHA '$GIT_COMMIT'"
LDFLAGS_IMPORTS="-X github.com/object88/tugboat/pkg/version.GitCommit=${GIT_COMMIT} -X github.com/object88/pkg/version.AppVersion=${VERSION}"
cd "$CWD"
# default to mostly true, set env val to override
DO_GEN=${DO_GEN:-"true"}
DO_LOCAL_INSTALL=${DO_LOCAL_INSTALL:-"true"}
DO_PACKAGE=${DO_PACKAGE:-"false"}
DO_TEST=${DO_TEST:-"true"}
DO_VERIFY=${DO_VERIFY:-"true"}
DO_VET=${DO_VET:-"true"}
TARGETS=()
while [[ $# -gt 0 ]]; do
KEY="$1"
case $KEY in
--fast)
DO_GEN="false"
DO_LOCAL_INSTALL="false"
DO_PACKAGE="false"
DO_TEST="false"
DO_VERIFY="false"
DO_VET="false"
shift
;;
--no-gen)
DO_GEN="false"
shift
;;
--no-local-install)
DO_LOCAL_INSTALL="false"
shift
;;
--no-test)
DO_TEST="false"
shift
;;
--no-verify)
DO_VERIFY="false"
shift
;;
--no-vet)
DO_VET="false"
shift
;;
--package)
DO_PACKAGE="true"
shift
;;
*)
TARGETS+=($KEY)
shift
;;
esac
done
if [ ${#TARGETS[@]} -eq 0 ]; then
echo "No targets specified; building all apps."
TARGETS=( $(ls apps) )
fi
if [[ $DO_GEN == "true" ]]; then
# Run go generate to generate any go generate generated go code.
echo "Running generator"
go generate -x ./...
echo ""
fi
if [[ $DO_TEST == "true" ]]; then
echo "Running generator for tests"
go generate -tags=test -x ./...
echo ""
fi
if [[ $DO_VERIFY == "true" ]]; then
echo "Verifying modules"
# returns non-zero if this doesn't verify out
time go mod verify
echo ""
fi
if [[ $DO_VET == "true" ]]; then
# Vet's exit code is non-zero for erroneous invocation of the tool
# or if a problem was reported, and 0 otherwise. Note that the
# tool does not check every possible problem and depends on
# unreliable heuristics, so it should be used as guidance only,
# not as a firm indicator of program correctness.
# [snip]
# By default, all checks are performed.
#
# https://golang.org/cmd/vet/
echo "Running vet"
time go vet $(go list ./...)
echo ""
fi
# test executables and binaries
if [[ $DO_TEST == "true" ]]; then
export TEST_SHA=${GIT_COMMIT}
export TEST_VERSION=${VERSION}
echo "Testing with $TEST_BINARY_NAME"
time go test ./... -count=1 -tags test_integration
echo ""
fi
# build executable(s)
# method found here https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04
echo "Building..."
DEFAULT_GOOS=$(uname | tr '[:upper:]' '[:lower:]')
PLATFORMS=( "$DEFAULT_GOOS/amd64" )
if [ "$BUILD_AND_RELEASE" == "true" ]; then
PLATFORMS=( "linux/amd64" "darwin/amd64" )
fi
for TARGET in "${TARGETS[@]}"; do
if [ -z "apps/$TARGET" ]; then
echo "Target '$TARGET' could not be found in apps directory; skipping."
continue
fi
if ! [ -f "apps/$TARGET/main/main.go" ]; then
echo "Target '$TARGET' does not have main/main.go; skipping."
continue
fi
# build executable for each platform...
for PLATFORM in "${PLATFORMS[@]}"; do
export GOOS=$(cut -d'/' -f1 <<< $PLATFORM)
export GOARCH=$(cut -d'/' -f2 <<< $PLATFORM)
BINARY_NAME="$TARGET-${GOOS}-${GOARCH}"
if [ $DEFAULT_GOOS == $GOOS ]; then
export TEST_BINARY_NAME="$CWD/bin/$BINARY_NAME"
fi
echo "Building as $BINARY_NAME"
if [ $(uname) == "Darwin" ]; then
# Cannot do a static compilation on Darwin.
time go build -o ./bin/$BINARY_NAME -ldflags "-s -w $LDFLAGS_IMPORTS" ./apps/$TARGET/main/main.go
else
time go build -o ./bin/$BINARY_NAME -tags "netgo" -ldflags "-extldflags \"-static\" -s -w $LDFLAGS_IMPORTS" ./apps/$TARGET/main/main.go
fi
if [ $DO_PACKAGE == "true" ]; then
zip -j ./bin/$BINARY_NAME.zip ./bin/$BINARY_NAME
fi
echo ""
done
if [ $DO_LOCAL_INSTALL == "true" ]; then
echo "Copying to /usr/local/bin and installing bash/zsh completion"
cp $TEST_BINARY_NAME /usr/local/bin/$TARGET
$TEST_BINARY_NAME completion bash > /usr/local/etc/bash_completion.d/$TARGET
$TEST_BINARY_NAME completion zsh > /usr/local/share/zsh/site-functions/_$TARGET
echo ""
fi
done
echo "Done"