Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

Added design time support for ViewComponent TagHelpers #98

Merged
merged 1 commit into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Razor.Compilation.TagHelpers;
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Razor.Design.Internal
{
public class AssemblyTagHelperDescriptorResolver
{
private readonly TagHelperDescriptorFactory _descriptorFactory = new TagHelperDescriptorFactory(designTime: true);
private const string TypeFullName = "Microsoft.AspNetCore.Mvc.DesignTimeMvcServiceCollectionProvider";
private const string MvcAssemblyName = "Microsoft.AspNetCore.Mvc";
private const string MethodName = "PopulateServiceCollection";

private readonly TagHelperDescriptorFactory _tagHelperDescriptorFactory;
private readonly TagHelperTypeResolver _tagHelperTypeResolver;

private bool _isInitialized;
private Action<IServiceCollection, string> _populateMethodDelegate;

public AssemblyTagHelperDescriptorResolver()
: this(new TagHelperTypeResolver())
{
Expand All @@ -22,6 +32,7 @@ public AssemblyTagHelperDescriptorResolver()
public AssemblyTagHelperDescriptorResolver(TagHelperTypeResolver tagHelperTypeResolver)
{
_tagHelperTypeResolver = tagHelperTypeResolver;
_tagHelperDescriptorFactory = new TagHelperDescriptorFactory(designTime: true);
}

public static int DefaultProtocolVersion { get; } = 1;
Expand All @@ -35,19 +46,7 @@ public IEnumerable<TagHelperDescriptor> Resolve(string assemblyName, ErrorSink e
throw new ArgumentNullException(nameof(assemblyName));
}

if (ProtocolVersion == 1)
{
var tagHelperTypes = GetTagHelperTypes(assemblyName, errorSink);
var tagHelperDescriptors = new List<TagHelperDescriptor>();
foreach (var tagHelperType in tagHelperTypes)
{
var descriptors = _descriptorFactory.CreateDescriptors(assemblyName, tagHelperType, errorSink);
tagHelperDescriptors.AddRange(descriptors);
}

return tagHelperDescriptors;
}
else
if (ProtocolVersion != 1)
{
// Unknown protocol
throw new InvalidOperationException(
Expand All @@ -56,14 +55,68 @@ public IEnumerable<TagHelperDescriptor> Resolve(string assemblyName, ErrorSink e
DesignResources.InvalidProtocolValue,
typeof(TagHelperDescriptor).FullName, ProtocolVersion));
}

EnsureMvcInitialized();

ITagHelperDescriptorResolver resolver;
if (_populateMethodDelegate != null)
{
var serviceCollection = new ServiceCollection();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to talk to @Eilon about having DI in RazorTooling.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we decided this is fine.

serviceCollection.AddSingleton<ITagHelperTypeResolver>(_tagHelperTypeResolver);
serviceCollection.AddSingleton<ITagHelperDescriptorFactory>(_tagHelperDescriptorFactory);

// Populate the service collection
_populateMethodDelegate(serviceCollection, assemblyName);

var services = serviceCollection.BuildServiceProvider();
resolver = services.GetRequiredService<ITagHelperDescriptorResolver>();
}
else
{
// MVC assembly does not exist. Manually create the resolver.
resolver = new TagHelperDescriptorResolver(_tagHelperTypeResolver, _tagHelperDescriptorFactory);
}

var directiveDescriptors = new[]
{
new TagHelperDirectiveDescriptor
{
DirectiveText = $"*, {assemblyName}"
}
};
var context = new TagHelperDescriptorResolutionContext(directiveDescriptors, errorSink);
var descriptors = resolver.Resolve(context);

return descriptors;
}

/// <summary>
/// Protected virtual for testing.
/// </summary>
protected virtual IEnumerable<Type> GetTagHelperTypes(string assemblyName, ErrorSink errorSink)
private void EnsureMvcInitialized()
{
return _tagHelperTypeResolver.Resolve(assemblyName, SourceLocation.Zero, errorSink);
if (_isInitialized)
{
return;
}

_isInitialized = true;

var providerClass = Type.GetType($"{TypeFullName}, {MvcAssemblyName}");

// Get the method from the type
var populateMethod = providerClass?.GetMethods(BindingFlags.Public | BindingFlags.Static)
.FirstOrDefault(methodInfo =>
{
if (string.Equals(MethodName, methodInfo.Name, StringComparison.Ordinal))
{
var methodParams = methodInfo.GetParameters();
return methodParams.Length == 2
&& methodParams[0].ParameterType.Equals(typeof(IServiceCollection))
&& methodParams[1].ParameterType.Equals(typeof(string));
}
return false;
});

_populateMethodDelegate = (Action<IServiceCollection, string>) populateMethod
?.CreateDelegate(typeof(Action<IServiceCollection, string>));
}
}
}
7 changes: 6 additions & 1 deletion src/Microsoft.AspNetCore.Razor.Design/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"Microsoft.AspNetCore.Razor.Runtime": "1.1.0-*",
"Microsoft.Extensions.CommandLineUtils": "1.1.0-*",
"Microsoft.Extensions.DependencyInjection": "1.1.0-*",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
Expand All @@ -49,6 +50,10 @@
}
}
},
"net451": {}
"net451": {
"frameworkAssemblies": {
"System.Runtime": ""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Razor.Compilation.TagHelpers;
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Razor.Test.Internal;
using Xunit;
Expand Down Expand Up @@ -52,11 +53,11 @@ public void Resolve_ThrowsWhenInvalidProtocol(int protocol)
public void Resolve_ResolvesTagHelperDescriptors()
{
// Arrange
var assemblyNameLookups = new Dictionary<string, IEnumerable<Type>>
var assemblyNameLookups = new Dictionary<string, IEnumerable<TypeInfo>>
{
{ CustomTagHelperAssembly, new[] { typeof(CustomTagHelper) } }
{ CustomTagHelperAssembly, new[] { typeof(CustomTagHelper).GetTypeInfo() } }
};
var descriptorResolver = new TestAssemblyTagHelperDescriptorResolver(assemblyNameLookups);
var descriptorResolver = new AssemblyTagHelperDescriptorResolver(new TestTagHelperTypeResolver(assemblyNameLookups));
var errorSink = new ErrorSink();

// Act
Expand All @@ -74,11 +75,11 @@ public void Resolve_ResolvesTagHelperDescriptors()
public void Resolve_ResolvesDesignTimeTagHelperDescriptors()
{
// Arrange
var assemblyNameLookups = new Dictionary<string, IEnumerable<Type>>
var assemblyNameLookups = new Dictionary<string, IEnumerable<TypeInfo>>
{
{ CustomTagHelperAssembly, new[] { typeof(DesignTimeTagHelper) } }
{ CustomTagHelperAssembly, new[] { typeof(DesignTimeTagHelper).GetTypeInfo() } }
};
var descriptorResolver = new TestAssemblyTagHelperDescriptorResolver(assemblyNameLookups);
var descriptorResolver = new AssemblyTagHelperDescriptorResolver(new TestTagHelperTypeResolver(assemblyNameLookups));
var expectedDescriptor = new TagHelperDescriptor
{
Prefix = DefaultPrefix,
Expand Down Expand Up @@ -109,11 +110,12 @@ public void Resolve_ResolvesDesignTimeTagHelperDescriptors()
public void Resolve_CreatesErrors()
{
// Arrange
var assemblyNameLookups = new Dictionary<string, IEnumerable<Type>>
var assemblyNameLookups = new Dictionary<string, IEnumerable<TypeInfo>>
{
{ CustomTagHelperAssembly, new[] { typeof(InvalidTagHelper) } }
{ CustomTagHelperAssembly, new[] { typeof(InvalidTagHelper).GetTypeInfo() } }
};
var descriptorResolver = new TestAssemblyTagHelperDescriptorResolver(assemblyNameLookups);

var descriptorResolver = new AssemblyTagHelperDescriptorResolver(new TestTagHelperTypeResolver(assemblyNameLookups));
var errorSink = new ErrorSink();

// Act
Expand All @@ -130,35 +132,35 @@ public void Resolve_CreatesErrors()
Assert.Equal(0, error.Length);
}

private class TestAssemblyTagHelperDescriptorResolver : AssemblyTagHelperDescriptorResolver
private class TestTagHelperTypeResolver : TagHelperTypeResolver
{
private readonly IDictionary<string, IEnumerable<Type>> _assemblyTypeLookups;
private readonly IDictionary<string, IEnumerable<TypeInfo>> _assemblyTypeLookups;

public TestAssemblyTagHelperDescriptorResolver(IDictionary<string, IEnumerable<Type>> assemblyTypeLookups)
public TestTagHelperTypeResolver(IDictionary<string, IEnumerable<TypeInfo>> assemblyTypeLookups)
{
_assemblyTypeLookups = assemblyTypeLookups;
}

protected override IEnumerable<Type> GetTagHelperTypes(string assemblyName, ErrorSink errorSink)
protected override IEnumerable<TypeInfo> GetExportedTypes(AssemblyName assemblyName)
{
return _assemblyTypeLookups[assemblyName];
return _assemblyTypeLookups[assemblyName.Name];
}
}
}

private class CustomTagHelper : TagHelper
{
}
public class CustomTagHelper : TagHelper
{
}

[RestrictChildren("br")]
[OutputElementHint("strong")]
[HtmlTargetElement("design-time", TagStructure = TagStructure.NormalOrSelfClosing)]
private class DesignTimeTagHelper : TagHelper
{
}
[RestrictChildren("br")]
[OutputElementHint("strong")]
[HtmlTargetElement("design-time", TagStructure = TagStructure.NormalOrSelfClosing)]
public class DesignTimeTagHelper : TagHelper
{
}

[HtmlTargetElement("inv@lid")]
private class InvalidTagHelper : TagHelper
{
}
[HtmlTargetElement("inv@lid")]
public class InvalidTagHelper : TagHelper
{
}
}