Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Add distribution script
Browse files Browse the repository at this point in the history
  • Loading branch information
hashworks committed Feb 20, 2018
1 parent a0b43c0 commit 84853a2
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions distribute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

name=plexStats

checkCommand() {
which "$1" >/dev/null 2>&1
if [ "$?" != "0" ]; then
echo Please make sure the following command is available: "$1" >&2
exit "$?"
fi
}

checkCommand go
checkCommand tar
checkCommand zip

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# NOTE: The build process is somewhat similar to https://github.com/syncthing/syncthing, thanks for that!
platforms=(
linux-amd64 windows-amd64 darwin-amd64 dragonfly-amd64 freebsd-amd64 netbsd-amd64 openbsd-amd64 solaris-amd64
freebsd-386 linux-386 netbsd-386 openbsd-386 windows-386
linux-arm linux-arm64 linux-ppc64 linux-ppc64le
)

cd "$DIR"
commit="$(git rev-parse --short HEAD 2>/dev/null)"
date="$(date --iso-8601=seconds)"

if [ "$commit" == "" ]; then
commit="unknown"
fi

if [ "$1" == "" ]; then
echo You didn\'t provide a version string as the first parameter, setting version to \"unknown\".
version="unknown"
else
version="$1"
fi

rm -Rf ./bin/
mkdir ./bin/ 2>/dev/null

for plat in "${platforms[@]}"; do
echo Building "$plat" ...

GOOS="${plat%-*}"
GOARCH="${plat#*-}"

if [ "$GOOS" != "windows" ]; then
tmpFile="/tmp/${name}/bin/${name}"
else
tmpFile="/tmp/${name}/bin/${name}.exe"
fi

GOOS="${plat%-*}" GOARCH="${plat#*-}" go build -ldflags '-X main.VERSION='"$version"' -X main.BUILD_COMMIT='"$commit"' -X main.BUILD_DATE='"$date" \
-o "$tmpFile" "$DIR"/*.go

if [ "$?" != 0 ]; then
echo Build failed! >&2
exit "$?"
fi

if [ "$GOOS" != "windows" ]; then
tarPath="$DIR"/bin/${name}-"$plat".tar.gz
echo Build succeeded, creating "$tarPath" ...
tar -czf "$tarPath" -C "${tmpFile%/*}" ${name}
else
zipPath="$DIR"/bin/${name}-"$plat".zip
echo Build succeeded, creating "$zipPath" ...
zip -j "$zipPath" "$tmpFile"
fi

if [ "$?" != 0 ]; then
echo Failed to pack the binary! >&2
exit "$?"
fi
echo Done!

rm "$tmpFile"

echo
done

0 comments on commit 84853a2

Please sign in to comment.