Remove usages of obsolete APIs

This commit is contained in:
Ajay Bhargav Baaskaran 2018-04-13 14:47:08 -07:00
parent d995b0418a
commit bfbd286ab6
3 changed files with 72 additions and 29 deletions

View File

@ -2,6 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Mvc.RazorPages.Internal; using Microsoft.AspNetCore.Mvc.RazorPages.Internal;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
@ -12,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{ {
public static class PageDirectiveFeature public static class PageDirectiveFeature
{ {
private static readonly RazorEngine PageDirectiveEngine = RazorEngine.Create(builder => private static readonly RazorProjectEngine PageDirectiveEngine = RazorProjectEngine.Create(RazorConfiguration.Default, new EmptyRazorProjectFileSystem(), builder =>
{ {
for (var i = builder.Phases.Count - 1; i >= 0; i--) for (var i = builder.Phases.Count - 1; i >= 0; i--)
{ {
@ -35,24 +38,14 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
throw new ArgumentNullException(nameof(projectItem)); throw new ArgumentNullException(nameof(projectItem));
} }
var sourceDocument = RazorSourceDocument.ReadFrom(projectItem); var codeDocument = PageDirectiveEngine.Process(projectItem);
return TryGetPageDirective(logger, sourceDocument, out template);
}
static bool TryGetPageDirective(
ILogger logger,
RazorSourceDocument sourceDocument,
out string template)
{
var codeDocument = RazorCodeDocument.Create(sourceDocument);
PageDirectiveEngine.Process(codeDocument);
var documentIRNode = codeDocument.GetDocumentIntermediateNode(); var documentIRNode = codeDocument.GetDocumentIntermediateNode();
if (PageDirective.TryGetPageDirective(documentIRNode, out var pageDirective)) if (PageDirective.TryGetPageDirective(documentIRNode, out var pageDirective))
{ {
if (pageDirective.DirectiveNode is MalformedDirectiveIntermediateNode malformedNode) if (pageDirective.DirectiveNode is MalformedDirectiveIntermediateNode malformedNode)
{ {
logger.MalformedPageDirective(sourceDocument.FilePath, malformedNode.Diagnostics); logger.MalformedPageDirective(projectItem.FilePath, malformedNode.Diagnostics);
} }
template = pageDirective.RouteTemplate; template = pageDirective.RouteTemplate;
@ -72,5 +65,47 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
options.ParseLeadingDirectives = true; options.ParseLeadingDirectives = true;
} }
} }
private class EmptyRazorProjectFileSystem : RazorProjectFileSystem
{
public override IEnumerable<RazorProjectItem> EnumerateItems(string basePath)
{
return Enumerable.Empty<RazorProjectItem>();
}
public override IEnumerable<RazorProjectItem> FindHierarchicalItems(string basePath, string path, string fileName)
{
return Enumerable.Empty<RazorProjectItem>();
}
public override RazorProjectItem GetItem(string path)
{
return new NotFoundProjectItem(string.Empty, path);
}
private class NotFoundProjectItem : RazorProjectItem
{
public NotFoundProjectItem(string basePath, string path)
{
BasePath = basePath;
FilePath = path;
}
/// <inheritdoc />
public override string BasePath { get; }
/// <inheritdoc />
public override string FilePath { get; }
/// <inheritdoc />
public override bool Exists => false;
/// <inheritdoc />
public override string PhysicalPath => throw new NotSupportedException();
/// <inheritdoc />
public override Stream Read() => throw new NotSupportedException();
}
}
} }
} }

View File

@ -18,11 +18,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
// Arrange // Arrange
var viewPath = "/Views/Home/Index.cshtml"; var viewPath = "/Views/Home/Index.cshtml";
var razorEngine = RazorEngine.Create();
var fileSystem = new VirtualRazorProjectFileSystem(); var fileSystem = new VirtualRazorProjectFileSystem();
fileSystem.Add(new TestRazorProjectItem(viewPath, "<span name=\"@(User.Id\">")); fileSystem.Add(new TestRazorProjectItem(viewPath, "<span name=\"@(User.Id\">"));
var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine;
var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem);
var codeDocument = templateEngine.CreateCodeDocument(viewPath); var codeDocument = templateEngine.CreateCodeDocument(viewPath);
@ -52,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
var fileSystem = new VirtualRazorProjectFileSystem(); var fileSystem = new VirtualRazorProjectFileSystem();
fileSystem.Add(new TestRazorProjectItem(viewPath, "<span name=\"@(User.Id\">", physicalPath: physicalPath)); fileSystem.Add(new TestRazorProjectItem(viewPath, "<span name=\"@(User.Id\">", physicalPath: physicalPath));
var razorEngine = RazorEngine.Create(); var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine;
var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem);
var codeDocument = templateEngine.CreateCodeDocument(viewPath); var codeDocument = templateEngine.CreateCodeDocument(viewPath);
@ -79,9 +80,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
} }
</span>"; </span>";
var razorEngine = RazorEngine.Create();
var fileSystem = new VirtualRazorProjectFileSystem(); var fileSystem = new VirtualRazorProjectFileSystem();
fileSystem.Add(new TestRazorProjectItem(viewPath, fileContent)); fileSystem.Add(new TestRazorProjectItem(viewPath, fileContent));
var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine;
var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem);
var codeDocument = templateEngine.CreateCodeDocument(viewPath); var codeDocument = templateEngine.CreateCodeDocument(viewPath);
@ -108,7 +110,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
fileSystem.Add(new TestRazorProjectItem(viewPath, fileContent)); fileSystem.Add(new TestRazorProjectItem(viewPath, fileContent));
fileSystem.Add(new TestRazorProjectItem("/Views/_MyImports.cshtml", importsContent)); fileSystem.Add(new TestRazorProjectItem("/Views/_MyImports.cshtml", importsContent));
var razorEngine = RazorEngine.Create(); var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine;
var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem) var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem)
{ {
Options = Options =

View File

@ -26,9 +26,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
.Returns(Mock.Of<IChangeToken>()); .Returns(Mock.Of<IChangeToken>());
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider.Object); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider.Object);
var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment);
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine,
new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); fileSystem);
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options); var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
@ -50,9 +51,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
.Returns(Mock.Of<IChangeToken>()); .Returns(Mock.Of<IChangeToken>());
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider.Object); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider.Object);
var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment);
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine,
new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); fileSystem);
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = rootDirectory; options.Value.RootDirectory = rootDirectory;
@ -74,9 +76,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
.Returns(Mock.Of<IChangeToken>()); .Returns(Mock.Of<IChangeToken>());
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider.Object); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider.Object);
var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment);
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine,
new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); fileSystem);
var options = Options.Create(new RazorPagesOptions { AllowAreas = true }); var options = Options.Create(new RazorPagesOptions { AllowAreas = true });
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options); var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
@ -94,9 +97,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider();
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment);
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine,
new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); fileSystem);
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = "/dir1/dir2"; options.Value.RootDirectory = "/dir1/dir2";
@ -118,9 +122,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider();
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment);
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine,
new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); fileSystem);
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = "/dir1/dir2"; options.Value.RootDirectory = "/dir1/dir2";
@ -144,9 +149,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider();
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment);
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine,
new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); fileSystem);
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions { AllowAreas = false }); var options = Options.Create(new RazorPagesOptions { AllowAreas = false });