Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH3138: Add Settings_Color configuration option #2999

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
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
5 changes: 3 additions & 2 deletions src/Cake/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using Cake.Cli;
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 @@ -88,7 +89,7 @@ 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 @@ -8,6 +8,7 @@
using Cake.Cli;
using Cake.Commands;
using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Features.Bootstrapping;
Expand Down