-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBuildEventContext.cs
316 lines (274 loc) · 10.4 KB
/
BuildEventContext.cs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Microsoft.Build.Framework
{
/// <summary>
/// Will provide location information for an event, this is especially
/// needed in a multi processor environment
/// </summary>
[Serializable]
public class BuildEventContext
{
#region Data
/// <summary>
/// Node event was in
/// </summary>
private readonly int _nodeId;
/// <summary>
/// Target event was in
/// </summary>
private readonly int _targetId;
/// <summary>
/// The node-unique project request context the event was in
/// </summary>
private readonly int _projectContextId;
/// <summary>
/// Id of the task the event was caused from
/// </summary>
private readonly int _taskId;
/// <summary>
/// The id of the project instance to which this event refers.
/// </summary>
private readonly int _projectInstanceId;
/// <summary>
/// The id of the submission.
/// </summary>
private readonly int _submissionId;
/// <summary>
/// The id of the evaluation
/// </summary>
private readonly int _evaluationId;
#endregion
#region Constructor
/// <summary>
/// This is the original constructor. No one should ever use this except internally for backward compatibility.
/// </summary>
public BuildEventContext(
int nodeId,
int targetId,
int projectContextId,
int taskId)
: this(InvalidSubmissionId, nodeId, InvalidEvaluationId, InvalidProjectInstanceId, projectContextId, targetId, taskId)
{
// UNDONE: This is obsolete.
}
/// <summary>
/// Constructs a BuildEventContext with a specified project instance id.
/// </summary>
public BuildEventContext(
int nodeId,
int projectInstanceId,
int projectContextId,
int targetId,
int taskId)
: this(InvalidSubmissionId, nodeId, InvalidEvaluationId, projectInstanceId, projectContextId, targetId, taskId)
{
}
/// <summary>
/// Constructs a BuildEventContext with a specific submission id
/// </summary>
public BuildEventContext(
int submissionId,
int nodeId,
int projectInstanceId,
int projectContextId,
int targetId,
int taskId)
: this(submissionId, nodeId, InvalidEvaluationId, projectInstanceId, projectContextId, targetId, taskId)
{
}
/// <summary>
/// Constructs a BuildEventContext
/// </summary>
public BuildEventContext(
int submissionId,
int nodeId,
int evaluationId,
int projectInstanceId,
int projectContextId,
int targetId,
int taskId)
{
_submissionId = submissionId;
_nodeId = nodeId;
_evaluationId = evaluationId;
_targetId = targetId;
_projectContextId = projectContextId;
_taskId = taskId;
_projectInstanceId = projectInstanceId;
}
#endregion
internal BuildEventContext WithInstanceIdAndContextId(int projectInstanceId, int projectContextId)
{
return new BuildEventContext(_submissionId, _nodeId, _evaluationId, projectInstanceId, projectContextId,
_targetId, _taskId);
}
internal BuildEventContext WithInstanceIdAndContextId(BuildEventContext other)
{
return WithInstanceIdAndContextId(other.ProjectInstanceId, other.ProjectContextId);
}
#region Properties
/// <summary>
/// Returns a default invalid BuildEventContext
/// </summary>
public static BuildEventContext Invalid { get; } = new BuildEventContext(InvalidNodeId, InvalidTargetId, InvalidProjectContextId, InvalidTaskId);
/// <summary>
/// Retrieves the Evaluation id.
/// </summary>
public int EvaluationId => _evaluationId;
/// <summary>
/// NodeId where event took place
/// </summary>
public int NodeId => _nodeId;
/// <summary>
/// Id of the target the event was in when the event was fired
/// </summary>
public int TargetId => _targetId;
/// <summary>
/// Retrieves the Project Context id.
/// </summary>
public int ProjectContextId => _projectContextId;
/// <summary>
/// Retrieves the task id.
/// </summary>
public int TaskId => _taskId;
/// <summary>
/// Retrieves the project instance id.
/// </summary>
public int ProjectInstanceId => _projectInstanceId;
/// <summary>
/// Retrieves the Submission id.
/// </summary>
public int SubmissionId => _submissionId;
/// <summary>
/// Retrieves the BuildRequest id. Note that this is not the same as the global request id on a BuildRequest or BuildResult.
/// </summary>
public long BuildRequestId => GetHashCode();
#endregion
#region Constants
/// <summary>
/// Indicates an invalid project context identifier.
/// </summary>
public const int InvalidProjectContextId = -2;
/// <summary>
/// Indicates an invalid task identifier.
/// </summary>
public const int InvalidTaskId = -1;
/// <summary>
/// Indicates an invalid target identifier.
/// </summary>
public const int InvalidTargetId = -1;
/// <summary>
/// Indicates an invalid node identifier.
/// </summary>
public const int InvalidNodeId = -2;
/// <summary>
/// Indicates an invalid project instance identifier.
/// </summary>
public const int InvalidProjectInstanceId = -1;
/// <summary>
/// Indicates an invalid submission identifier.
/// </summary>
public const int InvalidSubmissionId = -1;
/// <summary>
/// Indicates an invalid evaluation identifier.
/// </summary>
public const int InvalidEvaluationId = -1;
#endregion
#region Equals
/// <summary>
/// Retrieves a hash code for this BuildEventContext.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
var hash = 17;
// submission ID does not contribute to equality
// hash = hash * 31 + _submissionId;
hash = (hash * 31) + _nodeId;
hash = (hash * 31) + _evaluationId;
hash = (hash * 31) + _targetId;
hash = (hash * 31) + _projectContextId;
hash = (hash * 31) + _taskId;
hash = (hash * 31) + _projectInstanceId;
return hash;
}
/// <summary>
/// Compare a BuildEventContext with this BuildEventContext.
/// A build event context is compared in the following way.
///
/// 1. If the object references are the same the contexts are equivalent
/// 2. If the object type is the same and the Id values in the context are the same, the contexts are equivalent
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object? obj)
{
// If the references are the same no need to do any more comparing
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj is null)
{
return false;
}
// The types do not match, they cannot be the same
if (GetType() != obj.GetType())
{
return false;
}
return InternalEquals((BuildEventContext)obj);
}
/// <summary>
/// Override == so the equals comparison using this operator will be the same as
/// .Equals
/// </summary>
/// <param name="left">Left hand side operand</param>
/// <param name="right">Right hand side operand</param>
/// <returns>True if the object values are identical, false if they are not identical</returns>
public static bool operator ==(BuildEventContext? left, BuildEventContext? right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (left is null)
{
return false;
}
return left.Equals(right);
}
/// <summary>
/// Override != so the equals comparison using this operator will be the same as
/// ! Equals
/// </summary>
/// <param name="left">Left hand side operand</param>
/// <param name="right">Right hand side operand</param>
/// <returns>True if the object values are not identical, false if they are identical</returns>
public static bool operator !=(BuildEventContext? left, BuildEventContext? right)
{
return !(left == right);
}
/// <summary>
/// Verify the fields are identical
/// </summary>
/// <param name="buildEventContext">BuildEventContext to compare to this instance</param>
/// <returns>True if the value fields are the same, false if otherwise</returns>
private bool InternalEquals(BuildEventContext buildEventContext)
{
return _nodeId == buildEventContext.NodeId
&& _projectContextId == buildEventContext.ProjectContextId
&& _targetId == buildEventContext.TargetId
&& _taskId == buildEventContext.TaskId
&& _evaluationId == buildEventContext._evaluationId
&& _projectInstanceId == buildEventContext._projectInstanceId;
}
#endregion
public override string ToString()
{
return $"Node={NodeId} Submission={SubmissionId} ProjectContext={ProjectContextId} ProjectInstance={ProjectInstanceId} Eval={EvaluationId} Target={TargetId} Task={TaskId}";
}
}
}