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

DoesForEach() extension method #1761

Closed
david-driscoll opened this issue Aug 20, 2017 · 2 comments
Closed

DoesForEach() extension method #1761

david-driscoll opened this issue Aug 20, 2017 · 2 comments
Labels
Milestone

Comments

@david-driscoll
Copy link
Contributor

I ran into a scenario where I wanted to run a set of actions over a collection, even in the event that one of more fails. The main one is running unit tests, but there are other scenarios like handling many files with a common action, that I've found it handy.

For my code it's replaced anywhere I would use a foreach at the top of my Does() statement, hence calling the extension method DoesForEach().

public static CakeTaskBuilder<ActionTask> DoesForEach<T>(this CakeTaskBuilder<ActionTask> builder, IEnumerable<T> items, Action<T> action)
{
    return DoesForEach(builder, () => items, (i, c) => action(i));
}

public static CakeTaskBuilder<ActionTask> DoesForEach<T>(this CakeTaskBuilder<ActionTask> builder, IEnumerable<T> items, Action<T, ICakeContext> action)
{
    return DoesForEach(builder, () => items, action);
}
public static CakeTaskBuilder<ActionTask> DoesForEach<T>(this CakeTaskBuilder<ActionTask> builder, Func<IEnumerable<T>> items, Action<T> action)
{
    return DoesForEach(builder, items, (i, c) => action(i));
}

public static CakeTaskBuilder<ActionTask> DoesForEach<T>(this CakeTaskBuilder<ActionTask> builder, Func<IEnumerable<T>> items, Action<T, ICakeContext> action)
{
    builder.Does(c =>
    {
        var exceptions = new List<Exception>();
        foreach (var item in items())
        {
            try
            {
                action(item, c);
            }
            catch (Exception e)
            {
                exceptions.Add(e);
            }
        }
        if (exceptions.Any()) throw new AggregateException("Task failed with following exceptions", exceptions);
    });
    return builder;
}
@devlead
Copy link
Member

devlead commented Aug 20, 2017

Sounds like a reasonable addition to cake, would you be willing to send in a PR for review?

@david-driscoll
Copy link
Contributor Author

Closing merged in #1767

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

No branches or pull requests

2 participants