// 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;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
{
///
/// An for .
///
public class ViewsFeatureProvider : IApplicationFeatureProvider
{
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)
{
foreach (var assemblyPart in parts.OfType())
{
var viewContainer = GetManifest(assemblyPart);
if (viewContainer == null)
{
continue;
}
foreach (var item in viewContainer.ViewInfos)
{
var relativePath = ViewPath.NormalizePath(item.Path);
var viewDescriptor = new CompiledViewDescriptor
{
ExpirationTokens = Array.Empty(),
RelativePath = relativePath,
ViewAttribute = new RazorViewAttribute(relativePath, item.Type),
IsPrecompiled = true,
};
feature.ViewDescriptors.Add(viewDescriptor);
}
}
}
///
/// Gets the type of for the specified .
///
/// The .
/// The .
protected virtual ViewInfoContainer GetManifest(AssemblyPart assemblyPart)
{
var type = CompiledViewManfiest.GetManifestType(assemblyPart, FullyQualifiedManifestTypeName);
if (type != null)
{
return (ViewInfoContainer)Activator.CreateInstance(type);
}
return null;
}
}
}