165 lines
4.5 KiB
PowerShell
165 lines
4.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"
|
|
|
|
Write-Host "===================================================================="
|
|
Write-Host "===================== Starting Unity Build ========================="
|
|
Write-Host "===================================================================="
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# 1. Define paths
|
|
$logFile = "$PWD/unity_build.log"
|
|
$artifactLog = "$PWD/Builds/unity_build.log"
|
|
|
|
New-Item -ItemType Directory -Force -Path "$PWD/BuildLog" | Out-Null
|
|
|
|
# 2. Build arguments
|
|
$unityArgs = @(
|
|
"-batchmode",
|
|
"-nographics",
|
|
"-quit",
|
|
"-projectPath", "$PWD",
|
|
"-executeMethod", "$method",
|
|
"-logFile", "$logFile",
|
|
"-verbose"
|
|
)
|
|
|
|
Write-Host "Starting Unity..."
|
|
Write-Host "Log: $logFile"
|
|
|
|
# 3. Start Unity
|
|
$process = Start-Process `
|
|
-FilePath $unityEditorPath `
|
|
-ArgumentList $unityArgs `
|
|
-NoNewWindow `
|
|
-PassThru
|
|
|
|
# 4. Stream log live (simple + reliable)
|
|
while (-not $process.HasExited) {
|
|
if (Test-Path $logFile) {
|
|
Get-Content $logFile -Tail 20 -ErrorAction SilentlyContinue | ForEach-Object {
|
|
Write-Host $_
|
|
}
|
|
}
|
|
Start-Sleep -Milliseconds 500
|
|
}
|
|
|
|
$process.WaitForExit()
|
|
|
|
# 5. Copy Log to Builds Folder
|
|
Copy-Item $logFile $artifactLog -Force
|
|
|
|
|
|
# 6. Exit handling
|
|
if ($process.ExitCode -ne 0) {
|
|
Write-Error "Unity build failed with exit code $($process.ExitCode)"
|
|
exit $process.ExitCode
|
|
}
|
|
|
|
Write-Host "===================================="
|
|
Write-Host " BUILD SUCCESS: $Target"
|
|
Write-Host "===================================="
|
|
}
|
|
}
|
|
catch {
|
|
throw
|
|
}
|
|
finally {
|
|
Write-Host "Cleaning Up"
|
|
# Stop-Process -Name "Unity", "Unity Hub" -Force
|
|
}
|