From 5671bd968906d2eec413ac6165c28c9a9385ecea Mon Sep 17 00:00:00 2001 From: JuergenRB <22403655+JuergenRB@users.noreply.github.com> Date: Tue, 28 Dec 2021 05:21:44 -0800 Subject: [PATCH 1/5] Use of IConsole in AzurePipelineLog to bypass ANSI formating --- .../AzurePipelinesLog.cs | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/Cake.AzurePipelines.Module/AzurePipelinesLog.cs b/src/Cake.AzurePipelines.Module/AzurePipelinesLog.cs index 82797201..d9047a8c 100644 --- a/src/Cake.AzurePipelines.Module/AzurePipelinesLog.cs +++ b/src/Cake.AzurePipelines.Module/AzurePipelinesLog.cs @@ -1,4 +1,4 @@ -using System; +using System; using Cake.Core; using Cake.Core.Diagnostics; using JetBrains.Annotations; @@ -11,6 +11,9 @@ namespace Cake.AzurePipelines.Module [UsedImplicitly] public class AzurePipelinesLog : ICakeLog { + private readonly ICakeLog _cakeLogImplementation; + private readonly IConsole _console; + /// /// Initializes a new instance of the class. /// @@ -19,36 +22,41 @@ public class AzurePipelinesLog : ICakeLog public AzurePipelinesLog(IConsole console, Verbosity verbosity = Verbosity.Normal) { _cakeLogImplementation = new CakeBuildLog(console, verbosity); + _console = console; } - private readonly ICakeLog _cakeLogImplementation; - /// public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) { - if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) { - switch (level) - { - case LogLevel.Fatal: - case LogLevel.Error: - _cakeLogImplementation.Write(Verbosity.Quiet, LogLevel.Information, - "##vso[task.logissue type=error;]{0}", string.Format(format, args)); - break; - case LogLevel.Warning: - _cakeLogImplementation.Write(Verbosity.Quiet, LogLevel.Information, - "##vso[task.logissue type=warning;]{0}", string.Format(format, args)); - break; - case LogLevel.Information: - case LogLevel.Verbose: - case LogLevel.Debug: - break; - default: - throw new ArgumentOutOfRangeException(nameof(level), level, null); - } + _cakeLogImplementation.Write(verbosity, level, format, args); } - _cakeLogImplementation.Write(verbosity, level, format, args); + if (verbosity > Verbosity) + { + return; + } + + switch (level) + { + case LogLevel.Fatal: + case LogLevel.Error: + _console.WriteLine("##vso[task.logissue type=error;]{0}", string.Format(format, args)); + break; + case LogLevel.Warning: + _console.WriteLine("##vso[task.logissue type=warning;]{0}", string.Format(format, args)); + break; + case LogLevel.Information: + case LogLevel.Verbose: + _console.WriteLine(format, args); + break; + case LogLevel.Debug: + _console.WriteLine("##[debug]{0}", string.Format(format, args)); + break; + default: + throw new ArgumentOutOfRangeException(nameof(level), level, null); + } } /// From 55d7ce326c80a4dae348e8a4d6beb3e50cab9b6f Mon Sep 17 00:00:00 2001 From: JuergenRB <22403655+JuergenRB@users.noreply.github.com> Date: Tue, 28 Dec 2021 06:16:57 -0800 Subject: [PATCH 2/5] Add AzurePipelinesExecutionStrategy --- .../AzurePipelinesExecutionStrategy.cs | 184 ++++++++++++++++++ .../AzurePipelinesModule.cs | 1 + 2 files changed, 185 insertions(+) create mode 100644 src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs diff --git a/src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs b/src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs new file mode 100644 index 00000000..977aa40a --- /dev/null +++ b/src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs @@ -0,0 +1,184 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +using Cake.Core; +using Cake.Core.Diagnostics; + +namespace Cake.AzurePipelines.Module +{ + /// + /// The AzurePipelines execution strategy. + /// + public class AzurePipelinesExecutionStrategy : IExecutionStrategy + { + private readonly ICakeLog _log; + private readonly IExecutionStrategy _defaultStrategy; + + /// + /// Initializes a new instance of the class. + /// + /// The log. + public AzurePipelinesExecutionStrategy(ICakeLog log) + { + _log = log; + _defaultStrategy = new DefaultExecutionStrategy(log); + } + + /// + public async Task ExecuteAsync(CakeTask task, ICakeContext context) + { + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + { + await _defaultStrategy.ExecuteAsync(task, context).ConfigureAwait(false); + return; + } + + if (task != null) + { + _log.Information(string.Empty); + _log.Information("##[group]{0}", task.Name); + + await task.Execute(context).ConfigureAwait(false); + + _log.Information("##[endgroup]"); + } + } + + /// + public async Task HandleErrorsAsync(Func action, Exception exception, ICakeContext context) + { + await _defaultStrategy.HandleErrorsAsync(action, exception, context).ConfigureAwait(false); + } + + /// + public async Task InvokeFinallyAsync(Func action) + { + await _defaultStrategy.InvokeFinallyAsync(action).ConfigureAwait(false); + } + + /// + public void PerformSetup(Action action, ISetupContext context) + { + + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + { + _defaultStrategy.PerformSetup(action, context); + return; + } + + if (action != null) + { + _log.Information(string.Empty); + _log.Information("##[group]Setup"); + + action(context); + + _log.Information("##[endgroup]"); + } + } + + /// + public void PerformTaskSetup(Action action, ITaskSetupContext taskSetupContext) + { + if (taskSetupContext == null) + { + throw new ArgumentNullException(nameof(taskSetupContext)); + } + + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + { + _defaultStrategy.PerformTaskSetup(action, taskSetupContext); + return; + } + + if (action != null) + { + _log.Information(string.Empty); + _log.Information("##[group]TaskSetup: {0}", taskSetupContext.Task.Name); + + action(taskSetupContext); + + _log.Information("##[endgroup]"); + } + } + + /// + public void PerformTaskTeardown(Action action, ITaskTeardownContext taskTeardownContext) + { + if (taskTeardownContext == null) + { + throw new ArgumentNullException(nameof(taskTeardownContext)); + } + + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + { + _defaultStrategy.PerformTaskTeardown(action, taskTeardownContext); + } + + if (action != null) + { + _log.Information(string.Empty); + _log.Information("##[group]TaskTeardown: {0}", taskTeardownContext.Task.Name); + + action(taskTeardownContext); + + _log.Information("##[endgroup]"); + } + } + + /// + public void PerformTeardown(Action action, ITeardownContext teardownContext) + { + if (teardownContext == null) + { + throw new ArgumentNullException(nameof(teardownContext)); + } + + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + { + _defaultStrategy.PerformTeardown(action, teardownContext); + return; + } + + if (action != null) + { + _log.Information(string.Empty); + _log.Information("##[group]Teardown"); + + action(teardownContext); + + _log.Information("##[endgroup]"); + } + } + + /// + public async Task ReportErrorsAsync(Func action, Exception exception) + { + await _defaultStrategy.ReportErrorsAsync(action, exception).ConfigureAwait(false); + } + + /// + public void Skip(CakeTask task, CakeTaskCriteria criteria) + { + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) + { + _defaultStrategy.Skip(task, criteria); + return; + } + + if (task != null) + { + _log.Verbose(string.Empty); + _log.Information("##[group]{0}", task.Name); + + var message = string.IsNullOrWhiteSpace(criteria.Message) + ? task.Name : criteria.Message; + _log.Verbose("Skipping task: {0}", message); + + _log.Information("##[endgroup]"); + } + } + } +} diff --git a/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs b/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs index bf87b539..3e063976 100644 --- a/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs +++ b/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs @@ -17,6 +17,7 @@ public void Register(ICakeContainerRegistrar registrar) { if (!string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("TF_BUILD"))) { + registrar.RegisterType().As().Singleton(); registrar.RegisterType().As().Singleton(); registrar.RegisterType().As().Singleton(); registrar.RegisterType().As().Singleton(); From a52f1847a6bef0543f3554dfc60e303bfedc7462 Mon Sep 17 00:00:00 2001 From: JuergenRB <22403655+JuergenRB@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:29:12 -0800 Subject: [PATCH 3/5] Remove AzurePipelinesExecutionStrategy --- .../AzurePipelinesExecutionStrategy.cs | 184 ------------------ .../AzurePipelinesModule.cs | 1 - 2 files changed, 185 deletions(-) delete mode 100644 src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs diff --git a/src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs b/src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs deleted file mode 100644 index 977aa40a..00000000 --- a/src/Cake.AzurePipelines.Module/AzurePipelinesExecutionStrategy.cs +++ /dev/null @@ -1,184 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -using Cake.Core; -using Cake.Core.Diagnostics; - -namespace Cake.AzurePipelines.Module -{ - /// - /// The AzurePipelines execution strategy. - /// - public class AzurePipelinesExecutionStrategy : IExecutionStrategy - { - private readonly ICakeLog _log; - private readonly IExecutionStrategy _defaultStrategy; - - /// - /// Initializes a new instance of the class. - /// - /// The log. - public AzurePipelinesExecutionStrategy(ICakeLog log) - { - _log = log; - _defaultStrategy = new DefaultExecutionStrategy(log); - } - - /// - public async Task ExecuteAsync(CakeTask task, ICakeContext context) - { - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) - { - await _defaultStrategy.ExecuteAsync(task, context).ConfigureAwait(false); - return; - } - - if (task != null) - { - _log.Information(string.Empty); - _log.Information("##[group]{0}", task.Name); - - await task.Execute(context).ConfigureAwait(false); - - _log.Information("##[endgroup]"); - } - } - - /// - public async Task HandleErrorsAsync(Func action, Exception exception, ICakeContext context) - { - await _defaultStrategy.HandleErrorsAsync(action, exception, context).ConfigureAwait(false); - } - - /// - public async Task InvokeFinallyAsync(Func action) - { - await _defaultStrategy.InvokeFinallyAsync(action).ConfigureAwait(false); - } - - /// - public void PerformSetup(Action action, ISetupContext context) - { - - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) - { - _defaultStrategy.PerformSetup(action, context); - return; - } - - if (action != null) - { - _log.Information(string.Empty); - _log.Information("##[group]Setup"); - - action(context); - - _log.Information("##[endgroup]"); - } - } - - /// - public void PerformTaskSetup(Action action, ITaskSetupContext taskSetupContext) - { - if (taskSetupContext == null) - { - throw new ArgumentNullException(nameof(taskSetupContext)); - } - - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) - { - _defaultStrategy.PerformTaskSetup(action, taskSetupContext); - return; - } - - if (action != null) - { - _log.Information(string.Empty); - _log.Information("##[group]TaskSetup: {0}", taskSetupContext.Task.Name); - - action(taskSetupContext); - - _log.Information("##[endgroup]"); - } - } - - /// - public void PerformTaskTeardown(Action action, ITaskTeardownContext taskTeardownContext) - { - if (taskTeardownContext == null) - { - throw new ArgumentNullException(nameof(taskTeardownContext)); - } - - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) - { - _defaultStrategy.PerformTaskTeardown(action, taskTeardownContext); - } - - if (action != null) - { - _log.Information(string.Empty); - _log.Information("##[group]TaskTeardown: {0}", taskTeardownContext.Task.Name); - - action(taskTeardownContext); - - _log.Information("##[endgroup]"); - } - } - - /// - public void PerformTeardown(Action action, ITeardownContext teardownContext) - { - if (teardownContext == null) - { - throw new ArgumentNullException(nameof(teardownContext)); - } - - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) - { - _defaultStrategy.PerformTeardown(action, teardownContext); - return; - } - - if (action != null) - { - _log.Information(string.Empty); - _log.Information("##[group]Teardown"); - - action(teardownContext); - - _log.Information("##[endgroup]"); - } - } - - /// - public async Task ReportErrorsAsync(Func action, Exception exception) - { - await _defaultStrategy.ReportErrorsAsync(action, exception).ConfigureAwait(false); - } - - /// - public void Skip(CakeTask task, CakeTaskCriteria criteria) - { - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TF_BUILD"))) - { - _defaultStrategy.Skip(task, criteria); - return; - } - - if (task != null) - { - _log.Verbose(string.Empty); - _log.Information("##[group]{0}", task.Name); - - var message = string.IsNullOrWhiteSpace(criteria.Message) - ? task.Name : criteria.Message; - _log.Verbose("Skipping task: {0}", message); - - _log.Information("##[endgroup]"); - } - } - } -} diff --git a/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs b/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs index 3e063976..bf87b539 100644 --- a/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs +++ b/src/Cake.AzurePipelines.Module/AzurePipelinesModule.cs @@ -17,7 +17,6 @@ public void Register(ICakeContainerRegistrar registrar) { if (!string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("TF_BUILD"))) { - registrar.RegisterType().As().Singleton(); registrar.RegisterType().As().Singleton(); registrar.RegisterType().As().Singleton(); registrar.RegisterType().As().Singleton(); From 67c4e16f02e6382d8a4773c4f92ebccac5eec10b Mon Sep 17 00:00:00 2001 From: JuergenRB <22403655+JuergenRB@users.noreply.github.com> Date: Mon, 3 Jan 2022 13:30:43 -0800 Subject: [PATCH 4/5] Add Groups to AzurePipelinesEngine --- .../AzurePipelinesEngine.cs | 71 ++++++++++++++++--- .../Cake.AzurePipelines.Module.csproj | 4 ++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/Cake.AzurePipelines.Module/AzurePipelinesEngine.cs b/src/Cake.AzurePipelines.Module/AzurePipelinesEngine.cs index 107ecc09..7c1e2b5f 100644 --- a/src/Cake.AzurePipelines.Module/AzurePipelinesEngine.cs +++ b/src/Cake.AzurePipelines.Module/AzurePipelinesEngine.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using Cake.Common.Build; @@ -24,17 +24,54 @@ public sealed class AzurePipelinesEngine : CakeEngineBase public AzurePipelinesEngine(ICakeDataService dataService, ICakeLog log) : base(new CakeEngine(dataService, log)) { - _engine.BeforeSetup += BuildSetup; - _engine.BeforeTaskSetup += OnTaskSetup; - _engine.BeforeTaskTeardown += OnTaskTeardown; - _engine.BeforeTeardown += OnBuildTeardown; + _log = log; + _engine.BeforeSetup += OnBeforeSetup; + _engine.AfterSetup += OnAfterSetup; + + _engine.BeforeTaskSetup += OnBeforeTaskSetup; + + _engine.BeforeTaskTeardown += OnBeforeTaskTeardown; + + _engine.AfterTaskTeardown += OnAfterTaskTeardown; + + _engine.BeforeTeardown += OnBeforeTeardown; + _engine.AfterTeardown += OnAfterTeardown; + } + + private void OnAfterTaskTeardown(object sender, AfterTaskTeardownEventArgs e) + { + var b = e.TaskTeardownContext.BuildSystem(); + if (b.IsRunningOnPipelines()) + { + WriteGroupEndCommand(); + } + } + + private void OnAfterTeardown(object sender, AfterTeardownEventArgs e) + { + var b = e.TeardownContext.BuildSystem(); + if (b.IsRunningOnPipelines()) + { + WriteGroupEndCommand(); + } } - private void OnBuildTeardown(object sender, BeforeTeardownEventArgs e) + private void OnAfterSetup(object sender, AfterSetupEventArgs e) + { + var b = e.Context.BuildSystem(); + if (b.IsRunningOnPipelines()) + { + WriteGroupEndCommand(); + } + } + + private void OnBeforeTeardown(object sender, BeforeTeardownEventArgs e) { var b = e.TeardownContext.BuildSystem(); if (b.IsRunningOnPipelines()) { + WriteGroupCommand("Teardown"); + b.AzurePipelines.Commands.UpdateRecord(_parentRecord, new AzurePipelinesRecordData { FinishTime = DateTime.Now, @@ -45,7 +82,7 @@ private void OnBuildTeardown(object sender, BeforeTeardownEventArgs e) } } - private void OnTaskTeardown(object sender, BeforeTaskTeardownEventArgs e) + private void OnBeforeTaskTeardown(object sender, BeforeTaskTeardownEventArgs e) { var b = e.TaskTeardownContext.BuildSystem(); if (b.IsRunningOnPipelines()) @@ -74,11 +111,13 @@ private void OnTaskTeardown(object sender, BeforeTaskTeardownEventArgs e) return AzurePipelinesTaskResult.Succeeded; } - private void OnTaskSetup(object sender, BeforeTaskSetupEventArgs e) + private void OnBeforeTaskSetup(object sender, BeforeTaskSetupEventArgs e) { var b = e.TaskSetupContext.BuildSystem(); if (b.IsRunningOnPipelines()) { + WriteGroupCommand(e.TaskSetupContext.Task.Name); + var currentTask = _engine.Tasks.First(t => t.Name == e.TaskSetupContext.Task.Name); var currentIndex = _engine.Tasks.ToList().IndexOf(currentTask); @@ -97,11 +136,13 @@ private int GetProgress(int currentTask, int count) return Convert.ToInt32(Math.Truncate(f)); } - private void BuildSetup(object sender, BeforeSetupEventArgs e) + private void OnBeforeSetup(object sender, BeforeSetupEventArgs e) { var b = e.Context.BuildSystem(); if (b.IsRunningOnPipelines()) { + WriteGroupCommand("Setup"); + e.Context.AzurePipelines().Commands.SetProgress(0, string.Empty); var g = e.Context.AzurePipelines() .Commands.CreateNewRecord("Cake Build", "build", 0, new AzurePipelinesRecordData { StartTime = DateTime.Now }); @@ -109,7 +150,19 @@ private void BuildSetup(object sender, BeforeSetupEventArgs e) } } + private void WriteGroupCommand(string groupName) + { + _log.Verbose(string.Empty); + _log.Information("##[group]{0}", groupName); + } + + private void WriteGroupEndCommand() + { + _log.Information("##[endgroup]"); + } + private Guid _parentRecord; + private ICakeLog _log; private Dictionary TaskRecords { get; } = new Dictionary(); } diff --git a/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj b/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj index 8a46f2b0..bec11a0e 100644 --- a/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj +++ b/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj @@ -7,6 +7,10 @@ false + + + + From 736196c283b97f3d0ce6595c6546042c028bd68a Mon Sep 17 00:00:00 2001 From: JuergenRB <22403655+JuergenRB@users.noreply.github.com> Date: Tue, 4 Jan 2022 00:07:10 -0800 Subject: [PATCH 5/5] Removes overlooked tag in csproj --- .../Cake.AzurePipelines.Module.csproj | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj b/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj index bec11a0e..6c798e31 100644 --- a/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj +++ b/src/Cake.AzurePipelines.Module/Cake.AzurePipelines.Module.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -7,10 +7,6 @@ false - - - -