-
Is it possible to check if a task has already executed? I am using Cake Frosting. Something like
|
Beta Was this translation helpful? Give feedback.
Answered by
nils-a
Mar 3, 2021
Replies: 1 comment 1 reply
-
I'm not aware of such a feature. However, this is quickly implemented in Cake Frosting. The idea is to implement a I've modified the starter project to demonstrate that.: using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting;
public static class Program
{
public static int Main(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.UseTaskLifetime<TaskLifetime>()
.Run(args);
}
}
public class TaskLifetime : FrostingTaskLifetime<BuildContext>
{
public override void Setup(BuildContext context, ITaskSetupContext info)
{
}
public override void Teardown(BuildContext context, ITaskTeardownContext info)
{
context.TasksThatWereExecuted.Add(info.Task.Name);
}
}
public class BuildContext : FrostingContext
{
public bool Delay { get; set; }
public List<string> TasksThatWereExecuted { get; }
public bool HasTaskExecuted<T>()
where T: IFrostingTask
{
var name = typeof(T).GetCustomAttributes(typeof(TaskNameAttribute), false)
.Cast<TaskNameAttribute>()
.Single() // probably add some sanity checks here..
.Name;
return TasksThatWereExecuted.Contains(name);
}
public BuildContext(ICakeContext context)
: base(context)
{
Delay = context.Arguments.HasArgument("delay");
TasksThatWereExecuted = new List<string>();
}
}
[TaskName("Hello")]
public sealed class HelloTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information("Hello");
}
}
[TaskName("World")]
[IsDependentOn(typeof(HelloTask))]
public sealed class WorldTask : AsyncFrostingTask<BuildContext>
{
// Tasks can be asynchronous
public override async Task RunAsync(BuildContext context)
{
context.Log.Information("Hello has been executed: " + context.HasTaskExecuted<HelloTask>());
context.Log.Information("World has been executed: " + context.HasTaskExecuted<WorldTask>());
if (context.Delay)
{
context.Log.Information("Waiting...");
await Task.Delay(1500);
}
context.Log.Information("World");
}
}
[TaskName("Default")]
[IsDependentOn(typeof(WorldTask))]
public class DefaultTask : FrostingTask
{
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mgnslndh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not aware of such a feature. However, this is quickly implemented in Cake Frosting.
The idea is to implement a
FrostingTaskLifetime
that will simply add the current task to a list of tasks that have been executed. That way you could simply "ask" the list which task has been run already.I've modified the starter project to demonstrate that.: