|
| 1 | +namespace MSBuildExtensionTask |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.IO; |
| 5 | + using System.Linq; |
| 6 | + using System.Reflection; |
| 7 | +#if NETCOREAPP1_0 |
| 8 | + using System.Runtime.Loader; |
| 9 | +#endif |
| 10 | + using Microsoft.Build.Framework; |
| 11 | + using Microsoft.Build.Utilities; |
| 12 | + |
| 13 | + public abstract class ContextAwareTask : Task |
| 14 | + { |
| 15 | + protected virtual string ManagedDllDirectory => Path.GetDirectoryName(new Uri(this.GetType().GetTypeInfo().Assembly.CodeBase).LocalPath); |
| 16 | + |
| 17 | + protected virtual string UnmanagedDllDirectory => null; |
| 18 | + |
| 19 | + public override bool Execute() |
| 20 | + { |
| 21 | +#if NETCOREAPP1_0 |
| 22 | + string taskAssemblyPath = new Uri(this.GetType().GetTypeInfo().Assembly.CodeBase).LocalPath; |
| 23 | + var ctxt = new CustomAssemblyLoader(this); |
| 24 | + Assembly inContextAssembly = ctxt.LoadFromAssemblyPath(taskAssemblyPath); |
| 25 | + Type innerTaskType = inContextAssembly.GetType(this.GetType().FullName); |
| 26 | + object innerTask = Activator.CreateInstance(innerTaskType); |
| 27 | + |
| 28 | + var outerProperties = this.GetType().GetRuntimeProperties().ToDictionary(i => i.Name); |
| 29 | + var innerProperties = innerTaskType.GetRuntimeProperties().ToDictionary(i => i.Name); |
| 30 | + var propertiesDiscovery = from outerProperty in outerProperties.Values |
| 31 | + where outerProperty.SetMethod != null && outerProperty.GetMethod != null |
| 32 | + let innerProperty = innerProperties[outerProperty.Name] |
| 33 | + select new { outerProperty, innerProperty }; |
| 34 | + var propertiesMap = propertiesDiscovery.ToArray(); |
| 35 | + var outputPropertiesMap = propertiesMap.Where(pair => pair.outerProperty.GetCustomAttribute<OutputAttribute>() != null).ToArray(); |
| 36 | + |
| 37 | + foreach (var propertyPair in propertiesMap) |
| 38 | + { |
| 39 | + object outerPropertyValue = propertyPair.outerProperty.GetValue(this); |
| 40 | + propertyPair.innerProperty.SetValue(innerTask, outerPropertyValue); |
| 41 | + } |
| 42 | + |
| 43 | + var executeInnerMethod = innerTaskType.GetMethod(nameof(ExecuteInner), BindingFlags.Instance | BindingFlags.NonPublic); |
| 44 | + bool result = (bool)executeInnerMethod.Invoke(innerTask, new object[0]); |
| 45 | + |
| 46 | + foreach (var propertyPair in outputPropertiesMap) |
| 47 | + { |
| 48 | + propertyPair.outerProperty.SetValue(this, propertyPair.innerProperty.GetValue(innerTask)); |
| 49 | + } |
| 50 | + |
| 51 | + return result; |
| 52 | +#else |
| 53 | + // On .NET Framework (on Windows), we find native binaries by adding them to our PATH. |
| 54 | + if (this.UnmanagedDllDirectory != null) |
| 55 | + { |
| 56 | + string pathEnvVar = Environment.GetEnvironmentVariable("PATH"); |
| 57 | + string[] searchPaths = pathEnvVar.Split(Path.PathSeparator); |
| 58 | + if (!searchPaths.Contains(this.UnmanagedDllDirectory, StringComparer.OrdinalIgnoreCase)) |
| 59 | + { |
| 60 | + pathEnvVar += Path.PathSeparator + this.UnmanagedDllDirectory; |
| 61 | + Environment.SetEnvironmentVariable("PATH", pathEnvVar); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return this.ExecuteInner(); |
| 66 | +#endif |
| 67 | + } |
| 68 | + |
| 69 | + protected abstract bool ExecuteInner(); |
| 70 | + |
| 71 | +#if NETCOREAPP1_0 |
| 72 | + private class CustomAssemblyLoader : AssemblyLoadContext |
| 73 | + { |
| 74 | + private readonly ContextAwareTask loaderTask; |
| 75 | + |
| 76 | + internal CustomAssemblyLoader(ContextAwareTask loaderTask) |
| 77 | + { |
| 78 | + this.loaderTask = loaderTask; |
| 79 | + } |
| 80 | + |
| 81 | + protected override Assembly Load(AssemblyName assemblyName) |
| 82 | + { |
| 83 | + string assemblyPath = Path.Combine(this.loaderTask.ManagedDllDirectory, assemblyName.Name) + ".dll"; |
| 84 | + if (File.Exists(assemblyPath)) |
| 85 | + { |
| 86 | + return LoadFromAssemblyPath(assemblyPath); |
| 87 | + } |
| 88 | + |
| 89 | + return Default.LoadFromAssemblyName(assemblyName); |
| 90 | + } |
| 91 | + |
| 92 | + protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) |
| 93 | + { |
| 94 | + string unmanagedDllPath = Directory.EnumerateFiles( |
| 95 | + this.loaderTask.UnmanagedDllDirectory, |
| 96 | + $"{unmanagedDllName}.*").Concat( |
| 97 | + Directory.EnumerateFiles( |
| 98 | + this.loaderTask.UnmanagedDllDirectory, |
| 99 | + $"lib{unmanagedDllName}.*")) |
| 100 | + .FirstOrDefault(); |
| 101 | + if (unmanagedDllPath != null) |
| 102 | + { |
| 103 | + return this.LoadUnmanagedDllFromPath(unmanagedDllPath); |
| 104 | + } |
| 105 | + |
| 106 | + return base.LoadUnmanagedDll(unmanagedDllName); |
| 107 | + } |
| 108 | + } |
| 109 | +#endif |
| 110 | + } |
| 111 | +} |
0 commit comments