|
| 1 | +$TempBatFile = Join-Path ([System.IO.Path]::GetTempPath()) "run_bash_$(Get-Random).bat" |
| 2 | +$CurrentWorkingDirectory = Get-Location |
| 3 | +$BatchContent = @" |
| 4 | +@echo off |
| 5 | +echo %cd% |
| 6 | +cd "$CurrentWorkingDirectory" |
| 7 | +"C:\Program Files\Git\bin\bash.exe" -c "./tools/test-interrupt.sh" |
| 8 | +exit /b %ERRORLEVEL% |
| 9 | +"@ |
| 10 | + |
| 11 | +try { |
| 12 | + $BatchContent | Out-File -FilePath $TempBatFile -Encoding ASCII |
| 13 | + |
| 14 | + # Use conhost.exe to run the batch file with output capture |
| 15 | + $ProcessStartInfo = New-Object System.Diagnostics.ProcessStartInfo |
| 16 | + $ProcessStartInfo.FileName = "conhost.exe" |
| 17 | + $ProcessStartInfo.Arguments = "`"$TempBatFile`"" |
| 18 | + $ProcessStartInfo.RedirectStandardOutput = $true |
| 19 | + $ProcessStartInfo.RedirectStandardError = $true |
| 20 | + $ProcessStartInfo.UseShellExecute = $false |
| 21 | + $ProcessStartInfo.CreateNoWindow = $false |
| 22 | + |
| 23 | + # Start the process |
| 24 | + $Process = New-Object System.Diagnostics.Process |
| 25 | + $Process.StartInfo = $ProcessStartInfo |
| 26 | + $OutputAction = { |
| 27 | + if ($Event.SourceEventArgs.Data -ne $null) { |
| 28 | + Write-Host $Event.SourceEventArgs.Data -ForegroundColor Green |
| 29 | + } |
| 30 | + } |
| 31 | + $ErrorAction = { |
| 32 | + if ($Event.SourceEventArgs.Data -ne $null) { |
| 33 | + Write-Host $Event.SourceEventArgs.Data -ForegroundColor Red |
| 34 | + } |
| 35 | + } |
| 36 | + $OutputEvent = Register-ObjectEvent -InputObject $Process -EventName OutputDataReceived -Action $OutputAction |
| 37 | + $ErrorEvent = Register-ObjectEvent -InputObject $Process -EventName ErrorDataReceived -Action $ErrorAction |
| 38 | + |
| 39 | + try { |
| 40 | + $Process.Start() | Out-Null |
| 41 | + $Process.BeginOutputReadLine() |
| 42 | + $Process.BeginErrorReadLine() |
| 43 | + |
| 44 | + Write-Host "Started conhost process with PID: $($Process.Id)" |
| 45 | + |
| 46 | + # Wait for the process to complete |
| 47 | + $Process.WaitForExit() |
| 48 | + $ExitCode = $Process.ExitCode |
| 49 | + |
| 50 | + Write-Host "" |
| 51 | + Write-Host "Process completed with exit code: $ExitCode" |
| 52 | + |
| 53 | + exit $ExitCode |
| 54 | + } |
| 55 | + finally { |
| 56 | + # Clean up event handlers |
| 57 | + if ($OutputEvent) { Unregister-Event -SourceIdentifier $OutputEvent.Name } |
| 58 | + if ($ErrorEvent) { Unregister-Event -SourceIdentifier $ErrorEvent.Name } |
| 59 | + |
| 60 | + # Dispose of the process |
| 61 | + if ($Process) { $Process.Dispose() } |
| 62 | + } |
| 63 | +} |
| 64 | +catch { |
| 65 | + Write-Error "Error starting process: $_" |
| 66 | + exit 1 |
| 67 | +} |
| 68 | +finally { |
| 69 | + Remove-Item $TempBatFile -Force -ErrorAction SilentlyContinue |
| 70 | +} |
0 commit comments