forked from cake-build/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCakeTaskBuilderExtensions.cs
More file actions
222 lines (203 loc) · 9.31 KB
/
CakeTaskBuilderExtensions.cs
File metadata and controls
222 lines (203 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Cake.Core
{
/// <summary>
/// Contains extension methods for <see cref="CakeTaskBuilder{T}"/>.
/// </summary>
public static class CakeTaskBuilderExtensions
{
/// <summary>
/// Creates a dependency between two tasks.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The task builder.</param>
/// <param name="name">The name of the dependent task.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> IsDependentOn<T>(this CakeTaskBuilder<T> builder, string name)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.AddDependency(name);
return builder;
}
/// <summary>
/// Adds a criteria that has to be fulfilled for the task to run.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The task builder.</param>
/// <param name="criteria">The criteria.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> WithCriteria<T>(this CakeTaskBuilder<T> builder, bool criteria)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.AddCriteria(_ => criteria);
return builder;
}
/// <summary>
/// Adds a criteria that has to be fulfilled for the task to run.
/// The criteria is evaluated when traversal of the graph occurs.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The task builder.</param>
/// <param name="criteria">The criteria.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> WithCriteria<T>(this CakeTaskBuilder<T> builder, Func<bool> criteria)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.AddCriteria(criteria);
return builder;
}
/// <summary>
/// Adds a criteria that has to be fulfilled for the task to run.
/// The criteria is evaluated when traversal of the graph occurs.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The task builder.</param>
/// <param name="criteria">The criteria.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> WithCriteria<T>(this CakeTaskBuilder<T> builder, Func<ICakeContext, bool> criteria)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.AddCriteria(criteria);
return builder;
}
/// <summary>
/// Adds an action to be executed when the task is invoked.
/// </summary>
/// <param name="builder">The task builder.</param>
/// <param name="action">The action.</param>
/// <returns>The same <see cref="CakeTaskBuilder{ActionTask}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<ActionTask> Does(this CakeTaskBuilder<ActionTask> builder, Action action)
{
if (action == null)
{
throw new ArgumentNullException("action");
}
return Does(builder, context => action());
}
/// <summary>
/// Adds an action to be executed when the task is invoked.
/// </summary>
/// <param name="builder">The task builder.</param>
/// <param name="action">The action.</param>
/// <returns>The same <see cref="CakeTaskBuilder{ActionTask}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<ActionTask> Does(this CakeTaskBuilder<ActionTask> builder,
Action<ICakeContext> action)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.AddAction(action);
return builder;
}
/// <summary>
/// Adds a description to the task.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The task builder.</param>
/// <param name="description">The description.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> Description<T>(this CakeTaskBuilder<T> builder, string description)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.Description = description;
return builder;
}
/// <summary>
/// Adds an indication to the task that a thrown exception will not halt the script execution.
/// </summary>
/// <param name="builder">The task builder.</param>
/// <returns>The same <see cref="CakeTaskBuilder{ActionTask}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<ActionTask> ContinueOnError(this CakeTaskBuilder<ActionTask> builder)
{
return OnError(builder, () => { });
}
/// <summary>
/// Adds an error handler to be executed if an exception occurs in the task.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="errorHandler">The error handler.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> OnError<T>(this CakeTaskBuilder<T> builder, Action errorHandler)
where T : CakeTask
{
return OnError(builder, exception => errorHandler());
}
/// <summary>
/// Adds an error handler to be executed if an exception occurs in the task.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="errorHandler">The error handler.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> OnError<T>(this CakeTaskBuilder<T> builder, Action<Exception> errorHandler)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.SetErrorHandler(errorHandler);
return builder;
}
/// <summary>
/// Adds a finally handler to be executed after the task have finished executing.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="finallyHandler">The finally handler.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> Finally<T>(this CakeTaskBuilder<T> builder, Action finallyHandler)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.SetFinallyHandler(finallyHandler);
return builder;
}
/// <summary>
/// Adds an error reporter for the task to be executed when an exception is thrown from the task.
/// This action is invoked before the error handler, but gives no opportunity to recover from the error.
/// </summary>
/// <typeparam name="T">The task type.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="errorReporter">The finally handler.</param>
/// <returns>The same <see cref="CakeTaskBuilder{T}"/> instance so that multiple calls can be chained.</returns>
public static CakeTaskBuilder<T> ReportError<T>(this CakeTaskBuilder<T> builder, Action<Exception> errorReporter)
where T : CakeTask
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
builder.Task.SetErrorReporter(errorReporter);
return builder;
}
}
}