-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathbuild.sh
executable file
·350 lines (290 loc) · 8.73 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
# Copyright 2018 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o pipefail
source_dirs="cmd pkg test lib"
# Store for later
if [ -z "$1" ]; then
ARGS=("")
else
ARGS=("$@")
fi
set -eu
# Run build
run() {
# Switch on modules unconditionally
export GO111MODULE=on
# Jump into project directory
pushd $(basedir) >/dev/null 2>&1
# Print help if requested
if $(has_flag --help -h); then
display_help
exit 0
fi
if $(has_flag --watch -w); then
# Build and test first
go_build
if $(has_flag --test -t); then
go_test
fi
# Go in endless loop, to be stopped with CTRL-C
watch
fi
# Fast mode: Only compile and maybe run test
if $(has_flag --fast -f); then
go_build
if $(has_flag --test -t); then
go_test
fi
exit 0
fi
# Run only tests
if $(has_flag --test -t); then
go_test
exit 0
fi
# Run only codegen
if $(has_flag --codegen -c); then
codegen
exit 0
fi
# Cross compile only
if $(has_flag --all -x); then
cross_build || (echo "✋ Cross platform build failed" && exit 1)
exit 0
fi
# Default flow
codegen
go_build
go_test
echo "────────────────────────────────────────────"
./kn version
}
codegen() {
# Update dependencies
update_deps
# Format source code and cleanup imports
source_format
# Check for license headers
check_license
# Auto generate cli docs
generate_docs
}
go_fmt() {
echo "🧹 ${S}Format"
find $(echo $source_dirs) -name "*.go" -print0 | xargs -0 gofmt -s -w
}
source_format() {
set +e
which goimports >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "✋ No 'goimports' found. Please use"
echo "✋ go install golang.org/x/tools/cmd/goimports"
echo "✋ to enable import cleanup. Import cleanup skipped."
# Run go fmt instead
go_fmt
else
echo "🧽 ${X}Format"
goimports -w $(echo $source_dirs)
find $(echo $source_dirs) -name "*.go" -print0 | xargs -0 gofmt -s -w
fi
set -e
}
go_build() {
echo "🚧 Compile"
go build -mod=vendor -ldflags "$(build_flags $(basedir))" -o kn ./cmd/...
}
go_test() {
local test_output=$(mktemp /tmp/kn-client-test-output.XXXXXX)
local red=""
local reset=""
# Use color only when a terminal is set
if [ -t 1 ]; then
red="[31m"
reset="[39m"
fi
echo "🧪 ${X}Test"
set +e
go test -v ./pkg/... >$test_output 2>&1
local err=$?
if [ $err -ne 0 ]; then
echo "🔥 ${red}Failure${reset}"
cat $test_output | sed -e "s/^.*\(FAIL.*\)$/$red\1$reset/"
rm $test_output
exit $err
fi
rm $test_output
}
check_license() {
echo "⚖️ ${S}License"
local required_keywords=("Authors" "Apache License" "LICENSE-2.0")
local extensions_to_check=("sh" "go" "yaml" "yml" "json")
local check_output=$(mktemp /tmp/kn-client-licence-check.XXXXXX)
for ext in "${extensions_to_check[@]}"; do
find . -name "*.$ext" -a \! -path "./vendor/*" -a \! -path "./.*" -print0 |
while IFS= read -r -d '' path; do
for rword in "${required_keywords[@]}"; do
if ! grep -q "$rword" "$path"; then
echo " $path" >> $check_output
fi
done
done
done
if [ -s $check_output ]; then
echo "🔥 No license header found in:"
cat $check_output | sort | uniq
echo "🔥 Please fix and retry."
rm $check_output
exit 1
fi
rm $check_output
}
update_deps() {
echo "🚒 Update"
go mod tidy
go mod vendor
}
generate_docs() {
echo "📖 Docs"
rm -rf "./docs/cmd"
mkdir -p "./docs/cmd"
go run "./hack/generate-docs.go" "."
}
watch() {
local command="./hack/build.sh --fast"
local fswatch_opts='-e "^\..*$" -o pkg cmd'
if $(has_flag --test -t); then
command="$command --test"
fi
if $(has_flag --verbose); then
fswatch_opts="$fswatch_opts -v"
fi
set +e
which fswatch >/dev/null 2>&1
if [ $? -ne 0 ]; then
local green="[32m"
local reset="[39m"
echo "🤷 Watch: Cannot find ${green}fswatch${reset}"
echo "🌏 Please see ${green}http://emcrisostomo.github.io/fswatch/${reset} for installation instructions"
exit 1
fi
set -e
echo "🔁 Watch"
fswatch $fswatch_opts | xargs -n1 -I{} sh -c "$command && echo 👌 OK"
}
# Dir where this script is located
basedir() {
# Default is current directory
local script=${BASH_SOURCE[0]}
# Resolve symbolic links
if [ -L $script ]; then
if readlink -f $script >/dev/null 2>&1; then
script=$(readlink -f $script)
elif readlink $script >/dev/null 2>&1; then
script=$(readlink $script)
elif realpath $script >/dev/null 2>&1; then
script=$(realpath $script)
else
echo "ERROR: Cannot resolve symbolic link $script"
exit 1
fi
fi
local dir=$(dirname "$script")
local full_dir=$(cd "${dir}/.." && pwd)
echo ${full_dir}
}
# Checks if a flag is present in the arguments.
has_flag() {
filters="$@"
for var in "${ARGS[@]}"; do
for filter in $filters; do
if [ "$var" = "$filter" ]; then
echo 'true'
return
fi
done
done
echo 'false'
}
cross_build() {
local basedir=$(basedir)
local ld_flags="$(build_flags $basedir)"
local pkg="github.com/knative/client/pkg/kn/commands"
local failed=0
echo "⚔️ ${S}Compile"
export CGO_ENABLED=0
echo " 🐧 kn-linux-amd64"
GOOS=linux GOARCH=amd64 go build -mod=vendor -ldflags "${ld_flags}" -o ./kn-linux-amd64 ./cmd/... || failed=1
echo " 🍏 kn-darwin-amd64"
GOOS=darwin GOARCH=amd64 go build -mod=vendor -ldflags "${ld_flags}" -o ./kn-darwin-amd64 ./cmd/... || failed=1
echo " 🎠 kn-windows-amd64.exe"
GOOS=windows GOARCH=amd64 go build -mod=vendor -ldflags "${ld_flags}" -o ./kn-windows-amd64.exe ./cmd/... || failed=1
return ${failed}
}
# Spaced fillers needed for certain emojis in certain terminals
S=""
X=""
# Calculate space fixing variables S and X
apply_emoji_fixes() {
# Temporary fix for iTerm issue https://gitlab.com/gnachman/iterm2/issues/7901
if [ -n "${ITERM_PROFILE:-}" ]; then
S=" "
# This issue has been fixed with iTerm2 3.3.7, so let's check for this
# We can remove this code alltogether if iTerm2 3.3.7 is in common usage everywhere
if [ -n "${TERM_PROGRAM_VERSION}" ]; then
args=$(echo $TERM_PROGRAM_VERSION | sed -e 's#[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)#\1 \2 \3#')
expanded=$(printf '%03d%03d%03d' $args)
if [ $expanded -lt "003003007" ]; then
X=" "
fi
fi
fi
}
# Display a help message.
display_help() {
local command="${1:-}"
cat <<EOT
Knative client build script
Usage: $(basename $BASH_SOURCE) [... options ...]
with the following options:
-f --fast Only compile (without dep update, formatting, testing, doc gen)
-t --test Run tests when used with --fast or --watch
-c --codegen Runs formatting, doc gen and update without compiling/testing
-w --watch Watch for source changes and recompile in fast mode
-x --all Only build cross platform binaries without code-generation/testing
-h --help Display this help message
--verbose More output
--debug Debug information for this script (set -x)
You can add a symbolic link to this build script into your PATH so that it can be
called from everywhere. E.g.:
ln -s $(basedir)/hack/build.sh /usr/local/bin/kn_build.sh
Examples:
* Update deps, format, license check,
gen docs, compile, test: ........... build.sh
* Compile only: ...................... build.sh --fast
* Run only tests: .................... build.sh --test
* Compile with tests: ................ build.sh -f -t
* Automatic recompilation: ........... build.sh --watch
* Build cross platform binaries: ..... build.sh --all
EOT
}
if $(has_flag --debug); then
export PS4='+($(basename ${BASH_SOURCE[0]}):${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x
fi
# Shared funcs with CI
source $(basedir)/hack/build-flags.sh
# Fixe emoji labels for certain terminals
apply_emoji_fixes
run $*