Cleanup unused precompilation code

This commit is contained in:
Pranav K 2017-06-09 11:57:16 -07:00
parent b1b7252ddc
commit 6961cf9211
9 changed files with 65 additions and 229 deletions

View File

@ -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;
}
}
}

View File

@ -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
{
/// <summary>
/// Provides information for precompiled views.
/// </summary>
public class ViewInfo
{
/// <summary>
/// Creates a new instance of <see cref="ViewInfo" />.
/// </summary>
/// <param name="path">The path of the view.</param>
/// <param name="type">The view <see cref="System.Type"/>.</param>
public ViewInfo(string path, Type type)
{
Path = path;
Type = type;
}
/// <summary>
/// The path of the view.
/// </summary>
public string Path { get; }
/// <summary>
/// The view <see cref="System.Type"/>.
/// </summary>
public Type Type { get; }
}
}

View File

@ -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
{
/// <summary>
/// A container for <see cref="ViewInfo"/> instances.
/// </summary>
public class ViewInfoContainer
{
/// <summary>
/// Initializes a new instance of <see cref="ViewInfos"/>.
/// </summary>
/// <param name="views">The sequence of <see cref="ViewInfo"/>.</param>
public ViewInfoContainer(IReadOnlyList<ViewInfo> views)
{
ViewInfos = views;
}
/// <summary>
/// The <see cref="IReadOnlyList{T}"/> of <see cref="ViewInfo"/>.
/// </summary>
public IReadOnlyList<ViewInfo> ViewInfos { get; }
}
}

View File

@ -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";
/// <summary>
/// Gets the namespace for the <see cref="ViewInfoContainer"/> type in the view assembly.
/// </summary>
public static readonly string ViewInfoContainerNamespace = "AspNetCore";
/// <summary>
/// Gets the type name for the view collection type in the view assembly.
/// </summary>
public static readonly string ViewInfoContainerTypeName = "__PrecompiledViewCollection";
private static readonly string FullyQualifiedManifestTypeName = ViewInfoContainerNamespace + "." + ViewInfoContainerTypeName;
/// <inheritdoc />
public void PopulateFeature(IEnumerable<ApplicationPart> 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<RazorViewAttribute>();
@ -72,5 +61,34 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
return Enumerable.Empty<RazorViewAttribute>();
}
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;
}
}
}

View File

@ -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
{
/// <summary>
/// An <see cref="IApplicationFeatureProvider{TFeature}"/> for <see cref="ViewsFeature"/>.
/// </summary>
public class CompiledPageFeatureProvider
{
/// <summary>
/// Gets the namespace for the <see cref="ViewInfoContainer"/> type in the view assembly.
/// </summary>
public static readonly string CompiledPageManifestNamespace = "AspNetCore";
/// <summary>
/// Gets the type name for the view collection type in the view assembly.
/// </summary>
public static readonly string CompiledPageManifestTypeName = "__CompiledRazorPagesManifest";
private static readonly string FullyQualifiedManifestTypeName =
CompiledPageManifestNamespace + "." + CompiledPageManifestTypeName;
}
}

View File

@ -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; }
}
}

View File

@ -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
{
/// <summary>
/// Initializes a new instance of <see cref="CompiledPageManifest"/>.
/// </summary>
/// <param name="pages">The sequence of <see cref="CompiledPageInfo"/>.</param>
public CompiledPageManifest(IReadOnlyList<CompiledPageInfo> pages)
{
CompiledPages = pages;
}
/// <summary>
/// The <see cref="IReadOnlyList{T}"/> of <see cref="CompiledPageInfo"/>.
/// </summary>
public IReadOnlyList<CompiledPageInfo> CompiledPages { get; }
}
}

View File

@ -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<PageApplicationModel>();
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
}
/// <summary>
/// Gets the sequence of <see cref="CompiledViewDescriptor"/> from <paramref name="parts"/>.
/// Gets the sequence of <see cref="CompiledViewDescriptor"/> from <paramref name="applicationManager"/>.
/// </summary>
/// <param name="parts">The <see cref="ApplicationPart"/>s</param>
/// <param name="applicationManager">The <see cref="ApplicationPartManager"/>s</param>
/// <returns>The sequence of <see cref="CompiledViewDescriptor"/>.</returns>
protected virtual IEnumerable<RazorPageAttribute> GetRazorPageAttributes(IEnumerable<ApplicationPart> parts)
protected virtual IEnumerable<CompiledViewDescriptor> GetViewDescriptors(ApplicationPartManager applicationManager)
{
if (parts == null)
if (applicationManager == null)
{
throw new ArgumentNullException(nameof(parts));
throw new ArgumentNullException(nameof(applicationManager));
}
return _applicationManager.ApplicationParts
.OfType<AssemblyPart>()
.SelectMany(GetAttributes);
}
var viewsFeature = new ViewsFeature();
applicationManager.PopulateFeature(viewsFeature);
private static IEnumerable<RazorPageAttribute> GetAttributes(AssemblyPart assemblyPart)
{
var featureAssembly = CompiledViewManfiest.GetFeatureAssembly(assemblyPart);
if (featureAssembly != null)
{
return featureAssembly.GetCustomAttributes<RazorPageAttribute>();
}
return Enumerable.Empty<RazorPageAttribute>();
return viewsFeature.ViewDescriptors.Where(d => d.IsPrecompiled && d.ViewAttribute is RazorPageAttribute);
}
private string GetViewEnginePath(string rootDirectory, string path)

View File

@ -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<RazorPageAttribute> _attributes;
private readonly IEnumerable<CompiledViewDescriptor> _descriptors;
public TestCompiledPageApplicationModelProvider(IEnumerable<RazorPageAttribute> attributes, RazorPagesOptions options)
public TestCompiledPageApplicationModelProvider(IEnumerable<CompiledViewDescriptor> descriptors, RazorPagesOptions options)
: base(new ApplicationPartManager(), new TestOptionsManager<RazorPagesOptions>(options))
{
_attributes = attributes;
_descriptors = descriptors;
}
protected override IEnumerable<RazorPageAttribute> GetRazorPageAttributes(IEnumerable<ApplicationPart> parts) => _attributes;
protected override IEnumerable<CompiledViewDescriptor> GetViewDescriptors(ApplicationPartManager applicationManager) => _descriptors;
}
}
}