2016-07-10 23:57:39 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "Build and bundle Go releases with the current dir as the build dir."
|
|
|
|
echo "Usage: $0 PACKAGE [ASSETS...]"
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
set -eo pipefail
|
|
|
|
[[ "$TRACE" ]] && set -x
|
|
|
|
|
|
|
|
if [[ ! "$1" ]]; then
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! "$GOOS" ]]; then
|
|
|
|
export GOOS="linux"
|
2016-07-17 23:58:57 +02:00
|
|
|
echo "Defaulting to GOOS=$GOOS"
|
2016-07-10 23:57:39 +02:00
|
|
|
fi
|
|
|
|
if [[ ! "$GOARCH" ]]; then
|
|
|
|
export GOARCH="amd64"
|
2016-07-17 23:58:57 +02:00
|
|
|
echo "Defaulting to GOARCH=$GOARCH"
|
2016-07-10 23:57:39 +02:00
|
|
|
fi
|
|
|
|
if [[ ! "$BUILDDIR" ]]; then
|
|
|
|
export BUILDDIR="build"
|
2016-07-17 23:58:57 +02:00
|
|
|
echo "Defaulting to BUILDDIR=$BUILDDIR"
|
2016-07-10 23:57:39 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
build "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
|
|
|
local package="$1"; shift
|
|
|
|
local assets="$@"
|
|
|
|
|
|
|
|
local bin="$(basename $package)"
|
|
|
|
local tarball="${bin}-${GOOS}_${GOARCH}.tgz"
|
|
|
|
local outdir="$BUILDDIR/$bin"
|
2016-09-15 19:52:55 +02:00
|
|
|
local tardir="$bin"
|
|
|
|
if [ "$GOOS" == "windows" ]; then
|
|
|
|
bin="$bin.exe"
|
|
|
|
fi
|
|
|
|
|
2016-07-10 23:57:39 +02:00
|
|
|
|
|
|
|
if [[ -d "$outdir" ]]; then
|
|
|
|
echo "err: outdir already exists: $PWD/$outdir"
|
|
|
|
fi
|
|
|
|
mkdir -p "$outdir"
|
|
|
|
|
|
|
|
go build -ldflags "$LDFLAGS" -o "$outdir/$bin" "$package"
|
|
|
|
|
|
|
|
# Stage asset bundle
|
|
|
|
if [[ "$assets" ]]; then
|
|
|
|
ln -f $assets "$outdir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create tarball
|
2016-09-15 19:52:55 +02:00
|
|
|
tar -C "$BUILDDIR" -czvf "$BUILDDIR/$tarball" "$tardir"
|
2016-07-10 23:57:39 +02:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
rm -rf "$outdir"
|
|
|
|
|
|
|
|
echo "Packaged: $tarball"
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|