Skip to content

Commit a0c6edc

Browse files
leojonathanohjoeltimothyoh
authored andcommitted
Populate the $global:LASTEXITCODE with exit code of compiler
1 parent 94703a8 commit a0c6edc

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/Compile-SourceScript/Public/Compile-SourceScript.ps1

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function Compile-SourceScript {
134134
RedirectStandardInput = $stdInFile.FullName
135135
Wait = $true
136136
NoNewWindow = $true
137+
PassThru = $true
137138
}
138139
if ($PSBoundParameters['SkipWrapper']) {
139140
$processArgs['ArgumentList'] = @(
@@ -149,7 +150,38 @@ function Compile-SourceScript {
149150

150151
# Begin compilation
151152
if ($PSBoundParameters['SkipWrapper']) { "Compiling $($sourceFile.Name)..." | Write-Host -ForegroundColor Yellow }
152-
Start-Process @processArgs
153+
switch($MOD_NAME) {
154+
'sourcemod' {
155+
$p = Start-Process @processArgs
156+
$global:LASTEXITCODE = $p.ExitCode
157+
}
158+
'amxmodx' {
159+
# The amxmodx compiler always exits with exit code 0, so we have to use a regex on the stdout
160+
$global:LASTEXITCODE = 0
161+
162+
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) (New-Guid).Guid
163+
$stdoutFile = Join-Path $tempDir 'stdout'
164+
$stderrFile = Join-Path $tempDir 'stderr'
165+
$processArgs['RedirectStandardOutput'] = $stdoutFile
166+
$processArgs['RedirectStandardError'] = $stderrFile
167+
New-Item $tempDir -ItemType Directory -Force
168+
$p = Start-Process @processArgs
169+
$stdout = Get-Content $stdoutFile
170+
$stderr = Get-Content $stderrFile
171+
foreach ($line in $stdout) {
172+
if ($line -match '^.*\.sma\(\d+\)\s*:\s*error (\d+)') {
173+
$global:LASTEXITCODE = $matches[1] -as [int] | Select-Object -First 1
174+
break
175+
}
176+
}
177+
178+
# Cleanup
179+
Remove-Item $tempDir -Recurse -Force
180+
}
181+
default {
182+
throw "Invalid mod name $MOD_NAME."
183+
}
184+
}
153185
if ($PSBoundParameters['SkipWrapper']) { "End of compilation." | Write-Host -ForegroundColor Yellow }
154186

155187
# Get all items in compiled directory after compilation by hash

0 commit comments

Comments
 (0)