Powershell: Create a Start-Process wrapper

This commit is contained in:
Chris Denton 2022-12-28 19:41:42 +00:00
parent 874cefad9b
commit 96501bdbe4
No known key found for this signature in database
GPG Key ID: 713472F2F45627DE

13
x.ps1
View File

@ -14,6 +14,12 @@ function Get-Application($app) {
return Get-Command $app -ErrorAction SilentlyContinue -CommandType Application return Get-Command $app -ErrorAction SilentlyContinue -CommandType Application
} }
function Invoke-Application($application, $arguments) {
$process = Start-Process -NoNewWindow -PassThru $application $arguments
$process.WaitForExit()
Exit $process.ExitCode
}
foreach ($python in "py", "python3", "python", "python2") { foreach ($python in "py", "python3", "python", "python2") {
# NOTE: this only tests that the command exists in PATH, not that it's actually # NOTE: this only tests that the command exists in PATH, not that it's actually
# executable. The latter is not possible in a portable way, see # executable. The latter is not possible in a portable way, see
@ -23,17 +29,14 @@ foreach ($python in "py", "python3", "python", "python2") {
# Use python3, not python2 # Use python3, not python2
$xpy_args = @("-3") + $xpy_args $xpy_args = @("-3") + $xpy_args
} }
$process = Start-Process -NoNewWindow -PassThru $python $xpy_args Invoke-Application $python $xpy_args
$process.WaitForExit()
Exit $process.ExitCode
} }
} }
$found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'}) $found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'})
if (($null -ne $found) -and ($found.Length -ge 1)) { if (($null -ne $found) -and ($found.Length -ge 1)) {
$python = $found[0] $python = $found[0]
$process = Start-Process -NoNewWindow -Wait -PassThru $python $xpy_args Invoke-Application $python $xpy_args
Exit $process.ExitCode
} }
Write-Error "${PSCommandPath}: error: did not find python installed" Write-Error "${PSCommandPath}: error: did not find python installed"