Skip to content

Commit

Permalink
Fix red (#3325)
Browse files Browse the repository at this point in the history
* Fix red color being incorrectly used as foreground color after script ends

* Add lock

* Update scripts/common.lib.ps1

Co-authored-by: Medeni Baykal <433724+Haplois@users.noreply.github.com>
  • Loading branch information
nohwnd and Haplois committed Feb 4, 2022
1 parent 5a368ac commit f33445b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 54 deletions.
39 changes: 19 additions & 20 deletions scripts/common.lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,11 @@ function Write-Log {
[string]
$Level = "Success"
)

$currentColor = $Host.UI.RawUI.ForegroundColor
try {
$Host.UI.RawUI.ForegroundColor = if ("Success" -eq $Level) { "Green" } else { "Red" }
if ($message)
{
Write-Output "... $message"
}
}
finally {
$Host.UI.RawUI.ForegroundColor = $currentColor

if ($message)
{
$color = if ("Success" -eq $Level) { "Green" } else { "Red" }
Write-Host "... $message" -ForegroundColor $color
}
}

Expand Down Expand Up @@ -265,15 +259,20 @@ public class ProcessOutputter {
AppendLine(e.Data);
if (!suppressOutput) {
var fg = Console.ForegroundColor;
try
{
Console.ForegroundColor = _color;
Console.WriteLine(e.Data);
}
finally
{
Console.ForegroundColor = fg;
// These handlers can run at the same time,
// without lock they sometimes grab the color the other
// one set.
lock (Console.Out) {
var fg = Console.ForegroundColor;
try
{
Console.ForegroundColor = _color;
Console.WriteLine(e.Data);
}
finally
{
Console.ForegroundColor = fg;
}
}
}
};
Expand Down
40 changes: 20 additions & 20 deletions scripts/perf/perf.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,10 @@ function Get-ConsoleRunnerPath($runner, $targetFrameWork)

function Write-Log ([string] $message, $messageColor = "Green")
{
$currentColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = $messageColor
if ($message)
{
Write-Output "... $message"
Write-Host "... $message" -ForegroundColor $messageColor
}
$Host.UI.RawUI.ForegroundColor = $currentColor
}

function Get-ProductVersion($filePath)
Expand Down Expand Up @@ -352,24 +349,27 @@ function Invoke-PerformanceTests
#
function Invoke-DisplayResults
{
$currentColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = "Green"
$osDetails = Get-SystemInfo
"`n"
"Machine Configuration"
$osDetails | Format-List 'MachineName', 'OSName', 'OSVersion', 'MachineType' , 'Processor', 'LogicalCores', 'RAMSize'

if($DefaultAction -eq "Both" -or $DefaultAction -eq "Discovery")
{
$Script:TPT_Results | Where-Object {$_.Action -like "Discovery"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize
}
try {
$currentColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = "Green"
$osDetails = Get-SystemInfo
"`n"
"Machine Configuration"
$osDetails | Format-List 'MachineName', 'OSName', 'OSVersion', 'MachineType' , 'Processor', 'LogicalCores', 'RAMSize'

if($DefaultAction -eq "Both" -or $DefaultAction -eq "Discovery")
{
$Script:TPT_Results | Where-Object {$_.Action -like "Discovery"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize
}

if($DefaultAction -eq "Both" -or $DefaultAction -eq "Execution")
{
$Script:TPT_Results | Where-Object {$_.Action -like "Execution"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize
if($DefaultAction -eq "Both" -or $DefaultAction -eq "Execution")
{
$Script:TPT_Results | Where-Object {$_.Action -like "Execution"} | Format-Table 'Runner', 'Adapter', 'Action', 'ElapsedTime', 'Goal', 'Delta', 'Status', 'PayLoad', 'RunnerVersion', 'AdapterVersion' -AutoSize
}
}
finally {
$Host.UI.RawUI.ForegroundColor = $currentColor
}

$Host.UI.RawUI.ForegroundColor = $currentColor

if($ExportResults -ne $null -and $ExportResults -eq "csv")
{
Expand Down
5 changes: 1 addition & 4 deletions scripts/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,10 @@ $Script:ScriptFailed = $false

function Write-Log ([string] $message, $messageColor = "Green")
{
$currentColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = $messageColor
if ($message)
{
Write-Output "... $message"
Write-Host "... $message" -ForegroundColor $messageColor
}
$Host.UI.RawUI.ForegroundColor = $currentColor
}

function Write-VerboseLog([string] $message)
Expand Down
14 changes: 4 additions & 10 deletions scripts/verify-sign.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,22 @@ function Write-Debug ([string] $message)

function Write-ToCI ([string] $message, [string]$type, [switch]$vso)
{
$currentColor = $Host.UI.RawUI.ForegroundColor

if($type -eq "error") {
$Host.UI.RawUI.ForegroundColor = "Red"
}

if ($message -or $vso -or $type)
{
$prefix = ""
if ($vso) {
$prefix = "vso"
}

Write-Output "##$prefix[$type]$message"
$color = if($type -eq "error") { "Red" } else { $Host.UI.RawUI.ForegroundColor }
Write-Host "##$prefix[$type]$message" -ForegroundColor $color
}
$Host.UI.RawUI.ForegroundColor = $currentColor
}

Write-Debug "Variables used: "
Get-ChildItem variable:TPB_*
Write-Output ""
Write-Output ""
Write-Host ""
Write-Host ""

Verify-Assemblies
Verify-NugetPackages
Expand Down

0 comments on commit f33445b

Please sign in to comment.