### Workspace ### $workspaceSettings = "$env:USERPROFILE\.pwsh_workspace_settings.txt" $activeWorkspace = "" # Check that the workspace settings file exists function Test-Workspace-Settings { $result = Test-Path -Path $global:workspaceSettings return $result } # If the alias exists, returns the path of the workspace function Test-Workspace-Alias { param([Parameter(Mandatory=$true)] [string]$Alias) $result = "" if(Test-Workspace-Settings) { $workspaceData = Get-Content $global:workspaceSettings foreach($entry in $workspaceData) { $parts = $entry -split "," if($parts.Length -eq 2) { if($parts[0] -eq $Alias) { $result = $parts[1] break; } } else { Write-Host "Expecting workspace entry to have two parts, it has $parts.Length" } } } return $result } # Check that an active workspace has been set function Test-Active-Workspace { return Test-Path -path $global:activeWorkspace } # Add a new workspace to workspace settings # Name is required # If no path is given, current working directory is used function Add-Workspace { param([Parameter(Mandatory=$true)] [string]$Name, [Parameter(Mandatory=$false)] [string]$Path) if(-Not $Path) { $Path = Get-Location } if(Test-Path -Path $Path) { $workspaceSettings = Test-Workspace-Settings $workspaceEntry = "{0},{1}" -f $Name, $Path if($workspaceSettings) { if(Test-Workspace-Alias -Alias $Name) { Write-Host "Alias already exists: $Name" } else { Add-Content -Path $global:workspaceSettings -Value $workspaceEntry Show-Workspace-Settings } } else { # just write out to the file Set-Content -Path $global:workspaceSettings -Value $workspaceEntry Show-Workspace-Settings } } else { Write-Output "$Path does not exist, cannot create workspace" } } # Set the active workspace using workspace alias # Will look in workspace settings to see if there is an entry # matching the provided alias, if so, will set active workspace path # to the path found in workspace settings function Set-Workspace { param([Parameter(Mandatory=$true)] [string]$Name) $result = Test-Workspace-Alias -Alias $Name if($result) { if(Test-Path -Path $result) { Write-Host "Setting active workspace to: $result" $global:activeWorkspace = $result } else { Write-Host "Workspace path $result does not exist" } } else { Write-Host "Workspace alias $Name not found" } return $result } # Open the workspace settings file in notepad function Open-Workspace-Settings { if(Test-Workspace-Settings) { Start-Process notepad.exe $global:workspaceSettings } } # Delete workspace settings file function Remove-Workspace-Settings { if(Test-Workspace-Settings) { Remove-Item -Path $global:workspaceSettings } } # Remove a workspace by name # Checks workspace settings to see if there is an entry with # the provided name. Removes the entry if it exists. function Remove-Workspace { param([Parameter(Mandatory=$true)] [string]$Name) $existing = @() if(Test-Workspace-Settings) { $workspaceData = Get-Content $global:workspaceSettings # rebuild the workspace settings contents and leave out # an entry if it matches the incoming $Name foreach($entry in $workspaceData) { $parts = $entry -split "," if($parts.Length -eq 2) { if($parts[0] -ne $Name) { $existing += $entry } } else { Write-Host "Expecting workspace entry to have two parts, it has $parts.Length" } } if($existing.Length -gt 0) { # clear the file contents $existing | Set-Content -Path $global:workspaceSettings } Show-Workspace-Settings } } # Print the contents of the workspace settings file to console function Show-Workspace-Settings { if(Test-Workspace-Settings) { $workspaceData = Get-Content $global:workspaceSettings foreach($entry in $workspaceData) { $parts = $entry -split "," if($parts.Length -eq 2) { $showString = "Alias: {0}, Path: {1}" -f $parts[0], $parts[1] Write-Host $showString } else { Write-Host "Incorrect workspace entry, cannot parse alias and path: $entry" } } } } # Open the active workspace in file explorer function Open-Active-Workspace { if(Test-Active-Workspace) { Write-Host "Opening workspace: $global:activeWorkspace" Start-Process explorer.exe $global:activeWorkspace } else { Write-Host "Active workspace not set or not valid path" } } # Look up workspace path by name, if found set it to active workspace # and navigate to the workspace path function Use-Workspace { param([Parameter(Mandatory=$true)] [string]$Name) if(Set-Workspace -Name $Name) { Write-Host "Changing to workspace: $global:activeWorkspace" Set-Location $global:activeWorkspace } } ### end workspace ### #### ushell #### # Expects that Set-Workspace has been called # Navigate to the active workspace path and attemp to load ushell # For ushell import to work correctly, workspace path is expected # to be something like C:\\UE function Use-Active-UE-Workspace { if(Test-Active-Workspace) { Write-Host "Changing to workspace: $global:activeWorkspace" Set-Location $global:activeWorkspace $ushellRelativePath = "Engine\Extras\ushell\ushell.psm1" $ushellPath = "{0}\{1}" -f $global:activeWorkspace, $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-Host "No active workspace to use" } } # 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, [Parameter(Mandatory=$false)] [bool]$NewSession = $true) if($NewSession) { New-UE-Session -Name $Name } else { 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 ###