|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +using Cake.Core; |
| 5 | +using Cake.Core.Diagnostics; |
| 6 | + |
| 7 | +using JetBrains.Annotations; |
| 8 | + |
| 9 | +namespace Cake.AzurePipelines.Module |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// <see cref="ICakeLog"/> implementation for GitLab CI. |
| 13 | + /// </summary> |
| 14 | + [UsedImplicitly] |
| 15 | + public class GitLabCILog : ICakeLog |
| 16 | + { |
| 17 | + private readonly ICakeLog _cakeLogImplementation; |
| 18 | + private readonly IConsole _console; |
| 19 | + |
| 20 | + // Define the escape sequenes to make GitLab show colored messages |
| 21 | + // For reference, see https://docs.gitlab.com/ee/ci/yaml/script.html#add-color-codes-to-script-output |
| 22 | + // For the colors, match the colors used by Cake, see https://github.com/cake-build/cake/blob/ed612029b92f5da2b6cbdfe295c62e6b99a2963d/src/Cake.Core/Diagnostics/Console/ConsolePalette.cs#L34C17-L34C17 |
| 23 | + private readonly Dictionary<LogLevel, string> _escapeSequences = new Dictionary<LogLevel, string>() |
| 24 | + { |
| 25 | + { LogLevel.Fatal, $"{AnsiEscapeCodes.BackgroundMagenta}{AnsiEscapeCodes.ForegroundWhite}" }, |
| 26 | + { LogLevel.Error, $"{AnsiEscapeCodes.BackgroundRed}{AnsiEscapeCodes.ForegroundWhite}" }, |
| 27 | + { LogLevel.Warning, AnsiEscapeCodes.ForegroundYellow }, |
| 28 | + { LogLevel.Verbose, AnsiEscapeCodes.ForegroundLightGray }, |
| 29 | + { LogLevel.Debug, AnsiEscapeCodes.ForegroundDarkGray }, |
| 30 | + }; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the <see cref="GitLabCILog"/> class. |
| 34 | + /// </summary> |
| 35 | + /// <param name="console">Implementation of <see cref="IConsole"/>.</param> |
| 36 | + /// <param name="verbosity">Default <see cref="Verbosity"/>.</param> |
| 37 | + public GitLabCILog(IConsole console, Verbosity verbosity = Verbosity.Normal) |
| 38 | + { |
| 39 | + _cakeLogImplementation = new CakeBuildLog(console, verbosity); |
| 40 | + _console = console; |
| 41 | + } |
| 42 | + |
| 43 | + /// <inheritdoc /> |
| 44 | + public Verbosity Verbosity |
| 45 | + { |
| 46 | + get { return _cakeLogImplementation.Verbosity; } |
| 47 | + set { _cakeLogImplementation.Verbosity = value; } |
| 48 | + } |
| 49 | + |
| 50 | + /// <inheritdoc /> |
| 51 | + public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) |
| 52 | + { |
| 53 | + if (!StringComparer.OrdinalIgnoreCase.Equals(Environment.GetEnvironmentVariable("CI_SERVER"), "yes")) |
| 54 | + { |
| 55 | + _cakeLogImplementation.Write(verbosity, level, format, args); |
| 56 | + } |
| 57 | + |
| 58 | + if (verbosity > Verbosity) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + string message; |
| 64 | + if (_escapeSequences.TryGetValue(level, out var sequence)) |
| 65 | + { |
| 66 | + message = $"{sequence}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}"; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + message = $"{level}: {string.Format(format, args)}"; |
| 71 | + } |
| 72 | + |
| 73 | + if (level > LogLevel.Error) |
| 74 | + { |
| 75 | + _console.WriteLine(message); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + _console.WriteErrorLine(message); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments