Re-structured and formalized the scripts and modules.

This commit is contained in:
dodexahedron
2024-04-25 13:39:01 -07:00
parent 683c087537
commit b9b68ba1a6
8 changed files with 440 additions and 103 deletions

View File

@@ -1,100 +1,142 @@
Function Build-Analyzers {
<#
.SYNOPSIS
(Windows Only) Opens Visual Studio and loads Terminal.sln.
.DESCRIPTION
(Windows Only) Opens Visual Studio and loads Terminal.sln.
.PARAMETER SolutionFilePath
(Optional) If specified, the path to the solution file. Typically unnecessary to supply this parameter.
.INPUTS
None
.OUTPUTS
None
#>
Function Open-Solution {
[CmdletBinding()]
param(
[Parameter(Mandatory=$false, HelpMessage="Automatically close running Visual Studio processes which have the Terminal.sln solution loaded, before taking any other actions.")]
[switch]$AutoClose,
[Parameter(Mandatory=$false, HelpMessage="Automatically start a new Visual Studio process and load the solution after completion.")]
[switch]$AutoLaunch,
[Parameter(Mandatory=$false, HelpMessage="Carry out operations unconditionally and do not prompt for confirmation.")]
[switch]$Force,
[Parameter(Mandatory=$false, HelpMessage="Do not delete the bin and obj folders before building the analyzers.")]
[switch]$NoClean,
[Parameter(Mandatory=$false, HelpMessage="Write less text output to the terminal.")]
[switch]$Quiet
[Parameter(Mandatory=$false, HelpMessage="The path to the solution file to open.")]
[Uri]$SolutionFilePath
)
if($AutoClose) {
if(!$Quiet) {
Write-Host Closing Visual Studio processes
}
Close-Solution
}
if($Force){
$response = 'Y'
}
elseif(!$Force && $NoClean){
$response = ($r = Read-Host "Pre-build Terminal.Gui.InternalAnalyzers without removing old build artifacts? [Y/n]") ? $r : 'Y'
}
else{
$response = ($r = Read-Host "Delete bin and obj folders for Terminal.Gui and Terminal.Gui.InternalAnalyzers and pre-build Terminal.Gui.InternalAnalyzers? [Y/n]") ? $r : 'Y'
}
if (($response -ne 'Y')) {
Write-Host Took no action
return
if(!$IsWindows) {
[string]$warningMessage = "The Open-Solution cmdlet is only supported on Windows.`n`
Attempt to open file $SolutionFilePath with the system default handler?"
Write-Warning $warningMessage -WarningAction Inquire
}
New-Variable -Name solutionRoot -Visibility Public -Value (Resolve-Path ..)
Push-Location $solutionRoot
New-Variable -Name solutionFile -Visibility Public -Value (Resolve-Path ./Terminal.sln)
$mainProjectRoot = Resolve-Path ./Terminal.Gui
$mainProjectFile = Join-Path $mainProjectRoot Terminal.Gui.csproj
$analyzersRoot = Resolve-Path ./Analyzers
$internalAnalyzersProjectRoot = Join-Path $analyzersRoot Terminal.Gui.Analyzers.Internal
$internalAnalyzersProjectFile = Join-Path $internalAnalyzersProjectRoot Terminal.Gui.Analyzers.Internal.csproj
if(!$NoClean) {
if(!$Quiet) {
Write-Host Deleting bin and obj folders for Terminal.Gui
}
if(Test-Path $mainProjectRoot/bin) {
Remove-Item -Recurse -Force $mainProjectRoot/bin
Remove-Item -Recurse -Force $mainProjectRoot/obj
}
if(!$Quiet) {
Write-Host Deleting bin and obj folders for Terminal.Gui.InternalAnalyzers
}
if(Test-Path $internalAnalyzersProjectRoot/bin) {
Remove-Item -Recurse -Force $internalAnalyzersProjectRoot/bin
Remove-Item -Recurse -Force $internalAnalyzersProjectRoot/obj
}
}
if(!$Quiet) {
Write-Host Building analyzers in Debug configuration
}
dotnet build $internalAnalyzersProjectFile --no-incremental --nologo --force --configuration Debug
if(!$Quiet) {
Write-Host Building analyzers in Release configuration
}
dotnet build $internalAnalyzersProjectFile --no-incremental --nologo --force --configuration Release
if(!$AutoLaunch) {
Write-Host -ForegroundColor Green Finished. Restart Visual Studio for changes to take effect.
} else {
if(!$Quiet) {
Write-Host -ForegroundColor Green Finished. Re-loading Terminal.sln.
}
Open-Solution
}
Invoke-Item $SolutionFilePath
return
}
Function Open-Solution {
Invoke-Item $solutionFile
return
}
<#
.SYNOPSIS
(Windows Only) Closes Visual Studio processes with Terminal.sln loaded.
.DESCRIPTION
(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.
.INPUTS
None
.OUTPUTS
None
#>
Function Close-Solution {
$vsProcesses = Get-Process -Name devenv | Where-Object { ($_.CommandLine -Match ".*Terminal\.sln.*" -or $_.MainWindowTitle -Match "Terminal.*") }
Stop-Process -InputObject $vsProcesses
Remove-Variable vsProcesses
}
Export-ModuleMember -Function Build-Analyzers
Export-ModuleMember -Function Open-Solution
Export-ModuleMember -Function Close-Solution
<#
.SYNOPSIS
Resets changes made by ConfigureEnvironment.pst to the current PowerShell environment.
.DESCRIPTION
Optional function to undo changes to the current session made by ConfigureEnvironment.ps1.
Changes only affect the current session, so exiting will also "reset."
.PARAMETER Exit
Switch parameter that, if specified, exits the current PowerShell environment.
Does not bother doing any other operations, as none are necessary.
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
Reset-PowerShellEnvironment
To undo changes in the current session.
.EXAMPLE
Reset-PowerShellEnvironment -Exit
To exit the current session. Same as simply using the Exit command.
#>
Function Reset-PowerShellEnvironment {
param(
[Parameter(Mandatory = $false)]
[switch]$Exit
)
if($Exit) {
[Environment]::Exit(0)
}
if(Get-Variable -Name NormalPrompt -Scope Global -ErrorAction SilentlyContinue){
Set-Item Function:prompt $NormalPrompt
Remove-Variable -Name NormalPrompt -Scope Global -Force
}
if(Get-Variable -Name OriginalPSModulePath -Scope Global -ErrorAction SilentlyContinue){
$Env:PSModulePath = $OriginalPSModulePath
Remove-Variable -Name OriginalPSModulePath -Scope Global -Force
}
Remove-Variable -Name PathVarSeparator -Scope Global -Force -ErrorAction SilentlyContinue
}
<#
.SYNOPSIS
Sets up a standard environment for other Terminal.Gui.PowerShell scripts and modules.
.DESCRIPTION
Configures environment variables and global variables for other Terminal.Gui.PowerShell scripts to use.
Also modifies the prompt to indicate the session has been altered.
Reset changes by exiting the session or by calling Reset-PowerShellEnvironment or ./ResetEnvironment.ps1.
#>
<#
.SYNOPSIS
Sets up a standard environment for other Terminal.Gui.PowerShell scripts and modules.
.DESCRIPTION
Configures environment variables and global variables for other Terminal.Gui.PowerShell scripts to use.
Also modifies the prompt to indicate the session has been altered.
Reset changes by exiting the session or by calling Reset-PowerShellEnvironment or ./ResetEnvironment.ps1.
#>
Function Set-PowerShellEnvironment {
# Set a custom prompt to indicate we're in our modified environment.
# Save the normal one first, though.
New-Variable -Name NormalPrompt -Option ReadOnly -Scope Global -Value (Get-Item Function:prompt).ScriptBlock -ErrorAction SilentlyContinue
Set-Item Function:prompt { "TGPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; }
# Save existing PSModulePath for optional reset later.
# If it is already saved, do not overwrite, but continue anyway.
New-Variable -Name OriginalPSModulePath -Visibility Public -Option ReadOnly -Scope Global -Value ($Env:PSModulePath) -ErrorAction SilentlyContinue
Write-Debug -Message "`$OriginalPSModulePath is $OriginalPSModulePath"
# Get platform-specific path variable entry separator. Continue if it's already set.
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
if(!$IsWindows) {
$PathVarSeparator = ':'
}
Write-Debug -Message "`$PathVarSeparator is $PathVarSeparator"
# Now make it constant.
Set-Variable PathVarSeparator -Option Constant -ErrorAction SilentlyContinue
# If Env:PSModulePath already has the current path, don't append it again.
if($Env:PSModulePath -notlike "*$((Resolve-Path .).Path)*") {
Write-Debug -Message "Appending $((Resolve-Path .).Path) to `$Env:PSModulePath"
$env:PSModulePath = Join-String -Separator $PathVarSeparator -InputObject @( $env:PSModulePath, (Resolve-Path .).Path )
}
Write-Debug -Message "`$Env:PSModulePath is $Env:PSModulePath"
}
# This ensures the environment is reset when unloading the module.
# Without this, function:prompt will be undefined.
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
Reset-PowerShellEnvironment
Pop-Location
}