Skip to content

Commit b77e9cf

Browse files
committed
Refactor: Improve code flow in generation of command line arguments and preparation of compilation environment
1 parent 0b76d30 commit b77e9cf

1 file changed

Lines changed: 25 additions & 21 deletions

File tree

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,39 @@ function Compile-SourceScript {
123123
# Get all items in compiled directory before compilation by hash
124124
$compiledDirItemsPre = Get-ChildItem -Path $COMPILED_DIR -File -Recurse -Force | ? { $_.Extension -eq $MOD[$MOD_NAME]['plugin_ext'] } | Select-Object *, @{name='md5'; expression={(Get-FileHash -Path $_.Fullname -Algorithm MD5).Hash}}
125125

126-
# Prepare for compilation
127-
"Compiling..." | Write-Host -ForegroundColor Cyan
126+
# Generate command line arguments
128127
$epoch = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat "%s"))
129-
$stdInFile = New-Item -Path (Join-Path $SCRIPTING_DIR ".$epoch") -ItemType File -Force
130-
'1' | Out-File -FilePath $stdInFile.FullName -Force -Encoding utf8
128+
$stdInFile = Join-Path $SCRIPTING_DIR ".$epoch"
129+
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) (New-Guid).Guid
130+
$stdoutFile = Join-Path $tempDir 'stdout'
131+
$stderrFile = Join-Path $tempDir 'stderr'
131132
$processArgs = @{
132133
FilePath = $compiler.FullName
134+
ArgumentList = @(
135+
if ($PSBoundParameters['SkipWrapper']) {
136+
$sourceFile.Name
137+
"-o$($MOD[$MOD_NAME]['compiled_dir_name'])/$($sourceFile.Basename)$($MOD[$MOD_NAME]['plugin_ext'])"
138+
}else {
139+
$sourceFile.Name
140+
}
141+
)
133142
WorkingDirectory = $SCRIPTING_DIR
134-
RedirectStandardInput = $stdInFile.FullName
143+
RedirectStandardInput = $stdInFile
144+
RedirectStandardOutput = $stdoutFile
145+
RedirectStandardError = $stderrFile
135146
Wait = $true
136147
NoNewWindow = $true
137148
PassThru = $true
138149
}
139-
if ($PSBoundParameters['SkipWrapper']) {
140-
$processArgs['ArgumentList'] = @(
141-
$sourceFile.Name
142-
"-o$($MOD[$MOD_NAME]['compiled_dir_name'])/$($sourceFile.Basename)$($MOD[$MOD_NAME]['plugin_ext'])"
143-
)
144-
}else {
145-
$processArgs['ArgumentList'] = @(
146-
$sourceFile.Name
147-
)
150+
151+
# Prepare compilation environment
152+
if ($item = New-Item -Path $stdInFile -ItemType File -Force) {
153+
# This dummy input bypasses the 'Press any key to continue' prompt of the compiler
154+
'1' | Out-File -FilePath $item.FullName -Force -Encoding utf8
148155
}
156+
New-Item $tempDir -ItemType Directory -Force > $null
157+
New-Item -Path $COMPILED_DIR -ItemType Directory -Force | Out-Null
158+
149159
$returnExitCode = $false
150160
if ($MOD_NAME -eq 'sourcemod') {
151161
$pluginErrorRegexPattern = '^.*\.sp\(\d+\)\s*:\s*error (\d+)'
@@ -156,21 +166,15 @@ function Compile-SourceScript {
156166
$pluginErrorRegexPattern = '^.*\.sma\(\d+\)\s*:\s*error (\d+)'
157167
$returnExitCode = $true
158168
}
159-
New-Item -Path $COMPILED_DIR -ItemType Directory -Force | Out-Null
160169

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

164174
if ($returnExitCode) {
165175
# The amxmodx compiler always exits with exit code 0, so we have to use a regex on the stdout
166176
$global:LASTEXITCODE = 0
167177

168-
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) (New-Guid).Guid
169-
$stdoutFile = Join-Path $tempDir 'stdout'
170-
$stderrFile = Join-Path $tempDir 'stderr'
171-
$processArgs['RedirectStandardOutput'] = $stdoutFile
172-
$processArgs['RedirectStandardError'] = $stderrFile
173-
New-Item $tempDir -ItemType Directory -Force > $null
174178
$p = Start-Process @processArgs
175179
$stdout = Get-Content $stdoutFile
176180
$stdout | Write-Host

0 commit comments

Comments
 (0)