Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to ITaskSetupContext in Frosting #3946

Closed
devlead opened this issue Aug 19, 2022 · 3 comments · Fixed by #4022
Closed

Access to ITaskSetupContext in Frosting #3946

devlead opened this issue Aug 19, 2022 · 3 comments · Fixed by #4022
Assignees
Milestone

Comments

@devlead
Copy link
Member

devlead commented Aug 19, 2022

Sorry to resurrect an old thread, but does someone know how to do this for a
"Frosting" project? i.e. to get access to ISetupContext.TasksToExecute.

It is exposed in FrostingTaskLifetime<T>.Setup but that is before each task.
I can't find an equivalent for FrostingLifetime<T>.Setup which is before all tasks.
Maybe I'm looking in the wrong places?

Originally posted by @lonix1 in #1772 (comment)

A quick look at the code, it looks like currently, the SetupContext isn't passed to the Frosting setup, adding it to IFrostingLifetime / IFrostingSetup would be a breaking change, so maybe first fixed in the next mayor release, or at least some more thought will need to go into it.

_engine.RegisterSetupAction(info => _setup.Setup(_context));

like it is for the teardown

_engine.RegisterTeardownAction(info => _teardown.Teardown(_context, info));

A dirty workaround, in this case, could be to register the setup task yourself.

This could be done by injecting a custom setup task in IoC and adding it and ICakeEngine to your own custom context. that could look something like

using System.Threading.Tasks;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting;
using Microsoft.Extensions.DependencyInjection;

public static class Program
{
    public static int Main(string[] args)
    {
        return new CakeHost()
            .ConfigureServices(services => services.AddSingleton<CustomBuildSetup>())
            .UseContext<BuildContext>()
            .Run(args);
    }
}

public class BuildContext : FrostingContext
{
    public BuildContext(ICakeContext context, ICakeEngine cakeEngine, CustomBuildSetup customBuildSetup)
        : base(context)
    {
        cakeEngine.RegisterSetupAction(setupContext => customBuildSetup.Setup(this, setupContext));
    }
}

public class CustomBuildSetup
{
    public void Setup(BuildContext buildContext, ISetupContext setupContext)
    {
        buildContext.Log.Information("Custom setup found {0} tasks starting with {1}", setupContext.TasksToExecute.Count, setupContext.TargetTask.Name);
    }
}
@lonix1
Copy link

lonix1 commented Aug 19, 2022

Thank you for now I will try to use your workaround, it's very helpful.

@pitermarx
Copy link
Contributor

pitermarx commented Aug 19, 2022

Shameless plug here for Cake.Console

if you write this

using Cake.Console;
using Cake.Common.Diagnostics;
using Cake.Core.Diagnostics;
using Cake.Core;
using System.Linq;

var host = new CakeHostBuilder().BuildHost(args);

host.Setup(ctx =>
    ctx.Information($"The following tasks will execute {string.Join("->", ctx.TasksToExecute.Select(t => t.Name))}"));

host.Task("Default")
    .Does(c => c.Information("DefaultTask"));

host.Task("Dependency")
    .IsDependeeOf("Default")
    .Does(c => c.Information("DependencyTask"));

host.Task("NonDependency")
    .Does(c => c.Information("NonDependencyTask"));

host.RunTarget("Default");

You will get this

image

devlead added a commit to devlead/cake that referenced this issue Oct 25, 2022
@devlead devlead self-assigned this Oct 25, 2022
@devlead devlead added this to the v3.0.0 milestone Oct 25, 2022
devlead added a commit to devlead/cake that referenced this issue Oct 27, 2022
devlead added a commit to devlead/cake that referenced this issue Oct 28, 2022
devlead added a commit to devlead/cake that referenced this issue Oct 29, 2022
devlead added a commit that referenced this issue Oct 29, 2022
GH3946: Add ISetupContext to Frosting lifetime
@cake-build-bot
Copy link

🎉 This issue has been resolved in version v3.0.0 🎉

The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants