Skip to content

Commit

Permalink
Fix: Add try catch finally around compilation environment preparation…
Browse files Browse the repository at this point in the history
…, compile, and compilation cleanup stages to ensure cleanup occurs
  • Loading branch information
leojonathanoh committed Aug 31, 2020
1 parent fb8517a commit d3f8694
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions src/Compile-SourceScript/Public/Compile-SourceScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,40 @@ function Compile-SourceScript {
PassThru = $true
}

# Prepare compilation environment
if ($item = New-Item -Path $stdInFile -ItemType File -Force) {
# This dummy input bypasses the 'Press any key to continue' prompt of the compiler
'1' | Out-File -FilePath $item.FullName -Force -Encoding utf8
}
New-Item $tempDir -ItemType Directory -Force > $null
New-Item -Path $COMPILED_DIR -ItemType Directory -Force | Out-Null

# Begin compilation
"Compiling..." | Write-Host -ForegroundColor Cyan
if ($PSBoundParameters['SkipWrapper']) { "Compiling $($sourceFile.Name)..." | Write-Host -ForegroundColor Yellow }

# Compile
$global:LASTEXITCODE = 0
$p = Start-Process @processArgs
$stdout = Get-Content $stdoutFile
$stdout | Write-Host
$stderr = Get-Content $stderrFile
$stderr | Write-Host
foreach ($line in $stdout) {
if ($line -match $MOD[$MOD_NAME]['compiler'][$OS]['error_regex']) {
$global:LASTEXITCODE = 1
break
try {
# Prepare compilation environment
if ($item = New-Item -Path $stdInFile -ItemType File -Force) {
# This dummy input bypasses the 'Press any key to continue' prompt of the compiler
'1' | Out-File -FilePath $item.FullName -Force -Encoding utf8
}
New-Item $tempDir -ItemType Directory -Force > $null
New-Item -Path $COMPILED_DIR -ItemType Directory -Force | Out-Null

# Begin compilation
"Compiling..." | Write-Host -ForegroundColor Cyan
if ($PSBoundParameters['SkipWrapper']) { "Compiling $($sourceFile.Name)..." | Write-Host -ForegroundColor Yellow }

# Compile
$global:LASTEXITCODE = 0
$p = Start-Process @processArgs
$stdout = Get-Content $stdoutFile
$stdout | Write-Host
$stderr = Get-Content $stderrFile
$stderr | Write-Host
foreach ($line in $stdout) {
if ($line -match $MOD[$MOD_NAME]['compiler'][$OS]['error_regex']) {
$global:LASTEXITCODE = 1
break
}
}
}catch {
throw
}finally {
# Cleanup
Remove-Item $stdInFile -Force
Remove-Item $tempDir -Recurse -Force
}

# Cleanup
Remove-Item $stdInFile -Force
Remove-Item $tempDir -Recurse -Force

if ($PSBoundParameters['SkipWrapper']) { "End of compilation." | Write-Host -ForegroundColor Yellow }

# Get all items in compiled directory after compilation by hash
Expand Down

0 comments on commit d3f8694

Please sign in to comment.