diff --git a/pwsh_workspaces.ps1 b/pwsh_workspaces.ps1 index a2104e0..22e9275 100644 --- a/pwsh_workspaces.ps1 +++ b/pwsh_workspaces.ps1 @@ -1,4 +1,4 @@ -### workspace ### +### Workspace ### $workspaceSettings = "$env:USERPROFILE\.pwsh_workspace_settings.txt" $activeWorkspace = "" @@ -212,33 +212,54 @@ function Use-Active-UE-Workspace { } } -# Basically like Use-Active-UE-Workspace but this function -# does the additional work of looking up and setting the -# active workspace based on the provided workspace name/alias +# Creates a new powershell session then navigates to the +# workspace path and imports the ushell module. Creating a +# new session is needed so that when you switch UE workspaces, +# ushell is installed with the correct context of the workspace. function Use-UE-Workspace { param([Parameter(Mandatory=$true)] - [string]$Name) + [string]$Name, + [Parameter(Mandatory=$false)] + [bool]$NewSession = $true) - Use-Workspace -Name $Name - $location = Get-Location - - $resolvedCurrentLocation = Resolve-Path $location - $resolvedActiveWorkspace = Resolve-Path $global:activeWorkspace - # check that we are in the expected place then proceed - if($resolvedCurrentLocation.ProviderPath && $resolvedActiveWorkspace.ProviderPath) { - if(Test-Active-Workspace) { - $ushellRelativePath = "Engine\Extras\ushell\ushell.psm1" - $ushellPath = "{0}\{1}" -f $resolvedActiveWorkspace, $ushellRelativePath - Write-Host "Checking for ushell at: $ushellPath" - if(Test-Path -Path $ushellPath) { - Import-Module -Name $ushellPath - } - else { - Write-Host "Unable to import ushell -- is workspace an Unreal project?" - } - } + if($NewSession) { + New-UE-Session -Name $Name } else { - Write-Error "Did not correctly switch to workspace $Name" + Use-Workspace -Name $Name + $location = Get-Location + + $resolvedCurrentLocation = Resolve-Path $location + $resolvedActiveWorkspace = Resolve-Path $global:activeWorkspace + # Check that we are in the expected place then proceed + if($resolvedCurrentLocation.ProviderPath -eq $resolvedActiveWorkspace.ProviderPath) { + if(Test-Active-Workspace) { + $ushellRelativePath = "Engine\Extras\ushell\ushell.psm1" + $ushellPath = "{0}\{1}" -f $resolvedActiveWorkspace, $ushellRelativePath + Write-Host "Checking for ushell at: $ushellPath" + if(Test-Path -Path $ushellPath) { + Import-Module -Name $ushellPath + } + else { + Write-Host "Unable to import ushell -- is workspace an Unreal project?" + } + } + } + else { + Write-Error "Did not correctly switch to workspace $Name" + } } } + +function New-UE-Session { + param([Parameter(Mandatory=$true)] + [string]$Name) + if(Test-Workspace-Alias -Alias $Name) { + Start-Process pwsh -ArgumentList @("-NoExit", "-Command", "& {. $PROFILE; Use-UE-Workspace -Name $Name -NewSession `$false`}") + Stop-Process -Id $PID + } + else { + Write-Host "Alias $Name does not exist, not creating new session" + } +} +### Workspace ###