-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathCodeTaskFactory.cs
1060 lines (904 loc) · 42 KB
/
CodeTaskFactory.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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
#nullable disable
namespace Microsoft.Build.Tasks
{
#if FEATURE_CODETASKFACTORY
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Microsoft.Build.Shared.FileSystem;
/// <summary>
/// A task factory which can take code dom supported languages and create a task out of it
/// </summary>
public class CodeTaskFactory : ITaskFactory
{
/// <summary>
/// This dictionary keeps track of custom references to compiled assemblies. The in-memory assembly is loaded from a byte
/// stream and as such its dependencies cannot be found unless they are in the MSBuild.exe directory or the GAC. They
/// cannot be found even if they are already loaded in the AppDomain. This dictionary knows the FullName of the assembly
/// and a reference to the assembly itself. In the <see cref="CurrentDomainOnAssemblyResolve"/> handler, the dictionary
/// is used to return the loaded assemblies as a way to allow custom references that are not in the normal assembly Load
/// context.
/// </summary>
private static readonly IDictionary<string, Assembly> s_knownReferenceAssemblies = new ConcurrentDictionary<string, Assembly>(StringComparer.OrdinalIgnoreCase);
static CodeTaskFactory()
{
// Populate default-reference-assembly information
Assembly frameworkAssembly = Assembly.GetAssembly(typeof(ITask));
_msbuildFrameworkName = frameworkAssembly.FullName;
_msbuildFrameworkPath = frameworkAssembly.Location;
Assembly utilitiesAssembly = Assembly.GetAssembly(typeof(Task));
_msbuildUtilitiesName = utilitiesAssembly.FullName;
_msbuildUtilitiesPath = utilitiesAssembly.Location;
// The handler is not detached because it only returns assemblies for custom references that cannot be found in the normal Load context
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
}
/// <summary>
/// Handles the <see cref="AppDomain.AssemblyResolve"/> event to return assemblies loaded from custom references.
/// </summary>
private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
// Return the assembly loaded from the custom reference if the FullName matches what is being looked for
s_knownReferenceAssemblies.TryGetValue(args.Name, out Assembly assembly);
return assembly;
}
private static readonly string _msbuildFrameworkName;
private static readonly string _msbuildFrameworkPath;
private static readonly string _msbuildUtilitiesName;
private static readonly string _msbuildUtilitiesPath;
/// <summary>
/// Default assemblies names to reference during inline code compilation - from the .NET Framework
/// </summary>
private static readonly string[] s_defaultReferencedFrameworkAssemblyNames = { @"System.Core" };
/// <summary>
/// Default using's for the code
/// </summary>
private readonly string[] _defaultUsingNamespaces = { "System", "System.Collections", "System.Collections.Generic", "System.Text", "System.Linq", "System.IO", "Microsoft.Build.Framework", "Microsoft.Build.Utilities" };
/// <summary>
/// A collection of task assemblies which have been instantiated by any CodeTaskFactory. Used to prevent us from creating
/// duplicate assemblies.
/// </summary>
private static readonly ConcurrentDictionary<FullTaskSpecification, Assembly> s_compiledTaskCache = new ConcurrentDictionary<FullTaskSpecification, Assembly>();
/// <summary>
/// Merged set of assembly reference paths (default + specified)
/// </summary>
private List<string> _referencedAssemblies;
/// <summary>
/// Merged set of namespaces (default + specified)
/// </summary>
private List<string> _usingNamespaces;
/// <summary>
/// Type of code fragment, ie Fragment, Class, Method
/// </summary>
private string _type;
/// <summary>
/// Is the type a fragment or not
/// </summary>
private bool _typeIsFragment;
/// <summary>
/// Is the type a method or not
/// </summary>
private bool _typeIsMethod;
/// <summary>
/// By default the language supported is C#, but anything that supports code dom will work
/// </summary>
private string _language = "cs";
/// <summary>
/// The source that will be compiled
/// </summary>
private string _sourceCode;
/// <summary>
/// The name of the task for which this is the factory
/// </summary>
private string _nameOfTask;
/// <summary>
/// Path to source that is outside the project file
/// </summary>
private string _sourcePath;
/// <summary>
/// The using task node from the project file
/// </summary>
private XmlNode _taskNode;
/// <summary>
/// The inline source compiled into an in memory assembly
/// </summary>
private Assembly _compiledAssembly;
/// <summary>
/// Helper to assist in logging messages
/// </summary>
private TaskLoggingHelper _log;
/// <summary>
/// Task parameter type information
/// </summary>
private IDictionary<string, TaskPropertyInfo> _taskParameterTypeInfo;
/// <summary>
/// MSBuild engine uses this for logging where the task comes from
/// </summary>
public string FactoryName => "Code Task Factory";
/// <summary>
/// Gets the type of the generated task.
/// </summary>
public Type TaskType { get; private set; }
/// <summary>
/// Get the type information for all task parameters.
/// </summary>
public TaskPropertyInfo[] GetTaskParameters()
{
var properties = new TaskPropertyInfo[_taskParameterTypeInfo.Count];
_taskParameterTypeInfo.Values.CopyTo(properties, 0);
return properties;
}
/// <summary>
/// Initializes the task factory.
/// </summary>
public bool Initialize(string taskName, IDictionary<string, TaskPropertyInfo> taskParameters, string taskElementContents, IBuildEngine taskFactoryLoggingHost)
{
_nameOfTask = taskName;
_log = new TaskLoggingHelper(taskFactoryLoggingHost, taskName)
{
TaskResources = AssemblyResources.PrimaryResources,
HelpKeywordPrefix = "MSBuild."
};
XmlNode taskContent = ExtractTaskContent(taskElementContents);
if (taskContent == null)
{
// Just return false because we have already logged the error in ExtractTaskContents
return false;
}
bool validatedTaskNode = ValidateTaskNode();
if (!validatedTaskNode)
{
return false;
}
if (taskContent.Attributes["Type"] != null)
{
_type = taskContent.Attributes["Type"].Value;
if (_type.Length == 0)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmpty", "Type");
return false;
}
}
if (taskContent.Attributes["Language"] != null)
{
_language = taskContent.Attributes["Language"].Value;
if (_language.Length == 0)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmpty", "Language");
return false;
}
}
if (taskContent.Attributes["Source"] != null)
{
_sourcePath = taskContent.Attributes["Source"].Value;
if (_sourcePath.Length == 0)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmpty", "Source");
return false;
}
if (_type == null)
{
_type = "Class";
}
}
_referencedAssemblies = ExtractReferencedAssemblies();
if (_log.HasLoggedErrors)
{
return false;
}
_usingNamespaces = ExtractUsingNamespaces();
if (_log.HasLoggedErrors)
{
return false;
}
_sourceCode = taskContent.InnerText;
if (_type == null)
{
_type = "Fragment";
}
if (_language == null)
{
_language = "cs";
}
if (String.Equals(_type, "Fragment", StringComparison.OrdinalIgnoreCase))
{
_typeIsFragment = true;
_typeIsMethod = false;
}
else if (String.Equals(_type, "Method", StringComparison.OrdinalIgnoreCase))
{
_typeIsFragment = false;
_typeIsMethod = true;
}
_taskParameterTypeInfo = taskParameters;
_compiledAssembly = CompileInMemoryAssembly();
// If it wasn't compiled, it logged why.
// If it was, continue.
if (_compiledAssembly != null)
{
// Now go find the type int he compiled assembly.
Type[] exportedTypes = _compiledAssembly.GetExportedTypes();
Type fullNameMatch = null;
Type partialNameMatch = null;
foreach (Type exportedType in exportedTypes)
{
string exportedTypeName = exportedType.FullName;
if (exportedTypeName.Equals(_nameOfTask, StringComparison.OrdinalIgnoreCase))
{
fullNameMatch = exportedType;
break;
}
else if (partialNameMatch == null && exportedTypeName.EndsWith(_nameOfTask, StringComparison.OrdinalIgnoreCase))
{
partialNameMatch = exportedType;
}
}
TaskType = fullNameMatch ?? partialNameMatch;
if (TaskType == null)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.CouldNotFindTaskInAssembly", _nameOfTask);
}
}
return !_log.HasLoggedErrors;
}
/// <summary>
/// Create a taskfactory instance which contains the data that needs to be refreshed between task invocations.
/// </summary>
public ITask CreateTask(IBuildEngine loggingHost)
{
// The assembly will have been compiled during class factory initialization, create an instance of it
if (_compiledAssembly != null)
{
// In order to use the resource strings from the tasks assembly we need to register the resources with the task logging helper.
var log = new TaskLoggingHelper(loggingHost, _nameOfTask)
{
TaskResources = AssemblyResources.PrimaryResources,
HelpKeywordPrefix = "MSBuild."
};
ITask taskInstance = Activator.CreateInstance(TaskType) as ITask;
if (taskInstance == null)
{
log.LogErrorWithCodeFromResources("CodeTaskFactory.NeedsITaskInterface", _nameOfTask);
}
return taskInstance;
}
return null;
}
/// <summary>
/// Cleans up any context or state that may have been built up for a given task.
/// </summary>
/// <param name="task">The task to clean up.</param>
/// <remarks>
/// For many factories, this method is a no-op. But some factories may have built up
/// an AppDomain as part of an individual task instance, and this is their opportunity
/// to shutdown the AppDomain.
/// </remarks>
public void CleanupTask(ITask task)
{
ErrorUtilities.VerifyThrowArgumentNull(task);
}
/// <summary>
/// Create a property (with the corresponding private field) from the given type information
/// </summary>
private static void CreateProperty(CodeTypeDeclaration ctd, string propertyName, Type propertyType, object defaultValue)
{
var field = new CodeMemberField(new CodeTypeReference(propertyType), "_" + propertyName)
{
Attributes = MemberAttributes.Private
};
if (defaultValue != null)
{
field.InitExpression = new CodePrimitiveExpression(defaultValue);
}
ctd.Members.Add(field);
var prop = new CodeMemberProperty
{
Name = propertyName,
Type = new CodeTypeReference(propertyType),
Attributes = MemberAttributes.Public,
HasGet = true,
HasSet = true
};
var fieldRef = new CodeFieldReferenceExpression { FieldName = field.Name };
prop.GetStatements.Add(new CodeMethodReturnStatement(fieldRef));
var fieldAssign = new CodeAssignStatement
{
Left = fieldRef,
Right = new CodeArgumentReferenceExpression("value")
};
prop.SetStatements.Add(fieldAssign);
ctd.Members.Add(prop);
}
/// <summary>
/// Create the Execute() method for the task from the fragment of code from the <Task /> element
/// </summary>
private static void CreateExecuteMethodFromFragment(CodeTypeDeclaration codeTypeDeclaration, string executeCode)
{
var executeMethod = new CodeMemberMethod
{
Name = "Execute",
Attributes = MemberAttributes.Override | MemberAttributes.Public
};
executeMethod.Statements.Add(new CodeSnippetStatement(executeCode));
executeMethod.ReturnType = new CodeTypeReference(typeof(Boolean));
executeMethod.Statements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(null, "_Success")));
codeTypeDeclaration.Members.Add(executeMethod);
}
/// <summary>
/// Create the body of the task's code by simply using the taskCode as a snippet for the CodeDom
/// </summary>
private static void CreateTaskBody(CodeTypeDeclaration codeTypeDeclaration, string taskCode)
{
var snippet = new CodeSnippetTypeMember(taskCode);
codeTypeDeclaration.Members.Add(snippet);
}
/// <summary>
/// Create a property (with the corresponding private field) from the given type information
/// </summary>
private static void CreateProperty(CodeTypeDeclaration codeTypeDeclaration, TaskPropertyInfo propInfo, object defaultValue)
{
CreateProperty(codeTypeDeclaration, propInfo.Name, propInfo.PropertyType, defaultValue);
}
/// <summary>
/// Extract the <Reference /> elements from the <UsingTask />
/// </summary>
/// <returns>string[] of reference paths</returns>
private List<string> ExtractReferencedAssemblies()
{
XmlNodeList referenceNodes = _taskNode.SelectNodes("//*[local-name()='Reference']");
var references = new List<string>();
for (int i = 0; i < referenceNodes.Count; i++)
{
XmlAttribute attribute = referenceNodes[i].Attributes["Include"];
bool hasInvalidChildNodes = HasInvalidChildNodes(referenceNodes[i], [XmlNodeType.Comment, XmlNodeType.Whitespace]);
if (hasInvalidChildNodes)
{
return null;
}
if (string.IsNullOrWhiteSpace(attribute?.Value))
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmptyWithTaskElement", "Include", "Reference", _nameOfTask);
return null;
}
references.Add(FileUtilities.MaybeAdjustFilePath(attribute.Value));
}
return references;
}
/// <summary>
/// Extract the <Using /> elements from the <UsingTask />
/// </summary>
/// <returns>string[] of using's</returns>
private List<string> ExtractUsingNamespaces()
{
XmlNodeList usingNodes = _taskNode.SelectNodes("//*[local-name()='Using']");
var usings = new List<string>();
for (int i = 0; i < usingNodes.Count; i++)
{
bool hasInvalidChildNodes = HasInvalidChildNodes(usingNodes[i], [XmlNodeType.Comment, XmlNodeType.Whitespace]);
if (hasInvalidChildNodes)
{
return null;
}
XmlAttribute attribute = usingNodes[i].Attributes["Namespace"];
if (attribute == null || attribute.Value.Length == 0)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmpty", "Namespace");
return null;
}
usings.Add(attribute.Value);
}
return usings;
}
/// <summary>
/// Extract the <Task /> node from the UsingTask node
/// </summary>
/// <param name="taskElementContents">textual content of the <Task /> node</param>
/// <returns>XmlNode <Task /></returns>
private XmlNode ExtractTaskContent(string taskElementContents)
{
// We need to get the InnerXml of the <Task /> node back into
// a root node so that we can execute the appropriate XPath on it
var document = new XmlDocument();
_taskNode = document.CreateElement("Task");
document.AppendChild(_taskNode);
// record our internal representation of the <Task /> node
_taskNode.InnerXml = taskElementContents;
XmlNodeList codeNodes = _taskNode.SelectNodes("//*[local-name()='Code']");
if (codeNodes.Count > 1)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.MultipleCodeNodes");
return null;
}
else if (codeNodes.Count == 0)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.CodeElementIsMissing", _nameOfTask);
return null;
}
bool hasInvalidChildNodes = HasInvalidChildNodes(codeNodes[0], [XmlNodeType.Comment, XmlNodeType.Whitespace, XmlNodeType.Text, XmlNodeType.CDATA]);
if (hasInvalidChildNodes)
{
return null;
}
return codeNodes[0];
}
/// <summary>
/// Make sure the task node only contains Code, Reference, Usings
/// </summary>
private bool ValidateTaskNode()
{
if (_taskNode.HasChildNodes)
{
foreach (XmlNode childNode in _taskNode.ChildNodes)
{
bool foundInvalidNode;
switch (childNode.NodeType)
{
case XmlNodeType.Comment:
case XmlNodeType.Whitespace:
case XmlNodeType.Text:
// These are legal, and ignored
continue;
case XmlNodeType.Element:
if (childNode.Name.Equals("Code", StringComparison.OrdinalIgnoreCase) || childNode.Name.Equals("Reference", StringComparison.OrdinalIgnoreCase) || childNode.Name.Equals("Using", StringComparison.OrdinalIgnoreCase))
{
continue;
}
else
{
foundInvalidNode = true;
}
break;
default:
foundInvalidNode = true;
break;
}
if (foundInvalidNode)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.InvalidElementLocation", childNode.Name, _taskNode.Name);
return false;
}
}
}
return true;
}
/// <summary>
/// If a parent node has a child node and it is not supposed to, log an error indicating it has an invalid element.
/// </summary>
private bool HasInvalidChildNodes(XmlNode parentNode, XmlNodeType[] allowedNodeTypes)
{
bool hasInvalidNode = false;
if (parentNode.HasChildNodes)
{
foreach (XmlNode childNode in parentNode.ChildNodes)
{
bool elementAllowed = false;
foreach (XmlNodeType nodeType in allowedNodeTypes)
{
if (nodeType == childNode.NodeType)
{
elementAllowed = true;
break;
}
}
if (!elementAllowed)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.InvalidElementLocation", childNode.Name, parentNode.Name);
hasInvalidNode = true;
}
else
{
continue;
}
}
}
return hasInvalidNode;
}
/// <summary>
/// Add a reference assembly to the list of references passed to the compiler. We will try and load the assembly to make sure it is found
/// before sending it to the compiler. The reason we load here is that we will be using it in this appdomin anyways as soon as we are going to compile, which should be right away.
/// </summary>
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadWithPartialName", Justification = "Necessary since we don't have the full assembly name. ")]
private void AddReferenceAssemblyToReferenceList(List<string> referenceAssemblyList, string referenceAssembly)
{
if (referenceAssemblyList != null)
{
string candidateAssemblyLocation = null;
if (!String.IsNullOrEmpty(referenceAssembly))
{
try
{
if (!FileSystems.Default.FileExists(referenceAssembly))
{
if (!referenceAssembly.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || !referenceAssembly.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
candidateAssemblyLocation = GetPathFromPartialAssemblyName(referenceAssembly);
}
}
else
{
if (!TryCacheAssemblyIdentityFromPath(referenceAssembly, out candidateAssemblyLocation))
{
// Assembly should be skipped; return
return;
}
}
}
catch (Exception e) when (!ExceptionHandling.IsCriticalException(e))
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.ReferenceAssemblyIsInvalid", referenceAssembly, e.Message);
}
}
if (candidateAssemblyLocation != null)
{
referenceAssemblyList.Add(candidateAssemblyLocation);
}
else
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.CouldNotFindReferenceAssembly", referenceAssembly);
}
}
static string GetPathFromPartialAssemblyName(string partialName)
{
string candidateAssemblyLocation = null;
#pragma warning disable 618, 612
// Unfortunately Assembly.Load is not an alternative to LoadWithPartialName, since
// Assembly.Load requires the full assembly name to be passed to it.
// Therefore we must ignore the deprecated warning.
Assembly candidateAssembly = Assembly.LoadWithPartialName(partialName);
if (candidateAssembly != null)
{
candidateAssemblyLocation = candidateAssembly.Location;
}
#pragma warning restore 618, 612
return candidateAssemblyLocation;
}
bool TryCacheAssemblyIdentityFromPath(string assemblyFile, out string candidateAssemblyLocation)
{
candidateAssemblyLocation = null;
try
{
// Framework and Utilities are default references but are often
// specified in the UsingTask anyway; if so just ignore them.
//
// Do this with an explicit upfront check rather than loading the
// assembly and then checking its name, because that can cause
// the loader to have multiple copies of these assemblies as in
// https://github.com/dotnet/msbuild/issues/7108.
string name = AssemblyName.GetAssemblyName(assemblyFile).FullName;
if (name == _msbuildFrameworkName ||
name == _msbuildUtilitiesName)
{
return false;
}
Assembly candidateAssembly = Assembly.UnsafeLoadFrom(assemblyFile);
if (candidateAssembly != null)
{
candidateAssemblyLocation = candidateAssembly.Location;
s_knownReferenceAssemblies[candidateAssembly.FullName] = candidateAssembly;
}
}
catch (BadImageFormatException e)
{
Debug.Assert(e.Message.Contains("0x80131058"), "Expected Message to contain 0x80131058");
AssemblyName.GetAssemblyName(assemblyFile);
candidateAssemblyLocation = assemblyFile;
_log.LogMessageFromResources(MessageImportance.Low, "CodeTaskFactory.HaveReflectionOnlyAssembly", assemblyFile);
}
return true;
}
}
/// <summary>
/// Compile the assembly in memory and get a reference to the assembly itself.
/// If compilation fails, returns null.
/// </summary>
private Assembly CompileInMemoryAssembly()
{
// Combine our default assembly references with those specified
var finalReferencedAssemblies = CombineReferencedAssemblies();
// Combine our default using's with those specified
string[] finalUsingNamespaces = CombineUsingNamespaces();
// Language can be anything that has a codedom provider, in the standard naming method
// "c#;cs;csharp", "vb;vbs;visualbasic;vbscript", "js;jscript;javascript", "vj#;vjs;vjsharp", "c++;mc;cpp"
using (CodeDomProvider provider = CodeDomProvider.CreateProvider(_language))
{
if (provider is CSharp.CSharpCodeProvider)
{
AddReferenceAssemblyToReferenceList(finalReferencedAssemblies, "System");
}
var compilerParameters =
new CompilerParameters(finalReferencedAssemblies.ToArray())
{
// We don't need debug information
IncludeDebugInformation = true,
// Not a file based assembly
GenerateInMemory = true,
// Indicates that a .dll should be generated.
GenerateExecutable = false
};
// Horrible code dom / compilation declarations
var codeBuilder = new StringBuilder();
using var writer = new StringWriter(codeBuilder, CultureInfo.CurrentCulture);
var codeGeneratorOptions = new CodeGeneratorOptions
{
BlankLinesBetweenMembers = true,
VerbatimOrder = true
};
var compilationUnit = new CodeCompileUnit();
// If our code is in a separate file, then read it in here
if (_sourcePath != null)
{
_sourceCode = File.ReadAllText(_sourcePath);
}
// A fragment is essentially the contents of the execute method (except the final return true/false)
// A method is the whole execute method specified
// Anything else assumes that the whole class is being supplied
if (_typeIsFragment || _typeIsMethod)
{
CodeTypeDeclaration codeTypeDeclaration = CreateTaskClass();
CreateTaskProperties(codeTypeDeclaration);
if (_typeIsFragment)
{
CreateExecuteMethodFromFragment(codeTypeDeclaration, _sourceCode);
}
else
{
CreateTaskBody(codeTypeDeclaration, _sourceCode);
}
var codeNamespace = new CodeNamespace("InlineCode");
foreach (string importname in finalUsingNamespaces)
{
codeNamespace.Imports.Add(new CodeNamespaceImport(importname));
}
codeNamespace.Types.Add(codeTypeDeclaration);
compilationUnit.Namespaces.Add(codeNamespace);
// Create the source for the CodeDom
provider.GenerateCodeFromCompileUnit(compilationUnit, writer, codeGeneratorOptions);
}
else
{
// We are a full class, so just create the CodeDom from the source
provider.GenerateCodeFromStatement(new CodeSnippetStatement(_sourceCode), writer, codeGeneratorOptions);
}
// Our code generation is complete, grab the source from the builder ready for compilation
string fullCode = codeBuilder.ToString();
// Embed generated file in the binlog
string fileNameInBinlog = $"{Guid.NewGuid()}-{_nameOfTask}-compilation-file.tmp";
_log.LogIncludeGeneratedFile(fileNameInBinlog, fullCode);
var fullSpec = new FullTaskSpecification(finalReferencedAssemblies, fullCode);
if (!s_compiledTaskCache.TryGetValue(fullSpec, out Assembly existingAssembly))
{
// Invokes compilation.
CompilerResults compilerResults = provider.CompileAssemblyFromSource(compilerParameters, fullCode);
string outputPath = null;
if (compilerResults.Errors.Count > 0 || Environment.GetEnvironmentVariable("MSBUILDLOGCODETASKFACTORYOUTPUT") != null)
{
string tempDirectory = FileUtilities.TempFileDirectory;
string fileName = Guid.NewGuid().ToString() + ".txt";
outputPath = Path.Combine(tempDirectory, fileName);
File.WriteAllText(outputPath, fullCode);
}
if (compilerResults.NativeCompilerReturnValue != 0 && compilerResults.Errors.Count > 0)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.FindSourceFileAt", outputPath);
foreach (CompilerError e in compilerResults.Errors)
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.CompilerError", e.ToString());
}
return null;
}
// Add to the cache. Failing to add is not a fatal error.
s_compiledTaskCache.TryAdd(fullSpec, compilerResults.CompiledAssembly);
return compilerResults.CompiledAssembly;
}
else
{
return existingAssembly;
}
}
}
/// <summary>
/// Combine our default referenced assemblies with those explicitly specified
/// </summary>
private List<string> CombineReferencedAssemblies()
{
List<string> finalReferenceList = new List<string>(s_defaultReferencedFrameworkAssemblyNames.Length + 2 + _referencedAssemblies.Count);
// Set some default references:
// Loading with the partial name is fine for framework assemblies -- we'll always get the correct one
// through the magic of unification
foreach (string defaultReference in s_defaultReferencedFrameworkAssemblyNames)
{
AddReferenceAssemblyToReferenceList(finalReferenceList, defaultReference);
}
// We also want to add references to two MSBuild assemblies: Microsoft.Build.Framework.dll and
// Microsoft.Build.Utilities.Core.dll. If we just let the CLR unify the simple name, it will
// pick the highest version on the machine, which means that in hosts with restrictive binding
// redirects, or no binding redirects, we'd end up creating an inline task that could not be
// run. Instead, to make sure that we can actually use what we're building, just use the Framework
// and Utilities currently loaded into this process -- Since we're in Microsoft.Build.Tasks.Core.dll
// right now, by definition both of them are always already loaded.
//
// NOTE Dec 2020: I don't think the above really applies given the eternally-15.1.0.0 version policy
// we are currently using. But loading these from an explicit path seems fine so I'm not changing
// that.
finalReferenceList.Add(_msbuildFrameworkPath);
finalReferenceList.Add(_msbuildUtilitiesPath);
// Now for the explicitly-specified references:
if (_referencedAssemblies != null)
{
foreach (string referenceAssembly in _referencedAssemblies)
{
AddReferenceAssemblyToReferenceList(finalReferenceList, referenceAssembly);
}
}
return finalReferenceList;
}
/// <summary>
/// Combine our default imported namespaces with those explicitly specified
/// </summary>
private string[] CombineUsingNamespaces()
{
int usingNamespaceCount = _defaultUsingNamespaces.Length;
if (_usingNamespaces != null)
{
usingNamespaceCount += _usingNamespaces.Count;
}
var finalUsingNamespaces = new string[usingNamespaceCount];
_defaultUsingNamespaces.CopyTo(finalUsingNamespaces, 0);
_usingNamespaces?.CopyTo(finalUsingNamespaces, _defaultUsingNamespaces.Length);
return finalUsingNamespaces;
}
/// <summary>
/// Create the task properties
/// </summary>
private void CreateTaskProperties(CodeTypeDeclaration codeTypeDeclaration)
{
// If we are only a fragment, then create a default task parameter called
// Success - that we can use in the fragment to indicate success or failure of the task
if (_typeIsFragment)
{
CreateProperty(codeTypeDeclaration, "Success", typeof(bool), true);
}
foreach (TaskPropertyInfo propInfo in _taskParameterTypeInfo.Values)
{
CreateProperty(codeTypeDeclaration, propInfo, null);
}
}
/// <summary>
/// Create the task class
/// </summary>
private CodeTypeDeclaration CreateTaskClass()
{
CodeTypeDeclaration codeTypeDeclaration = new CodeTypeDeclaration
{
IsClass = true,
Name = _nameOfTask,
TypeAttributes = TypeAttributes.Public,
Attributes = MemberAttributes.Final
};
codeTypeDeclaration.BaseTypes.Add("Microsoft.Build.Utilities.Task");
return codeTypeDeclaration;
}
/// <summary>
/// Class used as a key for the compiled assembly cache
/// </summary>
private class FullTaskSpecification : IComparable<FullTaskSpecification>, IEquatable<FullTaskSpecification>
{
/// <summary>
/// The set of assemblies referenced by this task.
/// </summary>
private readonly List<string> _referenceAssemblies;
/// <summary>
/// The complete source code for the task.
/// </summary>
private readonly string _fullCode;
/// <summary>
/// Constructor
/// </summary>
public FullTaskSpecification(List<string> references, string fullCode)
{
_referenceAssemblies = references;
_fullCode = fullCode;
}
/// <summary>
/// Override of GetHashCode
/// </summary>
public override int GetHashCode()
{
return _fullCode.GetHashCode();
}
/// <summary>
/// Override of Equals
/// </summary>
public override bool Equals(object other)
{
if (ReferenceEquals(this, other))
{
return true;
}
if (!(other is FullTaskSpecification otherSpec))
{
return false;
}
return ((IEquatable<FullTaskSpecification>)this).Equals(otherSpec);
}
/// <summary>
/// Implementation of Equals.
/// </summary>
bool IEquatable<FullTaskSpecification>.Equals(FullTaskSpecification other)
{
if (_referenceAssemblies.Count != other._referenceAssemblies.Count)
{
return false;
}
for (int i = 0; i < _referenceAssemblies.Count; i++)
{
if (_referenceAssemblies[i] != other._referenceAssemblies[i])
{
return false;
}
}
return other._fullCode == _fullCode;