Skip to content

Commit

Permalink
Add GitLab CI module and implement ICakeLog for GitLab CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0llo committed Dec 2, 2023
1 parent 2597531 commit 9d069d8
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Cake.BuildSystems.Module.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Cake.BuildSystems.Module/Cake.BuildSystems.Module.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<ProjectReference Include="..\Cake.GitHubActions.Module\Cake.GitHubActions.Module.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<ProjectReference Include="..\Cake.GitLabCI.Module\Cake.GitLabCI.Module.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
</ItemGroup>

<ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions src/Cake.GitLabCI.Module/Cake.GitLabCI.Module.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Cake.GitLabCI.Module</AssemblyName>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Cake.Module.Shared\Cake.Module.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cake.Core" Version="4.0.0" PrivateAssets="All" />
<PackageReference Include="Cake.Common" Version="4.0.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
78 changes: 78 additions & 0 deletions src/Cake.GitLabCI.Module/GitLabCILog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using Cake.Core;
using Cake.Core.Diagnostics;
using JetBrains.Annotations;

namespace Cake.AzurePipelines.Module
{
/// <summary>
/// <see cref="ICakeLog"/> implementation for GitLab CI.
/// </summary>
[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;

/// <summary>
/// Initializes a new instance of the <see cref="GitLabCILog"/> class.
/// </summary>
/// <param name="console">Implementation of <see cref="IConsole"/>.</param>
/// <param name="verbosity">Default <see cref="Verbosity"/>.</param>
public GitLabCILog(IConsole console, Verbosity verbosity = Verbosity.Normal)
{
_cakeLogImplementation = new CakeBuildLog(console, verbosity);
_console = console;
}

/// <inheritdoc />
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);
}
}

/// <inheritdoc />
public Verbosity Verbosity
{
get { return _cakeLogImplementation.Verbosity; }
set { _cakeLogImplementation.Verbosity = value; }
}
}
}
25 changes: 25 additions & 0 deletions src/Cake.GitLabCI.Module/GitLabCIModule.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// <see cref="ICakeModule"/> implementation for GitLab CI.
/// </summary>
public class GitLabCIModule : ICakeModule
{
/// <inheritdoc cref="ICakeModule.Register"/>
public void Register(ICakeContainerRegistrar registrar)
{
if (StringComparer.OrdinalIgnoreCase.Equals(Environment.GetEnvironmentVariable("CI_SERVER"), "yes"))
{
registrar.RegisterType<GitLabCILog>().As<ICakeLog>().Singleton();
}
}
}
}

0 comments on commit 9d069d8

Please sign in to comment.