Skip to content

Commit d3f8694

Browse files
committed
Fix: Add try catch finally around compilation environment preparation, compile, and compilation cleanup stages to ensure cleanup occurs
1 parent fb8517a commit d3f8694

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -153,36 +153,40 @@ function Compile-SourceScript {
153153
PassThru = $true
154154
}
155155

156-
# Prepare compilation environment
157-
if ($item = New-Item -Path $stdInFile -ItemType File -Force) {
158-
# This dummy input bypasses the 'Press any key to continue' prompt of the compiler
159-
'1' | Out-File -FilePath $item.FullName -Force -Encoding utf8
160-
}
161-
New-Item $tempDir -ItemType Directory -Force > $null
162-
New-Item -Path $COMPILED_DIR -ItemType Directory -Force | Out-Null
163-
164-
# Begin compilation
165-
"Compiling..." | Write-Host -ForegroundColor Cyan
166-
if ($PSBoundParameters['SkipWrapper']) { "Compiling $($sourceFile.Name)..." | Write-Host -ForegroundColor Yellow }
167-
168-
# Compile
169-
$global:LASTEXITCODE = 0
170-
$p = Start-Process @processArgs
171-
$stdout = Get-Content $stdoutFile
172-
$stdout | Write-Host
173-
$stderr = Get-Content $stderrFile
174-
$stderr | Write-Host
175-
foreach ($line in $stdout) {
176-
if ($line -match $MOD[$MOD_NAME]['compiler'][$OS]['error_regex']) {
177-
$global:LASTEXITCODE = 1
178-
break
156+
try {
157+
# Prepare compilation environment
158+
if ($item = New-Item -Path $stdInFile -ItemType File -Force) {
159+
# This dummy input bypasses the 'Press any key to continue' prompt of the compiler
160+
'1' | Out-File -FilePath $item.FullName -Force -Encoding utf8
161+
}
162+
New-Item $tempDir -ItemType Directory -Force > $null
163+
New-Item -Path $COMPILED_DIR -ItemType Directory -Force | Out-Null
164+
165+
# Begin compilation
166+
"Compiling..." | Write-Host -ForegroundColor Cyan
167+
if ($PSBoundParameters['SkipWrapper']) { "Compiling $($sourceFile.Name)..." | Write-Host -ForegroundColor Yellow }
168+
169+
# Compile
170+
$global:LASTEXITCODE = 0
171+
$p = Start-Process @processArgs
172+
$stdout = Get-Content $stdoutFile
173+
$stdout | Write-Host
174+
$stderr = Get-Content $stderrFile
175+
$stderr | Write-Host
176+
foreach ($line in $stdout) {
177+
if ($line -match $MOD[$MOD_NAME]['compiler'][$OS]['error_regex']) {
178+
$global:LASTEXITCODE = 1
179+
break
180+
}
179181
}
182+
}catch {
183+
throw
184+
}finally {
185+
# Cleanup
186+
Remove-Item $stdInFile -Force
187+
Remove-Item $tempDir -Recurse -Force
180188
}
181189

182-
# Cleanup
183-
Remove-Item $stdInFile -Force
184-
Remove-Item $tempDir -Recurse -Force
185-
186190
if ($PSBoundParameters['SkipWrapper']) { "End of compilation." | Write-Host -ForegroundColor Yellow }
187191

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

0 commit comments

Comments
 (0)