Files
cocos/tools/deploy-to-main.sh
刘宇飞 cba5105908 Initial Cocos Creator port of main-site Unity WebGL game.
Includes core gameplay, 600 exported levels, visual assets, web bridge, and bootstrap scene.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-22 14:57:46 +08:00

93 lines
2.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 将 Cocos Web 构建产物 + cocos-bridge 合并到主站静态目录
#
# 用法:
# ./tools/deploy-to-main.sh \
# --build build/web-desktop \
# --target "/path/to/主站静态目录/cocos"
#
# 示例(部署到 Unity 项目 Template 旁,便于本地对比):
# ./tools/deploy-to-main.sh \
# --build build/web-desktop \
# --target "/Users/liuyufei/tfrh/主站文件/主站/Template/cocos"
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_DIR=""
TARGET_DIR=""
while [[ $# -gt 0 ]]; do
case "$1" in
--build) BUILD_DIR="$2"; shift 2 ;;
--target) TARGET_DIR="$2"; shift 2 ;;
-h|--help)
echo "Usage: $0 --build <build/web-desktop> --target <主站/cocos目录>"
exit 0
;;
*) echo "Unknown arg: $1"; exit 1 ;;
esac
done
if [[ -z "$BUILD_DIR" || -z "$TARGET_DIR" ]]; then
echo "Error: 需要 --build 和 --target"
echo "Example:"
echo " $0 --build build/web-desktop --target /path/to/main-site/cocos"
exit 1
fi
BUILD_DIR="$(cd "$BUILD_DIR" 2>/dev/null && pwd || true)"
if [[ -z "$BUILD_DIR" || ! -f "$BUILD_DIR/index.html" ]]; then
echo "Error: 构建目录无效或缺少 index.html: $BUILD_DIR"
echo "请先在 Cocos Creator 中执行: 项目 → 构建发布 → Web Desktop"
exit 1
fi
mkdir -p "$TARGET_DIR"
echo ">>> 复制构建产物: $BUILD_DIR -> $TARGET_DIR"
rsync -a --delete \
--exclude '.DS_Store' \
"$BUILD_DIR/" "$TARGET_DIR/"
echo ">>> 复制 cocos-bridge.js"
cp "$PROJECT_DIR/web-template/cocos-bridge.js" "$TARGET_DIR/../cocos-bridge.js" 2>/dev/null || \
cp "$PROJECT_DIR/web-template/cocos-bridge.js" "$TARGET_DIR/cocos-bridge.js"
# 若 target 是 .../cocosbridge 放在上一级(与 index 同级)
if [[ -f "$TARGET_DIR/cocos-bridge.js" ]]; then
BRIDGE_PATH="$TARGET_DIR/cocos-bridge.js"
else
BRIDGE_PATH="$(dirname "$TARGET_DIR")/cocos-bridge.js"
cp "$PROJECT_DIR/web-template/cocos-bridge.js" "$BRIDGE_PATH"
fi
echo ">>> 生成联调页 cocos-demo.html"
DEMO="$PROJECT_DIR/web-template/main-site.html"
if [[ -f "$DEMO" ]]; then
cp "$DEMO" "$(dirname "$TARGET_DIR")/cocos-demo.html" 2>/dev/null || \
cp "$DEMO" "$TARGET_DIR/cocos-demo.html"
fi
cat <<EOF
部署完成。
目录结构建议:
$(dirname "$TARGET_DIR")/
cocos-bridge.js ← JS 回调 + unityInstance 兼容
cocos/ ← 构建产物(本脚本输出)
index.html
src/ ...
cocos-demo.html ← 可选联调页
主站 index.html 需:
1. 在 <head> 或游戏脚本前引入: <script src="cocos-bridge.js"></script>
2. 用 cocos/index.html 中的 script 标签替换原 Unity Build/*.js 段
3. 保留 processData / externalResult 等全局函数
本地预览(需先 cd 到含 cocos 的目录):
npx --yes serve "$(dirname "$TARGET_DIR")" -p 8080
EOF