Terminal.Gui.PowerShell.Core.psm1 6.0 KB

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