Files
EdgeAdmin/build/build.sh

165 lines
4.3 KiB
Bash
Raw Permalink Normal View History

2020-10-14 14:46:22 +08:00
#!/usr/bin/env bash
function build() {
2022-06-29 17:00:05 +08:00
ROOT=$(dirname "$0")
JS_ROOT=$ROOT/../web/public/js
2020-10-14 14:46:22 +08:00
NAME="edge-admin"
DIST=$ROOT/"../dist/${NAME}"
OS=${1}
ARCH=${2}
2021-07-25 16:28:48 +08:00
TAG=${3}
2020-10-14 14:46:22 +08:00
2022-06-29 17:00:05 +08:00
if [ -z "$OS" ]; then
2020-10-14 14:46:22 +08:00
echo "usage: build.sh OS ARCH"
exit
fi
2022-06-29 17:00:05 +08:00
if [ -z "$ARCH" ]; then
2020-10-14 14:46:22 +08:00
echo "usage: build.sh OS ARCH"
exit
fi
2022-06-29 17:00:05 +08:00
if [ -z "$TAG" ]; then
2021-07-25 16:28:48 +08:00
TAG="community"
fi
2020-10-14 14:46:22 +08:00
2021-11-30 11:05:58 +08:00
# checking environment
echo "checking required commands ..."
commands=("zip" "unzip" "go" "find" "sed")
for cmd in "${commands[@]}"; do
2022-06-29 17:00:05 +08:00
if [ "$(which "${cmd}")" ]; then
2021-11-30 11:05:58 +08:00
echo "checking ${cmd}: ok"
else
echo "checking ${cmd}: not found"
return
fi
done
2022-06-29 17:00:05 +08:00
VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
2021-07-25 16:28:48 +08:00
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
2020-10-14 14:46:22 +08:00
2021-08-11 17:16:36 +08:00
# build edge-api
2022-06-29 17:00:05 +08:00
APINodeVersion=$(lookup-version "$ROOT""/../../EdgeAPI/internal/const/const.go")
2020-10-14 14:46:22 +08:00
echo "building edge-api v${APINodeVersion} ..."
EDGE_API_BUILD_SCRIPT=$ROOT"/../../EdgeAPI/build/build.sh"
2022-06-29 17:00:05 +08:00
if [ ! -f "$EDGE_API_BUILD_SCRIPT" ]; then
2020-10-14 14:46:22 +08:00
echo "unable to find edge-api build script 'EdgeAPI/build/build.sh'"
exit
fi
2022-06-29 17:00:05 +08:00
cd "$ROOT""/../../EdgeAPI/build" || exit
2020-10-14 14:46:22 +08:00
echo "=============================="
2022-06-29 17:00:05 +08:00
./build.sh "$OS" "$ARCH" $TAG
2020-10-14 14:46:22 +08:00
echo "=============================="
2022-06-29 17:00:05 +08:00
cd - || exit
2020-10-14 14:46:22 +08:00
# generate files
echo "generating files ..."
2024-03-18 15:59:29 +08:00
env CGO_ENABLED=0 go run -tags $TAG "$ROOT"/../cmd/edge-admin/main.go generate
2022-06-29 17:00:05 +08:00
if [ "$(which uglifyjs)" ]; then
echo "compress to component.js ..."
2022-06-29 17:00:05 +08:00
uglifyjs --compress --mangle -- "${JS_ROOT}"/components.src.js > "${JS_ROOT}"/components.js
uglifyjs --compress --mangle -- "${JS_ROOT}"/utils.js > "${JS_ROOT}"/utils.min.js
else
echo "copy to component.js ..."
2022-06-29 17:00:05 +08:00
cp "${JS_ROOT}"/components.src.js "${JS_ROOT}"/components.js
cp "${JS_ROOT}"/utils.js "${JS_ROOT}"/utils.min.js
fi
2020-10-14 14:46:22 +08:00
# create dir & copy files
echo "copying ..."
2022-06-29 17:00:05 +08:00
if [ ! -d "$DIST" ]; then
mkdir "$DIST"
mkdir "$DIST"/bin
mkdir "$DIST"/configs
mkdir "$DIST"/logs
2020-10-14 14:46:22 +08:00
fi
2022-06-29 17:00:05 +08:00
cp -R "$ROOT"/../web "$DIST"/
rm -f "$DIST"/web/tmp/*
rm -rf "$DIST"/web/public/js/components
rm -f "$DIST"/web/public/js/components.src.js
cp "$ROOT"/configs/server.template.yaml "$DIST"/configs/
2020-10-14 14:46:22 +08:00
2021-11-30 11:05:58 +08:00
# change _plus.[ext] to .[ext]
if [ "${TAG}" = "plus" ]; then
echo "converting filenames ..."
exts=("html" "js" "css")
for ext in "${exts[@]}"; do
pattern="*_plus."${ext}
2022-06-29 17:00:05 +08:00
find "$DIST"/web/views -type f -name "$pattern" | \
2021-11-30 11:05:58 +08:00
while read filename; do
2022-06-29 17:00:05 +08:00
mv "${filename}" "${filename/_plus."${ext}"/."${ext}"}"
2021-11-30 11:05:58 +08:00
done
done
fi
2021-07-25 16:28:48 +08:00
EDGE_API_ZIP_FILE=$ROOT"/../../EdgeAPI/dist/edge-api-${OS}-${ARCH}-${TAG}-v${APINodeVersion}.zip"
2022-06-29 17:00:05 +08:00
cp "$EDGE_API_ZIP_FILE" "$DIST"/
cd "$DIST"/ || exit
unzip -q "$(basename "$EDGE_API_ZIP_FILE")"
rm -f "$(basename "$EDGE_API_ZIP_FILE")"
cd - || exit
2020-10-14 14:46:22 +08:00
2024-03-18 15:59:29 +08:00
# find gcc
GCC_DIR=""
CC_PATH=""
CXX_PATH=""
2024-07-27 15:42:58 +08:00
if [[ "${ARCH}" == "amd64" && "${OS}" == "linux" ]]; then
CC_PATH=$(command -v x86_64-linux-musl-gcc)
CXX_PATH=$(command -v x86_64-linux-musl-g++)
2024-03-18 15:59:29 +08:00
fi
2024-07-27 15:42:58 +08:00
if [[ "${ARCH}" == "arm64" && "${OS}" == "linux" ]]; then
CC_PATH=$(command -v aarch64-linux-musl-gcc)
CXX_PATH=$(command -v aarch64-linux-musl-g++)
2024-03-18 15:59:29 +08:00
fi
2020-10-14 14:46:22 +08:00
# build
2022-06-29 17:00:05 +08:00
echo "building ${NAME} ..."
2024-07-27 15:42:58 +08:00
if [ -f $CC_PATH ]; then
echo " building ${NAME} with musl gcc ..."
env CC=$CC_PATH \
CXX=$CXX_PATH \
2024-03-18 15:59:29 +08:00
CGO_ENABLED=1 \
GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags "${TAG} gcc" -ldflags="-linkmode external -extldflags -static -s -w" -o "$DIST"/bin/${NAME} "$ROOT"/../cmd/edge-admin/main.go
2024-03-18 15:59:29 +08:00
else
GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags $TAG -ldflags="-s -w" -o "$DIST"/bin/${NAME} "$ROOT"/../cmd/edge-admin/main.go
fi
2024-01-22 18:48:51 +08:00
if [ ! -f "${DIST}/bin/${NAME}" ]; then
2024-03-18 15:59:29 +08:00
echo "build '${NAME}' failed!"
2024-01-22 18:48:51 +08:00
exit
fi
2020-10-14 14:46:22 +08:00
2020-11-10 21:37:48 +08:00
# delete hidden files
2022-06-29 17:00:05 +08:00
find "$DIST" -name ".DS_Store" -delete
find "$DIST" -name ".gitignore" -delete
find "$DIST" -name "*.less" -delete
#find "$DIST" -name "*.css.map" -delete
#find "$DIST" -name "*.js.map" -delete
2020-11-10 21:37:48 +08:00
2020-10-14 14:46:22 +08:00
# zip
echo "zip files ..."
cd "${DIST}/../" || exit
if [ -f "${ZIP}" ]; then
rm -f "${ZIP}"
fi
zip -r -X -q "${ZIP}" ${NAME}/
rm -rf ${NAME}
cd - || exit
echo "[done]"
}
function lookup-version() {
FILE=$1
2022-06-29 17:00:05 +08:00
VERSION_DATA=$(cat "$FILE")
2020-10-14 14:46:22 +08:00
re="Version[ ]+=[ ]+\"([0-9.]+)\""
if [[ $VERSION_DATA =~ $re ]]; then
VERSION=${BASH_REMATCH[1]}
2022-06-29 17:00:05 +08:00
echo "$VERSION"
2020-10-14 14:46:22 +08:00
else
echo "could not match version"
exit
fi
}
2022-06-29 17:00:05 +08:00
build "$1" "$2" "$3"