@@ -124,8 +124,10 @@ function Invoke-External {
124124 [Parameter (Mandatory )][string ]$Exe ,
125125 [Parameter ()][string []]$Arguments = @ (),
126126 [string ]$LogFile ,
127- [string ]$WorkingDirectory
127+ [string ]$WorkingDirectory ,
128+ [int ]$TimeoutSeconds = 0 # 0 = no timeout (current behaviour)
128129 )
130+
129131 $psi = New-Object System.Diagnostics.ProcessStartInfo
130132 $exeToRun = $Exe
131133 $argLine = ($Arguments | ForEach-Object { if ($_ -match ' \s' ) { ' "{0}"' -f $_ } else { $_ } }) -join ' '
@@ -142,8 +144,9 @@ function Invoke-External {
142144
143145 $psi.RedirectStandardOutput = $true
144146 $psi.RedirectStandardError = $true
145- $psi.UseShellExecute = $false
146- $psi.CreateNoWindow = $true
147+ $psi.UseShellExecute = $false
148+ $psi.CreateNoWindow = $true
149+
147150 if ([string ]::IsNullOrWhiteSpace($WorkingDirectory )) {
148151 $psi.WorkingDirectory = (Get-Location ).Path
149152 } else {
@@ -155,7 +158,9 @@ function Invoke-External {
155158
156159 if ($LogFile ) {
157160 $logDir = Split-Path - Parent $LogFile
158- if ($logDir -and ! (Test-Path $logDir )) { New-Item - ItemType Directory - Path $logDir | Out-Null }
161+ if ($logDir -and ! (Test-Path $logDir )) {
162+ New-Item - ItemType Directory - Path $logDir | Out-Null
163+ }
159164
160165 $stdoutAction = {
161166 if (-not [string ]::IsNullOrEmpty($EventArgs.Data )) {
@@ -169,27 +174,51 @@ function Invoke-External {
169174 }
170175
171176 $stdoutEvent = Register-ObjectEvent - InputObject $p - EventName OutputDataReceived - Action $stdoutAction - MessageData $LogFile
172- $stderrEvent = Register-ObjectEvent - InputObject $p - EventName ErrorDataReceived - Action $stderrAction - MessageData $LogFile
177+ $stderrEvent = Register-ObjectEvent - InputObject $p - EventName ErrorDataReceived - Action $stderrAction - MessageData $LogFile
173178
174179 [void ]$p.Start ()
175180 $p.BeginOutputReadLine ()
176181 $p.BeginErrorReadLine ()
177- $p.WaitForExit ()
182+
183+ if ($TimeoutSeconds -gt 0 ) {
184+ $waitMs = $TimeoutSeconds * 1000
185+ if (-not $p.WaitForExit ($waitMs )) {
186+ try {
187+ Log- Line " ⚠️ Process '$Exe $argLine ' exceeded timeout of $TimeoutSeconds seconds. Killing it..." $GLOBAL_LOG
188+ } catch {}
189+ try { $p.Kill () } catch {}
190+ $p.WaitForExit ()
191+ }
192+ } else {
193+ $p.WaitForExit ()
194+ }
178195
179196 Unregister-Event - SourceIdentifier $stdoutEvent.Name
180197 Unregister-Event - SourceIdentifier $stderrEvent.Name
181198 Remove-Job - Id $stdoutEvent.Id - Force
182199 Remove-Job - Id $stderrEvent.Id - Force
183200 } else {
184201 [void ]$p.Start ()
185- $stdout = $p.StandardOutput.ReadToEnd ()
186- $stderr = $p.StandardError.ReadToEnd ()
187- $p.WaitForExit ()
202+
203+ if ($TimeoutSeconds -gt 0 ) {
204+ $waitMs = $TimeoutSeconds * 1000
205+ if (-not $p.WaitForExit ($waitMs )) {
206+ try { $p.Kill () } catch {}
207+ $p.WaitForExit ()
208+ }
209+ $stdout = $p.StandardOutput.ReadToEnd ()
210+ $stderr = $p.StandardError.ReadToEnd ()
211+ } else {
212+ $stdout = $p.StandardOutput.ReadToEnd ()
213+ $stderr = $p.StandardError.ReadToEnd ()
214+ $p.WaitForExit ()
215+ }
188216 }
189217
190218 return $p.ExitCode
191219}
192220
221+
193222function Get-MavenCommand {
194223 param ([Parameter (Mandatory )][string ]$RepoDir )
195224 $mvnCmd = Get-Command mvn - ErrorAction SilentlyContinue
0 commit comments