diff --git a/src/Cake.Frosting/AsyncFrostingTask.cs b/src/Cake.Frosting/AsyncFrostingTask.cs index 13eb3b90ce..dda49a9edd 100644 --- a/src/Cake.Frosting/AsyncFrostingTask.cs +++ b/src/Cake.Frosting/AsyncFrostingTask.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Threading.Tasks; using Cake.Core; @@ -24,6 +25,9 @@ public abstract class AsyncFrostingTask : AsyncFrostingTask public abstract class AsyncFrostingTask : IFrostingTask where T : ICakeContext { + /// + public virtual List ShouldRunCriteria => new List(); + /// /// Runs the task using the specified context. /// @@ -41,6 +45,7 @@ public virtual Task RunAsync(T context) /// /// true if the task should run; otherwise false. /// + [Obsolete("ShouldRun method will be removed in a future version")] public virtual bool ShouldRun(T context) { return true; @@ -79,6 +84,7 @@ Task IFrostingTask.RunAsync(ICakeContext context) } /// + [Obsolete("ShouldRun method will be removed in a future version")] bool IFrostingTask.ShouldRun(ICakeContext context) { if (context is null) diff --git a/src/Cake.Frosting/FrostingTask.cs b/src/Cake.Frosting/FrostingTask.cs index 7e892f97e0..b1320eee19 100644 --- a/src/Cake.Frosting/FrostingTask.cs +++ b/src/Cake.Frosting/FrostingTask.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Threading.Tasks; using Cake.Core; @@ -24,6 +25,9 @@ public abstract class FrostingTask : FrostingTask public abstract class FrostingTask : IFrostingTask where T : ICakeContext { + /// + public virtual List ShouldRunCriteria => new List(); + /// /// Runs the task using the specified context. /// diff --git a/src/Cake.Frosting/IFrostingTask.cs b/src/Cake.Frosting/IFrostingTask.cs index 7563f3bd19..4d1ceb58d3 100644 --- a/src/Cake.Frosting/IFrostingTask.cs +++ b/src/Cake.Frosting/IFrostingTask.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Threading.Tasks; using Cake.Core; @@ -13,6 +14,11 @@ namespace Cake.Frosting /// public interface IFrostingTask { + /// + /// Gets ShouldRunCriteria for this task. + /// + List ShouldRunCriteria { get; } + /// /// Runs the task using the specified context. /// @@ -27,6 +33,7 @@ public interface IFrostingTask /// /// true if the task should run; otherwise false. /// + [Obsolete("ShouldRun method will be removed in a future version")] bool ShouldRun(ICakeContext context); /// diff --git a/src/Cake.Frosting/Internal/Extensions/FrostingTaskExtensions.cs b/src/Cake.Frosting/Internal/Extensions/FrostingTaskExtensions.cs index 01b140c4f4..d40f6eb72b 100644 --- a/src/Cake.Frosting/Internal/Extensions/FrostingTaskExtensions.cs +++ b/src/Cake.Frosting/Internal/Extensions/FrostingTaskExtensions.cs @@ -61,6 +61,7 @@ public static bool IsRunOverridden(this IFrostingTask task, IFrostingContext con throw new InvalidOperationException($"This method expects all {nameof(IFrostingTask)} to be instances of {nameof(FrostingTask)} or {nameof(AsyncFrostingTask)}."); } + [Obsolete("ShouldRun method will be removed in a future version")] public static bool IsShouldRunOverridden(this IFrostingTask task, IFrostingContext context) { return task.GetType().GetMethod(nameof(IFrostingTask.ShouldRun), new[] { context.GetType() }).IsOverriden(); diff --git a/src/Cake.Frosting/Internal/FrostingEngine.cs b/src/Cake.Frosting/Internal/FrostingEngine.cs index b2747e2c42..fb0654eabb 100644 --- a/src/Cake.Frosting/Internal/FrostingEngine.cs +++ b/src/Cake.Frosting/Internal/FrostingEngine.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Linq; using Cake.Core; using Cake.Core.Diagnostics; using Cake.Core.Scripting; @@ -125,11 +126,21 @@ private void ConfigureTasks() cakeTask.Does(task.RunAsync); } - // Is the criteria method overridden? - if (task.IsShouldRunOverridden(_context)) + // Check if and criteria added to ShouldRunCriteria? + if (task.ShouldRunCriteria.Any()) + { + foreach (var criteria in task.ShouldRunCriteria) + { + cakeTask.WithCriteria(criteria.Predicate, criteria.Message); + } + } + // Check to see if obsoleted ShouldRun is overridden? Done to not break old projects. +#pragma warning disable CS0618 // Type or member is obsolete + else if (task.IsShouldRunOverridden(_context)) { cakeTask.WithCriteria(task.ShouldRun, task.SkippedMessage); } +#pragma warning restore CS0618 // Type or member is obsolete // Continue on error? if (task.IsContinueOnError())