cake-build / cake

:cake: Cake (C# Make) is a cross platform build automation system.
https://cakebuild.net
MIT License
3.91k stars 730 forks source link

Access to ITaskSetupContext in Frosting #3946

Closed devlead closed 2 years ago

devlead commented 2 years ago

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 https://github.com/cake-build/cake/issues/1772#issuecomment-1220288335

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. https://github.com/cake-build/cake/blob/a1893a2bba26f49e089d66beeb78421cedf6126d/src/Cake.Frosting/Internal/FrostingEngine.cs#L83

like it is for the teardown https://github.com/cake-build/cake/blob/a1893a2bba26f49e089d66beeb78421cedf6126d/src/Cake.Frosting/Internal/FrostingEngine.cs#L89

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 commented 2 years ago

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

pitermarx commented 2 years ago

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

cake-build-bot commented 1 year ago

:tada: This issue has been resolved in version v3.0.0 :tada:

The release is available on:

Your GitReleaseManager bot :package::rocket: