From 419c9066dd0e18abb75a86c2ccc80aa883317077 Mon Sep 17 00:00:00 2001 From: Todd Stewart Date: Tue, 17 Jun 2025 16:51:57 -0500 Subject: [PATCH 1/3] GH-4517 - Cake Frosting WithCriteria Description Marked the current ShouldRun method obsolete. Added a new property to IFrostingTask, ShouldRunCriteria which is a List of CakeTaskCriteria. This allows you to have multiple criteria just like what could be done in Cake.Tool. --- src/Cake.Frosting/AsyncFrostingTask.cs | 6 ++++++ src/Cake.Frosting/FrostingTask.cs | 4 ++++ src/Cake.Frosting/IFrostingTask.cs | 7 +++++++ .../Internal/Extensions/FrostingTaskExtensions.cs | 1 + src/Cake.Frosting/Internal/FrostingEngine.cs | 8 ++++++-- 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Cake.Frosting/AsyncFrostingTask.cs b/src/Cake.Frosting/AsyncFrostingTask.cs index 13eb3b90ce..ff3a32ff55 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(); + /// /// 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..e3014ab8ab 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(); + /// /// 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..b6329c509e 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; @@ -126,9 +127,12 @@ private void ConfigureTasks() } // Is the criteria method overridden? - if (task.IsShouldRunOverridden(_context)) + if (task.ShouldRunCriteria.Any()) { - cakeTask.WithCriteria(task.ShouldRun, task.SkippedMessage); + foreach (var criteria in task.ShouldRunCriteria) + { + cakeTask.WithCriteria(criteria.Predicate, criteria.Message); + } } // Continue on error? From acbad7dca56c57869e225da030cc2cfcc40288c4 Mon Sep 17 00:00:00 2001 From: Todd Stewart Date: Tue, 17 Jun 2025 22:58:41 -0500 Subject: [PATCH 2/3] Fix for two build warnings. --- src/Cake.Frosting/AsyncFrostingTask.cs | 2 +- src/Cake.Frosting/FrostingTask.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cake.Frosting/AsyncFrostingTask.cs b/src/Cake.Frosting/AsyncFrostingTask.cs index ff3a32ff55..dda49a9edd 100644 --- a/src/Cake.Frosting/AsyncFrostingTask.cs +++ b/src/Cake.Frosting/AsyncFrostingTask.cs @@ -26,7 +26,7 @@ public abstract class AsyncFrostingTask : IFrostingTask where T : ICakeContext { /// - public virtual List ShouldRunCriteria => new(); + public virtual List ShouldRunCriteria => new List(); /// /// Runs the task using the specified context. diff --git a/src/Cake.Frosting/FrostingTask.cs b/src/Cake.Frosting/FrostingTask.cs index e3014ab8ab..b1320eee19 100644 --- a/src/Cake.Frosting/FrostingTask.cs +++ b/src/Cake.Frosting/FrostingTask.cs @@ -26,7 +26,7 @@ public abstract class FrostingTask : IFrostingTask where T : ICakeContext { /// - public virtual List ShouldRunCriteria => new(); + public virtual List ShouldRunCriteria => new List(); /// /// Runs the task using the specified context. From 75c7a6c17289619341ecd1d8adc7e4d19dd68e3e Mon Sep 17 00:00:00 2001 From: Todd Stewart Date: Sun, 22 Jun 2025 16:46:21 -0500 Subject: [PATCH 3/3] Added back the ShouldRun overidden check. Had removed the ShouldRun method from the tasks but that would be a breaking change. Forgot to add the check back in to see if it was overridden. --- src/Cake.Frosting/Internal/FrostingEngine.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Cake.Frosting/Internal/FrostingEngine.cs b/src/Cake.Frosting/Internal/FrostingEngine.cs index b6329c509e..fb0654eabb 100644 --- a/src/Cake.Frosting/Internal/FrostingEngine.cs +++ b/src/Cake.Frosting/Internal/FrostingEngine.cs @@ -126,7 +126,7 @@ private void ConfigureTasks() cakeTask.Does(task.RunAsync); } - // Is the criteria method overridden? + // Check if and criteria added to ShouldRunCriteria? if (task.ShouldRunCriteria.Any()) { foreach (var criteria in task.ShouldRunCriteria) @@ -134,6 +134,13 @@ private void ConfigureTasks() 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())