#!/usr/bin/env bash function build() { ROOT=$(dirname "$0") NAME="edge-user" DIST=$ROOT/"../dist/${NAME}" OS=${1} ARCH=${2} JS_ROOT=$ROOT/../web/public/js if [ -z "$OS" ]; then echo "usage: build.sh OS ARCH" exit fi if [ -z "$ARCH" ]; then echo "usage: build.sh OS ARCH" exit fi VERSION=$(lookup-version "$ROOT"/../internal/const/const.go) ZIP="${NAME}-${OS}-${ARCH}-v${VERSION}.zip" # generate files echo "generating files ..." go run -tags=plus "$ROOT"/../cmd/edge-user/main.go generate if [ "$(which uglifyjs)" ]; then echo "compress to component.js ..." 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 ..." cp "${JS_ROOT}"/components.src.js "${JS_ROOT}"/components.js cp "${JS_ROOT}"/utils.js "${JS_ROOT}"/utils.min.js fi # create dir & copy files echo "copying ..." if [ ! -d "$DIST" ]; then mkdir "$DIST" mkdir "$DIST"/bin mkdir "$DIST"/configs mkdir "$DIST"/logs mkdir "$DIST"/www fi 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/ cp "$ROOT"/configs/api_user.template.yaml "$DIST"/configs/ cp -R "$ROOT"/portal "$DIST"/ # find gcc GCC_DIR="" CC_PATH="" CXX_PATH="" if [ "${ARCH}" == "amd64" ]; then GCC_DIR="/usr/local/gcc/x86_64-unknown-linux-gnu/bin" CC_PATH="x86_64-unknown-linux-gnu-gcc" CXX_PATH="x86_64-unknown-linux-gnu-g++" fi if [ "${ARCH}" == "arm64" ]; then GCC_DIR="/usr/local/gcc/aarch64-unknown-linux-gnu/bin" CC_PATH="aarch64-unknown-linux-gnu-gcc" CXX_PATH="aarch64-unknown-linux-gnu-g++" fi # build echo "building v${VERSION}/${OS}/${ARCH} ..." if [ -f "${GCC_DIR}/${CC_PATH}" ]; then echo " building ${NAME} with gcc ..." env CC="${GCC_DIR}/${CC_PATH}" \ CXX="${GCC_DIR}/${CXX_PATH}" \ CGO_ENABLED=1 \ GOOS="$OS" GOARCH="$ARCH" go build -tags="plus gcc" -trimpath -ldflags="-linkmode external -extldflags -static -s -w" -o "$DIST"/bin/${NAME} "$ROOT"/../cmd/edge-user/main.go else env GOOS="$OS" GOARCH="$ARCH" go build -tags="plus" -trimpath -ldflags="-s -w" -o "$DIST"/bin/${NAME} "$ROOT"/../cmd/edge-user/main.go fi if [ ! -f "${DIST}/bin/${NAME}" ]; then echo "build failed!" exit fi # delete hidden files 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 # 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 VERSION_DATA=$(cat "$FILE") re="Version[ ]+=[ ]+\"([0-9.]+)\"" if [[ $VERSION_DATA =~ $re ]]; then VERSION=${BASH_REMATCH[1]} echo "$VERSION" else echo "could not match version" exit fi } build "$1" "$2"