Terminal.Gui.PowerShell.Core.psm1 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. # Save existing PSModulePath for optional reset later.
  70. # If it is already saved, do not overwrite, but continue anyway.
  71. New-Variable -Name OriginalPSModulePath -Visibility Public -Option ReadOnly -Scope Global -Value ($Env:PSModulePath) -ErrorAction SilentlyContinue
  72. Write-Debug -Message "`$OriginalPSModulePath is $OriginalPSModulePath" -Debug:$DebugPreference
  73. # Get platform-specific path variable entry separator. Continue if it's already set.
  74. New-Variable -Name PathVarSeparator -Visibility Public -Option ReadOnly -Scope Global -Value ";" -Description 'Separator character used in environment variables such as $Env:PSModulePath' -ErrorAction SilentlyContinue
  75. if(!$IsWindows) {
  76. $PathVarSeparator = ':'
  77. }
  78. Write-Debug -Message "`$PathVarSeparator is $PathVarSeparator" -Debug:$DebugPreference
  79. # If Env:PSModulePath already has the current path, don't append it again.
  80. if($Env:PSModulePath -notlike "*$((Resolve-Path .).Path)*") {
  81. Write-Debug -Message "Appending $((Resolve-Path .).Path) to `$Env:PSModulePath" -Debug:$DebugPreference
  82. $env:PSModulePath = Join-String -Separator $PathVarSeparator -InputObject @( $env:PSModulePath, (Resolve-Path .).Path )
  83. }
  84. Write-Debug -Message "`$Env:PSModulePath is $Env:PSModulePath" -Debug:$DebugPreference
  85. }
  86. <#
  87. .SYNOPSIS
  88. Resets changes made by ConfigureEnvironment.pst to the current PowerShell environment.
  89. .DESCRIPTION
  90. Optional function to undo changes to the current session made by ConfigureEnvironment.ps1.
  91. Changes only affect the current session, so exiting will also "reset."
  92. .PARAMETER Exit
  93. Switch parameter that, if specified, exits the current PowerShell environment.
  94. Does not bother doing any other operations, as none are necessary.
  95. .INPUTS
  96. None
  97. .OUTPUTS
  98. None
  99. .EXAMPLE
  100. Reset-PowerShellEnvironment
  101. To undo changes in the current session.
  102. .EXAMPLE
  103. Reset-PowerShellEnvironment -Exit
  104. To exit the current session. Same as simply using the Exit command.
  105. #>
  106. Function Reset-PowerShellEnvironment {
  107. [CmdletBinding(DefaultParameterSetName="Basic")]
  108. param(
  109. [Parameter(Mandatory=$false, ParameterSetName="Basic")]
  110. [switch]$Exit
  111. )
  112. if($Exit) {
  113. [Environment]::Exit(0)
  114. }
  115. if(Get-Variable -Name OriginalPSModulePath -Scope Global -ErrorAction SilentlyContinue){
  116. $Env:PSModulePath = $OriginalPSModulePath
  117. Remove-Variable -Name OriginalPSModulePath -Scope Global -Force -ErrorAction SilentlyContinue
  118. }
  119. Remove-Variable -Name PathVarSeparator -Scope Global -Force -ErrorAction SilentlyContinue
  120. Remove-Variable -Name RepositoryRootDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  121. Remove-Variable -Name SolutionFilePath -Scope Global -Force -ErrorAction SilentlyContinue
  122. Remove-Variable -Name TerminalGuiProjectDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  123. Remove-Variable -Name TerminalGuiProjectFilePath -Scope Global -Force -ErrorAction SilentlyContinue
  124. Remove-Variable -Name ScriptsDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  125. Remove-Variable -Name AnalyzersDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  126. Remove-Variable -Name InternalAnalyzersProjectDirectory -Scope Global -Force -ErrorAction SilentlyContinue
  127. Remove-Variable -Name InternalAnalyzersProjectFilePath -Scope Global -Force -ErrorAction SilentlyContinue
  128. }
  129. # This ensures the environment is reset when unloading the module.
  130. # Without this, function:prompt will be undefined.
  131. $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
  132. Reset-PowerShellEnvironment
  133. }
  134. Set-PowerShellEnvironment