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

@@ -0,0 +1,11 @@
<#
.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.
#>
Set-Environment

10
Scripts/Load-Module.ps1 Normal file
View File

@@ -0,0 +1,10 @@
<#
.SYNOPSIS
Loads the Terminal.Gui.PowerShell modules and pushes the current path to the location stack.
#>
$tgScriptsPath = Push-Location -PassThru
$tgModule = Import-Module "./Terminal.Gui.PowerShell.psd1" -PassThru
Set-PowerShellEnvironment

View File

@@ -0,0 +1,32 @@
<#
.SYNOPSIS
Resets changes made by ConfigureEnvironment.pst to the current PowerShell environment.
.DESCRIPTION
Optional script 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
.\ResetEnvironment.ps1
To run the script to undo changes in the current session.
.EXAMPLE
.\ResetEnvironment.ps1 -Exit
To exit the current session. Same as simply using the Exit command.
#>
# The two blank lines above must be preserved.
Import-Module ./Terminal.Gui.PowerShell.psd1
if($args -contains "-Exit"){
[Environment]::Exit(0)
} else {
Reset-PowerShellEnvironment
}
Remove-Module Terminal.Gui.PowerShell

View File

@@ -0,0 +1,8 @@
<#
.SYNOPSIS
Start a Terminal.Gui.PowerShell environment and load all modules.
.DESCRIPTION
Starts a new Terminal.Gui.PowerShell environment, with all modules imported.
#>
. ./Load-Module.ps1

View File

@@ -0,0 +1,105 @@
<#
.SYNOPSIS
Builds all analyzer projects in Debug and Release configurations.
.DESCRIPTION
Uses dotnet build to build all analyzer projects, with optional behavior changes via switch parameters.
.PARAMETER AutoClose
Automatically close running Visual Studio processes which have the Terminal.sln solution loaded, before taking any other actions.
.PARAMETER AutoLaunch
Automatically start a new Visual Studio process and load the solution after completion.
.PARAMETER Force
Carry out operations unconditionally and do not prompt for confirmation.
.PARAMETER NoClean
Do not delete the bin and obj folders before building the analyzers. Usually best not to use this, but can speed up the builds slightly.
.PARAMETER Quiet
Write less text output to the terminal.
.INPUTS
None
.OUTPUTS
None
#>
Function Build-Analyzers {
[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
)
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
}
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
}
return
}

View File

@@ -1,5 +1,5 @@
#
# Module manifest for module 'Terminal.Gui.Powershell'
# Module manifest for module 'Terminal.Gui.PowerShell'
#
# Generated by: Brandon Thetford (GitHub @dodexahedron)
#
@@ -15,7 +15,7 @@ RootModule = 'Terminal.Gui.PowerShell.psm1'
ModuleVersion = '1.0.0'
# Supported PSEditions
# CompatiblePSEditions = @()
CompatiblePSEditions = @('Core')
# ID used to uniquely identify this module
GUID = 'f28198f9-cf4b-4ab0-9f94-aef5616b7989'
@@ -27,19 +27,24 @@ Author = 'Brandon Thetford (GitHub @dodexahedron)'
CompanyName = 'The Terminal.Gui Project'
# Copyright statement for this module
Copyright = '(c) Brandon Thetford (GitHub @dodexahedron), provided under the MIT license'
Copyright = 'Brandon Thetford (GitHub @dodexahedron), provided to the Terminal.Gui project and you under the MIT license'
# Description of the functionality provided by this module
Description = 'Commands for operations on components of Terminal.Gui during development of Terminal.Gui'
Description = 'Utilities for development-time operations on and management of components of Terminal.Gui code and other assets.'
# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''
PowerShellVersion = '7.4.0'
# Name of the PowerShell "host" subsystem (not system host name). Helps ensure that we know what to expect from the environment.
PowerShellHostName = 'ConsoleHost'
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
PowerShellHostVersion = '7.4.0'
# Processor architecture (None, X86, Amd64) required by this module
ProcessorArchitecture = 'None'
# Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
# Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
# Has nothing to do with runtime use of Terminal.Gui.
ProcessorArchitecture = 'AMD64'
# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @('Microsoft.PowerShell.Utility','Microsoft.PowerShell.Management','PSReadLine')
@@ -57,28 +62,26 @@ RequiredModules = @('Microsoft.PowerShell.Utility','Microsoft.PowerShell.Managem
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
NestedModules = @('./Terminal.Gui.PowerShell.Analyzers.psd1')
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Build-Analyzers','Close-Solution','Open-Solution')
FunctionsToExport = @('Build-Analyzers','Close-Solution','Open-Solution','Reset-PowerShellEnvironment','Set-PowerShellEnvironment')
#FunctionsToExport = @('*')
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
# Variables to export from this module
# VariablesToExport = ()
VariablesToExport = @()
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
FileList = 'Terminal.Gui.PowerShell.psm1'
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
@@ -89,7 +92,7 @@ PrivateData = @{
# Tags = @()
# A URL to the license for this module.
LicenseUri = 'https://github.com/gui-cs/Terminal.Gui/Scripts/COPYRIGHT'
LicenseUri = 'https://github.com/gui-cs/Terminal.Gui/tree/v2_develop/Scripts/COPYRIGHT'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/gui-cs/Terminal.Gui'
@@ -98,13 +101,13 @@ PrivateData = @{
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
ReleaseNotes = 'See change history and releases for Terminal.Gui on GitHub'
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()

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
}

View File

@@ -0,0 +1,126 @@
#
# Module manifest for module 'Terminal.Gui.Powershell.Analyzers'
#
# Generated by: Brandon Thetford (GitHub @dodexahedron)
#
# Generated on: 4/24/2024
#
@{
# Script module or binary module file associated with this manifest.
RootModule = 'Terminal.Gui.PowerShell.Analyzers.psm1'
# Version number of this module.
ModuleVersion = '1.0.0'
# Supported PSEditions
CompatiblePSEditions = @('Core')
# ID used to uniquely identify this module
GUID = '3e85001d-6539-4cf1-b71c-ec9e983f7fc8'
# Author of this module
Author = 'Brandon Thetford (GitHub @dodexahedron)'
# Company or vendor of this module
CompanyName = 'The Terminal.Gui Project'
# Copyright statement for this module
Copyright = '(c) Brandon Thetford (GitHub @dodexahedron). Provided to the Terminal.Gui project and you under the terms of the MIT License.'
# Description of the functionality provided by this module
Description = 'Operations involving Terminal.Gui analyzer projects, fur use during development of Terminal.Gui'
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '7.4.0'
# Name of the PowerShell host required by this module
PowerShellHostName = 'ConsoleHost'
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
ProcessorArchitecture = 'Amd64'
# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @('Microsoft.PowerShell.Management','Microsoft.PowerShell.Utility')
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Build-Analyzers')
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
# Variables to export from this module
VariablesToExport = @()
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
FileList = './Terminal.Gui.Powershell.Analyzers.psm1'
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
# A URL to the license for this module.
LicenseUri = 'https://github.com/gui-cs/Terminal.Gui/Scripts/COPYRIGHT'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/gui-cs/Terminal.Gui'
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}