From 9d069d887bd4d7d375e3097cfba0564200a9c4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gr=C3=BCnwald?= Date: Sat, 2 Dec 2023 21:24:32 +0100 Subject: [PATCH] Add GitLab CI module and implement ICakeLog for GitLab CI --- src/Cake.BuildSystems.Module.sln | 14 ++++ .../Cake.BuildSystems.Module.csproj | 3 + .../Cake.GitLabCI.Module.csproj | 27 +++++++ src/Cake.GitLabCI.Module/GitLabCILog.cs | 78 +++++++++++++++++++ src/Cake.GitLabCI.Module/GitLabCIModule.cs | 25 ++++++ 5 files changed, 147 insertions(+) create mode 100644 src/Cake.GitLabCI.Module/Cake.GitLabCI.Module.csproj create mode 100644 src/Cake.GitLabCI.Module/GitLabCILog.cs create mode 100644 src/Cake.GitLabCI.Module/GitLabCIModule.cs diff --git a/src/Cake.BuildSystems.Module.sln b/src/Cake.BuildSystems.Module.sln index c8fc8274..13196012 100644 --- a/src/Cake.BuildSystems.Module.sln +++ b/src/Cake.BuildSystems.Module.sln @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cake.BuildSystems.Module", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cake.GitHubActions.Module", "Cake.GitHubActions.Module\Cake.GitHubActions.Module.csproj", "{A6BAA454-179C-422C-A954-38852265722F}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cake.GitLabCI.Module", "Cake.GitLabCI.Module\Cake.GitLabCI.Module.csproj", "{DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -115,6 +117,18 @@ Global {A6BAA454-179C-422C-A954-38852265722F}.Release|x64.Build.0 = Release|Any CPU {A6BAA454-179C-422C-A954-38852265722F}.Release|x86.ActiveCfg = Release|Any CPU {A6BAA454-179C-422C-A954-38852265722F}.Release|x86.Build.0 = Release|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Debug|x64.ActiveCfg = Debug|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Debug|x64.Build.0 = Debug|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Debug|x86.ActiveCfg = Debug|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Debug|x86.Build.0 = Debug|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Release|Any CPU.Build.0 = Release|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Release|x64.ActiveCfg = Release|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Release|x64.Build.0 = Release|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Release|x86.ActiveCfg = Release|Any CPU + {DA6236E5-E4B3-42B4-BB3D-69CE06DAB85B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Cake.BuildSystems.Module/Cake.BuildSystems.Module.csproj b/src/Cake.BuildSystems.Module/Cake.BuildSystems.Module.csproj index 55130d22..b202dec8 100644 --- a/src/Cake.BuildSystems.Module/Cake.BuildSystems.Module.csproj +++ b/src/Cake.BuildSystems.Module/Cake.BuildSystems.Module.csproj @@ -46,6 +46,9 @@ all + + all + diff --git a/src/Cake.GitLabCI.Module/Cake.GitLabCI.Module.csproj b/src/Cake.GitLabCI.Module/Cake.GitLabCI.Module.csproj new file mode 100644 index 00000000..6baf8915 --- /dev/null +++ b/src/Cake.GitLabCI.Module/Cake.GitLabCI.Module.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + true + Cake.GitLabCI.Module + false + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/src/Cake.GitLabCI.Module/GitLabCILog.cs b/src/Cake.GitLabCI.Module/GitLabCILog.cs new file mode 100644 index 00000000..3fdc5383 --- /dev/null +++ b/src/Cake.GitLabCI.Module/GitLabCILog.cs @@ -0,0 +1,78 @@ +using System; +using Cake.Core; +using Cake.Core.Diagnostics; +using JetBrains.Annotations; + +namespace Cake.AzurePipelines.Module +{ + /// + /// implementation for GitLab CI. + /// + [UsedImplicitly] + public class GitLabCILog : ICakeLog + { + private static class AnsiEscapeCodes + { + public static readonly string Reset = string.Format(FORMAT, 0); + public static readonly string ForegroundRed = string.Format(FORMAT, 31); + public static readonly string ForegroundYellow = string.Format(FORMAT, 33); + + private const string FORMAT = "\u001B[{0}m"; + } + + private readonly ICakeLog _cakeLogImplementation; + private readonly IConsole _console; + + /// + /// Initializes a new instance of the class. + /// + /// Implementation of . + /// Default . + public GitLabCILog(IConsole console, Verbosity verbosity = Verbosity.Normal) + { + _cakeLogImplementation = new CakeBuildLog(console, verbosity); + _console = console; + } + + /// + public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) + { + if (!StringComparer.OrdinalIgnoreCase.Equals(Environment.GetEnvironmentVariable("CI_SERVER"), "yes")) + { + _cakeLogImplementation.Write(verbosity, level, format, args); + } + + if (verbosity > Verbosity) + { + return; + } + + // Use colored output for log messages on GitLab CI + // For reference, see https://docs.gitlab.com/ee/ci/yaml/script.html#add-color-codes-to-script-output + switch (level) + { + case LogLevel.Fatal: + case LogLevel.Error: + _console.WriteLine($"{AnsiEscapeCodes.ForegroundRed}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}"); + break; + case LogLevel.Warning: + _console.WriteLine($"{AnsiEscapeCodes.ForegroundYellow}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}"); + break; + case LogLevel.Information: + case LogLevel.Verbose: + case LogLevel.Debug: + _console.WriteLine($"{level}: {string.Format(format, args)}"); + break; + default: + throw new ArgumentOutOfRangeException(nameof(level), level, null); + } + } + + /// + public Verbosity Verbosity + { + get { return _cakeLogImplementation.Verbosity; } + set { _cakeLogImplementation.Verbosity = value; } + } + } +} diff --git a/src/Cake.GitLabCI.Module/GitLabCIModule.cs b/src/Cake.GitLabCI.Module/GitLabCIModule.cs new file mode 100644 index 00000000..b3de95e3 --- /dev/null +++ b/src/Cake.GitLabCI.Module/GitLabCIModule.cs @@ -0,0 +1,25 @@ +using System; + +using Cake.Core.Annotations; +using Cake.Core.Composition; +using Cake.Core.Diagnostics; + +[assembly: CakeModule(typeof(Cake.AzurePipelines.Module.GitLabCIModule))] + +namespace Cake.AzurePipelines.Module +{ + /// + /// implementation for GitLab CI. + /// + public class GitLabCIModule : ICakeModule + { + /// + public void Register(ICakeContainerRegistrar registrar) + { + if (StringComparer.OrdinalIgnoreCase.Equals(Environment.GetEnvironmentVariable("CI_SERVER"), "yes")) + { + registrar.RegisterType().As().Singleton(); + } + } + } +}