Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Modify ICompilationLibrariesProvider to return a sequence of referenc…
Browse files Browse the repository at this point in the history
…e path

Fixes #4738
  • Loading branch information
pranavkm committed May 25, 2016
1 parent 9acd0f5 commit 5d72a7f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyModel;

Expand All @@ -11,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
/// <summary>
/// An <see cref="ApplicationPart"/> backed by an <see cref="Assembly"/>.
/// </summary>
public class AssemblyPart : ApplicationPart, IApplicationPartTypeProvider, ICompilationLibrariesProvider
public class AssemblyPart : ApplicationPart, IApplicationPartTypeProvider, ICompilationReferencesProvider
{
/// <summary>
/// Initalizes a new <see cref="AssemblyPart"/> instance.
Expand Down Expand Up @@ -41,15 +42,19 @@ public AssemblyPart(Assembly assembly)
public IEnumerable<TypeInfo> Types => Assembly.DefinedTypes;

/// <inheritdoc />
public IReadOnlyList<CompilationLibrary> GetCompilationLibraries()
public IEnumerable<string> GetReferencePaths()
{
var dependencyContext = DependencyContext.Load(Assembly);
if (dependencyContext != null)
{
return dependencyContext.CompileLibraries;
return dependencyContext.CompileLibraries.SelectMany(library => library.ResolveReferencePaths());
}

return new CompilationLibrary[0];
// If an application has been compiled without preserveCompilationContext, return the path to the assembly
// as a reference. For runtime compilation, this will allow the compilation to succeed as long as it least
// one application part has been compiled with preserveCompilationContext and contains a super set of types
// required for the compilation to succeed.
return new[] { Assembly.Location };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.Extensions.DependencyModel;

namespace Microsoft.AspNetCore.Mvc.ApplicationParts
{
/// <summary>
/// Exposes <see cref="CompilationLibrary"/> instances from an <see cref="ApplicationPart"/>.
/// Exposes one or more reference paths from an <see cref="ApplicationPart"/>.
/// </summary>
public interface ICompilationLibrariesProvider
public interface ICompilationReferencesProvider
{
/// <summary>
/// Gets the sequence of <see cref="CompilationLibrary"/> instances.
/// Gets reference paths used to perform runtime compilation.
/// </summary>
IReadOnlyList<CompilationLibrary> GetCompilationLibraries();
IEnumerable<string> GetReferencePaths();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,15 @@ public void PopulateFeature(IEnumerable<ApplicationPart> parts, MetadataReferenc
}

var libraryPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var providerPart in parts.OfType<ICompilationLibrariesProvider>())
foreach (var providerPart in parts.OfType<ICompilationReferencesProvider>())
{
var compileLibraries = providerPart.GetCompilationLibraries();

for (var i = 0; i < compileLibraries.Count; i++)
var referencePaths = providerPart.GetReferencePaths();
foreach (var path in referencePaths)
{
var library = compileLibraries[i];
var referencePaths = library.ResolveReferencePaths();
foreach (var path in referencePaths)
if (libraryPaths.Add(path))
{
if (libraryPaths.Add(path))
{
var metadataReference = CreateMetadataReference(path);
feature.MetadataReferences.Add(metadataReference);
}
var metadataReference = CreateMetadataReference(path);
feature.MetadataReferences.Add(metadataReference);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using System.Reflection;
using Xunit;

Expand Down Expand Up @@ -46,5 +47,36 @@ public void AssemblyPart_Assembly_ReturnsAssembly()
// Act & Assert
Assert.Equal(part.Assembly, assembly);
}

[Fact]
public void GetReferencePaths_ReturnsReferencesFromDependencyContext_IfPreserveCompilationContextIsSet()
{
// Arrange
var assembly = GetType().GetTypeInfo().Assembly;
var part = new AssemblyPart(assembly);

// Act
var references = part.GetReferencePaths().ToList();

// Assert
Assert.Contains(assembly.Location, references);
Assert.Contains(typeof(AssemblyPart).GetTypeInfo().Assembly.Location, references);
}

[Fact]
public void GetReferencePaths_ReturnsAssebmlyLocation_IfPreserveCompilationContextIsNotSet()
{
// Arrange
// src projects do not have preserveCompilationContext specified.
var assembly = typeof(AssemblyPart).GetTypeInfo().Assembly;
var part = new AssemblyPart(assembly);

// Act
var references = part.GetReferencePaths().ToList();

// Assert
var actual = Assert.Single(references);
Assert.Equal(assembly.Location, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ public void PopulateFeature_ReturnsEmptyList_IfNoAssemblyPartsAreRegistered()
Assert.Empty(feature.MetadataReferences);
}

[Fact]
public void PopulateFeature_ReturnsEmptySequence_IfAssemblyDoesNotPreserveCompilationContext()
{
// Arrange
var applicationPartManager = new ApplicationPartManager();
var assemblyPart = new AssemblyPart(typeof(MetadataReferenceFeatureProvider).GetTypeInfo().Assembly);
applicationPartManager.ApplicationParts.Add(assemblyPart);
applicationPartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider());
var feature = new MetadataReferenceFeature();

// Act
applicationPartManager.PopulateFeature(feature);

// Assert
Assert.Empty(feature.MetadataReferences);
}

[Fact]
public void PopulateFeature_AddsMetadataReferenceForAssemblyPartsWithDependencyContext()
{
Expand Down

0 comments on commit 5d72a7f

Please sign in to comment.