From 22f73d0e2f27a2a834e58adf4888af0bb9787225 Mon Sep 17 00:00:00 2001 From: "C. Augusto Proiete" Date: Sun, 7 Feb 2021 18:23:08 -0400 Subject: [PATCH] (WIP) Add Settings_Color configuration option --- src/Cake.Core/CakeConsole.cs | 10 +++++++-- src/Cake.Core/Constants.cs | 1 + .../Diagnostics/Console/AnsiDetector.cs | 22 ++++++++++++++++++- src/Cake/Commands/DefaultCommand.cs | 5 +++-- src/Cake/Program.cs | 1 + 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Cake.Core/CakeConsole.cs b/src/Cake.Core/CakeConsole.cs index 59242aa8d9..1fef6aff95 100644 --- a/src/Cake.Core/CakeConsole.cs +++ b/src/Cake.Core/CakeConsole.cs @@ -36,14 +36,20 @@ public ConsoleColor BackgroundColor /// Initializes a new instance of the class. /// /// The environment. - public CakeConsole(ICakeEnvironment environment) + /// The configuration. + public CakeConsole(ICakeEnvironment environment, ICakeConfiguration configuration) { if (environment == null) { throw new ArgumentNullException(nameof(environment)); } - _supportAnsiEscapeCodes = new Lazy(() => AnsiDetector.SupportsAnsi(environment)); + if (configuration is null) + { + throw new ArgumentNullException(nameof(configuration)); + } + + _supportAnsiEscapeCodes = new Lazy(() => AnsiDetector.SupportsAnsi(environment, configuration)); } /// diff --git a/src/Cake.Core/Constants.cs b/src/Cake.Core/Constants.cs index 8da2e1eca7..c748d12d71 100644 --- a/src/Cake.Core/Constants.cs +++ b/src/Cake.Core/Constants.cs @@ -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 diff --git a/src/Cake.Core/Diagnostics/Console/AnsiDetector.cs b/src/Cake.Core/Diagnostics/Console/AnsiDetector.cs index 274a44bd16..8918050ffb 100644 --- a/src/Cake.Core/Diagnostics/Console/AnsiDetector.cs +++ b/src/Cake.Core/Diagnostics/Console/AnsiDetector.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text.RegularExpressions; +using Cake.Core.Configuration; namespace Cake.Core.Diagnostics { @@ -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 @@ -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"))) { diff --git a/src/Cake/Commands/DefaultCommand.cs b/src/Cake/Commands/DefaultCommand.cs index 86bd740e1f..106eca6da6 100644 --- a/src/Cake/Commands/DefaultCommand.cs +++ b/src/Cake/Commands/DefaultCommand.cs @@ -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; @@ -88,7 +89,7 @@ private static int LogException(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()))); if (log.Verbosity == Verbosity.Diagnostic) { diff --git a/src/Cake/Program.cs b/src/Cake/Program.cs index 8f25ace33c..83dcde5734 100644 --- a/src/Cake/Program.cs +++ b/src/Cake/Program.cs @@ -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;