2
0

Terminal.Gui.PowerShell.Core.psm1 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. [ValidatePattern(".*Terminal\.sln" )]
  18. [string]$Path = $SolutionFilePath
  19. )
  20. if(!$IsWindows) {
  21. [string]$warningMessage = "The Open-Solution cmdlet is only supported on Windows.`n`
  22. Attempt to open file $Path with the system default handler?"
  23. Write-Warning $warningMessage -WarningAction Inquire
  24. }
  25. Invoke-Item $Path
  26. return
  27. }
  28. <#
  29. .SYNOPSIS
  30. (Windows Only) Closes Visual Studio processes with Terminal.sln loaded.
  31. .DESCRIPTION
  32. (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.
  33. .INPUTS
  34. None
  35. .OUTPUTS
  36. None
  37. #>
  38. Function Close-Solution {
  39. $vsProcesses = Get-Process -Name devenv | Where-Object { ($_.CommandLine -Match ".*Terminal\.sln.*" -or $_.MainWindowTitle -Match "Terminal.*") }
  40. Stop-Process -InputObject $vsProcesses
  41. Remove-Variable vsProcesses
  42. }
  43. <#
  44. .SYNOPSIS
  45. Sets up a standard environment for other Terminal.Gui.PowerShell scripts and modules.
  46. .DESCRIPTION
  47. Configures environment variables and global variables for other Terminal.Gui.PowerShell scripts to use.
  48. Also modifies the prompt to indicate the session has been altered.
  49. Reset changes by exiting the session or by calling Reset-PowerShellEnvironment or ./ResetEnvironment.ps1.
  50. .PARAMETER Debug
  51. Minimally supported for Write-Debug calls in this function only.
  52. .NOTES
  53. Mostly does not respect common parameters like WhatIf, Confirm, etc.
  54. This is just meant to be called by other scripts.
  55. Calling this manually is not supported.
  56. #>
  57. Function Set-PowerShellEnvironment {
  58. [CmdletBinding()]
  59. param()
  60. # Set up some common globals
  61. New-Variable -Name ScriptsDirectory -Value $PSScriptRoot -Option ReadOnly -Scope Global -Visibility Public
  62. New-Variable -Name RepositoryRootDirectory -Value (Join-Path -Resolve $ScriptsDirectory "..") -Option ReadOnly -Scope Global -Visibility Public
  63. New-Variable -Name SolutionFilePath -Value (Join-Path -Resolve $RepositoryRootDirectory "Terminal.sln") -Option ReadOnly -Scope Global -Visibility Public
  64. New-Variable -Name TerminalGuiProjectDirectory -Value (Join-Path -Resolve $RepositoryRootDirectory "Terminal.Gui") -Option ReadOnly -Scope Global -Visibility Public
  65. New-Variable -Name TerminalGuiProjectFilePath -Value (Join-Path -Resolve $TerminalGuiProjectDirectory "Terminal.Gui.csproj") -Option ReadOnly -Scope Global -Visibility Public
  66. New-Variable -Name AnalyzersDirectory -Value (Join-Path -Resolve $RepositoryRootDirectory "Analyzers") -Option ReadOnly -Scope Global -Visibility Public
  67. New-Variable -Name InternalAnalyzersProjectDirectory -Value (Join-Path -Resolve $AnalyzersDirectory "Terminal.Gui.Analyzers.Internal") -Option ReadOnly -Scope Global -Visibility Public
  68. New-Variable -Name InternalAnalyzersProjectFilePath -Value (Join-Path -Resolve $InternalAnalyzersProjectDirectory "Terminal.Gui.Analyzers.Internal.csproj") -Option ReadOnly -Scope Global -Visibility Public
  69. # Set a custom prompt to indicate we're in our modified environment.
  70. # Save the normal one first, though.
  71. # 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.
  72. New-Variable -Name NormalPrompt -Option ReadOnly -Scope Global -Value (Get-Item Function:prompt).ScriptBlock -ErrorAction SilentlyContinue
  73. Set-Item Function:prompt { "TGPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; }
  74. # Save existing PSModulePath for optional reset later.
  75. # If it is already saved, do not overwrite, but continue anyway.
  76. New-Variable -Name OriginalPSModulePath -Visibility Public -Option ReadOnly -Scope Global -Value ($Env:PSModulePath) -ErrorAction SilentlyContinue
  77. Write-Debug -Message "`$OriginalPSModulePath is $OriginalPSModulePath" -Debug:$DebugPreference
  78. # Get platform-specific path variable entry separator. Continue if it's already set.
  79. New-Variable -Name PathVarSeparator -Visibility Public -Option ReadOnly -Scope Global -Value ";" -Description 'Separator character used in environment variables such as $Env:PSModulePath' -ErrorAction SilentlyContinue
  80. if(!$IsWindows) {
  81. $PathVarSeparator = ':'
  82. }
  83. Write-Debug -Message "`$PathVarSeparator is $PathVarSeparator" -Debug:$DebugPreference
  84. # If Env:PSModulePath already has the current path, don't append it again.
  85. if($Env:PSModulePath -notlike "*$((Resolve-Path .).Path)*") {
  86. Write-Debug -Message "Appending $((Resolve-Path .).Path) to `$Env:PSModulePath" -Debug:$DebugPreference
  87. $env:PSModulePath = Join-String -Separator $PathVarSeparator -InputObject @( $env:PSModulePath, (Resolve-Path .).Path )
  88. }
  89. Write-Debug -Message "`$Env:PSModulePath is $Env:PSModulePath" -Debug:$DebugPreference
  90. }
  91. <#
  92. .SYNOPSIS
  93. Resets changes made by ConfigureEnvironment.pst to the current PowerShell environment.
  94. .DESCRIPTION
  95. Optional function to undo changes to the current session made by ConfigureEnvironment.ps1.
  96. Changes only affect the current session, so exiting will also "reset."
  97. .PARAMETER Exit
  98. Switch parameter that, if specified, exits the current PowerShell environment.
  99. Does not bother doing any other operations, as none are necessary.
  100. .INPUTS
  101. None
  102. .OUTPUTS
  103. None
  104. .EXAMPLE
  105. Reset-PowerShellEnvironment
  106. To undo changes in the current session.
  107. .EXAMPLE
  108. Reset-PowerShellEnvironment -Exit
  109. To exit the current session. Same as simply using the Exit command.
  110. #>
  111. Function Reset-PowerShellEnvironment {
  112. [CmdletBinding(DefaultParameterSetName="Basic")]
  113. param(
  114. [Parameter(Mandatory=$false, ParameterSetName="Basic")]
  115. [switch]$Exit
  116. )
  117. if($Exit) {
  118. [Environment]::Exit(0)
  119. }
  120. if(Get-Variable -Name NormalPrompt -Scope Global -ErrorAction SilentlyContinue){
  121. Set-Item Function:prompt $NormalPrompt
  122. Remove-Variable -Name NormalPrompt -Scope Global -Force -ErrorAction SilentlyContinue
  123. }
  124. if(Get-Variable -Name OriginalPSModulePath -Scope Global -ErrorAction SilentlyContinue){
  125. $Env:PSModulePath = $OriginalPSModulePath
  126. Remove-Variable -Name OriginalPSModulePath -Scope Global -Force -ErrorAction SilentlyContinue
  127. }
  128. Remove-Variable -Name PathVarSeparator -Scope Global -Force -ErrorAction SilentlyContinue
  129. Remove-Variable -Name RepositoryRootDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  130. Remove-Variable -Name SolutionFilePath -Scope Global -Force -ErrorAction SilentlyContinue
  131. Remove-Variable -Name TerminalGuiProjectDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  132. Remove-Variable -Name TerminalGuiProjectFilePath -Scope Global -Force -ErrorAction SilentlyContinue
  133. Remove-Variable -Name ScriptsDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  134. Remove-Variable -Name AnalyzersDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  135. Remove-Variable -Name InternalAnalyzersProjectDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  136. Remove-Variable -Name InternalAnalyzersProjectFilePath -Scope Global -Force -ErrorAction SilentlyContinue
  137. }
  138. # This ensures the environment is reset when unloading the module.
  139. # Without this, function:prompt will be undefined.
  140. $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
  141. Reset-PowerShellEnvironment
  142. }
  143. Set-PowerShellEnvironment