Moved VCTH types to Razor.Extensions

This commit is contained in:
Ajay Bhargav Baaskaran 2017-06-05 12:35:03 -07:00
parent 6664efb6d9
commit 74fef5f722
22 changed files with 122 additions and 71 deletions

View File

@ -1,5 +1,5 @@
// <auto-generated /> // <auto-generated />
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
using System.Globalization; using System.Globalization;
using System.Reflection; using System.Reflection;

View File

@ -4,7 +4,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
internal class ViewComponentDiagnosticFactory internal class ViewComponentDiagnosticFactory
{ {

View File

@ -3,7 +3,7 @@
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
/// <summary> /// <summary>
/// A library of methods used to generate <see cref="TagHelperDescriptor"/>s for view components. /// A library of methods used to generate <see cref="TagHelperDescriptor"/>s for view components.

View File

@ -6,8 +6,9 @@ using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
internal class ViewComponentTagHelperDescriptorFactory internal class ViewComponentTagHelperDescriptorFactory
{ {

View File

@ -4,8 +4,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
public sealed class ViewComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider public sealed class ViewComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider
{ {

View File

@ -4,8 +4,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
internal class ViewComponentTypeVisitor : SymbolVisitor internal class ViewComponentTypeVisitor : SymbolVisitor
{ {

View File

@ -3,7 +3,7 @@
using System; using System;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
internal static class ViewComponentTypes internal static class ViewComponentTypes
{ {

View File

@ -11,6 +11,5 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="$(RoslynVersion)" /> <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="$(RoslynVersion)" />
<ProjectReference Include="..\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" /> <ProjectReference Include="..\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
<ProjectReference Include="..\Microsoft.CodeAnalysis.Razor\Microsoft.CodeAnalysis.Razor.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.Host;

View File

@ -2,9 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Razor;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.CodeAnalysis.Remote.Razor
{ {
internal class DefaultTagHelperResolver : TagHelperResolver internal class DefaultTagHelperResolver : TagHelperResolver
{ {
@ -21,7 +23,7 @@ namespace Microsoft.CodeAnalysis.Razor
var providers = new ITagHelperDescriptorProvider[] var providers = new ITagHelperDescriptorProvider[]
{ {
new DefaultTagHelperDescriptorProvider() { DesignTime = true, }, new DefaultTagHelperDescriptorProvider() { DesignTime = DesignTime, },
new ViewComponentTagHelperDescriptorProvider(), new ViewComponentTagHelperDescriptorProvider(),
}; };

View File

@ -19,5 +19,6 @@
<PackageReference Include="StreamJsonRpc" Version="$(StreamJsonRpcVersion)" /> <PackageReference Include="StreamJsonRpc" Version="$(StreamJsonRpcVersion)" />
<ProjectReference Include="..\Microsoft.CodeAnalysis.Razor.Workspaces\Microsoft.CodeAnalysis.Razor.Workspaces.csproj" /> <ProjectReference Include="..\Microsoft.CodeAnalysis.Razor.Workspaces\Microsoft.CodeAnalysis.Razor.Workspaces.csproj" />
<ProjectReference Include="..\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" /> <ProjectReference Include="..\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
<ProjectReference Include="..\Microsoft.AspNetCore.Mvc.Razor.Extensions\Microsoft.AspNetCore.Mvc.Razor.Extensions.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -6,6 +6,8 @@ using System.Collections.Generic;
using System.Composition; using System.Composition;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor; using Microsoft.CodeAnalysis.Razor;
using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell;
@ -56,8 +58,8 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor
} }
// The OOP host is turned off, so let's do this in process. // The OOP host is turned off, so let's do this in process.
var resolver = new CodeAnalysis.Razor.DefaultTagHelperResolver(designTime: true); var compilation = await project.GetCompilationAsync(CancellationToken.None).ConfigureAwait(false);
result = await resolver.GetTagHelpersAsync(project, CancellationToken.None).ConfigureAwait(false); result = GetTagHelpers(compilation, designTime: true);
return result; return result;
} }
catch (Exception exception) catch (Exception exception)
@ -79,6 +81,32 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor
} }
} }
private TagHelperResolutionResult GetTagHelpers(Compilation compilation, bool designTime)
{
var descriptors = new List<TagHelperDescriptor>();
var providers = new ITagHelperDescriptorProvider[]
{
new DefaultTagHelperDescriptorProvider() { DesignTime = designTime, },
new ViewComponentTagHelperDescriptorProvider(),
};
var results = new List<TagHelperDescriptor>();
var context = TagHelperDescriptorProviderContext.Create(results);
context.SetCompilation(compilation);
for (var i = 0; i < providers.Length; i++)
{
var provider = providers[i];
provider.Execute(context);
}
var diagnostics = new List<RazorDiagnostic>();
var resolutionResult = new TagHelperResolutionResult(results, diagnostics);
return resolutionResult;
}
private TagHelperResolutionResult GetTagHelperResolutionResult(JObject jsonObject) private TagHelperResolutionResult GetTagHelperResolutionResult(JObject jsonObject)
{ {
var serializer = new JsonSerializer(); var serializer = new JsonSerializer();

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Razor;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions namespace Microsoft.AspNetCore.Mvc.Razor.Extensions

View File

@ -3,19 +3,22 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Razor.Language;
using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Xunit;
namespace Microsoft.CodeAnalysis.Razor.Workspaces namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
public class ViewComponentTagHelperDescriptorFactoryTest public class ViewComponentTagHelperDescriptorFactoryTest
{ {
private static readonly Assembly _assembly = typeof(ViewComponentTagHelperDescriptorFactoryTest).GetTypeInfo().Assembly;
[Fact] [Fact]
public void CreateDescriptor_UnderstandsStringParameters() public void CreateDescriptor_UnderstandsStringParameters()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(StringParameterViewComponent).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(StringParameterViewComponent).FullName);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var expectedDescriptor = TagHelperDescriptorBuilder.Create( var expectedDescriptor = TagHelperDescriptorBuilder.Create(
@ -53,7 +56,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_UnderstandsVariousParameterTypes() public void CreateDescriptor_UnderstandsVariousParameterTypes()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(VariousParameterViewComponent).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(VariousParameterViewComponent).FullName);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var expectedDescriptor = TagHelperDescriptorBuilder.Create( var expectedDescriptor = TagHelperDescriptorBuilder.Create(
@ -99,7 +102,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_UnderstandsGenericParameters() public void CreateDescriptor_UnderstandsGenericParameters()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(GenericParameterViewComponent).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(GenericParameterViewComponent).FullName);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var expectedDescriptor = TagHelperDescriptorBuilder.Create( var expectedDescriptor = TagHelperDescriptorBuilder.Create(
@ -137,7 +140,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_AddsDiagnostic_ForViewComponentWithNoInvokeMethod() public void CreateDescriptor_AddsDiagnostic_ForViewComponentWithNoInvokeMethod()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(ViewComponentWithoutInvokeMethod).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(ViewComponentWithoutInvokeMethod).FullName);
@ -154,7 +157,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvokeAsync_UnderstandsGenericTask() public void CreateDescriptor_ForViewComponentWithInvokeAsync_UnderstandsGenericTask()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithGenericTask).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithGenericTask).FullName);
@ -170,7 +173,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvokeAsync_UnderstandsNonGenericTask() public void CreateDescriptor_ForViewComponentWithInvokeAsync_UnderstandsNonGenericTask()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithNonGenericTask).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithNonGenericTask).FullName);
@ -186,7 +189,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvokeAsync_DoesNotUnderstandVoid() public void CreateDescriptor_ForViewComponentWithInvokeAsync_DoesNotUnderstandVoid()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithString).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithString).FullName);
@ -203,7 +206,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvokeAsync_DoesNotUnderstandString() public void CreateDescriptor_ForViewComponentWithInvokeAsync_DoesNotUnderstandString()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithString).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(AsyncViewComponentWithString).FullName);
@ -220,7 +223,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvoke_DoesNotUnderstandVoid() public void CreateDescriptor_ForViewComponentWithInvoke_DoesNotUnderstandVoid()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(SyncViewComponentWithVoid).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(SyncViewComponentWithVoid).FullName);
@ -237,7 +240,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvoke_DoesNotUnderstandNonGenericTask() public void CreateDescriptor_ForViewComponentWithInvoke_DoesNotUnderstandNonGenericTask()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(SyncViewComponentWithNonGenericTask).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(SyncViewComponentWithNonGenericTask).FullName);
@ -254,7 +257,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public void CreateDescriptor_ForViewComponentWithInvoke_DoesNotUnderstandGenericTask() public void CreateDescriptor_ForViewComponentWithInvoke_DoesNotUnderstandGenericTask()
{ {
// Arrange // Arrange
var testCompilation = TestCompilation.Create(); var testCompilation = TestCompilation.Create(_assembly);
var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation); var factory = new ViewComponentTagHelperDescriptorFactory(testCompilation);
var viewComponent = testCompilation.GetTypeByMetadataName(typeof(SyncViewComponentWithGenericTask).FullName); var viewComponent = testCompilation.GetTypeByMetadataName(typeof(SyncViewComponentWithGenericTask).FullName);

View File

@ -2,15 +2,20 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq; using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Razor;
using Xunit; using Xunit;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
// This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory. // This is just a basic integration test. There are detailed tests for the VCTH visitor and descriptor factory.
public class ViewComponentTagHelperDescriptorProviderTest public class ViewComponentTagHelperDescriptorProviderTest
{ {
private static readonly Assembly _assembly = typeof(ViewComponentTagHelperDescriptorProviderTest).GetTypeInfo().Assembly;
[Fact] [Fact]
public void DescriptorProvider_FindsVCTH() public void DescriptorProvider_FindsVCTH()
{ {
@ -22,7 +27,7 @@ namespace Microsoft.CodeAnalysis.Razor
} }
"; ";
var testCompilation = TestCompilation.Create(CSharpSyntaxTree.ParseText(code)); var testCompilation = TestCompilation.Create(_assembly, CSharpSyntaxTree.ParseText(code));
var context = TagHelperDescriptorProviderContext.Create(); var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(testCompilation); context.SetCompilation(testCompilation);

View File

@ -3,13 +3,17 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Xunit; using Xunit;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{ {
public class ViewComponentTypeVisitorTest public class ViewComponentTypeVisitorTest
{ {
private static Compilation Compilation { get; } = TestCompilation.Create(); private static readonly Assembly _assembly = typeof(ViewComponentTypeVisitorTest).GetTypeInfo().Assembly;
private static Compilation Compilation { get; } = TestCompilation.Create(_assembly);
// In practice MVC will provide a marker attribute for ViewComponents. To prevent a circular reference between MVC and Razor // In practice MVC will provide a marker attribute for ViewComponents. To prevent a circular reference between MVC and Razor
// we can use a test class as a marker. // we can use a test class as a marker.

View File

@ -13,6 +13,8 @@
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" /> <ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" /> <PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(RoslynVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(DependencyModelVersion)" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,43 +1,37 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyModel;
using System; 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.Reflection;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyModel;
using Xunit; using Xunit;
namespace Microsoft.CodeAnalysis.Razor namespace Microsoft.CodeAnalysis
{ {
public static class TestCompilation public static class TestCompilation
{ {
private static IEnumerable<MetadataReference> _metadataReferences; private static Dictionary<Assembly, IEnumerable<MetadataReference>> _assemblyMetadataReferences =
new Dictionary<Assembly, IEnumerable<MetadataReference>>();
public static IEnumerable<MetadataReference> MetadataReferences public static IEnumerable<MetadataReference> GetMetadataReferences(Assembly assembly)
{ {
get var dependencyContext = DependencyContext.Load(assembly);
{
if (_metadataReferences == null)
{
var currentAssembly = typeof(TestCompilation).GetTypeInfo().Assembly;
var dependencyContext = DependencyContext.Load(currentAssembly);
_metadataReferences = dependencyContext.CompileLibraries var metadataReferences = dependencyContext.CompileLibraries
.SelectMany(l => l.ResolveReferencePaths()) .SelectMany(l => l.ResolveReferencePaths())
.Select(assemblyPath => MetadataReference.CreateFromFile(assemblyPath)) .Select(assemblyPath => MetadataReference.CreateFromFile(assemblyPath))
.ToArray(); .ToArray();
}
return _metadataReferences; return metadataReferences;
}
} }
public static string AssemblyName => "TestAssembly"; public static string AssemblyName => "TestAssembly";
public static Compilation Create(SyntaxTree syntaxTree = null) public static Compilation Create(Assembly assembly, SyntaxTree syntaxTree = null)
{ {
IEnumerable<SyntaxTree> syntaxTrees = null; IEnumerable<SyntaxTree> syntaxTrees = null;
@ -46,7 +40,13 @@ namespace Microsoft.CodeAnalysis.Razor
syntaxTrees = new[] { syntaxTree }; syntaxTrees = new[] { syntaxTree };
} }
var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, MetadataReferences); if (!_assemblyMetadataReferences.TryGetValue(assembly, out IEnumerable<MetadataReference> metadataReferences))
{
metadataReferences = GetMetadataReferences(assembly);
_assemblyMetadataReferences[assembly] = metadataReferences;
}
var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences);
EnsureValidCompilation(compilation); EnsureValidCompilation(compilation);

View File

@ -1,27 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Legacy; using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Razor.Workspaces.Test; using Microsoft.CodeAnalysis.Razor.Workspaces.Test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit; using Xunit;
namespace Microsoft.CodeAnalysis.Razor.Workspaces namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
public class DefaultTagHelperDescriptorFactoryTest public class DefaultTagHelperDescriptorFactoryTest
{ {
protected static readonly AssemblyName TagHelperDescriptorFactoryTestAssembly = private static readonly Assembly _assembly = typeof(DefaultTagHelperDescriptorFactoryTest).GetTypeInfo().Assembly;
typeof(DefaultTagHelperDescriptorFactoryTest).GetTypeInfo().Assembly.GetName();
protected static readonly AssemblyName TagHelperDescriptorFactoryTestAssembly = _assembly.GetName();
protected static readonly string AssemblyName = TagHelperDescriptorFactoryTestAssembly.Name; protected static readonly string AssemblyName = TagHelperDescriptorFactoryTestAssembly.Name;
private static Compilation Compilation { get; } = TestCompilation.Create(); private static Compilation Compilation { get; } = TestCompilation.Create(_assembly);
public static TheoryData RequiredAttributeParserErrorData public static TheoryData RequiredAttributeParserErrorData
{ {
@ -1329,7 +1330,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{{ {{
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var attribute = tagHelperType.GetAttributes().Single(); var attribute = tagHelperType.GetAttributes().Single();
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -1510,7 +1511,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public string SomeAttribute {{ get; set; }} public string SomeAttribute {{ get; set; }}
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -1550,7 +1551,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public System.Collections.Generic.IDictionary<string, int> SomeAttribute {{ get; set; }} public System.Collections.Generic.IDictionary<string, int> SomeAttribute {{ get; set; }}
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -1593,7 +1594,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public string InvalidProperty {{ get; set; }} public string InvalidProperty {{ get; set; }}
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -1636,7 +1637,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public System.Collections.Generic.IDictionary<string, int> InvalidProperty {{ get; set; }} public System.Collections.Generic.IDictionary<string, int> InvalidProperty {{ get; set; }}
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -1678,7 +1679,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{{ {{
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -1719,7 +1720,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{{ {{
}}"; }}";
var syntaxTree = CSharpSyntaxTree.ParseText(text); var syntaxTree = CSharpSyntaxTree.ParseText(text);
var compilation = TestCompilation.Create(syntaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper"); var tagHelperType = compilation.GetTypeByMetadataName("DynamicTestTagHelper");
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: false);
@ -2031,7 +2032,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
// Arrange // Arrange
var errorSink = new ErrorSink(); var errorSink = new ErrorSink();
var sytnaxTree = CSharpSyntaxTree.ParseText(@" var syntaxTree = CSharpSyntaxTree.ParseText(@"
using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers;
/// <summary> /// <summary>
@ -2043,7 +2044,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
public class DocumentedTagHelper : " + typeof(AspNetCore.Razor.TagHelpers.TagHelper).Name + @" public class DocumentedTagHelper : " + typeof(AspNetCore.Razor.TagHelpers.TagHelper).Name + @"
{ {
}"); }");
var compilation = TestCompilation.Create(sytnaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: true); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: true);
var typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper"); var typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper");
var expectedDocumentation = var expectedDocumentation =
@ -2069,7 +2070,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
// Arrange // Arrange
var errorSink = new ErrorSink(); var errorSink = new ErrorSink();
var sytnaxTree = CSharpSyntaxTree.ParseText(@" var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System.Collections.Generic; using System.Collections.Generic;
public class DocumentedTagHelper : " + typeof(AspNetCore.Razor.TagHelpers.TagHelper).FullName + @" public class DocumentedTagHelper : " + typeof(AspNetCore.Razor.TagHelpers.TagHelper).FullName + @"
@ -2092,7 +2093,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces
/// </remarks> /// </remarks>
public List<bool> RemarksAndSummaryProperty { get; set; } public List<bool> RemarksAndSummaryProperty { get; set; }
}"); }");
var compilation = TestCompilation.Create(sytnaxTree); var compilation = TestCompilation.Create(_assembly, syntaxTree);
var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: true); var factory = new DefaultTagHelperDescriptorFactory(compilation, designTime: true);
var typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper"); var typeSymbol = compilation.GetTypeByMetadataName("DocumentedTagHelper");
var expectedDocumentations = new[] var expectedDocumentations = new[]

View File

@ -23,6 +23,7 @@
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" /> <ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Runtime\Microsoft.AspNetCore.Razor.Runtime.csproj" /> <ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Runtime\Microsoft.AspNetCore.Razor.Runtime.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor\Microsoft.AspNetCore.Razor.csproj" /> <ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor\Microsoft.AspNetCore.Razor.csproj" />
<ProjectReference Include="..\Microsoft.AspNetCore.Razor.Test.Common\Microsoft.AspNetCore.Razor.Test.Common.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -4,12 +4,15 @@
using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic; using System.Collections.Generic;
using Xunit; using Xunit;
using System.Reflection;
namespace Microsoft.CodeAnalysis.Razor.Workspaces namespace Microsoft.CodeAnalysis.Razor.Workspaces
{ {
public class TagHelperTypeVisitorTest public class TagHelperTypeVisitorTest
{ {
private static Compilation Compilation { get; } = TestCompilation.Create(); private static readonly Assembly _assembly = typeof(TagHelperTypeVisitorTest).GetTypeInfo().Assembly;
private static Compilation Compilation { get; } = TestCompilation.Create(_assembly);
private static INamedTypeSymbol ITagHelperSymbol { get; } = Compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper); private static INamedTypeSymbol ITagHelperSymbol { get; } = Compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper);