Terminal.Gui.PowerShell.psm1 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <#
  2. .SYNOPSIS
  3. (Windows Only) Opens Visual Studio and loads Terminal.sln.
  4. .DESCRIPTION
  5. (Windows Only) Opens Visual Studio and loads Terminal.sln.
  6. .PARAMETER SolutionFilePath
  7. (Optional) If specified, the path to the solution file. Typically unnecessary to supply this parameter.
  8. .INPUTS
  9. None
  10. .OUTPUTS
  11. None
  12. #>
  13. Function Open-Solution {
  14. [CmdletBinding()]
  15. param(
  16. [Parameter(Mandatory=$false, HelpMessage="The path to the solution file to open.")]
  17. [Uri]$SolutionFilePath
  18. )
  19. if(!$IsWindows) {
  20. [string]$warningMessage = "The Open-Solution cmdlet is only supported on Windows.`n`
  21. Attempt to open file $SolutionFilePath with the system default handler?"
  22. Write-Warning $warningMessage -WarningAction Inquire
  23. }
  24. Invoke-Item $SolutionFilePath
  25. return
  26. }
  27. <#
  28. .SYNOPSIS
  29. (Windows Only) Closes Visual Studio processes with Terminal.sln loaded.
  30. .DESCRIPTION
  31. (Windows Only) Closes Visual Studio processes with Terminal.sln loaded by finding any VS processes launched with the solution file or with 'Terminal' in their main window titles.
  32. .INPUTS
  33. None
  34. .OUTPUTS
  35. None
  36. #>
  37. Function Close-Solution {
  38. $vsProcesses = Get-Process -Name devenv | Where-Object { ($_.CommandLine -Match ".*Terminal\.sln.*" -or $_.MainWindowTitle -Match "Terminal.*") }
  39. Stop-Process -InputObject $vsProcesses
  40. Remove-Variable vsProcesses
  41. }
  42. <#
  43. .SYNOPSIS
  44. Resets changes made by ConfigureEnvironment.pst to the current PowerShell environment.
  45. .DESCRIPTION
  46. Optional function to undo changes to the current session made by ConfigureEnvironment.ps1.
  47. Changes only affect the current session, so exiting will also "reset."
  48. .PARAMETER Exit
  49. Switch parameter that, if specified, exits the current PowerShell environment.
  50. Does not bother doing any other operations, as none are necessary.
  51. .INPUTS
  52. None
  53. .OUTPUTS
  54. None
  55. .EXAMPLE
  56. Reset-PowerShellEnvironment
  57. To undo changes in the current session.
  58. .EXAMPLE
  59. Reset-PowerShellEnvironment -Exit
  60. To exit the current session. Same as simply using the Exit command.
  61. #>
  62. Function Reset-PowerShellEnvironment {
  63. param(
  64. [Parameter(Mandatory = $false)]
  65. [switch]$Exit
  66. )
  67. if($Exit) {
  68. [Environment]::Exit(0)
  69. }
  70. if(Get-Variable -Name NormalPrompt -Scope Global -ErrorAction SilentlyContinue){
  71. Set-Item Function:prompt $NormalPrompt
  72. Remove-Variable -Name NormalPrompt -Scope Global -Force
  73. }
  74. if(Get-Variable -Name OriginalPSModulePath -Scope Global -ErrorAction SilentlyContinue){
  75. $Env:PSModulePath = $OriginalPSModulePath
  76. Remove-Variable -Name OriginalPSModulePath -Scope Global -Force
  77. }
  78. Remove-Variable -Name PathVarSeparator -Scope Global -Force -ErrorAction SilentlyContinue
  79. }
  80. <#
  81. .SYNOPSIS
  82. Sets up a standard environment for other Terminal.Gui.PowerShell scripts and modules.
  83. .DESCRIPTION
  84. Configures environment variables and global variables for other Terminal.Gui.PowerShell scripts to use.
  85. Also modifies the prompt to indicate the session has been altered.
  86. Reset changes by exiting the session or by calling Reset-PowerShellEnvironment or ./ResetEnvironment.ps1.
  87. #>
  88. <#
  89. .SYNOPSIS
  90. Sets up a standard environment for other Terminal.Gui.PowerShell scripts and modules.
  91. .DESCRIPTION
  92. Configures environment variables and global variables for other Terminal.Gui.PowerShell scripts to use.
  93. Also modifies the prompt to indicate the session has been altered.
  94. Reset changes by exiting the session or by calling Reset-PowerShellEnvironment or ./ResetEnvironment.ps1.
  95. #>
  96. Function Set-PowerShellEnvironment {
  97. # Set a custom prompt to indicate we're in our modified environment.
  98. # Save the normal one first, though.
  99. New-Variable -Name NormalPrompt -Option ReadOnly -Scope Global -Value (Get-Item Function:prompt).ScriptBlock -ErrorAction SilentlyContinue
  100. Set-Item Function:prompt { "TGPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; }
  101. # Save existing PSModulePath for optional reset later.
  102. # If it is already saved, do not overwrite, but continue anyway.
  103. New-Variable -Name OriginalPSModulePath -Visibility Public -Option ReadOnly -Scope Global -Value ($Env:PSModulePath) -ErrorAction SilentlyContinue
  104. Write-Debug -Message "`$OriginalPSModulePath is $OriginalPSModulePath"
  105. # Get platform-specific path variable entry separator. Continue if it's already set.
  106. New-Variable -Name PathVarSeparator -Visibility Public -Option ReadOnly,Constant -Scope Global -Value ";" -Description 'Separator character used in environment variables such as $Env:PSModulePath' -ErrorAction SilentlyContinue
  107. if(!$IsWindows) {
  108. $PathVarSeparator = ':'
  109. }
  110. Write-Debug -Message "`$PathVarSeparator is $PathVarSeparator"
  111. # Now make it constant.
  112. Set-Variable PathVarSeparator -Option Constant -ErrorAction SilentlyContinue
  113. # If Env:PSModulePath already has the current path, don't append it again.
  114. if($Env:PSModulePath -notlike "*$((Resolve-Path .).Path)*") {
  115. Write-Debug -Message "Appending $((Resolve-Path .).Path) to `$Env:PSModulePath"
  116. $env:PSModulePath = Join-String -Separator $PathVarSeparator -InputObject @( $env:PSModulePath, (Resolve-Path .).Path )
  117. }
  118. Write-Debug -Message "`$Env:PSModulePath is $Env:PSModulePath"
  119. }
  120. # This ensures the environment is reset when unloading the module.
  121. # Without this, function:prompt will be undefined.
  122. $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
  123. Reset-PowerShellEnvironment
  124. Pop-Location
  125. }