134 lines
3.5 KiB
PowerShell
134 lines
3.5 KiB
PowerShell
param(
|
|
[string]$Target
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
Write-Host "===================================="
|
|
Write-Host " Unity CI Build (Windows Agent)"
|
|
Write-Host " Target: $Target"
|
|
Write-Host "===================================="
|
|
|
|
try {
|
|
|
|
# -----------------------------
|
|
# Load toolchain config
|
|
# -----------------------------
|
|
$config = Get-Content ".\ci\toolchain.json" | ConvertFrom-Json
|
|
|
|
$unityVersion = $config.unityVersion
|
|
$unityRoot = $config.windows.unityRoot
|
|
|
|
$hubExe = "${env:ProgramFiles}\Unity Hub\Unity Hub.exe"
|
|
|
|
if (!(Test-Path $hubExe)) {
|
|
throw "Unity Hub not found at: $hubExe"
|
|
}
|
|
|
|
# -----------------------------
|
|
# Ensure Unity is installed
|
|
# -----------------------------
|
|
$unityEditorPath = Join-Path $unityRoot "$unityVersion\Editor\Unity.exe"
|
|
|
|
if (!(Test-Path $unityEditorPath)) {
|
|
|
|
Write-Host "Unity $unityVersion not found. Installing via Unity Hub..."
|
|
|
|
# Install Unity Editor
|
|
& $hubExe -- --headless install `
|
|
--version $unityVersion
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Failed to install Unity $unityVersion"
|
|
}
|
|
}
|
|
|
|
Write-Host "Unity found: $unityEditorPath"
|
|
|
|
# -----------------------------
|
|
# Ensure required modules
|
|
# -----------------------------
|
|
# NOTE:
|
|
# Unity Hub CLI installs modules per version.
|
|
# These are cached per editor version, not reinstalled each build.
|
|
|
|
$modules = $config.windows.modules
|
|
|
|
Write-Host "Ensuring Unity modules: $($modules -join ', ')"
|
|
|
|
foreach ($module in $modules) {
|
|
|
|
Write-Host "Checking module: $module"
|
|
|
|
# Check installed modules via Unity Hub
|
|
$installed = & $hubExe -- --headless editors --installed
|
|
|
|
if ($installed -notmatch "$unityVersion.*$module") {
|
|
|
|
Write-Host "Installing module: $module"
|
|
|
|
& $hubExe -- --headless install-modules `
|
|
--version $unityVersion `
|
|
--module $module
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Failed to install module: $module"
|
|
}
|
|
}
|
|
else {
|
|
Write-Host "Module already installed: $module"
|
|
}
|
|
}
|
|
|
|
# -----------------------------
|
|
# Resolve Unity build method
|
|
# -----------------------------
|
|
if (!$Target) {
|
|
Write-Host "No Target Provided"
|
|
exit
|
|
}
|
|
else {
|
|
$method = switch ($Target) {
|
|
"Windows" { "BuildScript.Windows" }
|
|
"Android" { "BuildScript.Android" }
|
|
}
|
|
|
|
Write-Host "Using Unity method: $method"
|
|
|
|
# -----------------------------
|
|
# Run Unity build
|
|
# -----------------------------
|
|
$unityArgs = @(
|
|
"-batchmode",
|
|
"-nographics",
|
|
"-quit",
|
|
"-projectPath", "$PWD",
|
|
"-executeMethod", "$method",
|
|
"-logFile", "$PWD/unity_batch_build.log" # CRITICAL: Captures what goes wrong
|
|
)
|
|
|
|
# Execute using the array
|
|
& $unityEditorPath @unityArgs
|
|
|
|
$exitCode = $LASTEXITCODE
|
|
|
|
# -----------------------------
|
|
# Validate result
|
|
# -----------------------------
|
|
if ($exitCode -ne 0) {
|
|
throw "Unity build failed ($Target). Exit code: $exitCode"
|
|
}
|
|
|
|
Write-Host "===================================="
|
|
Write-Host " BUILD SUCCESS: $Target"
|
|
Write-Host "===================================="
|
|
}
|
|
}
|
|
catch {
|
|
throw
|
|
}
|
|
finally {
|
|
Write-Host "Cleaning Up"
|
|
# Stop-Process -Name "Unity", "Unity Hub" -Force
|
|
}
|