Skip to content

Commit d845593

Browse files
authored
Merge pull request #129 from ForrestTrepte/dev/unityErrorOutput
detect particular error messages in Unity log and output them via Write-Error
2 parents 0704262 + 3bb8a13 commit d845593

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,16 @@ function Start-UnityEditor {
962962

963963
$process = Start-Process @setProcessArgs
964964
if ( $Wait ) {
965-
if ( $process.ExitCode -ne 0 ) {
966-
if ( $LogFile -and (Test-Path $LogFile -Type Leaf) ) {
967-
Write-Verbose "Writing $LogFile to Information stream Tagged as 'Logs'"
968-
Get-Content $LogFile | ForEach-Object { Write-Information -MessageData $_ -Tags 'Logs' }
969-
}
965+
if ( $LogFile -and (Test-Path $LogFile -Type Leaf) ) {
966+
# Note that Unity sometimes returns a success ExitCode despite the presence of errors, but we want
967+
# to make sure that we flag such errors.
968+
Write-UnityErrors $LogFile
969+
970+
Write-Verbose "Writing $LogFile to Information stream Tagged as 'Logs'"
971+
Get-Content $LogFile | ForEach-Object { Write-Information -MessageData $_ -Tags 'Logs' }
972+
}
970973

974+
if ( $process.ExitCode -ne 0 ) {
971975
Write-Error "Unity quit with non-zero exit code: $($process.ExitCode)"
972976
}
973977
}
@@ -977,6 +981,46 @@ function Start-UnityEditor {
977981
}
978982
}
979983

984+
# Open the specified Unity log file and write any errors found in the file to the error stream.
985+
function Write-UnityErrors {
986+
param([string] $LogFileName)
987+
Write-Verbose "Checking $LogFileName for errors"
988+
$errors = Get-Content $LogFileName | Where-Object { Get-IsUnityError $_ }
989+
if ( $errors.Count -gt 0 ) {
990+
$errors = $errors | select -uniq # Unity prints out errors as they occur and also in a summary list. We only want to see each unique error once.
991+
$errorMessage = $errors -join "`r`n"
992+
$errorMessage = "Errors were found in $LogFileName`:`r`n$errorMessage"
993+
Write-Error $errorMessage
994+
}
995+
}
996+
997+
function Get-IsUnityError {
998+
param([string] $LogLine)
999+
1000+
# Detect Unity License error, for example:
1001+
# BatchMode: Unity has not been activated with a valid License. Could be a new activation or renewal...
1002+
if ( $LogLine -match 'Unity has not been activated with a valid License' ) {
1003+
return $true
1004+
}
1005+
1006+
# Detect that the method specified by -ExecuteMethod doesn't exist, for example:
1007+
# executeMethod method 'Invoke' in class 'Build' could not be found.
1008+
if ( $LogLine -match 'executeMethod method .* could not be found' ) {
1009+
return $true
1010+
}
1011+
1012+
# Detect compilation error, for example:
1013+
# Assets/Errors.cs(7,9): error CS0103: The name `NonexistentFunction' does not exist in the current context
1014+
if ( $LogLine -match '\.cs\(\d+,\d+\): error ' ) {
1015+
return $true
1016+
}
1017+
1018+
# In the future, additional kinds of errors that can be found in Unity logs could be added here:
1019+
# ...
1020+
1021+
return $false
1022+
}
1023+
9801024
function ConvertTo-DateTime {
9811025
param([string] $Text)
9821026

0 commit comments

Comments
 (0)