From 1342f5b8679843346e66d4e66297ae6f1d6ca550 Mon Sep 17 00:00:00 2001 From: "." <.@.> Date: Sun, 8 May 2016 14:42:23 +1200 Subject: [PATCH] track godist.sh --HG-- branch : nmdc-ircfrontend --- godist.sh | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 godist.sh diff --git a/godist.sh b/godist.sh new file mode 100644 index 0000000..9adf029 --- /dev/null +++ b/godist.sh @@ -0,0 +1,175 @@ +#!/bin/bash +# godist.sh is a template build script for golang applications. +# +# Copyright (c) 2016, The godist.sh Author(s) +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. +# + +set -eu + +DIST_DIR=./_dist + +sanitise() { + TARGETFILE="$1" php <<'EOD' + /dev/null + which php > /dev/null +} + +get_platform_description() { + if [[ $GOOS == windows ]] ; then + echo -n "win" + else + echo -n "${GOOS}" + fi + + if [[ $GOARCH == 386 ]] ; then + echo -n 32 + elif [[ $GOARCH == amd64 ]] ; then + echo -n 64 + else + echo -n "-${GOARCH}" + if [[ -v $GOARM ]] ; then + echo -n "v${GOARM}" + fi + fi +} + +pathfix() { + if [[ $(uname -o) == Cygwin ]] ; then + cygpath -w "$1" + else + echo "$1" + fi +} + +single_build() { + local version="$1" + + # Determine build output name + local bin_name='' + if [[ $GOOS == windows ]] ; then + local_bin_name="$(get_package_name).exe" + else + local_bin_name="$(get_package_name)" + fi + + if [[ -f ./$local_bin_name ]] ; then + rm -f "./$local_bin_name" + fi + + local tmpdir=$(mktemp -d) + + # Platform identifier + local platform="$(get_platform_description)" + echo "[INFO] Building ${version} for ${platform}..." + + # Build. + # GOARCH/GOOS supplied in function env + go build -a -ldflags '-s -w' -o "$(pathfix "${tmpdir}/${local_bin_name}")" + + # Sanitise. + sanitise "${tmpdir}/${local_bin_name}" + + # Archive. + if [[ ! -d $DIST_DIR ]] ; then + mkdir "$DIST_DIR" + fi + local archive_name="${DIST_DIR}/$(get_package_name)-${version}-${platform}" + if [[ $GOOS == windows ]] ; then + archive_name="${archive_name}.7z" + 7z a -mx9 "$archive_name" "${tmpdir}/${local_bin_name}" >/dev/null + else + archive_name="${archive_name}.tar.xz" + XZ_OPT='-9' tar caf "$archive_name" -C "${tmpdir}" "${local_bin_name}" --owner=0 --group=0 >/dev/null + fi + + # Cleanup + rm -f "${tmpdir}/${local_bin_name}" + rmdir "$tmpdir" + +} + +datestamp() { + ( TZ=UTC date +%Y%m%d%H%M%SZ ) +} + +usage() { + cat <&2 + exit 1 + fi + if [[ ! -d $GOPATH ]] ; then + echo "Invalid GOPATH set." >&2 + exit 1 + fi + + local version="" + while getopts ':v:' flag ; do + case "$flag" in + v) + version="$OPTARG" + ;; + *) + usage + ;; + esac + done + + if [[ $version == "" ]] ; then + read -p "Enter version string (blank for timestamp)> " version + if [[ $version == "" ]] ; then + version=$(datestamp) + fi + fi + + GOARCH=amd64 GOOS=windows single_build "$version" + GOARCH=386 GOOS=windows single_build "$version" + GOARCH=amd64 GOOS=linux single_build "$version" + GOARCH=386 GOOS=linux single_build "$version" + + echo "[INFO] Build complete." +} + +main "$@"