#!/bin/bash # # FunMC 客户端构建脚本 # 用法: ./scripts/build-client.sh [平台] [服务器URL] # # 示例: # ./scripts/build-client.sh all http://funmc.com:3000 # ./scripts/build-client.sh windows http://192.168.1.100:3000 # ./scripts/build-client.sh macos-arm64 https://mc.example.com # set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' # 默认值 PLATFORM="${1:-all}" SERVER_URL="${2:-}" VERSION=$(grep '^version' client/Cargo.toml | head -1 | cut -d'"' -f2) OUTPUT_DIR="./dist" echo -e "${CYAN}" echo "╔═══════════════════════════════════════════════════════════╗" echo "║ FunMC 客户端构建脚本 v${VERSION} ║" echo "║ 魔幻方开发 ║" echo "╚═══════════════════════════════════════════════════════════╝" echo -e "${NC}" # 检查依赖 check_deps() { echo -e "${YELLOW}检查构建依赖...${NC}" if ! command -v cargo &> /dev/null; then echo -e "${RED}错误: 未找到 Rust/Cargo${NC}" exit 1 fi if ! command -v node &> /dev/null; then echo -e "${RED}错误: 未找到 Node.js${NC}" exit 1 fi if ! command -v npm &> /dev/null; then echo -e "${RED}错误: 未找到 npm${NC}" exit 1 fi echo -e "${GREEN}✓ 依赖检查通过${NC}" } # 写入服务器配置 write_config() { if [ -n "$SERVER_URL" ]; then echo -e "${YELLOW}写入服务器配置: $SERVER_URL${NC}" cat > client/ui/src/config.json << EOF { "server_url": "$SERVER_URL", "version": "$VERSION" } EOF echo -e "${GREEN}✓ 配置写入完成${NC}" fi } # 构建前端 build_frontend() { echo -e "${YELLOW}构建前端...${NC}" cd client/ui npm install npm run build cd ../.. echo -e "${GREEN}✓ 前端构建完成${NC}" } # 构建 Windows build_windows() { echo -e "${YELLOW}构建 Windows (x64)...${NC}" cd client if [[ "$OSTYPE" == "linux-gnu"* ]] || [[ "$OSTYPE" == "darwin"* ]]; then # Cross compile cargo tauri build --target x86_64-pc-windows-msvc else cargo tauri build fi cd .. mkdir -p $OUTPUT_DIR cp client/target/release/bundle/msi/*.msi "$OUTPUT_DIR/FunMC-${VERSION}-windows-x64.msi" 2>/dev/null || true cp client/target/release/bundle/nsis/*.exe "$OUTPUT_DIR/FunMC-${VERSION}-windows-x64.exe" 2>/dev/null || true echo -e "${GREEN}✓ Windows 构建完成${NC}" } # 构建 macOS build_macos_arm64() { echo -e "${YELLOW}构建 macOS (Apple Silicon)...${NC}" cd client cargo tauri build --target aarch64-apple-darwin cd .. mkdir -p $OUTPUT_DIR cp client/target/aarch64-apple-darwin/release/bundle/dmg/*.dmg "$OUTPUT_DIR/FunMC-${VERSION}-macos-arm64.dmg" 2>/dev/null || true echo -e "${GREEN}✓ macOS (ARM64) 构建完成${NC}" } build_macos_x64() { echo -e "${YELLOW}构建 macOS (Intel)...${NC}" cd client cargo tauri build --target x86_64-apple-darwin cd .. mkdir -p $OUTPUT_DIR cp client/target/x86_64-apple-darwin/release/bundle/dmg/*.dmg "$OUTPUT_DIR/FunMC-${VERSION}-macos-x64.dmg" 2>/dev/null || true echo -e "${GREEN}✓ macOS (x64) 构建完成${NC}" } # 构建 Linux build_linux() { echo -e "${YELLOW}构建 Linux (x64)...${NC}" cd client cargo tauri build cd .. mkdir -p $OUTPUT_DIR cp client/target/release/bundle/appimage/*.AppImage "$OUTPUT_DIR/FunMC-${VERSION}-linux-x64.AppImage" 2>/dev/null || true cp client/target/release/bundle/deb/*.deb "$OUTPUT_DIR/FunMC-${VERSION}-linux-x64.deb" 2>/dev/null || true echo -e "${GREEN}✓ Linux 构建完成${NC}" } # 构建 Android build_android() { echo -e "${YELLOW}构建 Android...${NC}" if [ -z "$ANDROID_HOME" ]; then echo -e "${RED}错误: 未设置 ANDROID_HOME${NC}" return 1 fi cd client cargo tauri android build --apk cd .. mkdir -p $OUTPUT_DIR cp client/gen/android/app/build/outputs/apk/universal/release/*.apk "$OUTPUT_DIR/FunMC-${VERSION}-android.apk" 2>/dev/null || true echo -e "${GREEN}✓ Android 构建完成${NC}" } # 构建 iOS build_ios() { echo -e "${YELLOW}构建 iOS...${NC}" if [[ "$OSTYPE" != "darwin"* ]]; then echo -e "${RED}错误: iOS 只能在 macOS 上构建${NC}" return 1 fi cd client cargo tauri ios build cd .. echo -e "${GREEN}✓ iOS 构建完成 (需要在 Xcode 中归档和分发)${NC}" } # 显示构建结果 show_results() { echo "" echo -e "${CYAN}╔═══════════════════════════════════════════════════════════╗${NC}" echo -e "${CYAN}║ 构建完成! ║${NC}" echo -e "${CYAN}╚═══════════════════════════════════════════════════════════╝${NC}" echo "" echo -e "${GREEN}输出目录: $OUTPUT_DIR${NC}" echo "" if [ -d "$OUTPUT_DIR" ]; then echo "构建产物:" ls -lh $OUTPUT_DIR/ fi echo "" echo -e "${YELLOW}部署说明:${NC}" echo "1. 将 $OUTPUT_DIR 目录下的文件复制到服务器的 downloads 目录" echo "2. 用户访问 http://your-server:3000/download 即可下载" echo "" echo -e "${GREEN}魔幻方开发${NC}" } # 主流程 main() { check_deps write_config build_frontend case $PLATFORM in windows) build_windows ;; macos-arm64) build_macos_arm64 ;; macos-x64) build_macos_x64 ;; macos) build_macos_arm64 build_macos_x64 ;; linux) build_linux ;; android) build_android ;; ios) build_ios ;; all) build_windows || true build_macos_arm64 || true build_macos_x64 || true build_linux || true build_android || true ;; *) echo -e "${RED}未知平台: $PLATFORM${NC}" echo "支持的平台: windows, macos-arm64, macos-x64, macos, linux, android, ios, all" exit 1 ;; esac show_results } main "$@"