Merge pull request #2117 from dotnet/pakrym/dc-more-overloads

Add more overloads for assembly resolution in dependency model
This commit is contained in:
Pavel Krymets 2016-03-29 11:19:54 -07:00
commit 5f6c939c7b
3 changed files with 48 additions and 19 deletions

View file

@ -55,11 +55,15 @@ namespace Microsoft.Extensions.DependencyModel
foreach (var runtimeLibrary in context.RuntimeLibraries) foreach (var runtimeLibrary in context.RuntimeLibraries)
{ {
CheckMetadata(runtimeLibrary); 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()) foreach (var name in context.GetDefaultAssemblyNames())
{ {
var assembly = Assembly.Load(new AssemblyName(name)); var assembly = Assembly.Load(name);
} }
} }
} }

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
namespace Microsoft.Extensions.DependencyModel namespace Microsoft.Extensions.DependencyModel
{ {
@ -11,25 +12,45 @@ namespace Microsoft.Extensions.DependencyModel
public static IEnumerable<string> GetDefaultNativeAssets(this DependencyContext self) public static IEnumerable<string> GetDefaultNativeAssets(this DependencyContext self)
{ {
return ResolveAssets(self, string.Empty, l => l.NativeLibraryGroups); return self.RuntimeLibraries.SelectMany(library => library.GetDefaultNativeAssets(self));
} }
public static IEnumerable<string> GetRuntimeNativeAssets(this DependencyContext self, string runtimeIdentifier) public static IEnumerable<string> 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<string> GetDefaultAssemblyNames(this DependencyContext self) public static IEnumerable<string> 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<string> GetRuntimeAssemblyNames(this DependencyContext self, string runtimeIdentifier) public static IEnumerable<string> 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<AssemblyName> GetDefaultAssemblyNames(this DependencyContext self)
{
return self.RuntimeLibraries.SelectMany(library => library.GetDefaultAssemblyNames(self));
}
public static IEnumerable<AssemblyName> GetRuntimeAssemblyNames(this DependencyContext self, string runtimeIdentifier)
{
return self.RuntimeLibraries.SelectMany(library => library.GetRuntimeAssemblyNames(self, runtimeIdentifier));
}
public static IEnumerable<AssemblyName> GetDefaultAssemblyNames(this RuntimeLibrary self, DependencyContext context)
{
return ResolveAssets(context, string.Empty, self.RuntimeAssemblyGroups).Select(GetAssemblyName);
}
public static IEnumerable<AssemblyName> 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); var name = Path.GetFileNameWithoutExtension(assetPath);
if (name == null) if (name == null)
@ -42,14 +63,17 @@ namespace Microsoft.Extensions.DependencyModel
name = name.Substring(0, name.Length - NativeImageSufix.Length); name = name.Substring(0, name.Length - NativeImageSufix.Length);
} }
return name; return new AssemblyName(name);
} }
private static IEnumerable<string> ResolveAssets(DependencyContext context, string runtimeIdentifier, Func<RuntimeLibrary, IEnumerable<RuntimeAssetGroup>> groupSelector) private static IEnumerable<string> ResolveAssets(
DependencyContext context,
string runtimeIdentifier,
IEnumerable<RuntimeAssetGroup> assets)
{ {
var fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier); var fallbacks = context.RuntimeGraph.FirstOrDefault(f => f.Runtime == runtimeIdentifier);
var rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty<string>()); var rids = Enumerable.Concat(new[] { runtimeIdentifier }, fallbacks?.Fallbacks ?? Enumerable.Empty<string>());
return context.RuntimeLibraries.SelectMany(l => SelectAssets(rids, groupSelector(l))); return SelectAssets(rids, assets);
} }
private static IEnumerable<string> SelectAssets(IEnumerable<string> rids, IEnumerable<RuntimeAssetGroup> groups) private static IEnumerable<string> SelectAssets(IEnumerable<string> rids, IEnumerable<RuntimeAssetGroup> groups)

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
@ -11,9 +12,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests
public class DependencyContextTests public class DependencyContextTests
{ {
[Theory] [Theory]
[InlineData("System.Banana.dll", "System.Banana")] [InlineData("System.Collections.dll", "System.Collections")]
[InlineData("System.Banana.ni.dll", "System.Banana")] [InlineData("System.Collections.ni.dll", "System.Collections")]
[InlineData("FlibbidyFlob", "FlibbidyFlob")] [InlineData("mscorlib", "mscorlib")]
public void GetRuntimeAssemblyNamesExtractsCorrectAssemblyName(string path, string expected) public void GetRuntimeAssemblyNamesExtractsCorrectAssemblyName(string path, string expected)
{ {
var context = new DependencyContext(new TargetInfo(".NETStandard,Version=v1.3", string.Empty, string.Empty, true), 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[] { }); runtimeGraph: new RuntimeFallbacks[] { });
var assets = context.GetDefaultAssemblyNames(); var assets = context.GetDefaultAssemblyNames();
assets.Should().BeEquivalentTo(expected); assets.Should().OnlyContain(a => a.Name == expected);
} }
[Fact] [Fact]
@ -41,7 +42,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
var context = BuildTestContext(); var context = BuildTestContext();
var assets = context.GetRuntimeAssemblyNames("win7-x64"); var assets = context.GetRuntimeAssemblyNames("win7-x64");
assets.Should().BeEquivalentTo("System.Banana"); assets.Should().OnlyContain(a => a.Name == "System.Collections");
} }
[Fact] [Fact]
@ -50,7 +51,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
var context = BuildTestContext(); var context = BuildTestContext();
var assets = context.GetRuntimeAssemblyNames("win81-x64"); var assets = context.GetRuntimeAssemblyNames("win81-x64");
assets.Should().BeEquivalentTo("System.Banana"); assets.Should().OnlyContain(a => a.Name == "System.Collections");
} }
[Fact] [Fact]
@ -103,9 +104,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests
runtimeLibraries: new[] { runtimeLibraries: new[] {
new RuntimeLibrary("package", "System.Banana", "1.0.0", "hash", new RuntimeLibrary("package", "System.Banana", "1.0.0", "hash",
new [] { 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("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 [] {
new RuntimeAssetGroup("rhel"), new RuntimeAssetGroup("rhel"),