Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 1 | #!/usr/bin/env pwsh |
| 2 | |
Josh Stone | de8dedb | 2022-08-12 22:39:26 | [diff] [blame] | 3 | # See ./x for why these scripts exist. |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 4 | |
ozkanonur | eea6202 | 2023-05-03 17:32:39 | [diff] [blame] | 5 | $ErrorActionPreference = "Stop" |
| 6 | |
| 7 | # syntax check |
jyn | 664ffa4 | 2023-06-24 18:16:39 | [diff] [blame] | 8 | Get-Command -syntax ${PSCommandPath} >$null |
ozkanonur | eea6202 | 2023-05-03 17:32:39 | [diff] [blame] | 9 | |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 10 | $xpy = Join-Path $PSScriptRoot x.py |
jyn | 2cf54e9 | 2023-12-09 14:43:27 | [diff] [blame] | 11 | $xpy_args = @($xpy) + $args |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 12 | |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 13 | function Get-Application($app) { |
jyn | fa7e965 | 2023-06-24 18:09:48 | [diff] [blame] | 14 | $cmd = Get-Command $app -ErrorAction SilentlyContinue -CommandType Application | Select-Object -First 1 |
jyn | fa7e965 | 2023-06-24 18:09:48 | [diff] [blame] | 15 | return $cmd |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 16 | } |
| 17 | |
Chris Denton | 96501bd | 2022-12-28 19:41:42 | [diff] [blame] | 18 | function Invoke-Application($application, $arguments) { |
jyn | 2cf54e9 | 2023-12-09 14:43:27 | [diff] [blame] | 19 | & $application $arguments |
| 20 | Exit $LASTEXITCODE |
Chris Denton | 96501bd | 2022-12-28 19:41:42 | [diff] [blame] | 21 | } |
| 22 | |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 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. |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 27 | if (Get-Application $python) { |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 28 | if ($python -eq "py") { |
| 29 | # Use python3, not python2 |
| 30 | $xpy_args = @("-3") + $xpy_args |
| 31 | } |
Chris Denton | 96501bd | 2022-12-28 19:41:42 | [diff] [blame] | 32 | Invoke-Application $python $xpy_args |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 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] |
Chris Denton | 96501bd | 2022-12-28 19:41:42 | [diff] [blame] | 39 | Invoke-Application $python $xpy_args |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 40 | } |
| 41 | |
jyn | fa7e965 | 2023-06-24 18:09:48 | [diff] [blame] | 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 |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 45 | Exit 1 |