@@ -149,57 +149,72 @@ function Invoke-External {
149149 [string ]$WorkingDirectory
150150 )
151151
152- # Build argument string
152+ # Prepare argument string
153153 $argLine = ($Arguments | ForEach-Object {
154154 if ($_ -match ' \s' ) { ' "{0}"' -f $_ } else { $_ }
155155 }) -join ' '
156156
157- # Prepare ProcessStartInfo
157+ # Setup PSI
158158 $psi = New-Object System.Diagnostics.ProcessStartInfo
159-
160159 $ext = [System.IO.Path ]::GetExtension($Exe )
160+
161161 if ($ext -and ($ext.ToLower () -in @ (" .cmd" , " .bat" ))) {
162- # Run through cmd.exe for batch files
163162 $psi.FileName = " cmd.exe"
164163 $psi.Arguments = " /c `" $Exe `" $argLine "
165- }
166- else {
164+ } else {
167165 $psi.FileName = $Exe
168166 $psi.Arguments = $argLine
169167 }
170168
169+ $psi.UseShellExecute = $false
171170 $psi.RedirectStandardOutput = $true
172171 $psi.RedirectStandardError = $true
173- $psi.UseShellExecute = $false
174172 $psi.CreateNoWindow = $true
175173 $psi.WorkingDirectory = $ (if ($WorkingDirectory ) { $WorkingDirectory } else { (Get-Location ).Path })
176174
177175 # Start process
178176 $p = New-Object System.Diagnostics.Process
179177 $p.StartInfo = $psi
178+
179+ # Buffers to store output
180+ $stdout = New-Object System.Text.StringBuilder
181+ $stderr = New-Object System.Text.StringBuilder
182+
183+ # Event handlers (very stable compared to ObjectEvent)
184+ $p.add_OutputDataReceived ({
185+ if ($_.Data ) { [void ]$stdout.AppendLine ($_.Data ) }
186+ })
187+ $p.add_ErrorDataReceived ({
188+ if ($_.Data ) { [void ]$stderr.AppendLine ($_.Data ) }
189+ })
190+
180191 [void ]$p.Start ()
181192
182- # Read output synchronously (this avoids all hangs! )
183- $stdout = $p .StandardOutput.ReadToEnd ()
184- $stderr = $p .StandardError.ReadToEnd ()
193+ # Begin async reads (no deadlock )
194+ $p .BeginOutputReadLine ()
195+ $p .BeginErrorReadLine ()
185196
186- $p.WaitForExit ()
197+ # Wait without blocking streams (safe!)
198+ while (-not $p.HasExited ) {
199+ Start-Sleep - Milliseconds 100
200+ }
187201
188- # Logging (if required)
202+ # Logging
189203 if ($LogFile ) {
190204 $logDir = Split-Path $LogFile - Parent
191205 if ($logDir -and ! (Test-Path $logDir )) {
192206 New-Item - ItemType Directory - Path $logDir - Force | Out-Null
193207 }
194208
195- if ($stdout ) { Add-Content - Path $LogFile - Value $stdout }
196- if ($stderr ) { Add-Content - Path $LogFile - Value $stderr }
209+ if ($stdout.Length -gt 0 ) { Add-Content $LogFile $stdout.ToString () }
210+ if ($stderr.Length -gt 0 ) { Add-Content $LogFile $stderr.ToString () }
197211 }
198212
199213 return $p.ExitCode
200214}
201215
202216
217+
203218function Get-MavenCommand {
204219 param ([Parameter (Mandatory )][string ]$RepoDir )
205220 $mvnCmd = Get-Command mvn - ErrorAction SilentlyContinue
0 commit comments