Skip to content
Merged

Fix red #3325

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions scripts/common.lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,10 @@ function Write-Log {
$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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am looking for the source but I remember to have read somewhere that it was better to avoid Write-Host and always use Write-Output/Write-Information/Write-Error instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind the articles are from 2013, I can't find any new article so that's probably obsolete remark.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That used to be the gospel. But it was never true anyway. But still it was repeated thousands of times, every time you shown someone a script with Write-Host in it :D

In this case using Write-Host or Write-Output surrounded by setting the output color do in fact have a slightly different meaning and behavior, and we are losing the ability to capture the output of Write-Host just by assigning the result of this script into a variable (but if we tried enough there is new set of recent cmdlets that Write-Host goes through anyway so we now have a way to capture even that). But because we don't plan to do anything like capturing all our output, and because we write to console directly from our process output handlers, this change is absolutely safe to do.

}
}

Expand Down Expand Up @@ -233,6 +227,9 @@ function Invoke-Exe {

Write-Verbose "Done. Exit code: $exitCode"

if ($exitCode -ne 0) {

}
Comment thread
nohwnd marked this conversation as resolved.
Outdated
if ($exitCode -ne 0 -and ($IgnoreExitCode -notcontains $exitCode)) {
if($CaptureOutput)
{
Expand Down Expand Up @@ -265,15 +262,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