Give a better error on Windows if python isn't installed

Before:

```
PS C:\Users\vboxuser\rust> ./x
x.ps1

PS C:\Users\vboxuser\rust>
```

After:
```
PS C:\Users\vboxuser\rust> ./x
x.ps1

C:\Users\vboxuser\rust\x.ps1 : C:\Users\vboxuser\rust\x.ps1: error: did not find python installed
help: consider installing it from https://www.python.org/downloads/windows/
At line:1 char:1
+ ./x
+ ~~~
    + CategoryInfo          : NotInstalled: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,x.ps1
```

The existing message from the shell script is already decent and I decided not to change it:
```
$ ./x
Python was not found but can be installed from the Microsoft Store: ms-windows-store://pdp/?productid=9NJ46SX7X90P
```
This commit is contained in:
jyn 2023-06-24 13:09:48 -05:00
parent dd314f6533
commit fa7e965bf0

12
x.ps1
View file

@ -16,7 +16,13 @@ foreach ($arg in $args) {
}
function Get-Application($app) {
return Get-Command $app -ErrorAction SilentlyContinue -CommandType Application
$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) {
@ -51,5 +57,7 @@ if (($null -ne $found) -and ($found.Length -ge 1)) {
Invoke-Application $python $xpy_args
}
Write-Error "${PSCommandPath}: error: did not find python installed"
$msg = "${PSCommandPath}: error: did not find python installed`n"
$msg += "help: consider installing it from https://www.python.org/downloads/"
Write-Error $msg -Category NotInstalled
Exit 1