Terminal.Gui.PowerShell.Core.psm1 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = (Resolve-Path "../Terminal.sln")
  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. Sets up a standard environment for other Terminal.Gui.PowerShell scripts and modules.
  45. .DESCRIPTION
  46. Configures environment variables and global variables for other Terminal.Gui.PowerShell scripts to use.
  47. Also modifies the prompt to indicate the session has been altered.
  48. Reset changes by exiting the session or by calling Reset-PowerShellEnvironment or ./ResetEnvironment.ps1.
  49. .PARAMETER Debug
  50. Minimally supported for Write-Debug calls in this function only.
  51. .NOTES
  52. Mostly does not respect common parameters like WhatIf, Confirm, etc.
  53. This is just meant to be called by other scripts.
  54. Calling this manually is not supported.
  55. #>
  56. Function Set-PowerShellEnvironment {
  57. [CmdletBinding()]
  58. param()
  59. # Set a custom prompt to indicate we're in our modified environment.
  60. # Save the normal one first, though.
  61. # And save it as ReadOnly and without the -Force parameter, so this will be skipped if run more than once in the same session without a reset.
  62. New-Variable -Name NormalPrompt -Option ReadOnly -Scope Global -Value (Get-Item Function:prompt).ScriptBlock -ErrorAction SilentlyContinue
  63. Set-Item Function:prompt { "TGPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; }
  64. # Save existing PSModulePath for optional reset later.
  65. # If it is already saved, do not overwrite, but continue anyway.
  66. New-Variable -Name OriginalPSModulePath -Visibility Public -Option ReadOnly -Scope Global -Value ($Env:PSModulePath) -ErrorAction SilentlyContinue
  67. Write-Debug -Message "`$OriginalPSModulePath is $OriginalPSModulePath" -Debug:$DebugPreference
  68. # Get platform-specific path variable entry separator. Continue if it's already set.
  69. New-Variable -Name PathVarSeparator -Visibility Public -Option ReadOnly -Scope Global -Value ";" -Description 'Separator character used in environment variables such as $Env:PSModulePath' -ErrorAction SilentlyContinue
  70. if(!$IsWindows) {
  71. $PathVarSeparator = ':'
  72. }
  73. Write-Debug -Message "`$PathVarSeparator is $PathVarSeparator" -Debug:$DebugPreference
  74. # If Env:PSModulePath already has the current path, don't append it again.
  75. if($Env:PSModulePath -notlike "*$((Resolve-Path .).Path)*") {
  76. Write-Debug -Message "Appending $((Resolve-Path .).Path) to `$Env:PSModulePath" -Debug:$DebugPreference
  77. $env:PSModulePath = Join-String -Separator $PathVarSeparator -InputObject @( $env:PSModulePath, (Resolve-Path .).Path )
  78. }
  79. Write-Debug -Message "`$Env:PSModulePath is $Env:PSModulePath" -Debug:$DebugPreference
  80. }
  81. <#
  82. .SYNOPSIS
  83. Resets changes made by ConfigureEnvironment.pst to the current PowerShell environment.
  84. .DESCRIPTION
  85. Optional function to undo changes to the current session made by ConfigureEnvironment.ps1.
  86. Changes only affect the current session, so exiting will also "reset."
  87. .PARAMETER Exit
  88. Switch parameter that, if specified, exits the current PowerShell environment.
  89. Does not bother doing any other operations, as none are necessary.
  90. .INPUTS
  91. None
  92. .OUTPUTS
  93. None
  94. .EXAMPLE
  95. Reset-PowerShellEnvironment
  96. To undo changes in the current session.
  97. .EXAMPLE
  98. Reset-PowerShellEnvironment -Exit
  99. To exit the current session. Same as simply using the Exit command.
  100. #>
  101. Function Reset-PowerShellEnvironment {
  102. [CmdletBinding(DefaultParameterSetName="Basic")]
  103. param(
  104. [Parameter(Mandatory=$false, ParameterSetName="Basic")]
  105. [switch]$Exit
  106. )
  107. if($Exit) {
  108. [Environment]::Exit(0)
  109. }
  110. if(Get-Variable -Name NormalPrompt -Scope Global -ErrorAction SilentlyContinue){
  111. Set-Item Function:prompt $NormalPrompt
  112. Remove-Variable -Name NormalPrompt -Scope Global -Force -ErrorAction SilentlyContinue
  113. }
  114. if(Get-Variable -Name OriginalPSModulePath -Scope Global -ErrorAction SilentlyContinue){
  115. $Env:PSModulePath = $OriginalPSModulePath
  116. Remove-Variable -Name OriginalPSModulePath -Scope Global -Force -ErrorAction SilentlyContinue
  117. }
  118. Remove-Variable -Name PathVarSeparator -Scope Global -Force -ErrorAction SilentlyContinue
  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. }
  125. Set-PowerShellEnvironment