From b644ecfeaa5767949653fef818d9d8cb22fee818 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 19 Feb 2018 14:00:31 -0800 Subject: [PATCH] Relayer interaction between extensions and engine --- .../AssemblyExtension.cs | 24 ++++++++++++++++++ .../RazorProjectEngine.cs | 25 ++----------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.AspNetCore.Razor.Language/AssemblyExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/AssemblyExtension.cs index b93323e018..83b70cd7aa 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/AssemblyExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/AssemblyExtension.cs @@ -27,5 +27,29 @@ namespace Microsoft.AspNetCore.Razor.Language public override string ExtensionName { get; } public Assembly Assembly { get; } + + public RazorExtensionInitializer CreateInitializer() + { + // It's not an error to have an assembly with no initializers. This is useful to specify a dependency + // that doesn't really provide any Razor configuration. + var attributes = Assembly.GetCustomAttributes(); + foreach (var attribute in attributes) + { + // Using extension names and requiring them to line up allows a single assembly to ship multiple + // extensions/initializers for different configurations. + if (!string.Equals(attribute.ExtensionName, ExtensionName, StringComparison.Ordinal)) + { + continue; + } + + // There's no real protection/exception handling here because this set isn't really user-extensible + // right now. This would be a great place to add some additional diagnostics and hardening in the + // future. + var initializer = (RazorExtensionInitializer)Activator.CreateInstance(attribute.InitializerType); + return initializer; + } + + return null; + } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngine.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngine.cs index e76bdffc94..341be49296 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngine.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorProjectEngine.cs @@ -159,29 +159,8 @@ namespace Microsoft.AspNetCore.Razor.Language // lid on how things work until we add official support for extensibility everywhere. So, this is // intentionally inflexible for the time being. var extension = extensions[i] as AssemblyExtension; - if (extension == null) - { - continue; - } - - // It's not an error to have an assembly with no initializers. This is useful to specify a dependency - // that doesn't really provide any Razor configuration. - var attributes = extension.Assembly.GetCustomAttributes(); - foreach (var attribute in attributes) - { - // Using extension names and requiring them to line up allows a single assembly to ship multiple - // extensions/initializers for different configurations. - if (!string.Equals(attribute.ExtensionName, extension.ExtensionName, StringComparison.Ordinal)) - { - continue; - } - - // There's no real protection/exception handling here because this set isn't really user-extensible - // right now. This would be a great place to add some additional diagnostics and hardening in the - // future. - var initializer = (RazorExtensionInitializer)Activator.CreateInstance(attribute.InitializerType); - initializer.Initialize(builder); - } + var initializer = extension?.CreateInitializer(); + initializer?.Initialize(builder); } } }