Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update x.ps1 #116590

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions x.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,58 @@ $ErrorActionPreference = "Stop"
# syntax check
Get-Command -syntax ${PSCommandPath} >$null

$xpy = Join-Path $PSScriptRoot x.py
# Start-Process for some reason splits arguments on spaces. (Isn't powershell supposed to be simpler than bash?)
# Double-quote all the arguments so it doesn't do that.
$xpy_args = @("""$xpy""")
$pythonScriptArgs = @("$($PSScriptRoot)\x.py")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that work when there's a space in the value of $PSScriptRoot?
And why did you remove the comment?

foreach ($arg in $args) {
$xpy_args += """$arg"""
  $pythonScriptArgs += """$arg"""
}

function Get-Application($app) {
$cmd = Get-Command $app -ErrorAction SilentlyContinue -CommandType Application | Select-Object -First 1
if ($cmd.source -match '.*AppData\\Local\\Microsoft\\WindowsApps\\.*exe') {
# Windows for some reason puts a `python3.exe` executable in PATH that just opens the windows store.
# Ignore it.
return $false
}
return $cmd
  $cmd = Get-Command $app -ErrorAction SilentlyContinue -CommandType Application | Select-Object -First 1
  if ($cmd.source -match '.*AppData\\Local\\Microsoft\\WindowsApps\\.*exe') {
    # Windows for some reason puts a `python3.exe` executable in PATH that just opens the windows store.
    # Ignore it.
    return $false
  }
  return $cmd
}

function Invoke-Application($application, $arguments) {
$process = Start-Process -NoNewWindow -PassThru $application $arguments
# WORKAROUND: Caching the handle is necessary to make ExitCode work.
# See https://stackoverflow.com/a/23797762
$handle = $process.Handle
$process.WaitForExit()
if ($null -eq $process.ExitCode) {
Write-Error "Unable to read the exit code"
Exit 1
}
Exit $process.ExitCode
  $process = Start-Process -NoNewWindow -PassThru $application $arguments

  # WORKAROUND: Caching the handle is necessary to make ExitCode work.
  # See https://stackoverflow.com/a/23797762
  $handle = $process.Handle
  $process.WaitForExit()

  if ($null -eq $process.ExitCode) {
    Write-Error "Unable to read the exit code"
    Exit 1
  }

  Exit $process.ExitCode
}

foreach ($python in "py", "python3", "python", "python2") {
# 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
# https://github.com/PowerShell/PowerShell/issues/12625.
if (Get-Application $python) {
if ($python -eq "py") {
# Use python3, not python2
$xpy_args = @("-3") + $xpy_args
}
Invoke-Application $python $xpy_args
}
  # 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
  # https://github.com/PowerShell/PowerShell/issues/12625.
  if (Get-Application $python) {
    if ($python -eq "py") {
      # Use python3, not python2
      $pythonScriptArgs = @("-3") + $pythonScriptArgs
    }

    Invoke-Application $python $pythonScriptArgs
  }
}

$found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'})
if (($null -ne $found) -and ($found.Length -ge 1)) {
$python = $found[0]
Invoke-Application $python $xpy_args
  $python = $found[0]
  Invoke-Application $python $pythonScriptArgs
}

$msg = "${PSCommandPath}: error: did not find python installed`n"
$msg = "${PSCommandPath}: error: could not find a compatible Python installation`n"
Comment on lines -60 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just seems like a longer way to say the exact same thing, no? Personally I'd prefer to keep the succinct version unless there's something more actionable to add.

$msg += "help: consider installing it from https://www.python.org/downloads/"
Write-Error $msg -Category NotInstalled
Exit 1