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

Clear the Actions list through CakeTaskBuilder #2269

Open
Kemyke opened this issue Sep 5, 2018 · 5 comments
Open

Clear the Actions list through CakeTaskBuilder #2269

Kemyke opened this issue Sep 5, 2018 · 5 comments

Comments

@Kemyke
Copy link

Kemyke commented Sep 5, 2018

What You Are Seeing?

The only way to clear Actions from a CakeTaskBuilder is not cast its ICakeTaskInfo to CakeTask.

What is Expected?

I need to clean the actions from a Task. I have the CakeTaskBuilder which builds that task. I expect to be able to clear Actions without casting to exact type.

CakeTaskBuilder exampleTaskBuilder = Task("Example.Task")
  .Does(()=>
{
 // Some code
});

((CakeTask)exampleTaskBuilder .Task).Actions.Clear();

Possible solutions came to my mind:

  • ICakeTaskInfo might contains a List<Func<ICakeContext, Task>> Actions { get; } method
  • Or a specific ClearActions() method to clean the actions list.
  • If ICakeTaskInfo should be read-only then maybe the CakeTaskBuilder could have a funcion to clear the previously added tasks.
@jnm2
Copy link
Contributor

jnm2 commented Sep 6, 2018

Could it perhaps be simpler to wait and add the action conditionally later on, rather than eagerly adding and then conditionally removing? Builder patterns like this are usually additive.

@Kemyke
Copy link
Author

Kemyke commented Sep 7, 2018

The problem that we wrote some utility cake scripts and tasks with some default behavior which is appropriate most of the time.
But if a script which include the util script wants to change the default behavior first that should clear the previously registered action. I am looking for behavior like virtual methods in OOP in cake and that was the only way i can achieve that.
If there is a better way or recommendation to implement this, please share it with me.

@jnm2
Copy link
Contributor

jnm2 commented Sep 7, 2018

@Kemyke Interesting. I've done this the other way around, calling DefaultClean() and DefaultTest(). But you mentioned polymorphism; you actually could override virtual methods if you wanted. Not sure if this is a good pattern, but it's there:

class DefaultTasks
{
    protected ICakeContext Context { get; }

    public DefaultTasks(ICakeContext context)
    {
        Context = context ?? throw new ArgumentNullException(nameof(context));

        Context.Task("Build").Does((Action)Build);
        Context.Task("Test").Does((Action)Test);
    }

    public virtual Build()
    {
        Context.MSBuild(...);
    }

    public virtual Test()
    {
        Context.NUnit3(...);
    }
}
_ = new MyTasks(Context);

class MyTasks : DefaultTasks
{
    public MyTasks(ICakeContext context) : base(context)
    {
    }

    public override Build()
    {
        // Before
        base.Build();
        // After
    }

    public override Test()
    {
        // Replace
    }
}

@Kemyke
Copy link
Author

Kemyke commented Sep 10, 2018

@jnm2 hm, that is a nice idea, however the heavy use of classes could be a little bit confusing in cake scripts in my opinion. I use only inmutable, data classes in our 'cake build system'.

I only raised this issue because the lack of Clear actions functionality was strange to me. But if this is a design decision i can understand it and we can just close this issue. To be honest i asked this question twice on gitter but got no answer, so i decided to ask it here if this is a desing question or just a missing method. :)

@patriksvensson
Copy link
Member

@Kemyke I think this request is a little bit niche and adding and removing tasks manually is not a path that we normally encourage. Since ((CakeTask)exampleTaskBuilder .Task).Actions.Clear(); works for you, you could create an extension method that hides the casting.

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

No branches or pull requests

3 participants