fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.1 KiB63 linesraw
1#!/usr/bin/env pwsh
2
3# See ./x for why these scripts exist.
4
5$ErrorActionPreference = "Stop"
6
7# syntax check
8Get-Command -syntax ${PSCommandPath} >$null
9
10$xpy = Join-Path $PSScriptRoot x.py
11# Start-Process for some reason splits arguments on spaces. (Isn't powershell supposed to be simpler than bash?)
12# Double-quote all the arguments so it doesn't do that.
13$xpy_args = @("""$xpy""")
14foreach ($arg in $args) {
15    $xpy_args += """$arg"""
16}
17
18function Get-Application($app) {
19    $cmd = Get-Command $app -ErrorAction SilentlyContinue -CommandType Application | Select-Object -First 1
20    if ($cmd.source -match '.*AppData\\Local\\Microsoft\\WindowsApps\\.*exe') {
21        # Windows for some reason puts a `python3.exe` executable in PATH that just opens the windows store.
22        # Ignore it.
23        return $false
24    }
25    return $cmd
26}
27
28function Invoke-Application($application, $arguments) {
29    $process = Start-Process -NoNewWindow -PassThru $application $arguments
30    # WORKAROUND: Caching the handle is necessary to make ExitCode work.
31    # See https://stackoverflow.com/a/23797762
32    $handle = $process.Handle
33    $process.WaitForExit()
34    if ($null -eq $process.ExitCode) {
35        Write-Error "Unable to read the exit code"
36        Exit 1
37    }
38    Exit $process.ExitCode
39}
40
41foreach ($python in "py", "python3", "python", "python2") {
42    # NOTE: this only tests that the command exists in PATH, not that it's actually
43    # executable. The latter is not possible in a portable way, see
44    # https://github.com/PowerShell/PowerShell/issues/12625.
45    if (Get-Application $python) {
46        if ($python -eq "py") {
47            # Use python3, not python2
48            $xpy_args = @("-3") + $xpy_args
49        }
50        Invoke-Application $python $xpy_args
51    }
52}
53
54$found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'})
55if (($null -ne $found) -and ($found.Length -ge 1)) {
56    $python = $found[0]
57    Invoke-Application $python $xpy_args
58}
59
60$msg = "${PSCommandPath}: error: did not find python installed`n"
61$msg += "help: consider installing it from https://www.python.org/downloads/"
62Write-Error $msg -Category NotInstalled
63Exit 1