From 6961cf92116e40f07ae83dab563a6e7c1dff2c84 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 9 Jun 2017 11:57:16 -0700 Subject: [PATCH] Cleanup unused precompilation code --- .../Compilation/CompiledViewManifest.cs | 43 ------------------ .../Compilation/ViewInfo.cs | 34 -------------- .../Compilation/ViewInfoContainer.cs | 27 ------------ .../Compilation/ViewsFeatureProvider.cs | 44 +++++++++++++------ .../CompiledPageFeatureProvider.cs | 26 ----------- .../ApplicationParts/CompiledPageInfo.cs | 23 ---------- .../Infrastructure/CompiledPageManifest.cs | 25 ----------- .../CompiledPageApplicationModelProvider.cs | 38 ++++++---------- ...ompiledPageApplicationModelProviderTest.cs | 34 ++++++++------ 9 files changed, 65 insertions(+), 229 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Mvc.Razor/Compilation/CompiledViewManifest.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfo.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfoContainer.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageInfo.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/CompiledPageManifest.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/CompiledViewManifest.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/CompiledViewManifest.cs deleted file mode 100644 index 42c5c0be7c..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/CompiledViewManifest.cs +++ /dev/null @@ -1,43 +0,0 @@ -// 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.IO; -using System.Reflection; -using Microsoft.AspNetCore.Mvc.ApplicationParts; - -namespace Microsoft.AspNetCore.Mvc.Razor.Compilation -{ - public static class CompiledViewManfiest - { - public static readonly string PrecompiledViewsAssemblySuffix = ".PrecompiledViews"; - - public static Assembly GetFeatureAssembly(AssemblyPart assemblyPart) - { - if (assemblyPart.Assembly.IsDynamic || string.IsNullOrEmpty(assemblyPart.Assembly.Location)) - { - return null; - } - - var precompiledAssemblyFileName = assemblyPart.Assembly.GetName().Name - + PrecompiledViewsAssemblySuffix - + ".dll"; - var precompiledAssemblyFilePath = Path.Combine( - Path.GetDirectoryName(assemblyPart.Assembly.Location), - precompiledAssemblyFileName); - - if (File.Exists(precompiledAssemblyFilePath)) - { - try - { - return Assembly.LoadFile(precompiledAssemblyFilePath); - } - catch (FileLoadException) - { - // Don't throw if assembly cannot be loaded. This can happen if the file is not a managed assembly. - } - } - - return null; - } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfo.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfo.cs deleted file mode 100644 index eea6f630bc..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfo.cs +++ /dev/null @@ -1,34 +0,0 @@ -// 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; - -namespace Microsoft.AspNetCore.Mvc.ApplicationParts -{ - /// - /// Provides information for precompiled views. - /// - public class ViewInfo - { - /// - /// Creates a new instance of . - /// - /// The path of the view. - /// The view . - public ViewInfo(string path, Type type) - { - Path = path; - Type = type; - } - - /// - /// The path of the view. - /// - public string Path { get; } - - /// - /// The view . - /// - public Type Type { get; } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfoContainer.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfoContainer.cs deleted file mode 100644 index 22c5d39f22..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewInfoContainer.cs +++ /dev/null @@ -1,27 +0,0 @@ -// 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.Collections.Generic; - -namespace Microsoft.AspNetCore.Mvc.ApplicationParts -{ - /// - /// A container for instances. - /// - public class ViewInfoContainer - { - /// - /// Initializes a new instance of . - /// - /// The sequence of . - public ViewInfoContainer(IReadOnlyList views) - { - ViewInfos = views; - } - - /// - /// The of . - /// - public IReadOnlyList ViewInfos { get; } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs index 89fb227141..a9e3b15ed2 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationParts; @@ -18,18 +19,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation { public static readonly string PrecompiledViewsAssemblySuffix = ".PrecompiledViews"; - /// - /// Gets the namespace for the type in the view assembly. - /// - public static readonly string ViewInfoContainerNamespace = "AspNetCore"; - - /// - /// Gets the type name for the view collection type in the view assembly. - /// - public static readonly string ViewInfoContainerTypeName = "__PrecompiledViewCollection"; - - private static readonly string FullyQualifiedManifestTypeName = ViewInfoContainerNamespace + "." + ViewInfoContainerTypeName; - /// public void PopulateFeature(IEnumerable parts, ViewsFeature feature) { @@ -64,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation throw new ArgumentNullException(nameof(assemblyPart)); } - var featureAssembly = CompiledViewManfiest.GetFeatureAssembly(assemblyPart); + var featureAssembly = GetFeatureAssembly(assemblyPart); if (featureAssembly != null) { return featureAssembly.GetCustomAttributes(); @@ -72,5 +61,34 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation return Enumerable.Empty(); } + + private static Assembly GetFeatureAssembly(AssemblyPart assemblyPart) + { + if (assemblyPart.Assembly.IsDynamic || string.IsNullOrEmpty(assemblyPart.Assembly.Location)) + { + return null; + } + + var precompiledAssemblyFileName = assemblyPart.Assembly.GetName().Name + + PrecompiledViewsAssemblySuffix + + ".dll"; + var precompiledAssemblyFilePath = Path.Combine( + Path.GetDirectoryName(assemblyPart.Assembly.Location), + precompiledAssemblyFileName); + + if (File.Exists(precompiledAssemblyFilePath)) + { + try + { + return Assembly.LoadFile(precompiledAssemblyFilePath); + } + catch (FileLoadException) + { + // Don't throw if assembly cannot be loaded. This can happen if the file is not a managed assembly. + } + } + + return null; + } } } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs deleted file mode 100644 index 389f4b7915..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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 Microsoft.AspNetCore.Mvc.Razor.Compilation; - -namespace Microsoft.AspNetCore.Mvc.ApplicationParts -{ - /// - /// An for . - /// - public class CompiledPageFeatureProvider - { - /// - /// Gets the namespace for the type in the view assembly. - /// - public static readonly string CompiledPageManifestNamespace = "AspNetCore"; - - /// - /// Gets the type name for the view collection type in the view assembly. - /// - public static readonly string CompiledPageManifestTypeName = "__CompiledRazorPagesManifest"; - - private static readonly string FullyQualifiedManifestTypeName = - CompiledPageManifestNamespace + "." + CompiledPageManifestTypeName; - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageInfo.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageInfo.cs deleted file mode 100644 index 5c6418e7a6..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageInfo.cs +++ /dev/null @@ -1,23 +0,0 @@ -// 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; - -namespace Microsoft.AspNetCore.Mvc.ApplicationParts -{ - public class CompiledPageInfo - { - public CompiledPageInfo(string path, Type compiledType, string routePrefix) - { - Path = path; - CompiledType = compiledType; - RoutePrefix = routePrefix; - } - - public string Path { get; } - - public string RoutePrefix { get; } - - public Type CompiledType { get; } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/CompiledPageManifest.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/CompiledPageManifest.cs deleted file mode 100644 index 20ea60c86d..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/CompiledPageManifest.cs +++ /dev/null @@ -1,25 +0,0 @@ -// 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.Collections.Generic; -using Microsoft.AspNetCore.Mvc.ApplicationParts; - -namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure -{ - public abstract class CompiledPageManifest - { - /// - /// Initializes a new instance of . - /// - /// The sequence of . - public CompiledPageManifest(IReadOnlyList pages) - { - CompiledPages = pages; - } - - /// - /// The of . - /// - public IReadOnlyList CompiledPages { get; } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/CompiledPageApplicationModelProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/CompiledPageApplicationModelProvider.cs index 1fb3c0bf33..61779b3a03 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/CompiledPageApplicationModelProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/CompiledPageApplicationModelProvider.cs @@ -4,11 +4,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Razor.Compilation; -using Microsoft.AspNetCore.Mvc.Razor.Internal; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; using Microsoft.Extensions.Options; @@ -61,16 +59,16 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal } var cachedApplicationModels = new List(); - foreach (var pageAttribute in GetRazorPageAttributes(_applicationManager.ApplicationParts)) + foreach (var viewDescriptor in GetViewDescriptors(_applicationManager)) { - var normalizedPath = ViewPath.NormalizePath(pageAttribute.Path); - if (!normalizedPath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase)) + if (!viewDescriptor.RelativePath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase)) { continue; } - var viewEnginePath = GetViewEnginePath(rootDirectory, normalizedPath); - var model = new PageApplicationModel(normalizedPath, viewEnginePath); + var viewEnginePath = GetViewEnginePath(rootDirectory, viewDescriptor.RelativePath); + var model = new PageApplicationModel(viewDescriptor.RelativePath, viewEnginePath); + var pageAttribute = (RazorPageAttribute)viewDescriptor.ViewAttribute; PageSelectorModel.PopulateDefaults(model, pageAttribute.RouteTemplate); cachedApplicationModels.Add(model); @@ -81,31 +79,21 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal } /// - /// Gets the sequence of from . + /// Gets the sequence of from . /// - /// The s + /// The s /// The sequence of . - protected virtual IEnumerable GetRazorPageAttributes(IEnumerable parts) + protected virtual IEnumerable GetViewDescriptors(ApplicationPartManager applicationManager) { - if (parts == null) + if (applicationManager == null) { - throw new ArgumentNullException(nameof(parts)); + throw new ArgumentNullException(nameof(applicationManager)); } - return _applicationManager.ApplicationParts - .OfType() - .SelectMany(GetAttributes); - } + var viewsFeature = new ViewsFeature(); + applicationManager.PopulateFeature(viewsFeature); - private static IEnumerable GetAttributes(AssemblyPart assemblyPart) - { - var featureAssembly = CompiledViewManfiest.GetFeatureAssembly(assemblyPart); - if (featureAssembly != null) - { - return featureAssembly.GetCustomAttributes(); - } - - return Enumerable.Empty(); + return viewsFeature.ViewDescriptors.Where(d => d.IsPrecompiled && d.ViewAttribute is RazorPageAttribute); } private string GetViewEnginePath(string rootDirectory, string path) diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs index 0b291c29a7..c3807a15d2 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; using Xunit; @@ -18,8 +19,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Arrange var descriptors = new[] { - GetAttribute("/Pages/About.cshtml"), - GetAttribute("/Pages/Home.cshtml", "some-prefix"), + GetDescriptor("/Pages/About.cshtml"), + GetDescriptor("/Pages/Home.cshtml", "some-prefix"), }; var provider = new TestCompiledPageApplicationModelProvider(descriptors, new RazorPagesOptions()); var context = new PageApplicationModelProviderContext(); @@ -51,8 +52,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Arrange var descriptors = new[] { - GetAttribute("/Pages/Index.cshtml"), - GetAttribute("/Pages/Admin/Index.cshtml", "some-template"), + GetDescriptor("/Pages/Index.cshtml"), + GetDescriptor("/Pages/Admin/Index.cshtml", "some-template"), }; var provider = new TestCompiledPageApplicationModelProvider(descriptors, new RazorPagesOptions { RootDirectory = "/" }); var context = new PageApplicationModelProviderContext(); @@ -86,8 +87,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Arrange var descriptors = new[] { - GetAttribute("/Pages/Index.cshtml"), - GetAttribute("/Pages/Admin/Index.cshtml", "some-template"), + GetDescriptor("/Pages/Index.cshtml"), + GetDescriptor("/Pages/Admin/Index.cshtml", "some-template"), }; var provider = new TestCompiledPageApplicationModelProvider(descriptors, new RazorPagesOptions()); var context = new PageApplicationModelProviderContext(); @@ -121,8 +122,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Arrange var descriptors = new[] { - GetAttribute("/Pages/Index.cshtml"), - GetAttribute("/Pages/Home.cshtml", "/some-prefix"), + GetDescriptor("/Pages/Index.cshtml"), + GetDescriptor("/Pages/Home.cshtml", "/some-prefix"), }; var provider = new TestCompiledPageApplicationModelProvider(descriptors, new RazorPagesOptions()); var context = new PageApplicationModelProviderContext(); @@ -133,19 +134,26 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal ex.Message); } - private static RazorPageAttribute GetAttribute(string path, string prefix = "") => new RazorPageAttribute(path, typeof(object), prefix); + private static CompiledViewDescriptor GetDescriptor(string path, string prefix = "") + { + return new CompiledViewDescriptor + { + RelativePath = path, + ViewAttribute = new RazorPageAttribute(path, typeof(object), prefix), + }; + } public class TestCompiledPageApplicationModelProvider : CompiledPageApplicationModelProvider { - private readonly IEnumerable _attributes; + private readonly IEnumerable _descriptors; - public TestCompiledPageApplicationModelProvider(IEnumerable attributes, RazorPagesOptions options) + public TestCompiledPageApplicationModelProvider(IEnumerable descriptors, RazorPagesOptions options) : base(new ApplicationPartManager(), new TestOptionsManager(options)) { - _attributes = attributes; + _descriptors = descriptors; } - protected override IEnumerable GetRazorPageAttributes(IEnumerable parts) => _attributes; + protected override IEnumerable GetViewDescriptors(ApplicationPartManager applicationManager) => _descriptors; } } }