228 lines
5.5 KiB
PowerShell
228 lines
5.5 KiB
PowerShell
param(
|
|
[string]$Target
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
Write-Host "Unity CI Build - Target: $Target"
|
|
|
|
try {
|
|
|
|
|
|
# -----------------------------
|
|
# Load toolchain config
|
|
# -----------------------------
|
|
$config = Get-Content ".\ci\toolchain.json" | ConvertFrom-Json
|
|
|
|
$unityVersion = $config.unityVersion
|
|
$unityRoot = $config.windows.unityRoot
|
|
|
|
$hubExe = Join-Path $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 "Installing Unity $unityVersion..."
|
|
|
|
& $hubExe -- --headless install `
|
|
--version $unityVersion
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Failed to install Unity $unityVersion"
|
|
}
|
|
|
|
if (!(Test-Path $unityEditorPath)) {
|
|
throw "Unity installation completed but editor not found: $unityEditorPath"
|
|
}
|
|
}
|
|
|
|
Write-Host "Unity: $unityVersion"
|
|
|
|
# -----------------------------
|
|
# Ensure required modules
|
|
# -----------------------------
|
|
$modules = $config.windows.modules
|
|
|
|
if ($modules.Count -gt 0) {
|
|
|
|
Write-Host "Checking Unity modules..."
|
|
|
|
# Query installed editors/modules once
|
|
$installedEditors = & $hubExe -- --headless editors --installed
|
|
|
|
foreach ($module in $modules) {
|
|
|
|
if ($installedEditors -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"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# -----------------------------
|
|
# Resolve Unity build method
|
|
# -----------------------------
|
|
if ([string]::IsNullOrWhiteSpace($Target)) {
|
|
throw "No Target provided."
|
|
}
|
|
|
|
$method = switch ($Target) {
|
|
"Windows" { "BuildScript.Windows" }
|
|
"Android" { "BuildScript.Android" }
|
|
default { throw "Unsupported target: $Target" }
|
|
}
|
|
|
|
Write-Host "Build Method: $method"
|
|
|
|
# -----------------------------
|
|
# Paths
|
|
# -----------------------------
|
|
$logFile = Join-Path $PWD "unity_build.log"
|
|
|
|
$artifactDir = Join-Path $PWD "Builds"
|
|
$artifactLog = Join-Path $artifactDir "unity_build.log"
|
|
|
|
New-Item -ItemType Directory -Force -Path $artifactDir | Out-Null
|
|
|
|
# Remove previous log
|
|
if (Test-Path $logFile) {
|
|
Remove-Item $logFile -Force
|
|
}
|
|
|
|
# -----------------------------
|
|
# Unity Arguments
|
|
# -----------------------------
|
|
$unityArgs = @(
|
|
"-batchmode"
|
|
"-nographics"
|
|
"-quit"
|
|
"-projectPath", "$PWD"
|
|
"-executeMethod", "$method"
|
|
"-logFile", "$logFile"
|
|
)
|
|
|
|
Write-Host "Starting Unity build..."
|
|
|
|
# -----------------------------
|
|
# Start Unity
|
|
# -----------------------------
|
|
$process = Start-Process `
|
|
-FilePath $unityEditorPath `
|
|
-ArgumentList $unityArgs `
|
|
-NoNewWindow `
|
|
-PassThru
|
|
|
|
$process.WaitForExit()
|
|
|
|
# -----------------------------
|
|
# Wait for log file creation
|
|
# -----------------------------
|
|
$timeoutSeconds = 30
|
|
$elapsed = 0
|
|
|
|
while (!(Test-Path $logFile) -and $elapsed -lt $timeoutSeconds) {
|
|
Start-Sleep -Seconds 1
|
|
$elapsed++
|
|
}
|
|
|
|
if (!(Test-Path $logFile)) {
|
|
throw "Unity log file was not created."
|
|
}
|
|
|
|
# -----------------------------
|
|
# Wait for file writes to finish
|
|
# -----------------------------
|
|
$previousLength = -1
|
|
|
|
do {
|
|
$currentLength = (Get-Item $logFile).Length
|
|
|
|
if ($currentLength -eq $previousLength) {
|
|
break
|
|
}
|
|
|
|
$previousLength = $currentLength
|
|
|
|
Start-Sleep -Milliseconds 500
|
|
|
|
} while ($true)
|
|
|
|
# -----------------------------
|
|
# Archive log
|
|
# -----------------------------
|
|
Copy-Item $logFile $artifactLog -Force
|
|
|
|
# -----------------------------
|
|
# Build failed
|
|
# -----------------------------
|
|
if ($process.ExitCode -ne 0) {
|
|
|
|
Write-Host ""
|
|
Write-Host "===== Unity Build Failed ====="
|
|
Write-Host "Exit Code: $($process.ExitCode)"
|
|
Write-Host ""
|
|
|
|
if (Test-Path $logFile) {
|
|
Write-Host "===== Last 500 Log Lines ====="
|
|
Get-Content $logFile -Tail 500
|
|
}
|
|
|
|
exit $process.ExitCode
|
|
}
|
|
|
|
# -----------------------------
|
|
# Build succeeded
|
|
# -----------------------------
|
|
Write-Host ""
|
|
Write-Host "===== Build Summary ====="
|
|
|
|
if (Test-Path $logFile) {
|
|
|
|
Get-Content $logFile |
|
|
Select-String `
|
|
-Pattern @(
|
|
"Build completed"
|
|
"Build succeeded"
|
|
"Build finished"
|
|
"Total time"
|
|
) `
|
|
-SimpleMatch |
|
|
ForEach-Object {
|
|
Write-Host $_.Line
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "===== Last 50 Log Lines ====="
|
|
|
|
Get-Content $logFile -Tail 50
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "SUCCESS: $Target"
|
|
|
|
}
|
|
catch {
|
|
Write-Error $_.Exception.Message
|
|
exit 1
|
|
}
|
|
finally {
|
|
Write-Host "Cleanup complete."
|
|
# Stop-Process -Name "Unity","Unity Hub" -Force -ErrorAction SilentlyContinue
|
|
}
|