-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathTaskHostConfiguration.cs
479 lines (426 loc) · 17.5 KB
/
TaskHostConfiguration.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
#nullable disable
namespace Microsoft.Build.BackEnd
{
/// <summary>
/// TaskHostConfiguration contains information needed for the task host to
/// configure itself for to execute a particular task.
/// </summary>
internal class TaskHostConfiguration : INodePacket
{
/// <summary>
/// The node id (of the parent node, to make the logging work out)
/// </summary>
private int _nodeId;
/// <summary>
/// The startup directory
/// </summary>
private string _startupDirectory;
/// <summary>
/// The process environment.
/// </summary>
private Dictionary<string, string> _buildProcessEnvironment;
/// <summary>
/// The culture
/// </summary>
private CultureInfo _culture = CultureInfo.CurrentCulture;
/// <summary>
/// The UI culture.
/// </summary>
private CultureInfo _uiCulture = CultureInfo.CurrentUICulture;
#if FEATURE_APPDOMAIN
/// <summary>
/// The AppDomainSetup that we may want to use on AppDomainIsolated tasks.
/// </summary>
private AppDomainSetup _appDomainSetup;
#endif
/// <summary>
/// Line number where the instance of this task is defined.
/// </summary>
private int _lineNumberOfTask;
/// <summary>
/// Column number where the instance of this task is defined.
/// </summary>
private int _columnNumberOfTask;
/// <summary>
/// Project file where the instance of this task is defined.
/// </summary>
private string _projectFileOfTask;
/// <summary>
/// ContinueOnError flag for this particular task.
/// </summary>
private bool _continueOnError;
/// <summary>
/// Name of the task to be executed on the task host.
/// </summary>
private string _taskName;
/// <summary>
/// Location of the assembly containing the task to be executed.
/// </summary>
private string _taskLocation;
/// <summary>
/// Whether task inputs are logged.
/// </summary>
private bool _isTaskInputLoggingEnabled;
/// <summary>
/// The set of parameters to apply to the task prior to execution.
/// </summary>
private Dictionary<string, TaskParameter> _taskParameters;
private Dictionary<string, string> _globalParameters;
private ICollection<string> _warningsAsErrors;
private ICollection<string> _warningsNotAsErrors;
private ICollection<string> _warningsAsMessages;
#if FEATURE_APPDOMAIN
/// <summary>
/// Constructor
/// </summary>
/// <param name="nodeId">The ID of the node being configured.</param>
/// <param name="startupDirectory">The startup directory for the task being executed.</param>
/// <param name="buildProcessEnvironment">The set of environment variables to apply to the task execution process.</param>
/// <param name="culture">The culture of the thread that will execute the task.</param>
/// <param name="uiCulture">The UI culture of the thread that will execute the task.</param>
/// <param name="appDomainSetup">The AppDomainSetup that may be used to pass information to an AppDomainIsolated task.</param>
/// <param name="lineNumberOfTask">The line number of the location from which this task was invoked.</param>
/// <param name="columnNumberOfTask">The column number of the location from which this task was invoked.</param>
/// <param name="projectFileOfTask">The project file from which this task was invoked.</param>
/// <param name="continueOnError">Flag to continue with the build after a the task failed</param>
/// <param name="taskName">Name of the task.</param>
/// <param name="taskLocation">Location of the assembly the task is to be loaded from.</param>
/// <param name="isTaskInputLoggingEnabled">Whether task inputs are logged.</param>
/// <param name="taskParameters">Parameters to apply to the task.</param>
/// <param name="globalParameters">global properties for the current project.</param>
/// <param name="warningsAsErrors">Warning codes to be treated as errors for the current project.</param>
/// <param name="warningsNotAsErrors">Warning codes not to be treated as errors for the current project.</param>
/// <param name="warningsAsMessages">Warning codes to be treated as messages for the current project.</param>
#else
/// <summary>
/// Constructor
/// </summary>
/// <param name="nodeId">The ID of the node being configured.</param>
/// <param name="startupDirectory">The startup directory for the task being executed.</param>
/// <param name="buildProcessEnvironment">The set of environment variables to apply to the task execution process.</param>
/// <param name="culture">The culture of the thread that will execute the task.</param>
/// <param name="uiCulture">The UI culture of the thread that will execute the task.</param>
/// <param name="lineNumberOfTask">The line number of the location from which this task was invoked.</param>
/// <param name="columnNumberOfTask">The column number of the location from which this task was invoked.</param>
/// <param name="projectFileOfTask">The project file from which this task was invoked.</param>
/// <param name="continueOnError">Flag to continue with the build after a the task failed</param>
/// <param name="taskName">Name of the task.</param>
/// <param name="taskLocation">Location of the assembly the task is to be loaded from.</param>
/// <param name="isTaskInputLoggingEnabled">Whether task inputs are logged.</param>
/// <param name="taskParameters">Parameters to apply to the task.</param>
/// <param name="globalParameters">global properties for the current project.</param>
/// <param name="warningsAsErrors">Warning codes to be logged as errors for the current project.</param>
/// <param name="warningsNotAsErrors">Warning codes not to be treated as errors for the current project.</param>
/// <param name="warningsAsMessages">Warning codes to be treated as messages for the current project.</param>
#endif
public TaskHostConfiguration(
int nodeId,
string startupDirectory,
IDictionary<string, string> buildProcessEnvironment,
CultureInfo culture,
CultureInfo uiCulture,
#if FEATURE_APPDOMAIN
AppDomainSetup appDomainSetup,
#endif
int lineNumberOfTask,
int columnNumberOfTask,
string projectFileOfTask,
bool continueOnError,
string taskName,
string taskLocation,
bool isTaskInputLoggingEnabled,
IDictionary<string, object> taskParameters,
Dictionary<string, string> globalParameters,
ICollection<string> warningsAsErrors,
ICollection<string> warningsNotAsErrors,
ICollection<string> warningsAsMessages)
{
ErrorUtilities.VerifyThrowInternalLength(taskName, nameof(taskName));
ErrorUtilities.VerifyThrowInternalLength(taskLocation, nameof(taskLocation));
_nodeId = nodeId;
_startupDirectory = startupDirectory;
if (buildProcessEnvironment != null)
{
_buildProcessEnvironment = buildProcessEnvironment as Dictionary<string, string>;
if (_buildProcessEnvironment == null)
{
_buildProcessEnvironment = new Dictionary<string, string>(buildProcessEnvironment);
}
}
_culture = culture;
_uiCulture = uiCulture;
#if FEATURE_APPDOMAIN
_appDomainSetup = appDomainSetup;
#endif
_lineNumberOfTask = lineNumberOfTask;
_columnNumberOfTask = columnNumberOfTask;
_projectFileOfTask = projectFileOfTask;
_continueOnError = continueOnError;
_taskName = taskName;
_taskLocation = taskLocation;
_isTaskInputLoggingEnabled = isTaskInputLoggingEnabled;
_warningsAsErrors = warningsAsErrors;
_warningsNotAsErrors = warningsNotAsErrors;
_warningsAsMessages = warningsAsMessages;
if (taskParameters != null)
{
_taskParameters = new Dictionary<string, TaskParameter>(StringComparer.OrdinalIgnoreCase);
foreach (KeyValuePair<string, object> parameter in taskParameters)
{
_taskParameters[parameter.Key] = new TaskParameter(parameter.Value);
}
}
_globalParameters = globalParameters ?? new Dictionary<string, string>();
}
/// <summary>
/// Constructor for deserialization.
/// </summary>
private TaskHostConfiguration()
{
}
/// <summary>
/// The node id
/// </summary>
public int NodeId
{
[DebuggerStepThrough]
get
{ return _nodeId; }
}
/// <summary>
/// The startup directory
/// </summary>
public string StartupDirectory
{
[DebuggerStepThrough]
get
{ return _startupDirectory; }
}
/// <summary>
/// The process environment.
/// </summary>
public Dictionary<string, string> BuildProcessEnvironment
{
[DebuggerStepThrough]
get
{ return _buildProcessEnvironment; }
}
/// <summary>
/// The culture
/// </summary>
public CultureInfo Culture
{
[DebuggerStepThrough]
get
{ return _culture; }
}
/// <summary>
/// The UI culture.
/// </summary>
public CultureInfo UICulture
{
[DebuggerStepThrough]
get
{ return _uiCulture; }
}
#if FEATURE_APPDOMAIN
/// <summary>
/// The AppDomain configuration bytes that we may want to use to initialize
/// AppDomainIsolated tasks.
/// </summary>
public AppDomainSetup AppDomainSetup
{
[DebuggerStepThrough]
get
{ return _appDomainSetup; }
}
#endif
/// <summary>
/// Line number where the instance of this task is defined.
/// </summary>
public int LineNumberOfTask
{
[DebuggerStepThrough]
get
{ return _lineNumberOfTask; }
}
/// <summary>
/// Column number where the instance of this task is defined.
/// </summary>
public int ColumnNumberOfTask
{
[DebuggerStepThrough]
get
{ return _columnNumberOfTask; }
}
/// <summary>
/// ContinueOnError flag for this particular task
/// </summary>
public bool ContinueOnError
{
[DebuggerStepThrough]
get
{ return _continueOnError; }
}
/// <summary>
/// Project file where the instance of this task is defined.
/// </summary>
public string ProjectFileOfTask
{
[DebuggerStepThrough]
get
{ return _projectFileOfTask; }
}
/// <summary>
/// Name of the task to execute.
/// </summary>
public string TaskName
{
[DebuggerStepThrough]
get
{ return _taskName; }
}
/// <summary>
/// Path to the assembly to load the task from.
/// </summary>
public string TaskLocation
{
[DebuggerStepThrough]
get
{ return _taskLocation; }
}
/// <summary>
/// Returns <see langword="true"/> if the build is configured to log all task inputs.
/// </summary>
public bool IsTaskInputLoggingEnabled
{
[DebuggerStepThrough]
get
{ return _isTaskInputLoggingEnabled; }
}
/// <summary>
/// Parameters to set on the instantiated task prior to execution.
/// </summary>
public Dictionary<string, TaskParameter> TaskParameters
{
[DebuggerStepThrough]
get
{ return _taskParameters; }
}
/// <summary>
/// Gets the global properties for the current project.
/// </summary>
public Dictionary<string, string> GlobalProperties
{
[DebuggerStepThrough]
get
{ return _globalParameters; }
}
/// <summary>
/// The NodePacketType of this NodePacket
/// </summary>
public NodePacketType Type
{
[DebuggerStepThrough]
get
{ return NodePacketType.TaskHostConfiguration; }
}
public ICollection<string> WarningsAsErrors
{
[DebuggerStepThrough]
get
{
return _warningsAsErrors;
}
}
public ICollection<string> WarningsNotAsErrors
{
[DebuggerStepThrough]
get
{
return _warningsNotAsErrors;
}
}
public ICollection<string> WarningsAsMessages
{
[DebuggerStepThrough]
get
{
return _warningsAsMessages;
}
}
/// <summary>
/// Translates the packet to/from binary form.
/// </summary>
/// <param name="translator">The translator to use.</param>
public void Translate(ITranslator translator)
{
translator.Translate(ref _nodeId);
translator.Translate(ref _startupDirectory);
translator.TranslateDictionary(ref _buildProcessEnvironment, StringComparer.OrdinalIgnoreCase);
translator.TranslateCulture(ref _culture);
translator.TranslateCulture(ref _uiCulture);
#if FEATURE_APPDOMAIN
byte[] appDomainConfigBytes = null;
// Set the configuration bytes just before serialization in case the SetConfigurationBytes was invoked during lifetime of this instance.
if (translator.Mode == TranslationDirection.WriteToStream)
{
appDomainConfigBytes = _appDomainSetup?.GetConfigurationBytes();
}
translator.Translate(ref appDomainConfigBytes);
if (translator.Mode == TranslationDirection.ReadFromStream)
{
_appDomainSetup = new AppDomainSetup();
_appDomainSetup.SetConfigurationBytes(appDomainConfigBytes);
}
#endif
translator.Translate(ref _lineNumberOfTask);
translator.Translate(ref _columnNumberOfTask);
translator.Translate(ref _projectFileOfTask);
translator.Translate(ref _taskName);
translator.Translate(ref _taskLocation);
translator.Translate(ref _isTaskInputLoggingEnabled);
translator.TranslateDictionary(ref _taskParameters, StringComparer.OrdinalIgnoreCase, TaskParameter.FactoryForDeserialization);
translator.Translate(ref _continueOnError);
translator.TranslateDictionary(ref _globalParameters, StringComparer.OrdinalIgnoreCase);
translator.Translate(collection: ref _warningsAsErrors,
objectTranslator: (ITranslator t, ref string s) => t.Translate(ref s),
#if CLR2COMPATIBILITY
collectionFactory: count => new HashSet<string>(StringComparer.OrdinalIgnoreCase));
#else
collectionFactory: count => new HashSet<string>(count, StringComparer.OrdinalIgnoreCase));
#endif
translator.Translate(collection: ref _warningsNotAsErrors,
objectTranslator: (ITranslator t, ref string s) => t.Translate(ref s),
#if CLR2COMPATIBILITY
collectionFactory: count => new HashSet<string>(StringComparer.OrdinalIgnoreCase));
#else
collectionFactory: count => new HashSet<string>(count, StringComparer.OrdinalIgnoreCase));
#endif
translator.Translate(collection: ref _warningsAsMessages,
objectTranslator: (ITranslator t, ref string s) => t.Translate(ref s),
#if CLR2COMPATIBILITY
collectionFactory: count => new HashSet<string>(StringComparer.OrdinalIgnoreCase));
#else
collectionFactory: count => new HashSet<string>(count, StringComparer.OrdinalIgnoreCase));
#endif
}
/// <summary>
/// Factory for deserialization.
/// </summary>
internal static INodePacket FactoryForDeserialization(ITranslator translator)
{
TaskHostConfiguration configuration = new TaskHostConfiguration();
configuration.Translate(translator);
return configuration;
}
}
}