| 1 | #!/usr/bin/env pwsh |
| 2 | |
| 3 | # See ./x for why these scripts exist. |
| 4 | |
| 5 | $ErrorActionPreference = "Stop" |
| 6 | |
| 7 | # syntax check |
| 8 | Get-Command -syntax ${PSCommandPath} >$null |
| 9 | |
| 10 | $xpy = Join-Path $PSScriptRoot x.py |
| 11 | $xpy_args = @($xpy) + $args |
| 12 | |
| 13 | function Get-Application($app) { |
| 14 | $cmd = Get-Command $app -ErrorAction SilentlyContinue -CommandType Application | Select-Object -First 1 |
| 15 | return $cmd |
| 16 | } |
| 17 | |
| 18 | function Invoke-Application($application, $arguments) { |
| 19 | & $application $arguments |
| 20 | Exit $LASTEXITCODE |
| 21 | } |
| 22 | |
| 23 | foreach ($python in "py", "python3", "python", "python2") { |
| 24 | # NOTE: this only tests that the command exists in PATH, not that it's actually |
| 25 | # executable. The latter is not possible in a portable way, see |
| 26 | # https://github.com/PowerShell/PowerShell/issues/12625. |
| 27 | if (Get-Application $python) { |
| 28 | if ($python -eq "py") { |
| 29 | # Use python3, not python2 |
| 30 | $xpy_args = @("-3") + $xpy_args |
| 31 | } |
| 32 | Invoke-Application $python $xpy_args |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | $found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'}) |
| 37 | if (($null -ne $found) -and ($found.Length -ge 1)) { |
| 38 | $python = $found[0] |
| 39 | Invoke-Application $python $xpy_args |
| 40 | } |
| 41 | |
| 42 | $msg = "${PSCommandPath}: error: did not find python installed`n" |
| 43 | $msg += "help: consider installing it from https://www.python.org/downloads/" |
| 44 | Write-Error $msg -Category NotInstalled |
| 45 | Exit 1 |