diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageDirectiveFeature.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageDirectiveFeature.cs index aa2162fc83..ff09f6abbf 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageDirectiveFeature.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageDirectiveFeature.cs @@ -2,6 +2,9 @@ // 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.IO; +using System.Linq; using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Mvc.RazorPages.Internal; using Microsoft.AspNetCore.Razor.Language; @@ -12,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure { 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--) { @@ -35,24 +38,14 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure throw new ArgumentNullException(nameof(projectItem)); } - var sourceDocument = RazorSourceDocument.ReadFrom(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 codeDocument = PageDirectiveEngine.Process(projectItem); var documentIRNode = codeDocument.GetDocumentIntermediateNode(); if (PageDirective.TryGetPageDirective(documentIRNode, out var pageDirective)) { if (pageDirective.DirectiveNode is MalformedDirectiveIntermediateNode malformedNode) { - logger.MalformedPageDirective(sourceDocument.FilePath, malformedNode.Diagnostics); + logger.MalformedPageDirective(projectItem.FilePath, malformedNode.Diagnostics); } template = pageDirective.RouteTemplate; @@ -72,5 +65,47 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure options.ParseLeadingDirectives = true; } } + + private class EmptyRazorProjectFileSystem : RazorProjectFileSystem + { + public override IEnumerable EnumerateItems(string basePath) + { + return Enumerable.Empty(); + } + + public override IEnumerable FindHierarchicalItems(string basePath, string path, string fileName) + { + return Enumerable.Empty(); + } + + 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; + } + + /// + public override string BasePath { get; } + + /// + public override string FilePath { get; } + + /// + public override bool Exists => false; + + /// + public override string PhysicalPath => throw new NotSupportedException(); + + /// + public override Stream Read() => throw new NotSupportedException(); + } + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs index 6ce35abac7..05e156a6ef 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CompilerFailedExceptionFactoryTest.cs @@ -18,11 +18,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal { // Arrange var viewPath = "/Views/Home/Index.cshtml"; - var razorEngine = RazorEngine.Create(); var fileSystem = new VirtualRazorProjectFileSystem(); fileSystem.Add(new TestRazorProjectItem(viewPath, "")); + var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine; + var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var codeDocument = templateEngine.CreateCodeDocument(viewPath); @@ -52,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal var fileSystem = new VirtualRazorProjectFileSystem(); fileSystem.Add(new TestRazorProjectItem(viewPath, "", physicalPath: physicalPath)); - var razorEngine = RazorEngine.Create(); + var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine; var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var codeDocument = templateEngine.CreateCodeDocument(viewPath); @@ -79,9 +80,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal } "; - var razorEngine = RazorEngine.Create(); var fileSystem = new VirtualRazorProjectFileSystem(); fileSystem.Add(new TestRazorProjectItem(viewPath, fileContent)); + + var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine; var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); 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("/Views/_MyImports.cshtml", importsContent)); - var razorEngine = RazorEngine.Create(); + var razorEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine; var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem) { Options = diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionDescriptorChangeProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionDescriptorChangeProviderTest.cs index cb9bea92d1..21f9db7215 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionDescriptorChangeProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionDescriptorChangeProviderTest.cs @@ -26,9 +26,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal .Returns(Mock.Of()); var accessor = Mock.Of(a => a.FileProvider == fileProvider.Object); + var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new RazorTemplateEngine( - RazorEngine.Create(), - new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); + RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine, + fileSystem); var options = Options.Create(new RazorPagesOptions()); var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options); @@ -50,9 +51,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal .Returns(Mock.Of()); var accessor = Mock.Of(a => a.FileProvider == fileProvider.Object); + var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new RazorTemplateEngine( - RazorEngine.Create(), - new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); + RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine, + fileSystem); var options = Options.Create(new RazorPagesOptions()); options.Value.RootDirectory = rootDirectory; @@ -74,9 +76,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal .Returns(Mock.Of()); var accessor = Mock.Of(a => a.FileProvider == fileProvider.Object); + var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new RazorTemplateEngine( - RazorEngine.Create(), - new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); + RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine, + fileSystem); var options = Options.Create(new RazorPagesOptions { AllowAreas = true }); var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options); @@ -94,9 +97,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal var fileProvider = new TestFileProvider(); var accessor = Mock.Of(a => a.FileProvider == fileProvider); + var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new RazorTemplateEngine( - RazorEngine.Create(), - new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); + RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine, + fileSystem); templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; var options = Options.Create(new RazorPagesOptions()); options.Value.RootDirectory = "/dir1/dir2"; @@ -118,9 +122,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal var fileProvider = new TestFileProvider(); var accessor = Mock.Of(a => a.FileProvider == fileProvider); + var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new RazorTemplateEngine( - RazorEngine.Create(), - new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); + RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine, + fileSystem); templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; var options = Options.Create(new RazorPagesOptions()); options.Value.RootDirectory = "/dir1/dir2"; @@ -144,9 +149,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal var fileProvider = new TestFileProvider(); var accessor = Mock.Of(a => a.FileProvider == fileProvider); + var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new RazorTemplateEngine( - RazorEngine.Create(), - new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment)); + RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem).Engine, + fileSystem); templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; var options = Options.Create(new RazorPagesOptions { AllowAreas = false });