-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GitLab CI module and implement ICakeLog for GitLab CI
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |