Skip to content

Commit

Permalink
(WIP) Add Settings_Color configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoproiete committed Dec 22, 2020
1 parent 3dd4a54 commit df6bacb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Cake.Core/CakeConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ public ConsoleColor BackgroundColor
/// Initializes a new instance of the <see cref="CakeConsole"/> class.
/// </summary>
/// <param name="environment">The environment.</param>
public CakeConsole(ICakeEnvironment environment)
/// <param name="configuration">The configuration.</param>
public CakeConsole(ICakeEnvironment environment, ICakeConfiguration configuration)
{
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}

_supportAnsiEscapeCodes = new Lazy<bool>(() => AnsiDetector.SupportsAnsi(environment));
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}

_supportAnsiEscapeCodes = new Lazy<bool>(() => AnsiDetector.SupportsAnsi(environment, configuration));
}

/// <inheritdoc/>
Expand Down
1 change: 1 addition & 0 deletions src/Cake.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class Settings
public const string SkipPackageVersionCheck = "Settings_SkipPackageVersionCheck";
public const string NoMonoCoersion = "Settings_NoMonoCoersion";
public const string ShowProcessCommandLine = "Settings_ShowProcessCommandLine";
public const string Color = "Settings_Color";
}

public static class Paths
Expand Down
22 changes: 21 additions & 1 deletion src/Cake.Core/Diagnostics/Console/AnsiDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Cake.Core.Configuration;

namespace Cake.Core.Diagnostics
{
Expand Down Expand Up @@ -40,7 +41,7 @@ static AnsiDetector()
};
}

public static bool SupportsAnsi(ICakeEnvironment environment)
public static bool SupportsAnsi(ICakeEnvironment environment, ICakeConfiguration configuration)
{
// Prevents the addition of ANSI color if NO_COLOR env. variable is present
// https://no-color.org
Expand All @@ -49,6 +50,25 @@ public static bool SupportsAnsi(ICakeEnvironment environment)
return false;
}

if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

var color = configuration.GetValue(Constants.Settings.Color)?.ToLowerInvariant();

switch (color)
{
case "always":
return true;

case "never":
return false;

default:
break;
}

// Github action doesn't setup a correct PTY but supports ANSI.
if (!string.IsNullOrWhiteSpace(environment.GetEnvironmentVariable("GITHUB_ACTION")))
{
Expand Down
4 changes: 3 additions & 1 deletion src/Cake/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.Diagnostics;
using Cake.Features.Bootstrapping;
using Cake.Features.Building;
Expand Down Expand Up @@ -82,7 +84,7 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
private static int LogException<T>(ICakeLog log, T ex) where T : Exception
{
log = log ?? new CakeBuildLog(
new CakeConsole(new CakeEnvironment(new CakePlatform(), new CakeRuntime())));
new CakeConsole(new CakeEnvironment(new CakePlatform(), new CakeRuntime()), new CakeConfiguration(new Dictionary<string, string>())));

if (log.Verbosity == Verbosity.Diagnostic)
{
Expand Down
1 change: 1 addition & 0 deletions src/Cake/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Autofac;
using Cake.Commands;
using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Features.Bootstrapping;
Expand Down

0 comments on commit df6bacb

Please sign in to comment.