Files
waf-platform/EdgeAdmin/build/build.sh
2026-03-22 17:37:40 +08:00

224 lines
6.3 KiB
Bash

#!/usr/bin/env bash
set -e
function verify_components_bundle() {
local file_path="$1"
if [ ! -f "$file_path" ]; then
echo "[error] components.js not found: $file_path"
return 1
fi
local file_size
file_size=$(wc -c < "$file_path")
if [ "$file_size" -lt 100000 ]; then
echo "[error] components.js looks too small ($file_size bytes), generate likely failed"
return 1
fi
if ! grep -q 'Vue.component("csrf-token"' "$file_path"; then
echo "[error] components.js missing csrf-token component, generate likely failed"
return 1
fi
echo "verify components.js: ok ($file_size bytes)"
return 0
}
function build() {
ROOT=$(dirname "$0")
JS_ROOT=$ROOT/../web/public/js
NAME="edge-admin"
DIST=$ROOT/"../dist/${NAME}"
OS=${1}
ARCH=${2}
TAG=${3}
if [ -z "$OS" ]; then
echo "usage: build.sh OS ARCH"
exit
fi
if [ -z "$ARCH" ]; then
echo "usage: build.sh OS ARCH"
exit
fi
if [ -z "$TAG" ]; then
TAG="community"
fi
# checking environment
echo "checking required commands ..."
commands=("zip" "unzip" "go" "find" "sed" "protoc")
for cmd in "${commands[@]}"; do
if [ "$(which "${cmd}")" ]; then
echo "checking ${cmd}: ok"
else
echo "checking ${cmd}: not found"
return
fi
done
VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
# 鐢熸垚 zip 鏂囦欢鍚嶆椂涓嶅寘鍚?plus 鏍囪
if [ "${TAG}" = "plus" ]; then
ZIP="${NAME}-${OS}-${ARCH}-v${VERSION}.zip"
else
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
fi
# regenerate protobuf files to keep rawDesc in sync with .proto
echo "regenerating protobuf files ..."
EDGE_COMMON_BUILD="$ROOT/../../EdgeCommon/build"
if [ -f "$EDGE_COMMON_BUILD/build.sh" ]; then
(cd "$EDGE_COMMON_BUILD" && ./build.sh)
if [ $? -ne 0 ]; then
echo "protobuf generation failed! Fix errors before building."
exit 1
fi
else
echo "EdgeCommon/build/build.sh not found, skipping protobuf generation"
fi
# build edge-api
APINodeVersion=$(lookup-version "$ROOT""/../../EdgeAPI/internal/const/const.go")
echo "building edge-api v${APINodeVersion} ..."
EDGE_API_BUILD_SCRIPT=$ROOT"/../../EdgeAPI/build/build.sh"
if [ ! -f "$EDGE_API_BUILD_SCRIPT" ]; then
echo "unable to find edge-api build script 'EdgeAPI/build/build.sh'"
exit
fi
cd "$ROOT""/../../EdgeAPI/build" || exit
echo "=============================="
./build.sh "$OS" "$ARCH" $TAG
echo "=============================="
cd - || exit
# generate files
echo "generating files ..."
env TEAROOT="$ROOT" CGO_ENABLED=0 go run -tags "$TAG" "$ROOT"/../cmd/edge-admin/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
verify_components_bundle "${JS_ROOT}/components.js"
# create dir & copy files
echo "copying ..."
rm -rf "$DIST"
mkdir -p "$DIST"/bin
mkdir -p "$DIST"/configs
mkdir -p "$DIST"/logs
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/
# change _plus.[ext] to .[ext]
if [ "${TAG}" = "plus" ]; then
echo "converting filenames ..."
exts=("html" "js" "css")
for ext in "${exts[@]}"; do
pattern="*_plus."${ext}
find "$DIST"/web/views -type f -name "$pattern" | \
while read filename; do
mv "${filename}" "${filename/_plus."${ext}"/."${ext}"}"
done
done
fi
# 鏌ユ壘 EdgeAPI zip 鏂囦欢鏃朵笉鍖呭惈 plus 鏍囪
if [ "${TAG}" = "plus" ]; then
EDGE_API_ZIP_FILE=$ROOT"/../../EdgeAPI/dist/edge-api-${OS}-${ARCH}-v${APINodeVersion}.zip"
else
EDGE_API_ZIP_FILE=$ROOT"/../../EdgeAPI/dist/edge-api-${OS}-${ARCH}-${TAG}-v${APINodeVersion}.zip"
fi
cp "$EDGE_API_ZIP_FILE" "$DIST"/
cd "$DIST"/ || exit
unzip -q "$(basename "$EDGE_API_ZIP_FILE")"
rm -f "$(basename "$EDGE_API_ZIP_FILE")"
# ensure edge-api package always contains fluent-bit runtime assets/packages
# 鍒犻櫎 MaxMind 鏁版嵁搴撴枃浠讹紙浣跨敤宓屽叆鐨勬暟鎹簱锛屼笉闇€瑕佸閮ㄦ枃浠讹級
find . -name "*.mmdb" -type f -delete
find . -type d -name "iplibrary" -empty -delete
cd - || exit
# 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 ${NAME} ..."
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 -trimpath -tags "${TAG} gcc" -ldflags="-linkmode external -extldflags -static -s -w" -o "$DIST"/bin/${NAME} "$ROOT"/../cmd/edge-admin/main.go
else
GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags $TAG -ldflags="-s -w" -o "$DIST"/bin/${NAME} "$ROOT"/../cmd/edge-admin/main.go
fi
if [ ! -f "${DIST}/bin/${NAME}" ]; then
echo "build '${NAME}' 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
# 鍒犻櫎 MaxMind 鏁版嵁搴撴枃浠讹紙浣跨敤宓屽叆鐨勬暟鎹簱锛屼笉闇€瑕佸閮ㄦ枃浠讹級
find "$DIST" -name "*.mmdb" -type f -delete
find "$DIST" -type d -name "iplibrary" -empty -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" "$3"