PWRSWriteColorEX.psm1
|
foreach ($loaded in Get-Module -Name 'PWRSWriteColorEX' -All) { $same = if ([System.IO.Path]::DirectorySeparatorChar -eq '\') { $loaded.ModuleBase -eq $PSScriptRoot } else { $loaded.ModuleBase -ceq $PSScriptRoot } if ($same) { continue } if ($loaded.ModuleType -eq 'Script' -and $null -ne (& $loaded { $ExecutionContext.SessionState.PSVariable.GetValue('PwrsStoodDownFor') })) { continue } if ($loaded.Version -eq [version]'1.2.2') { Microsoft.PowerShell.Utility\Write-Verbose ('PWRSWriteColorEX' + ' ' + '1.2.2' + ' is already loaded from ' + $loaded.ModuleBase + ', so the copy at ' + $PSScriptRoot + ' is not loaded.') $ExecutionContext.SessionState.PSVariable.Set('PwrsStoodDownFor', $loaded.ModuleBase) return } throw [System.Management.Automation.ErrorRecord]::new([System.InvalidOperationException]::new('PWRSWriteColorEX' + ' ' + $loaded.Version + ' is already loaded from ' + $loaded.ModuleBase + ', and a session can have only one version of a module, so ' + 'PWRSWriteColorEX' + ' ' + '1.2.2' + ' at ' + $PSScriptRoot + ' is not loaded.'), 'PwrsOtherVersionLoaded', [System.Management.Automation.ErrorCategory]::ResourceExists, $loaded) } $root = $PSScriptRoot $tfm = if ($PSVersionTable.PSEdition -eq 'Core') { 'net10.0' } else { 'netstandard2.0' } $pwrsLoad = [System.AppDomain]::CurrentDomain.GetData('pwrs-load') if ($null -eq $pwrsLoad) { $pwrsLoad = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'pwrs-load') if ($tfm -eq 'net10.0' -and -not $IsWindows) { $pwrsUid = $null $pwrsProbe = [System.IO.Path]::GetTempFileName() try { $pwrsUid = $ExecutionContext.InvokeProvider.Item.Get([string[]]@($pwrsProbe), $true, $true)[0].UnixStat.UserId } finally { [System.IO.File]::Delete($pwrsProbe) } $pwrsMine = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'pwrs-load-' + $pwrsUid) $pwrsPrivate = [System.IO.UnixFileMode]'UserRead, UserWrite, UserExecute' $pwrsStat = $null $pwrsWhy = '' if ($null -ne $pwrsUid) { try { $null = [System.IO.Directory]::CreateDirectory($pwrsMine, $pwrsPrivate) $pwrsStat = $ExecutionContext.InvokeProvider.Item.Get([string[]]@($pwrsMine), $true, $true)[0].UnixStat } catch [System.IO.IOException], [System.UnauthorizedAccessException] { # A file by that name, or a folder this user cannot read: the # check below takes it as not this user's, and the warning # gives the error's message. $pwrsWhy = ' (' + $_.Exception.Message + ')' } } if ($null -ne $pwrsStat -and $pwrsStat.UserId -eq $pwrsUid -and [string]$pwrsStat.ItemType -eq 'Directory') { if (($pwrsStat.Mode -band 63) -ne 0) { [System.IO.File]::SetUnixFileMode($pwrsMine, $pwrsPrivate) } $pwrsLoad = $pwrsMine } else { $pwrsLoad = [System.IO.Directory]::CreateTempSubdirectory('pwrs-load-').FullName Microsoft.PowerShell.Utility\Write-Warning ('pwrs: ' + $pwrsMine + ' is not a folder of this user''s own' + $pwrsWhy + ', so this session stages its copies in ' + $pwrsLoad + ', which no other session uses.') } } [System.AppDomain]::CurrentDomain.SetData('pwrs-load', $pwrsLoad) } if (-not ('Pwrs.Bootstrap.Loader' -as [type])) { $stage = [System.IO.Path]::Combine($pwrsLoad, [string]$PID, 'boot', '877a0aad59d8d4a3', $tfm) $null = [System.IO.Directory]::CreateDirectory($stage) $boot = [System.IO.Path]::Combine($stage, 'Pwrs.Bootstrap.dll') if (-not [System.IO.File]::Exists($boot)) { $pdb = [System.IO.Path]::Combine($root, $tfm, 'Pwrs.Bootstrap.pdb') $bootPdb = [System.IO.Path]::Combine($stage, 'Pwrs.Bootstrap.pdb') if ([System.IO.File]::Exists($pdb) -and -not [System.IO.File]::Exists($bootPdb)) { $partial = $bootPdb + '.' + [System.Guid]::NewGuid().ToString('N') + '.partial' [System.IO.File]::Copy($pdb, $partial) try { [System.IO.File]::Move($partial, $bootPdb) } catch [System.IO.IOException] { [System.IO.File]::Delete($partial) if (-not [System.IO.File]::Exists($bootPdb)) { throw } } } $partial = $boot + '.' + [System.Guid]::NewGuid().ToString('N') + '.partial' [System.IO.File]::Copy([System.IO.Path]::Combine($root, $tfm, 'Pwrs.Bootstrap.dll'), $partial) try { [System.IO.File]::Move($partial, $boot) } catch [System.IO.IOException] { [System.IO.File]::Delete($partial) if (-not [System.IO.File]::Exists($boot)) { throw } } } $null = [System.Reflection.Assembly]::LoadFrom($boot) } try { $shell = [Pwrs.Bootstrap.Loader]::Load($root, 'PWRSWriteColorEX', $tfm) } catch { if ($tfm -eq 'net10.0' -and [Environment]::Version.Major -lt 8) { throw ('PWRSWriteColorEX' + ' needs PowerShell 7.4 or later: its PowerShell 7 half is built against .NET 8, and this is PowerShell ' + $PSVersionTable.PSVersion + ' on .NET ' + [Environment]::Version + '.') } throw } $shell.GetType('Pwrs.Modules.PWRSWriteColorEX.PwrsModuleRoot', $true).GetField('Value').SetValue($null, $root) if ($tfm -eq 'netstandard2.0') { $staged = [System.IO.Path]::GetDirectoryName($shell.Location) foreach ($dep in @('System.Numerics.Vectors.dll', 'System.Memory.dll', 'System.Buffers.dll', 'System.Runtime.CompilerServices.Unsafe.dll', 'System.Security.AccessControl.dll', 'System.Security.Principal.Windows.dll', 'System.IO.FileSystem.AccessControl.dll', 'Microsoft.Win32.Registry.dll')) { $to = [System.IO.Path]::Combine($staged, $dep) if (-not [System.IO.File]::Exists($to)) { $partial = $to + '.' + [System.Guid]::NewGuid().ToString('N') + '.partial' [System.IO.File]::Copy([System.IO.Path]::Combine($root, $tfm, $dep), $partial) try { [System.IO.File]::Move($partial, $to) } catch [System.IO.IOException] { [System.IO.File]::Delete($partial) if (-not [System.IO.File]::Exists($to)) { throw } } } } } foreach ($m in Get-Module -All) { if ($m.ModuleType -eq 'Binary' -and ($m.Path -eq $shell.Location -or $m.Name -like '*PWRSWriteColorEX.Shell*')) { Remove-Module -ModuleInfo $m -Force -ErrorAction SilentlyContinue } } $lifecycle = $shell.GetType('Pwrs.Modules.PWRSWriteColorEX.PwrsModuleLifecycle', $true) try { Import-Module -Assembly $shell -Force } catch { foreach ($e in $lifecycle::TakeImportErrors()) { Microsoft.PowerShell.Utility\Write-Error -ErrorRecord $e -ErrorAction Continue } $inner = $_.Exception while ($null -ne $inner.InnerException -and $inner.GetType().FullName -ne 'Pwrs.PwrsException') { $inner = $inner.InnerException } if ($inner -is [System.Management.Automation.IContainsErrorRecord]) { throw $inner.ErrorRecord } throw $inner } foreach ($e in $lifecycle::TakeImportErrors()) { Microsoft.PowerShell.Utility\Write-Error -ErrorRecord $e -ErrorAction Continue } $ExecutionContext.SessionState.Module.OnRemove = { foreach ($e in $lifecycle::Removed()) { Microsoft.PowerShell.Utility\Write-Error -ErrorRecord $e -ErrorAction Continue } } Export-ModuleMember -Cmdlet 'Write-ColorEX', 'Write-ColorError', 'Write-ColorWarning', 'Write-ColorInfo', 'Write-ColorSuccess', 'Write-ColorCritical', 'Write-ColorDebug', 'Set-ColorDefault', 'Get-ColorProfiles', 'New-ColorStyle', 'Test-AnsiSupport', 'Convert-HexToRGB', 'Convert-RGBToANSI8', 'Convert-RGBToANSI4', 'Get-ColorTableWithRGB', 'Measure-DisplayWidth', 'Get-LighterRGBColor', 'Get-LighterColorName', 'Get-LighterANSI8Color', 'Format-ColorEX', 'Show-ColorTable', 'Register-ColorName', 'Unregister-ColorName', 'Export-ColorProfile', 'Import-ColorProfile', 'Remove-ColorProfile', 'Get-GradientColor', 'Get-ColorContrast' -Alias 'Write-ColourEX', 'Write-Color', 'Write-Colour', 'WC', 'WCEX', 'wcolor', 'wcolour', 'WCE', 'Write-ErrorColor', 'Write-ErrorColour', 'Write-ColourError', 'WError', 'wcerror', 'WCW', 'Write-WarningColor', 'Write-WarningColour', 'Write-ColourWarning', 'WWarning', 'WCWarn', 'wcwarning', 'WCI', 'Write-InfoColor', 'Write-InfoColour', 'Write-ColourInfo', 'WInfo', 'wcinfo', 'WCS', 'Write-SuccessColor', 'Write-SuccessColour', 'Write-ColourSuccess', 'WSuccess', 'wcok', 'wcsuccess', 'WCC', 'Write-CriticalColor', 'Write-CriticalColour', 'Write-ColourCritical', 'WCritical', 'wccritical', 'WCD', 'Write-DebugColor', 'Write-DebugColour', 'Write-ColourDebug', 'WDebug', 'wcdebug', 'SCD', 'Set-ColourDefault', 'Set-DefaultColor', 'Set-DefaultColour', 'GCP', 'Get-ColourProfiles', 'Get-Profiles', 'gcprofiles', 'NCS', 'New-ColourStyle', 'New-Style', 'ncstyle', 'TAS', 'Test-ANSI', 'CHR', 'Hex2RGB', 'CRA8', 'RGB2ANSI8', 'CRA4', 'RGB2ANSI4', 'GCT', 'Get-ColorTable', 'Get-ColourTable', 'MDW', 'Get-DisplayWidth', 'Lighten-RGBColor', 'Lighten-ColorName', 'LA8', 'Lighten-ANSI8', 'Lighten-ANSI8Color', 'Format-ColourEX', 'FCEX', 'Show-ColourTable', 'Register-ColourName', 'Unregister-ColourName', 'Export-ColourProfile', 'Import-ColourProfile', 'Remove-ColourProfile', 'Get-GradientColour', 'Get-ColourContrast' |