/
ToolsetElement.cs
707 lines (624 loc) · 25.5 KB
/
ToolsetElement.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Microsoft.Build.Collections;
using Microsoft.Build.Shared;
namespace Microsoft.Build.Evaluation
{
/// <summary>
/// Helper class for reading toolsets out of the configuration file.
/// </summary>
internal static class ToolsetConfigurationReaderHelpers
{
internal static ToolsetConfigurationSection ReadToolsetConfigurationSection(Configuration configuration)
{
ToolsetConfigurationSection configurationSection = null;
// This will be null if the application config file does not have the following section
// definition for the msbuildToolsets section as the first child element.
// <configSections>
// <section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
// </configSections>";
// Note that the application config file may or may not contain an msbuildToolsets element.
// For example:
// If section definition is present and section is not present, this value is not null
// If section definition is not present and section is also not present, this value is null
// If the section definition is not present and section is present, then this value is null
if (configuration != null)
{
ConfigurationSection msbuildSection = configuration.GetSection("msbuildToolsets");
configurationSection = msbuildSection as ToolsetConfigurationSection;
if (configurationSection == null && msbuildSection != null) // we found msbuildToolsets but the wrong type of handler
{
if (String.IsNullOrEmpty(msbuildSection.SectionInformation.Type) ||
msbuildSection.SectionInformation.Type.IndexOf("Microsoft.Build", StringComparison.OrdinalIgnoreCase) >= 0)
{
// Set the configuration type handler to the current ToolsetConfigurationSection type
msbuildSection.SectionInformation.Type = typeof(ToolsetConfigurationSection).AssemblyQualifiedName;
try
{
// fabricate a temporary config file with the correct section handler type in it
string tempFileName = FileUtilities.GetTemporaryFile();
// Save the modified config
configuration.SaveAs(tempFileName + ".config");
// Open the configuration again, the new type for the section handler will do its stuff
// Note that the OpenExeConfiguration call uses the config filename *without* the .config
// extension
configuration = ConfigurationManager.OpenExeConfiguration(tempFileName);
// Get the toolset information from the section using our real handler
configurationSection = configuration.GetSection("msbuildToolsets") as ToolsetConfigurationSection;
File.Delete(tempFileName + ".config");
File.Delete(tempFileName);
}
catch (Exception ex) when (ExceptionHandling.IsIoRelatedException(ex))
{
}
}
}
}
return configurationSection;
}
}
/// <summary>
/// Class representing the Toolset element
/// </summary>
/// <remarks>
/// Internal for unit testing only
/// </remarks>
internal sealed class ToolsetElement : ConfigurationElement
{
/// <summary>
/// ToolsVersion attribute of the element
/// </summary>
[ConfigurationProperty("toolsVersion", IsKey = true, IsRequired = true)]
public string toolsVersion
{
get
{
return (string)base["toolsVersion"];
}
set
{
base[nameof(toolsVersion)] = value;
}
}
/// <summary>
/// Property element collection
/// </summary>
[ConfigurationProperty("", IsDefaultCollection = true)]
public PropertyElementCollection PropertyElements
{
get
{
return (PropertyElementCollection)base[""];
}
}
/// <summary>
/// Collection of all the search paths for project imports, per OS
/// </summary>
[ConfigurationProperty("projectImportSearchPaths")]
public ExtensionsPathsElementCollection AllProjectImportSearchPaths
{
get
{
return (ExtensionsPathsElementCollection)base["projectImportSearchPaths"];
}
}
/// <summary>
/// Class representing all the per-OS search paths for MSBuildExtensionsPath*
/// </summary>
internal sealed class ExtensionsPathsElementCollection : ConfigurationElementCollection
{
/// <summary>
/// We use this dictionary to track whether or not we've seen a given
/// searchPaths definition before, since the .NET configuration classes
/// won't perform this check without respect for case.
/// </summary>
private Dictionary<string, string> _previouslySeenOS = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Type of the collection
/// This has to be public as cannot change access modifier when overriding
/// </summary>
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
/// <summary>
/// Throw exception if an element with a duplicate key is added to the collection
/// </summary>
protected override bool ThrowOnDuplicate
{
get
{
return false;
}
}
/// <summary>
/// Name of the element
/// </summary>
protected override string ElementName
{
get
{
return "searchPaths";
}
}
/// <summary>
/// Gets an element with the specified name
/// </summary>
/// <param name="os">OS of the element</param>
/// <returns>element</returns>
public ExtensionsPathElement GetElement(string os)
{
return (ExtensionsPathElement)this.BaseGet(os);
}
/// <summary>
/// Gets an element based at the specified position
/// </summary>
/// <param name="index">position</param>
/// <returns>element</returns>
public ExtensionsPathElement GetElement(int index)
{
return (ExtensionsPathElement)this.BaseGet(index);
}
/// <summary>
/// Returns the key value for the given element
/// </summary>
/// <param name="element">element whose key is returned</param>
/// <returns>key</returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((ExtensionsPathElement)element).OS;
}
/// <summary>
/// Creates a new element of the collection
/// </summary>
/// <returns>Created element</returns>
protected override ConfigurationElement CreateNewElement()
{
return new ExtensionsPathElement();
}
/// <summary>
/// overridden so we can track previously seen elements
/// </summary>
protected override void BaseAdd(int index, ConfigurationElement element)
{
UpdateOSMap(element);
base.BaseAdd(index, element);
}
/// <summary>
/// overridden so we can track previously seen elements
/// </summary>
protected override void BaseAdd(ConfigurationElement element)
{
UpdateOSMap(element);
base.BaseAdd(element);
}
/// <summary>
/// Stores the name of the OS in a case-insensitive map
/// so we can detect if it is specified more than once but with
/// different case
/// </summary>
private void UpdateOSMap(ConfigurationElement element)
{
string os = GetElementKey(element).ToString();
if (_previouslySeenOS.ContainsKey(os))
{
string locationString = String.Empty;
if (!String.IsNullOrEmpty(element.ElementInformation.Source))
{
if (element.ElementInformation.LineNumber != 0)
{
locationString = String.Format("{0} ({1})", element.ElementInformation.Source, element.ElementInformation.LineNumber);
}
else
{
locationString = element.ElementInformation.Source;
}
}
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword("MultipleDefinitionsForSameExtensionsPathOS", os, locationString);
throw new ConfigurationErrorsException(message, element.ElementInformation.Source, element.ElementInformation.LineNumber);
}
_previouslySeenOS.Add(os, string.Empty);
}
}
/// <summary>
/// Class representing searchPaths element for a single OS
/// </summary>
internal sealed class ExtensionsPathElement : ConfigurationElement
{
/// <summary>
/// OS attribute of the element
/// </summary>
[ConfigurationProperty("os", IsKey = true, IsRequired = true)]
public string OS
{
get
{
return (string)base["os"];
}
set
{
base["os"] = value;
}
}
/// <summary>
/// Property element collection
/// </summary>
[ConfigurationProperty("", IsDefaultCollection = true)]
public PropertyElementCollection PropertyElements
{
get
{
return (PropertyElementCollection)base[""];
}
}
}
/// <summary>
/// Class representing collection of property elements
/// </summary>
internal sealed class PropertyElementCollection : ConfigurationElementCollection
{
#region Private Fields
/// <summary>
/// We use this dictionary to track whether or not we've seen a given
/// property definition before, since the .NET configuration classes
/// won't perform this check without respect for case.
/// </summary>
private Dictionary<string, string> _previouslySeenPropertyNames = new Dictionary<string, string>(MSBuildNameIgnoreCaseComparer.Default);
#endregion
#region Properties
/// <summary>
/// Collection type
/// This has to be public as cannot change access modifier when overriding
/// </summary>
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
/// <summary>
/// Throw exception if an element with a duplicate is added
/// </summary>
protected override bool ThrowOnDuplicate
{
get
{
return false;
}
}
/// <summary>
/// name of the element
/// </summary>
protected override string ElementName
{
get
{
return "property";
}
}
#endregion
#region Methods
/// <summary>
/// Gets an element with the specified name
/// </summary>
/// <param name="name">name of the element</param>
/// <returns>element</returns>
public PropertyElement GetElement(string name)
{
return (PropertyElement)this.BaseGet(name);
}
/// <summary>
/// Gets an element at the specified position
/// </summary>
/// <param name="index">position</param>
/// <returns>element</returns>
public PropertyElement GetElement(int index)
{
return (PropertyElement)this.BaseGet(index);
}
/// <summary>
/// Creates a new element
/// </summary>
/// <returns>element</returns>
protected override ConfigurationElement CreateNewElement()
{
return new PropertyElement();
}
/// <summary>
/// overridden so we can track previously seen property names
/// </summary>
protected override void BaseAdd(int index, ConfigurationElement element)
{
UpdatePropertyNameMap(element);
base.BaseAdd(index, element);
}
/// <summary>
/// overridden so we can track previously seen property names
/// </summary>
protected override void BaseAdd(ConfigurationElement element)
{
UpdatePropertyNameMap(element);
base.BaseAdd(element);
}
/// <summary>
/// Gets the key for the element
/// </summary>
/// <param name="element">element</param>
/// <returns>key</returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((PropertyElement)element).Name;
}
/// <summary>
/// Stores the name of the tools version in a case-insensitive map
/// so we can detect if it is specified more than once but with
/// different case
/// </summary>
private void UpdatePropertyNameMap(ConfigurationElement element)
{
string propertyName = GetElementKey(element).ToString();
if (_previouslySeenPropertyNames.ContainsKey(propertyName))
{
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword("MultipleDefinitionsForSameProperty", propertyName);
throw new ConfigurationErrorsException(message, element.ElementInformation.Source, element.ElementInformation.LineNumber);
}
_previouslySeenPropertyNames.Add(propertyName, string.Empty);
}
#endregion
}
/// <summary>
/// This class represents property element
/// </summary>
internal sealed class PropertyElement : ConfigurationElement
{
/// <summary>
/// name attribute
/// </summary>
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return (string)base["name"];
}
set
{
base["name"] = value;
}
}
/// <summary>
/// value attribute
/// </summary>
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get
{
return (string)base["value"];
}
set
{
base["value"] = value;
}
}
}
}
/// <summary>
/// Class representing the collection of toolset elements
/// </summary>
/// <remarks>
/// Internal for unit testing only
/// </remarks>
internal sealed class ToolsetElementCollection : ConfigurationElementCollection
{
/// <summary>
/// We use this dictionary to track whether or not we've seen a given
/// toolset definition before, since the .NET configuration classes
/// won't perform this check without respect for case.
/// </summary>
private Dictionary<string, string> _previouslySeenToolsVersions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Type of the collection
/// This has to be public as cannot change access modifier when overriding
/// </summary>
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
/// <summary>
/// Throw exception if an element with a duplicate key is added to the collection
/// </summary>
protected override bool ThrowOnDuplicate
{
get
{
return false;
}
}
/// <summary>
/// Name of the element
/// </summary>
protected override string ElementName
{
get
{
return "toolset";
}
}
/// <summary>
/// Gets an element with the specified name
/// </summary>
/// <param name="toolsVersion">toolsVersion of the element</param>
/// <returns>element</returns>
public ToolsetElement GetElement(string toolsVersion)
{
return (ToolsetElement)this.BaseGet(toolsVersion);
}
/// <summary>
/// Gets an element based at the specified position
/// </summary>
/// <param name="index">position</param>
/// <returns>element</returns>
public ToolsetElement GetElement(int index)
{
return (ToolsetElement)this.BaseGet(index);
}
/// <summary>
/// Returns the key value for the given element
/// </summary>
/// <param name="element">element whose key is returned</param>
/// <returns>key</returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((ToolsetElement)element).toolsVersion;
}
/// <summary>
/// Creates a new element of the collection
/// </summary>
/// <returns>Created element</returns>
protected override ConfigurationElement CreateNewElement()
{
return new ToolsetElement();
}
/// <summary>
/// overridden so we can track previously seen tools versions
/// </summary>
protected override void BaseAdd(int index, ConfigurationElement element)
{
UpdateToolsVersionMap(element);
base.BaseAdd(index, element);
}
/// <summary>
/// overridden so we can track previously seen tools versions
/// </summary>
protected override void BaseAdd(ConfigurationElement element)
{
UpdateToolsVersionMap(element);
base.BaseAdd(element);
}
/// <summary>
/// Stores the name of the tools version in a case-insensitive map
/// so we can detect if it is specified more than once but with
/// different case
/// </summary>
private void UpdateToolsVersionMap(ConfigurationElement element)
{
string toolsVersion = GetElementKey(element).ToString();
if (_previouslySeenToolsVersions.ContainsKey(toolsVersion))
{
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword("MultipleDefinitionsForSameToolset", toolsVersion);
throw new ConfigurationErrorsException(message, element.ElementInformation.Source, element.ElementInformation.LineNumber);
}
_previouslySeenToolsVersions.Add(toolsVersion, string.Empty);
}
}
/// <summary>
/// This class is used to programmatically read msbuildToolsets section
/// in from the configuration file. An example of application config file:
///
/// <configuration>
/// <msbuildToolsets default="2.0">
/// <toolset toolsVersion="2.0">
/// <property name="MSBuildBinPath" value="D:\windows\Microsoft.NET\Framework\v2.0.x86ret\"/>
/// <property name="SomeOtherProperty" value="SomeOtherPropertyValue"/>
/// </toolset>
/// <toolset toolsVersion="3.5">
/// <property name="MSBuildBinPath" value="D:\windows\Microsoft.NET\Framework\v3.5.x86ret\"/>
/// </toolset>
/// </msbuildToolsets>
/// </configuration>
///
/// </summary>
/// <remarks>
/// Internal for unit testing only
/// </remarks>
internal sealed class ToolsetConfigurationSection : ConfigurationSection
{
/// <summary>
/// toolsVersion element collection
/// </summary>
[ConfigurationProperty("", IsDefaultCollection = true)]
public ToolsetElementCollection Toolsets
{
get
{
return (ToolsetElementCollection)base[""];
}
}
/// <summary>
/// default attribute on msbuildToolsets element, specifying the default ToolsVersion
/// </summary>
[ConfigurationProperty("default")]
public string Default
{
get
{
// The ConfigurationPropertyAttribute constructor accepts a named parameter "DefaultValue"
// that doesn't seem to work if null is the desired default value. So here we return null
// whenever the base class gives us an empty string.
// Note this means we can't distinguish between the attribute being present but containing
// an empty string for its value and the attribute not being present at all.
string defaultValue = (string)base["default"];
return String.IsNullOrEmpty(defaultValue) ? null : defaultValue;
}
set
{
base["default"] = value;
}
}
/// <summary>
/// MsBuildOverrideTasksPath attribute on msbuildToolsets element, specifying the path to find msbuildOverrideTasks files
/// </summary>
[ConfigurationProperty("msbuildOverrideTasksPath")] // This string is case sensitive, can't change it
public string MSBuildOverrideTasksPath
{
get
{
// The ConfigurationPropertyAttribute constructor accepts a named parameter "DefaultValue"
// that doesn't seem to work if null is the desired default value. So here we return null
// whenever the base class gives us an empty string.
// Note this means we can't distinguish between the attribute being present but containing
// an empty string for its value and the attribute not being present at all.
string defaultValue = (string)base["msbuildOverrideTasksPath"];
return String.IsNullOrEmpty(defaultValue) ? null : defaultValue;
}
set
{
base["msbuildOverrideTasksPath"] = value;
}
}
/// <summary>
/// DefaultOverrideToolsVersion attribute on msbuildToolsets element, specifying the toolsversion that should be used by
/// default to build projects with this version of MSBuild.
/// </summary>
[ConfigurationProperty("DefaultOverrideToolsVersion")]
public string DefaultOverrideToolsVersion
{
get
{
// The ConfigurationPropertyAttribute constructor accepts a named parameter "DefaultValue"
// that doesn't seem to work if null is the desired default value. So here we return null
// whenever the base class gives us an empty string.
// Note this means we can't distinguish between the attribute being present but containing
// an empty string for its value and the attribute not being present at all.
string defaultValue = (string)base["DefaultOverrideToolsVersion"];
return String.IsNullOrEmpty(defaultValue) ? null : defaultValue;
}
set
{
base[nameof(DefaultOverrideToolsVersion)] = value;
}
}
}
}