gratitude.durian.xpcool.com/build-wechat.ps1
2026-08-14 18:01:45 +08:00

175 lines
5.4 KiB
PowerShell
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.

# ============================================
# 微信小游戏构建脚本 (PowerShell版)
#
# 使用方法:
# .\build-wechat.ps1 [-Version "1.0.0"] [-Debug]
#
# 参数:
# -Version: 版本号(默认: 1.0.0
# -Debug: 启用调试模式(默认: false
# ============================================
param(
[string]$Version = "1.0.0",
[switch]$Debug
)
# 配置变量
$ProjectName = "报恩榴莲"
$AppId = "wxb2b7651c561f9d53"
$BuildDir = "build"
$OutputDir = Join-Path $BuildDir "wechatgame"
$IsDebug = if ($Debug) { "true" } else { "false" }
# 颜色输出函数
function Write-Success {
param([string]$Message)
Write-Host "$Message" -ForegroundColor Green
}
function Write-Error-Custom {
param([string]$Message)
Write-Host "$Message" -ForegroundColor Red
}
function Write-Step {
param([string]$Step, [string]$Message)
Write-Host "[$Step] $Message" -ForegroundColor Cyan
}
# 横幅
Write-Host "========================================" -ForegroundColor Yellow
Write-Host " $ProjectName 微信小游戏构建" -ForegroundColor Yellow
Write-Host " 版本: $Version" -ForegroundColor Yellow
Write-Host " 时间: $(Get-Date)" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
Write-Host ""
# 步骤1: 检查 Cocos Creator
Write-Step "1/6" "检查 Cocos Creator..."
try {
$cocosCmd = Get-Command cocos -ErrorAction Stop
Write-Success "Cocos Creator 已安装: $($cocosCmd.Source)"
} catch {
Write-Error-Custom "未找到 Cocos Creator 命令行工具"
Write-Host "请先安装 Cocos Creator 3.8 并添加到系统PATH" -ForegroundColor Yellow
Read-Host "按回车键退出"
exit 1
}
Write-Host ""
# 步骤2: 清理旧构建文件
Write-Step "2/6" "清理旧构建文件..."
if (Test-Path $OutputDir) {
Remove-Item -Recurse -Force $OutputDir
Write-Host " 已删除旧的构建目录" -ForegroundColor Gray
}
Write-Success "清理完成"
Write-Host ""
# 步骤3: 执行构建
Write-Step "3/6" "开始构建微信小游戏..."
Write-Host " 这可能需要几分钟,请耐心等待..." -ForegroundColor Gray
$cocosArgs = @(
"build",
"--platform wechatgame",
"--output `"$OutputDir`"",
"--debug $IsDebug",
"--compress true",
"--native-code false"
)
try {
& cocos $cocosArgs
if ($LASTEXITCODE -ne 0) {
throw "构建失败,退出码: $LASTEXITCODE"
}
Write-Success "构建成功"
} catch {
Write-Error-Custom $_.Exception.Message
Read-Host "按回车键退出"
exit 1
}
Write-Host ""
# 步骤4: 优化资源
Write-Step "4/6" "优化资源文件..."
$resDir = Join-Path $OutputDir "res"
if (Test-Path $resDir) {
Write-Host " 发现资源目录,开始优化..." -ForegroundColor Gray
# 统计资源数量
$imageCount = (Get-ChildItem $resDir -Include *.png,*.jpg,*.jpeg -Recurse).Count
$audioCount = (Get-ChildItem $resDir -Include *.mp3,*.wav -Recurse).Count
Write-Host " 图片: $imageCount" -ForegroundColor Gray
Write-Host " 音频: $audioCount" -ForegroundColor Gray
Write-Host " 资源优化完成" -ForegroundColor Gray
}
Write-Success "资源优化完成"
Write-Host ""
# 步骤5: 生成版本信息
Write-Step "5/6" "生成版本信息..."
$versionInfo = @{
name = $ProjectName
version = $Version
appId = $AppId
buildTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
platform = "wechatgame"
engine = "Cocos Creator 3.8"
orientation = "portrait"
debug = $IsDebug
}
$versionJson = $versionInfo | ConvertTo-Json -Depth 10
$versionFile = Join-Path $OutputDir "version.json"
Set-Content -Path $versionFile -Value $versionJson -Encoding UTF8
Write-Success "版本信息已保存: $versionFile"
Write-Host ""
# 步骤6: 计算包体大小
Write-Step "6/6" "计算包体大小..."
# 总大小
$totalSize = (Get-ChildItem $OutputDir -Recurse | Measure-Object -Property Length -Sum).Sum
$totalSizeMB = [math]::Round($totalSize / 1MB, 2)
Write-Host " 总大小: $totalSizeMB MB" -ForegroundColor Gray
# 主包大小(排除分包)
$mainPkgItems = Get-ChildItem $OutputDir -Exclude "subpackages" -Recurse
$mainPkgSize = ($mainPkgItems | Measure-Object -Property Length -Sum).Sum
$mainPkgSizeMB = [math]::Round($mainPkgSize / 1MB, 2)
if ($mainPkgSizeMB -gt 20) {
Write-Host " ⚠️ 警告: 主包大小 $mainPkgSizeMB MB 超过20MB限制!" -ForegroundColor Red
} else {
Write-Host " ✅ 主包大小: $mainPkgSizeMB MB (符合规范)" -ForegroundColor Green
}
Write-Host ""
# 完成总结
Write-Host "========================================" -ForegroundColor Yellow
Write-Host " 🎉 构建完成!" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
Write-Host ""
Write-Host "📁 构建目录: $OutputDir" -ForegroundColor Cyan
Write-Host "📱 AppID: $AppId" -ForegroundColor Cyan
Write-Host "📦 版本: $Version" -ForegroundColor Cyan
Write-Host ""
Write-Host "下一步操作:" -ForegroundColor Yellow
Write-Host "1. 用微信开发者工具打开构建目录" -ForegroundColor White
Write-Host " 路径: $(Resolve-Path $OutputDir)" -ForegroundColor Gray
Write-Host ""
Write-Host "2. 点击「预览」生成二维码" -ForegroundColor White
Write-Host ""
Write-Host "3. 用手机微信扫码进行真机测试" -ForegroundColor White
Write-Host ""
Write-Host "4. 测试通过后,点击「上传」提交到微信后台" -ForegroundColor White
Write-Host ""
Read-Host "按回车键退出"