11# Guard: this file is intended to be dot-sourced (included) by the main script.
2- # If executed directly, warn and exit to avoid unintended behavior .
2+ # If executed directly, warn but continue so tests and other runspaces that dot-source still get the definitions .
33if ($MyInvocation.InvocationName -ne ' .' ) {
4- Write-Host " This script is a library and must be dot-sourced from the main script (e.g. `. .\Create-VSSolution.ps1`). Exiting." - ForegroundColor Yellow
5- exit 1
4+ Write-Host " This script is a library and is intended to be dot-sourced (e.g. `. .\Create-VSSolution.ps1`). Continuing anyway." - ForegroundColor Yellow
65}
76
8- # Ensure a script-scoped container object exists for module-level flags
9- if (-not (Test-Path variable:script:TirsvadScript)) {
10- $script :TirsvadScript = [PSCustomObject ]@ {}
11- }
12- # Mark logging as loaded in a safe way
13- if ($script :TirsvadScript -is [System.Management.Automation.PSCustomObject ]) {
14- $script :TirsvadScript | Add-Member - NotePropertyName LoggingLoaded - NotePropertyValue $true - Force
7+ # Mark that logging was loaded in a global helper object. Be defensive: the global may not exist or may be a primitive.
8+ $existingVar = Get-Variable - Name TirsvadScript - Scope Global - ErrorAction SilentlyContinue
9+ if ($null -eq $existingVar ) {
10+ # Create a PSCustomObject container to hold markers
11+ $global :TirsvadScript = [PSCustomObject ]@ { LoggingLoaded = $true }
1512}
1613else {
17- # Fallback: attempt safe assignment
18- try { $script :TirsvadScript.LoggingLoaded = $true } catch { }
14+ $container = $existingVar.Value
15+ if ($container -is [System.Collections.Hashtable ]) {
16+ $container [' LoggingLoaded' ] = $true
17+ }
18+ elseif ($container -is [System.Management.Automation.PSCustomObject ]) {
19+ # Add or update property
20+ if ($container.PSObject.Properties.Match (' LoggingLoaded' ).Count -eq 0 ) {
21+ $container | Add-Member - NotePropertyName LoggingLoaded - NotePropertyValue $true
22+ }
23+ else {
24+ $container.LoggingLoaded = $true
25+ }
26+ }
27+ else {
28+ # Primitive or other type: preserve original value under 'OriginalValue' and replace global with a hashtable
29+ $global :TirsvadScript = @ { OriginalValue = $container ; LoggingLoaded = $true }
30+ }
1931}
2032
21- # Respect an existing caller-set `VerboseMode`; only set a default if it's not already defined
22- if (-not $script :VerboseMode ) {
23- $script :VerboseMode = 2 # Default verbosity level
33+ # Ensure a reasonable default verbosity when the caller hasn't set one.
34+ # When this file is dot-sourced it runs in the caller's script scope, so
35+ # checking for a script-scoped variable lets callers pre-set it (test expects that).
36+ if (-not (Get-Variable - Name VerboseMode - Scope Script - ErrorAction SilentlyContinue)) {
37+ $script :VerboseMode = 2
2438}
2539
2640# Write helpers respect numeric verbosity:0 = silent,1 = errors/warnings only,2 = errors+warnings+ok+info,3+ = debug
27- function Write-Info { if ($script :VerboseMode -ge 2 ) { Write-Host " [INFO] " - ForegroundColor Cyan - NoNewline; Write-Host " $args " } }
28- function Write-Ok { if ($script :VerboseMode -ge 2 ) { Write-Host " [OK] " - ForegroundColor Green - NoNewline; Write-Host " $args " } }
29- function Write-Err { if ($script :VerboseMode -ge 1 ) { Write-Host " [ERR] " - ForegroundColor Red - NoNewline; Write-Host " $args " } }
30- function Write-Warn { if ($script :VerboseMode -ge 1 ) { Write-Host " [WARN] " - ForegroundColor Magenta - NoNewline; Write-Host " $args " } }
41+ function Write-Info { if ($script :VerboseMode -ge 2 ) { Write-Host " [INFO] " - ForegroundColor Cyan - NoNewline; Write-Host " $args " } }
42+ function Write-Ok { if ($script :VerboseMode -ge 2 ) { Write-Host " [OK] " - ForegroundColor Green - NoNewline; Write-Host " $args " } }
43+ function Write-Err { if ($script :VerboseMode -ge 1 ) { Write-Host " [ERR] " - ForegroundColor Red - NoNewline; Write-Host " $args " } }
44+ function Write-Warn { if ($script :VerboseMode -ge 1 ) { Write-Host " [WARN] " - ForegroundColor Magenta - NoNewline; Write-Host " $args " } }
3145function Write-Debug { if ($script :VerboseMode -ge 3 ) { Write-Host " [DEBUG] " - ForegroundColor Yellow - NoNewline; Write-Host " $args " } }
32- function Write-Run { if ($script :VerboseMode -ge 2 ) { Write-Host " [RUN] " - ForegroundColor DarkGray - NoNewline; Write-Host " $args " } }
46+ function Write-Run { if ($script :VerboseMode -ge 2 ) { Write-Host " [RUN] " - ForegroundColor DarkGray - NoNewline; Write-Host " $args " } }
47+
3348function Write-RunDone {
3449 param (
3550 [bool ]$Success = $true
@@ -49,7 +64,7 @@ function Write-RunDone {
4964 $pos = $raw.CursorPosition
5065 if ($pos.Y -gt 0 ) {
5166 # Move cursor to the start of the previous line
52- $up = New-Object System.Management.Automation.Host.Coordinates(0 , ($pos.Y -1 ))
67+ $up = New-Object System.Management.Automation.Host.Coordinates(0 , ($pos.Y - 1 ))
5368 $raw.CursorPosition = $up
5469 # Overwrite the [RUN] marker
5570 Write-Host " [$msg ] " - ForegroundColor $msgColor - NoNewline
@@ -77,32 +92,34 @@ function Write-Header {
7792 if ($script :VerboseMode -lt 1 ) { return }
7893
7994 # Determine maximum content width (consider submessages)
80- $maxLength = 0
95+ $maxLength = 0
8196 if ($Message ) { $maxLength = $Message.Length }
8297 foreach ($subMsg in $SubMessages ) {
8398 if ($subMsg.Length -gt $maxLength ) { $maxLength = $subMsg.Length }
8499 }
85100
86101 # Add extra whitespace padding around the message for visual separation
87- $innerWidth = $maxLength +4 # two spaces on each side when centered
102+ $innerWidth = $maxLength + 4 # two spaces on each side when centered
88103
89104 # Build top/bottom rule length: '===' + ' ' + inner + ' ' + '===' -> innerWidth +8
90- $ruleLength = $innerWidth +8
105+ $ruleLength = $innerWidth + 8
91106 $line = ' =' * $ruleLength
92107 Write-Host $line - ForegroundColor DarkCyan
93108
94109 # Center the main message within the inner width
95110 if ($Message ) {
96- $leftPadding = [int ](([double ]($innerWidth - $Message.Length ) / 2.0 ) + $Message.Length )
111+ $leftPadding = [int ](([double ]($innerWidth - $Message.Length ) / 2.0 ) + $Message.Length )
97112 if ($leftPadding -lt $Message.Length ) { $leftPadding = $Message.Length }
98113 $centered = $Message.PadLeft ($leftPadding ).PadRight($innerWidth )
99- Write-Host " ===$ ( [char ]27 ) [1m $centered $ ( [char ]27 ) [22m===" - ForegroundColor DarkCyan
114+ Write-Host " === " - NoNewline - ForegroundColor DarkCyan
115+ Write-Host " $centered " - NoNewline - ForegroundColor white
116+ Write-Host " ===" - ForegroundColor DarkCyan
100117 }
101118
102119 # Optionally print sub-messages centered as well
103- if ($SubMessages -and $SubMessages.Count -gt 0 ) {
120+ if ($SubMessages -and $SubMessages.Count -gt 0 ) {
104121 foreach ($sub in $SubMessages ) {
105- $leftPadding = [int ](([double ]($innerWidth - $sub.Length ) / 2.0 ) + $sub.Length )
122+ $leftPadding = [int ](([double ]($innerWidth - $sub.Length ) / 2.0 ) + $sub.Length )
106123 if ($leftPadding -lt $sub.Length ) { $leftPadding = $sub.Length }
107124 $centeredSub = $sub.PadLeft ($leftPadding ).PadRight($innerWidth )
108125 Write-Host " === $centeredSub ===" - ForegroundColor DarkCyan
0 commit comments