Skip to content

Commit b7eca56

Browse files
committed
Adds ExecuteOnlyWhen to group of tasks in target.
1 parent 9a9c241 commit b7eca56

File tree

4 files changed

+21
-54
lines changed

4 files changed

+21
-54
lines changed

src/FlubuCore/Context/FluentInterface/Interfaces/ITarget.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ public interface ITarget : ITargetBaseFluentInterfaceOfT<ITarget>
253253
/// <param name="targetAction">specify tasks to add in target action.</param>
254254
/// <param name="onFinally">action that will be taken when all task finish or when error occures.</param>
255255
/// <param name="onError">action that will be taken on any task actions.</param>
256-
/// <param name="when">Tasks will be added only if specified condition is meet.</param>
256+
/// <param name="when">Tasks in group will be added to target only if specified condition is meet. When condition is executed when target is configured</param>
257+
/// <param name="executeOnlyWhen">Tasks in group will be executed. When condition is executed when target is executed. </param>
258+
/// <param name="cleanupOnCancel">onFinally is executed even if script execution is cancelled with ctrl+c etc.</param>
257259
/// <returns></returns>
258-
ITarget Group(Action<ITargetBaseFluentInterfaceOfT<ITarget>> targetAction, Action<ITaskContext> onFinally = null, Action<ITaskContext, Exception> onError = null, Func<ITaskContext, bool> when = null, bool cleanupOnCancel = false);
260+
ITarget Group(Action<ITargetBaseFluentInterfaceOfT<ITarget>> targetAction, Action<ITaskContext> onFinally = null, Action<ITaskContext, Exception> onError = null, Func<ITaskContext, bool> when = null, Func<ITaskContext, bool> executeOnlyWhen = null, bool cleanupOnCancel = false);
259261
}
260262
}

src/FlubuCore/Context/FluentInterface/TargetFluentInterface.cs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,6 @@ namespace FlubuCore.Context.FluentInterface
1111
{
1212
public class TargetFluentInterface : TargetBaseFluentInterface<ITarget>, ITarget
1313
{
14-
public ITarget DependsOn(params string[] targetNames)
15-
{
16-
LastTargetAction = TargetAction.AddDependency;
17-
ActionCount = targetNames.Length;
18-
Target.DependsOn(targetNames);
19-
return this;
20-
}
21-
22-
public ITarget DependsOn(params ITargetInternal[] targets)
23-
{
24-
LastTargetAction = TargetAction.AddDependency;
25-
ActionCount = targets.Length;
26-
Target.DependsOn(targets);
27-
return this;
28-
}
29-
30-
public ITarget DependsOn(params ITarget[] targets)
31-
{
32-
LastTargetAction = TargetAction.AddDependency;
33-
ActionCount = targets.Length;
34-
foreach (var t in targets)
35-
{
36-
var target = (TargetFluentInterface)t;
37-
Target.DependsOn(target.Target);
38-
}
39-
40-
return this;
41-
}
42-
43-
public ITarget DependsOnAsync(params ITargetInternal[] targets)
44-
{
45-
LastTargetAction = TargetAction.AddDependency;
46-
ActionCount = targets.Length;
47-
Target.DependsOnAsync(targets);
48-
return this;
49-
}
50-
51-
public ITarget DependsOnAsync(params ITarget[] targets)
52-
{
53-
LastTargetAction = TargetAction.AddDependency;
54-
ActionCount = targets.Length;
55-
foreach (var t in targets)
56-
{
57-
var target = (TargetFluentInterface)t;
58-
Target.DependsOnAsync(target.Target);
59-
}
60-
61-
return this;
62-
}
63-
6414
public ITarget SetAsDefault()
6515
{
6616
LastTargetAction = TargetAction.Other;
@@ -85,7 +35,7 @@ public ITarget SetAsHidden()
8535
return this;
8636
}
8737

88-
public ITarget Group(Action<ITargetBaseFluentInterfaceOfT<ITarget>> targetAction, Action<ITaskContext> onFinally = null, Action<ITaskContext, Exception> onError = null, Func<ITaskContext, bool> when = null, bool cleanupOnCancel = false)
38+
public ITarget Group(Action<ITargetBaseFluentInterfaceOfT<ITarget>> targetAction, Action<ITaskContext> onFinally = null, Action<ITaskContext, Exception> onError = null, Func<ITaskContext, bool> when = null, Func<ITaskContext, bool> executeOnlyWhen = null, bool cleanupOnCancel = false)
8939
{
9040
LastTargetAction = TargetAction.Other;
9141
ActionCount = 0;
@@ -94,7 +44,8 @@ public ITarget Group(Action<ITargetBaseFluentInterfaceOfT<ITarget>> targetAction
9444
GroupId = Guid.NewGuid().ToString(),
9545
OnErrorAction = onError,
9646
FinallyAction = onFinally,
97-
CleanupOnCancel = cleanupOnCancel
47+
ExecuteOnlyWhen = executeOnlyWhen,
48+
CleanupOnCancel = cleanupOnCancel,
9849
};
9950

10051
var conditionMeet = when?.Invoke(Context);

src/FlubuCore/Targeting/Target.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@ protected override int DoExecute(ITaskContextInternal context)
528528
for (int i = 0; i < taskGroupsCount; i++)
529529
{
530530
int tasksCount = _taskGroups[i].Tasks.Count;
531+
532+
bool shouldExecute = true;
533+
if (_taskGroups[i].ExecuteOnlyWhen != null)
534+
{
535+
shouldExecute = _taskGroups[i].ExecuteOnlyWhen.Invoke(context);
536+
}
537+
538+
if (!shouldExecute)
539+
{
540+
continue;
541+
}
542+
531543
if (_taskGroups[i].CleanupOnCancel)
532544
{
533545
CleanUpStore.AddCleanupAction(_taskGroups[i].FinallyAction);

src/FlubuCore/Targeting/TaskGroup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ public TaskGroup()
2222
public bool CleanupOnCancel { get; set; }
2323

2424
public Action<ITaskContext, Exception> OnErrorAction { get; set; }
25+
26+
public Func<ITaskContext, bool> ExecuteOnlyWhen { get; set; }
2527
}
2628
}

0 commit comments

Comments
 (0)