From 9497ee8f66242c083a4d48837b7ea9b38fee466f Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 29 Mar 2016 09:28:55 -0700 Subject: [PATCH] Add more overloads for assembly resolution in dependency model --- .../DependencyContextValidator/Validator.cs | 6 ++- .../DependencyContextExtensions.cs | 44 ++++++++++++++----- .../DependencyContextTests.cs | 17 +++---- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs b/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs index 7fd657a92..4195d928d 100644 --- a/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs +++ b/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs @@ -55,11 +55,15 @@ namespace Microsoft.Extensions.DependencyModel foreach (var runtimeLibrary in context.RuntimeLibraries) { CheckMetadata(runtimeLibrary); + foreach (var assembly in runtimeLibrary.GetDefaultNativeAssets(context)) {} + foreach (var native in runtimeLibrary.GetDefaultAssemblyNames(context)) {} } + foreach (var native in context.GetDefaultNativeAssets()) {} + foreach (var name in context.GetDefaultAssemblyNames()) { - var assembly = Assembly.Load(new AssemblyName(name)); + var assembly = Assembly.Load(name); } } } diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextExtensions.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextExtensions.cs index db68b3e0d..fd0e7ed39 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextExtensions.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; namespace Microsoft.Extensions.DependencyModel { @@ -11,25 +12,45 @@ namespace Microsoft.Extensions.DependencyModel public static IEnumerable GetDefaultNativeAssets(this DependencyContext self) { - return ResolveAssets(self, string.Empty, l => l.NativeLibraryGroups); + return self.RuntimeLibraries.SelectMany(library => library.GetDefaultNativeAssets(self)); } public static IEnumerable GetRuntimeNativeAssets(this DependencyContext self, string runtimeIdentifier) { - return ResolveAssets(self, runtimeIdentifier, l => l.NativeLibraryGroups); + return self.RuntimeLibraries.SelectMany(library => library.GetRuntimeNativeAssets(self, runtimeIdentifier)); } - public static IEnumerable GetDefaultAssemblyNames(this DependencyContext self) + public static IEnumerable GetDefaultNativeAssets(this RuntimeLibrary self, DependencyContext context) { - return ResolveAssets(self, string.Empty, l => l.RuntimeAssemblyGroups).Select(GetAssemblyName); + return ResolveAssets(context, string.Empty, self.NativeLibraryGroups); } - public static IEnumerable GetRuntimeAssemblyNames(this DependencyContext self, string runtimeIdentifier) + public static IEnumerable GetRuntimeNativeAssets(this RuntimeLibrary self, DependencyContext context, string runtimeIdentifier) { - return ResolveAssets(self, runtimeIdentifier, l => l.RuntimeAssemblyGroups).Select(GetAssemblyName); + return ResolveAssets(context, runtimeIdentifier, self.NativeLibraryGroups); } - private static string GetAssemblyName(string assetPath) + public static IEnumerable GetDefaultAssemblyNames(this DependencyContext self) + { + return self.RuntimeLibraries.SelectMany(library => library.GetDefaultAssemblyNames(self)); + } + + public static IEnumerable GetRuntimeAssemblyNames(this DependencyContext self, string runtimeIdentifier) + { + return self.RuntimeLibraries.SelectMany(library => library.GetRuntimeAssemblyNames(self, runtimeIdentifier)); + } + + public static IEnumerable GetDefaultAssemblyNames(this RuntimeLibrary self, DependencyContext context) + { + return ResolveAssets(context, string.Empty, self.RuntimeAssemblyGroups).Select(GetAssemblyName); + } + + public static IEnumerable GetRuntimeAssemblyNames(this RuntimeLibrary self, DependencyContext context, string runtimeIdentifier) + { + return ResolveAssets(context, runtimeIdentifier, self.RuntimeAssemblyGroups).Select(GetAssemblyName); + } + + private static AssemblyName GetAssemblyName(string assetPath) { var name = Path.GetFileNameWithoutExtension(assetPath); if (name == null) @@ -42,14 +63,17 @@ namespace Microsoft.Extensions.DependencyModel name = name.Substring(0, name.Length - NativeImageSufix.Length); } - return name; + return new AssemblyName(name); } - private static IEnumerable ResolveAssets(DependencyContext context, string runtimeIdentifier, Func> groupSelector) + private static IEnumerable ResolveAssets( + DependencyContext context, + string runtimeIdentifier, + IEnumerable assets) { var fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); var rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty()); - return context.RuntimeLibraries.SelectMany(l => SelectAssets(rids, groupSelector(l))); + return SelectAssets(rids, assets); } private static IEnumerable SelectAssets(IEnumerable rids, IEnumerable groups) diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextTests.cs index b23d8e3a4..c04d1d6cf 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextTests.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Xunit; @@ -11,9 +12,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests public class DependencyContextTests { [Theory] - [InlineData("System.Banana.dll", "System.Banana")] - [InlineData("System.Banana.ni.dll", "System.Banana")] - [InlineData("FlibbidyFlob", "FlibbidyFlob")] + [InlineData("System.Collections.dll", "System.Collections")] + [InlineData("System.Collections.ni.dll", "System.Collections")] + [InlineData("mscorlib", "mscorlib")] public void GetRuntimeAssemblyNamesExtractsCorrectAssemblyName(string path, string expected) { var context = new DependencyContext(new TargetInfo(".NETStandard,Version=v1.3", string.Empty, string.Empty, true), @@ -32,7 +33,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests runtimeGraph: new RuntimeFallbacks[] { }); var assets = context.GetDefaultAssemblyNames(); - assets.Should().BeEquivalentTo(expected); + assets.Should().OnlyContain(a => a.Name == expected); } [Fact] @@ -41,7 +42,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests var context = BuildTestContext(); var assets = context.GetRuntimeAssemblyNames("win7-x64"); - assets.Should().BeEquivalentTo("System.Banana"); + assets.Should().OnlyContain(a => a.Name == "System.Collections"); } [Fact] @@ -50,7 +51,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests var context = BuildTestContext(); var assets = context.GetRuntimeAssemblyNames("win81-x64"); - assets.Should().BeEquivalentTo("System.Banana"); + assets.Should().OnlyContain(a => a.Name == "System.Collections"); } [Fact] @@ -103,9 +104,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests runtimeLibraries: new[] { new RuntimeLibrary("package", "System.Banana", "1.0.0", "hash", new [] { - new RuntimeAssetGroup(string.Empty, Path.Combine("lib", "netstandard1.3", "System.Banana.dll")), + new RuntimeAssetGroup(string.Empty, Path.Combine("lib", "netstandard1.3", "System.Collections.dll")), new RuntimeAssetGroup("win10"), - new RuntimeAssetGroup("win8", Path.Combine("runtimes", "win8", "lib", "netstandard1.3", "System.Banana.dll")) + new RuntimeAssetGroup("win8", Path.Combine("runtimes", "win8", "lib", "netstandard1.3", "System.Collections.dll")) }, new [] { new RuntimeAssetGroup("rhel"),