From 4905a71d6d60ea794d85e3d6c423b483c3a2d3ee Mon Sep 17 00:00:00 2001 From: Tom Scogland Date: Wed, 20 Apr 2022 17:11:44 -0700 Subject: refactor powershell setup to make it sourceable (#29987) * refactor powershell setup to make it sourceable * only set editor if it is unset * change directory to spack root in subshell * Update share/spack/setup-env.ps1 Co-authored-by: John W. Parent <45471568+johnwparent@users.noreply.github.com> Co-authored-by: John W. Parent <45471568+johnwparent@users.noreply.github.com> --- bin/spack_pwsh.ps1 | 59 ++++----------------------------------------- share/spack/setup-env.ps1 | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 54 deletions(-) create mode 100644 share/spack/setup-env.ps1 diff --git a/bin/spack_pwsh.ps1 b/bin/spack_pwsh.ps1 index 9747fd3254..b734421780 100644 --- a/bin/spack_pwsh.ps1 +++ b/bin/spack_pwsh.ps1 @@ -3,57 +3,8 @@ # # SPDX-License-Identifier: (Apache-2.0 OR MIT) -Push-Location $PSScriptRoot/.. -$Env:SPACK_ROOT = $PWD.Path -Push-Location $PWD/.. -$Env:spackinstdir = $PWD.Path -Pop-Location - -Set-Variable -Name python_pf_ver -Value (Get-Command -Name python -ErrorAction SilentlyContinue).Path - -# If python_pf_ver is not defined, we cannot find Python on the Path -# We next look for Spack vendored copys -if ($null -eq $python_pf_ver) -{ - $python_pf_ver_list = Resolve-Path -Path "$PWD\Python*" - if ($python_pf_ver_list.Length -gt 0) - { - $py_path = $python_pf_ver_list[$python_pf_ver_list.Length-1].Path - $py_exe = "$py_path\python.exe" - } - else { - Write-Error -Message "Python was not found on system" - Write-Output "Please install Python or add Python to the PATH" - } -} -else{ - Set-Variable -Name py_exe -Value $python_pf_ver -} - -if (!$null -eq $py_path) -{ - $Env:Path = "$py_path;$Env:Path" -} - -if (!$null -eq $py_exe) -{ - Invoke-Expression "$py_exe $Env:SPACK_ROOT\bin\haspywin.py" - Invoke-Expression "$py_exe $Env:SPACK_ROOT\bin\spack external find python" | Out-Null -} - -$Env:Path = "$Env:SPACK_ROOT\bin;$Env:Path" -$Env:EDITOR = "notepad" - - -Write-Output "*****************************************************************" -Write-Output "**************** Spack Package Manager **************************" -Write-Output "*****************************************************************" - -$command = 'function global:prompt -{ - $pth = $(Convert-Path $(Get-Location)) | Split-Path -leaf - "[spack] PS $pth>" -}' -$bytesc = [System.Text.Encoding]::Unicode.GetBytes($command) -$encoded_command = [Convert]::ToBase64String($bytesc) -powershell.exe -NoLogo -encodedCommand $encoded_command -NoExit +$Env:SPACK_PS1_PATH="$PSScriptRoot\..\share\spack\setup-env.ps1" +& (Get-Process -Id $pid).Path -NoExit { + . $Env:SPACK_PS1_PATH ; + Push-Location $ENV:SPACK_ROOT + } diff --git a/share/spack/setup-env.ps1 b/share/spack/setup-env.ps1 new file mode 100644 index 0000000000..c5b51ee1df --- /dev/null +++ b/share/spack/setup-env.ps1 @@ -0,0 +1,61 @@ +# Copyright 2013-2022 Lawrence Livermore National Security, LLC and other +# Spack Project Developers. See the top-level COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +Push-Location $PSScriptRoot/../.. +$Env:SPACK_ROOT = $PWD.Path +Push-Location $PWD/.. +$Env:spackinstdir = $PWD.Path +Pop-Location + +Set-Variable -Name python_pf_ver -Value (Get-Command -Name python -ErrorAction SilentlyContinue).Path + +# If python_pf_ver is not defined, we cannot find Python on the Path +# We next look for Spack vendored copys +if ($null -eq $python_pf_ver) +{ + $python_pf_ver_list = Resolve-Path -Path "$PWD\Python*" + if ($python_pf_ver_list.Length -gt 0) + { + $py_path = $python_pf_ver_list[$python_pf_ver_list.Length-1].Path + $py_exe = "$py_path\python.exe" + } + else { + Write-Error -Message "Python was not found on system" + Write-Output "Please install Python or add Python to the PATH" + } +} +else{ + Set-Variable -Name py_exe -Value $python_pf_ver +} + +if (!$null -eq $py_path) +{ + $Env:Path = "$py_path;$Env:Path" +} + +if (!$null -eq $py_exe) +{ + Invoke-Expression "$py_exe $Env:SPACK_ROOT\bin\haspywin.py" + Invoke-Expression "$py_exe $Env:SPACK_ROOT\bin\spack external find python" | Out-Null +} + +$Env:Path = "$Env:SPACK_ROOT\bin;$Env:Path" +if ($null -eq $Env:EDITOR) +{ + $Env:EDITOR = "notepad" +} + + +Write-Output "*****************************************************************" +Write-Output "**************** Spack Package Manager **************************" +Write-Output "*****************************************************************" + +function global:prompt +{ + $pth = $(Convert-Path $(Get-Location)) | Split-Path -leaf + "[spack] PS $pth>" +} +Pop-Location + -- cgit v1.2.3-70-g09d2