Files
dygou_go/build.sh
T
2026-06-26 15:29:26 +08:00

215 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
# 斗鱼抢购系统 - 一键启动/构建脚本
# 用法:
# ./build.sh # 开发模式(带热更新)
# ./build.sh dev # 开发模式(带热更新)
# ./build.sh build # 生产构建
# ./build.sh win # 构建 Windows 版本
# ./build.sh linux # 构建 Linux 版本
# ./build.sh clean # 清理构建产物
# ./build.sh install # 安装前端依赖
set -e
# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 获取版本号
get_version() {
grep '"version"' wails.json | head -1 | sed 's/.*"version": *"//;s/".*//'
}
# 打印带颜色的消息
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
# 检查依赖
check_deps() {
local missing=()
if ! command -v wails &> /dev/null; then
missing+=("wails")
fi
if ! command -v go &> /dev/null; then
missing+=("go")
fi
if ! command -v node &> /dev/null; then
missing+=("node")
fi
if ! command -v npm &> /dev/null; then
missing+=("npm")
fi
if [ ${#missing[@]} -ne 0 ]; then
error "缺少依赖: ${missing[*]}"
fi
}
# 安装前端依赖
install_deps() {
info "安装前端依赖..."
cd frontend
npm install
cd ..
success "前端依赖安装完成"
}
# 开发模式
dev_mode() {
local VERSION=$(get_version)
info "启动开发模式 v${VERSION}"
info "前端热更新 + 后端自动重编译"
echo ""
echo -e "${YELLOW}提示: 按 Ctrl+C 退出${NC}"
echo ""
wails dev
}
# 生产构建
build_production() {
local VERSION=$(get_version)
local BUILD_TIME=$(date '+%Y-%m-%d %H:%M:%S')
info "开始生产构建 v${VERSION} 构建时间=${BUILD_TIME}"
# 类型检查
info "TypeScript 类型检查..."
cd frontend
npm run type-check
cd ..
# Wails 构建,注入构建时间
wails build -ldflags "-X 'douyu_go/internal/constants.BuildTime=${BUILD_TIME}'"
success "构建完成!"
echo ""
echo "=== 构建产物 ==="
ls -lh build/bin/
}
# 构建 Windows 版本
build_windows() {
local VERSION=$(get_version)
local BUILD_TIME=$(date '+%Y-%m-%d %H:%M:%S')
local DATE_TAG=$(date '+%y.%-m.%-d')
local OUTPUT_NAME="douyu_go_v${VERSION}_${DATE_TAG}.exe"
info "构建 Windows 版本 v${VERSION} 构建时间=${BUILD_TIME}"
GOOS=windows GOARCH=amd64 wails build -platform windows/amd64 -o "${OUTPUT_NAME}" -ldflags "-X 'douyu_go/internal/constants.BuildTime=${BUILD_TIME}'"
success "Windows 构建完成!"
echo ""
echo "=== 构建产物 ==="
ls -lh build/bin/
}
# 构建 Linux 版本
build_linux() {
local VERSION=$(get_version)
local BUILD_TIME=$(date '+%Y-%m-%d %H:%M:%S')
local DATE_TAG=$(date '+%y.%-m.%-d')
local OUTPUT_NAME="douyu_go_v${VERSION}_${DATE_TAG}"
info "构建 Linux 版本 v${VERSION} 构建时间=${BUILD_TIME}"
GOOS=linux GOARCH=amd64 wails build -platform linux/amd64 -o "${OUTPUT_NAME}" -ldflags "-X 'douyu_go/internal/constants.BuildTime=${BUILD_TIME}'"
success "Linux 构建完成!"
echo ""
echo "=== 构建产物 ==="
ls -lh build/bin/
}
# 清理构建产物
clean_build() {
info "清理构建产物..."
rm -rf build/bin/*
rm -rf frontend/dist
rm -rf frontend/node_modules/.vite
success "清理完成"
}
# 显示帮助
show_help() {
echo "斗鱼抢购系统 - 一键启动/构建脚本"
echo ""
echo "用法:"
echo " ./build.sh # 开发模式(带热更新)"
echo " ./build.sh dev # 开发模式(带热更新)"
echo " ./build.sh build # 生产构建"
echo " ./build.sh win # 构建 Windows 版本"
echo " ./build.sh linux # 构建 Linux 版本"
echo " ./build.sh clean # 清理构建产物"
echo " ./build.sh install # 安装前端依赖"
echo " ./build.sh help # 显示此帮助"
echo ""
echo "环境变量:"
echo " GOOS 目标操作系统 (windows/linux/darwin)"
echo " GOARCH 目标架构 (amd64/arm64)"
}
# 主函数
main() {
# 检查依赖
check_deps
# 解析命令
local command="${1:-dev}"
case "$command" in
dev)
dev_mode
;;
build)
build_production
;;
win|windows)
build_windows
;;
linux)
build_linux
;;
clean)
clean_build
;;
install)
install_deps
;;
help|-h|--help)
show_help
;;
*)
error "未知命令: $command\n\n使用 ./build.sh help 查看帮助"
;;
esac
}
# 运行主函数
main "$@"