Cloudreve/build.sh

128 lines
2.6 KiB
Bash
Raw Normal View History

2020-03-09 06:30:43 -05:00
#!/bin/bash
2020-03-09 03:53:01 -05:00
REPO=$(cd $(dirname $0); pwd)
COMMIT_SHA=$(git rev-parse --short HEAD)
VERSION=$(git describe --tags)
ASSETS="false"
BINARY="false"
RELEASE="false"
debugInfo () {
echo "Repo: $REPO"
echo "Build assets: $ASSETS"
echo "Build binary: $BINARY"
echo "Release: $RELEASE"
echo "Version: $VERSION"
2020-03-09 23:46:17 -05:00
echo "Commit: $COMMIT_SHA"
2020-03-09 03:53:01 -05:00
}
buildAssets () {
cd $REPO
rm -rf assets/build
rm -f statik/statik.go
2020-03-11 05:37:57 -05:00
export CI=false
2020-03-09 03:53:01 -05:00
cd $REPO/assets
yarn install
yarn run build
if ! [ -x "$(command -v statik)" ]; then
2020-03-09 21:39:19 -05:00
export CGO_ENABLED=0
2020-03-09 03:53:01 -05:00
go get github.com/rakyll/statik
fi
cd $REPO
statik -src=assets/build/ -include=*.html,*.js,*.json,*.css,*.png,*.svg,*.ico -f
}
buildBinary () {
cd $REPO
go build -a -o cloudreve -ldflags " -X 'github.com/HFO4/cloudreve/pkg/conf.BackendVersion=$VERSION' -X 'github.com/HFO4/cloudreve/pkg/conf.LastCommit=$COMMIT_SHA'"
}
_build() {
local osarch=$1
IFS=/ read -r -a arr <<<"$osarch"
os="${arr[0]}"
arch="${arr[1]}"
2020-03-09 21:39:19 -05:00
gcc="${arr[2]}"
2020-03-09 03:53:01 -05:00
# Go build to build the binary.
export GOOS=$os
export GOARCH=$arch
2020-03-09 21:39:19 -05:00
export CC=$gcc
export CGO_ENABLED=1
out="release/cloudreve_${VERSION}_${os}_${arch}"
go build -a -o "${out}" -ldflags " -X 'github.com/HFO4/cloudreve/pkg/conf.BackendVersion=$VERSION' -X 'github.com/HFO4/cloudreve/pkg/conf.LastCommit=$COMMIT_SHA'"
if [ "$os" = "windows" ]; then
mv $out release/cloudreve.exe
zip -j -q "${out}.zip" release/cloudreve.exe
rm -f "release/cloudreve.exe"
else
mv $out release/cloudreve
tar -zcvf "${out}.tar.gz" -C release cloudreve
rm -f "release/cloudreve"
fi
2020-03-09 03:53:01 -05:00
}
release(){
cd $REPO
## List of architectures and OS to test coss compilation.
2020-03-09 21:39:19 -05:00
SUPPORTED_OSARCH="linux/amd64/gcc linux/arm/arm-linux-gnueabihf-gcc windows/amd64/x86_64-w64-mingw32-gcc"
2020-03-09 03:53:01 -05:00
2020-03-09 21:39:19 -05:00
echo "Release builds for OS/Arch/CC: ${SUPPORTED_OSARCH}"
2020-03-09 03:53:01 -05:00
for each_osarch in ${SUPPORTED_OSARCH}; do
_build "${each_osarch}"
done
}
usage() {
echo "Usage: $0 [-a] [-c] [-b] [-r]" 1>&2;
exit 1;
}
while getopts "bacr:d" o; do
case "${o}" in
b)
ASSETS="true"
BINARY="true"
;;
a)
ASSETS="true"
;;
c)
BINARY="true"
;;
r)
2020-03-09 22:07:37 -05:00
ASSETS="true"
2020-03-09 03:53:01 -05:00
RELEASE="true"
;;
d)
DEBUG="true"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ "$DEBUG" = "true" ]; then
debugInfo
fi
if [ "$ASSETS" = "true" ]; then
buildAssets
fi
if [ "$BINARY" = "true" ]; then
buildBinary
fi
if [ "$RELEASE" = "true" ]; then
release
2020-03-09 21:39:19 -05:00
fi