Skip to content

Commit

Permalink
Fix failfast (#3327)
Browse files Browse the repository at this point in the history
Co-authored-by: Medeni Baykal <433724+Haplois@users.noreply.github.com>
  • Loading branch information
nohwnd and Haplois committed Feb 4, 2022
1 parent f33445b commit 590aba3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 27 deletions.
1 change: 1 addition & 0 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Param(
)

$ErrorActionPreference = 'Stop'
$ErrorView = 'Normal'

. $PSScriptRoot\common.lib.ps1

Expand Down
84 changes: 57 additions & 27 deletions scripts/common.lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ function Get-ElapsedTime([System.Diagnostics.Stopwatch] $timer)

function Set-ScriptFailedOnError
{
param ($Command, $Arguments)
if ($lastExitCode -eq 0) {
param ($Command, $Arguments, $ExitCode)
if (0 -eq $ExitCode) {
return
}

if ($FailFast -eq $true) {
Write-Error "Build failed. Stopping as fail fast is set.`nFailed command: $Command $Arguments`nExit code: $LASTEXITCODE"
if ($FailFast) {
Write-Error "Build failed. Stopping as fail fast is set.`nFailed command: $Command $Arguments`nExit code: $ExitCode"
}

$script:ScriptFailedCommands += "$Command $Arguments"
Expand Down Expand Up @@ -232,7 +232,7 @@ function Invoke-Exe {
{
$process.StdErr
}
Set-ScriptFailedOnError -Command $Command -Arguments $Arguments
Set-ScriptFailedOnError -Command $Command -Arguments $Arguments -ExitCode $exitCode
}

if($CaptureOutput)
Expand All @@ -246,53 +246,83 @@ using System;
using System.Text;
using System.Collections.Generic;
public class ProcessOutputter {
public class ProcessOutputter
{
private readonly ConsoleColor _color;
private readonly ConsoleColor _warningColor;
private readonly ConsoleColor _errorColor;
private readonly List<string> _output;
private int nullCount = 0;
public ProcessOutputter (ConsoleColor color, bool suppressOutput = false) {
public ProcessOutputter(ConsoleColor color, ConsoleColor warningColor, ConsoleColor errorColor, bool suppressOutput = false)
{
_color = color;
_warningColor = warningColor;
_errorColor = errorColor;
_output = new List<string>();
OutputHandler = (s, e) => {
OutputHandler = (s, e) =>
{
AppendLine(e.Data);
if (!suppressOutput) {
// 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
if (suppressOutput || e.Data == null)
{
return;
}
// 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
{
var lines = e.Data.Split('\n');
foreach (var line in lines)
{
Console.ForegroundColor = fg;
// one extra space before the word, to avoid highlighting
// warnaserror and similar parameters that are not actual errors
//
// The comparison is not done using the insensitive overload because that
// is too new for PowerShell 5 compiler
var lineToLower = line.ToLowerInvariant();
Console.ForegroundColor = lineToLower.Contains(" warning")
? _warningColor
: lineToLower.Contains(" error")
? _errorColor
: _color;
Console.WriteLine(line);
}
}
finally
{
Console.ForegroundColor = fg;
}
}
};
}
public ProcessOutputter () {
public ProcessOutputter()
{
_output = new List<string>();
OutputHandler = (s, e) => AppendLine(e.Data);
}
public System.Diagnostics.DataReceivedEventHandler OutputHandler { get; private set; }
public IEnumerable<string> Output { get { return _output; } }
private void AppendLine(string line) {
if (string.IsNullOrEmpty(line)) {
private void AppendLine(string line)
{
if (string.IsNullOrEmpty(line))
{
nullCount++;
return;
}
while (nullCount > 0) {
while (nullCount > 0)
{
--nullCount;
_output.Add(string.Empty);
}
Expand Down Expand Up @@ -333,8 +363,8 @@ function Start-InlineProcess {
$processInfo.Verb = "runas"
}

$outputHandler = [ProcessOutputter]::new("White", $SuppressOutput.IsPresent)
$errorHandler = [ProcessOutputter]::new("Red", $SuppressOutput.IsPresent)
$outputHandler = [ProcessOutputter]::new("White", "Yellow", "Red", $SuppressOutput.IsPresent)
$errorHandler = [ProcessOutputter]::new("White", "Yellow", "Red", $SuppressOutput.IsPresent)
$outputDataReceived = $outputHandler.OutputHandler
$errorDataReceivedEvent = $errorHandler.OutputHandler

Expand Down

0 comments on commit 590aba3

Please sign in to comment.