alist/build.sh

156 lines
5.4 KiB
Bash
Raw Normal View History

2021-11-03 11:51:19 +00:00
#!/bin/bash
# 构建前端,在当前目录产生一个dist文件夹
BUILD_WEB() {
git clone https://github.com/alist-org/alist-web.git
cd alist-web
2021-11-09 08:46:03 +00:00
yarn
2022-02-21 12:31:01 +00:00
yarn build
2022-03-04 01:50:47 +00:00
sed -i -e "s/\/CDN_URL\//\//g" dist/index.html
sed -i -e "s/assets/\/assets/g" dist/index.html
rm -f dist/index.html-e
mv dist ..
2022-02-27 12:28:42 +00:00
cd .. || exit
rm -rf alist-web
}
2021-11-09 08:46:03 +00:00
CDN_WEB() {
curl -L https://github.com/alist-org/alist-web/releases/latest/download/dist.tar.gz -o dist.tar.gz
tar -zxvf dist.tar.gz
rm -f dist.tar.gz
}
2021-11-09 13:36:27 +00:00
# 在DOCKER中构建
BUILD_DOCKER() {
2021-11-09 08:46:03 +00:00
appName="alist"
builtAt="$(date +'%F %T %z')"
goVersion=$(go version | sed 's/go version //')
gitAuthor=$(git show -s --format='format:%aN <%ae>' HEAD)
gitCommit=$(git log --pretty=format:"%h" -1)
2021-11-09 09:54:38 +00:00
gitTag=$(git describe --long --tags --dirty --always)
2022-02-20 07:14:18 +00:00
webTag=$(wget -qO- -t1 -T2 "https://api.github.com/repos/alist-org/alist-web/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
2021-11-09 08:46:03 +00:00
ldflags="\
-w -s \
-X 'github.com/Xhofe/alist/conf.BuiltAt=$builtAt' \
-X 'github.com/Xhofe/alist/conf.GoVersion=$goVersion' \
-X 'github.com/Xhofe/alist/conf.GitAuthor=$gitAuthor' \
-X 'github.com/Xhofe/alist/conf.GitCommit=$gitCommit' \
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
2022-02-20 07:14:18 +00:00
-X 'github.com/Xhofe/alist/conf.WebTag=$webTag' \
"
go build -o ./bin/alist -ldflags="$ldflags" -tags=jsoniter alist.go
}
2021-11-03 11:51:19 +00:00
BUILD() {
cd alist
appName="alist"
builtAt="$(date +'%F %T %z')"
goVersion=$(go version | sed 's/go version //')
gitAuthor=$(git show -s --format='format:%aN <%ae>' HEAD)
gitCommit=$(git log --pretty=format:"%h" -1)
gitTag=$(git describe --long --tags --dirty --always)
2022-02-20 07:14:18 +00:00
webTag=$(wget -qO- -t1 -T2 "https://api.github.com/repos/alist-org/alist-web/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
2022-02-25 15:55:57 +00:00
echo "build version: $gitTag"
ldflags="\
2021-11-03 11:51:19 +00:00
-w -s \
2021-11-04 05:23:17 +00:00
-X 'github.com/Xhofe/alist/conf.BuiltAt=$builtAt' \
-X 'github.com/Xhofe/alist/conf.GoVersion=$goVersion' \
-X 'github.com/Xhofe/alist/conf.GitAuthor=$gitAuthor' \
-X 'github.com/Xhofe/alist/conf.GitCommit=$gitCommit' \
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
2022-02-20 07:14:18 +00:00
-X 'github.com/Xhofe/alist/conf.WebTag=$webTag' \
2022-02-25 15:55:57 +00:00
"
if [ "$1" == "release" ]; then
2022-02-25 15:55:57 +00:00
xgo -out "$appName" -ldflags="$ldflags" -tags=jsoniter .
else
xgo -targets=linux/amd64,windows/amd64,darwin/amd64 -out "$appName" -ldflags="$ldflags" -tags=jsoniter .
2022-01-20 05:47:21 +00:00
fi
2022-02-25 15:55:57 +00:00
mkdir -p "build"
mv alist-* build
if [ "$1" != "release" ]; then
cd build
2022-02-25 16:12:00 +00:00
upx -9 ./alist-linux*
upx -9 ./alist-windows*
2022-02-25 15:55:57 +00:00
find . -type f -print0 | xargs -0 md5sum >md5.txt
cat md5.txt
2022-02-27 12:28:42 +00:00
cd .. || exit
2022-02-25 15:55:57 +00:00
fi
2022-02-27 12:28:42 +00:00
cd .. || exit
2022-02-25 15:55:57 +00:00
}
BUILD_MUSL() {
BASE="https://musl.cc/"
FILES=(x86_64-linux-musl-cross aarch64-linux-musl-cross arm-linux-musleabihf-cross mips-linux-musl-cross mips64-linux-musl-cross mips64el-linux-musl-cross mipsel-linux-musl-cross powerpc64le-linux-musl-cross s390x-linux-musl-cross)
for i in "${FILES[@]}"; do
url="${BASE}${i}.tgz"
curl -L -o "${i}.tgz" "${url}"
sudo tar xf "${i}.tgz" --strip-components 1 -C /usr/local
done
cd alist
appName="alist"
builtAt="$(date +'%F %T %z')"
goVersion=$(go version | sed 's/go version //')
gitAuthor=$(git show -s --format='format:%aN <%ae>' HEAD)
gitCommit=$(git log --pretty=format:"%h" -1)
gitTag=$(git describe --long --tags --dirty --always)
webTag=$(wget -qO- -t1 -T2 "https://api.github.com/repos/alist-org/alist-web/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
ldflags="\
-w -s \
-X 'github.com/Xhofe/alist/conf.BuiltAt=$builtAt' \
-X 'github.com/Xhofe/alist/conf.GoVersion=$goVersion' \
-X 'github.com/Xhofe/alist/conf.GitAuthor=$gitAuthor' \
-X 'github.com/Xhofe/alist/conf.GitCommit=$gitCommit' \
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
-X 'github.com/Xhofe/alist/conf.WebTag=$webTag' \
"
OS_ARCHES=(linux-musl-amd64 linux-musl-arm64 linux-musl-arm linux-musl-mips linux-musl-mips64 linux-musl-mips64le linux-musl-mipsle linux-musl-ppc64le linux-musl-s390x)
CGO_ARGS=(x86_64-linux-musl-gcc aarch64-linux-musl-gcc arm-linux-musleabihf-gcc mips-linux-musl-gcc mips64-linux-musl-gcc mips64el-linux-musl-gcc mipsel-linux-musl-gcc powerpc64le-linux-musl-gcc s390x-linux-musl-gcc)
2022-01-20 06:23:31 +00:00
for i in "${!OS_ARCHES[@]}"; do
os_arch=${OS_ARCHES[$i]}
2022-02-25 15:55:57 +00:00
cgo_cc=${CGO_ARGS[$i]}
echo building for ${os_arch}
2022-02-25 15:55:57 +00:00
export GOOS=${os_arch%%-*}
export GOARCH=${os_arch##*-}
export CC=${cgo_cc}
export CGO_ENABLED=1
go build -o ./build/$appName-$os_arch -ldflags="$ldflags" -tags=jsoniter alist.go
done
2022-02-27 12:28:42 +00:00
cd .. || exit
}
RELEASE() {
cd alist/build
2022-03-03 11:44:13 +00:00
upx -9 ./alist-linux-amd64
2022-02-25 16:12:00 +00:00
upx -9 ./alist-windows*
2022-02-25 15:55:57 +00:00
find . -type f -print0 | xargs -0 md5sum >md5.txt
cat md5.txt
mkdir compress
mv md5.txt compress
2022-02-25 15:55:57 +00:00
for i in $(find . -type f -name "$appName-linux-*"); do
tar -czvf compress/"$i".tar.gz "$i"
done
2022-02-25 15:55:57 +00:00
for i in $(find . -type f -name "$appName-darwin-*"); do
tar -czvf compress/"$i".tar.gz "$i"
done
2022-02-25 15:55:57 +00:00
for i in $(find . -type f -name "$appName-windows-*"); do
zip compress/$(echo $i | sed 's/\.[^.]*$//').zip "$i"
done
2022-02-27 12:28:42 +00:00
cd ../.. || exit
}
if [ "$1" = "web" ]; then
BUILD_WEB
elif [ "$1" = "cdn" ]; then
CDN_WEB
elif [ "$1" = "docker" ]; then
BUILD_DOCKER
elif [ "$1" = "build" ]; then
2022-01-11 17:04:50 +00:00
BUILD build
elif [ "$1" = "release" ]; then
2022-01-11 17:04:50 +00:00
BUILD release
2022-02-25 15:55:57 +00:00
BUILD_MUSL
RELEASE
2021-11-04 05:23:17 +00:00
else
2022-01-18 10:19:58 +00:00
echo -e "${RED_COLOR} Parameter error ${RES}"
2022-02-25 15:55:57 +00:00
fi