From 7730d33482693e7e8feefcc134db2401b0de1be0 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 3 Mar 2017 15:00:09 -0800 Subject: [PATCH] Introducing DocumentWriter --- .../CodeGeneration/DefaultDocumentWriter.cs | 88 +++++++++++++++++++ .../CodeGeneration/DefaultRuntimeTarget.cs | 10 ++- .../CodeGeneration/DocumentWriter.cs | 12 +++ .../CodeGeneration/RuntimeTarget.cs | 2 +- .../DefaultRazorCSharpLoweringPhase.cs | 6 +- .../DefaultRuntimeTargetTest.cs | 23 +---- 6 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultDocumentWriter.cs create mode 100644 src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DocumentWriter.cs diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultDocumentWriter.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultDocumentWriter.cs new file mode 100644 index 0000000000..2fde3666a3 --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultDocumentWriter.cs @@ -0,0 +1,88 @@ +// 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 Microsoft.AspNetCore.Razor.Evolution.Intermediate; + +namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration +{ + internal class DefaultDocumentWriter : DocumentWriter + { + private readonly CSharpRenderingContext _context; + private readonly RuntimeTarget _target; + + private readonly PageStructureCSharpRenderer _renderer; + + public DefaultDocumentWriter(RuntimeTarget target, CSharpRenderingContext context, PageStructureCSharpRenderer renderer) + { + if (target == null) + { + throw new ArgumentNullException(nameof(target)); + } + + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (renderer == null) + { + throw new ArgumentNullException(nameof(renderer)); + } + + _target = target; + _context = context; + _renderer = renderer; + } + + public override void WriteDocument(DocumentIRNode node) + { + if (node == null) + { + throw new ArgumentNullException(nameof(node)); + } + + var visitor = new Visitor(_target, _context, _renderer); + _context.RenderChildren = visitor.RenderChildren; + + visitor.VisitDocument(node); + _context.RenderChildren = null; + } + + private class Visitor : RazorIRNodeVisitor + { + private readonly CSharpRenderingContext _context; + private readonly RuntimeTarget _target; + + private readonly PageStructureCSharpRenderer _renderer; + + public Visitor(RuntimeTarget target, CSharpRenderingContext context, PageStructureCSharpRenderer renderer) + { + _target = target; + _context = context; + _renderer = renderer; + } + + public void RenderChildren(RazorIRNode node) + { + for (var i = 0; i < node.Children.Count; i++) + { + var child = node.Children[i]; + Visit(child); + } + } + + public override void VisitDocument(DocumentIRNode node) + { + RenderChildren(node); + } + + public override void VisitDefault(RazorIRNode node) + { + // This is a temporary bridge to the renderer, which allows us to move functionality piecemeal + // into this class. + _renderer.Visit(node); + } + } + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultRuntimeTarget.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultRuntimeTarget.cs index f529ebb7a9..60c5fdfd26 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultRuntimeTarget.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DefaultRuntimeTarget.cs @@ -1,6 +1,7 @@ // 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; @@ -18,16 +19,19 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration public IRuntimeTargetExtension[] Extensions { get; } - internal override PageStructureCSharpRenderer CreateRenderer(CSharpRenderingContext context) + public override DocumentWriter CreateWriter(CSharpRenderingContext context) { + PageStructureCSharpRenderer renderer; if (_options.DesignTimeMode) { - return new DesignTimeCSharpRenderer(this, context); + renderer = new DesignTimeCSharpRenderer(this, context); } else { - return new RuntimeCSharpRenderer(this, context); + renderer = new RuntimeCSharpRenderer(this, context); } + + return new DefaultDocumentWriter(this, context, renderer); } public override TExtension GetExtension() diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DocumentWriter.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DocumentWriter.cs new file mode 100644 index 0000000000..1a63cd54a8 --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DocumentWriter.cs @@ -0,0 +1,12 @@ +// 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.Razor.Evolution.Intermediate; + +namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration +{ + public abstract class DocumentWriter + { + public abstract void WriteDocument(DocumentIRNode node); + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeTarget.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeTarget.cs index 009decbb10..62a274e895 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeTarget.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeTarget.cs @@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration } - internal abstract PageStructureCSharpRenderer CreateRenderer(CSharpRenderingContext context); + public abstract DocumentWriter CreateWriter(CSharpRenderingContext context); public abstract TExtension GetExtension() where TExtension : class, IRuntimeTargetExtension; diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs index 9414e0b25d..737a963e01 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs @@ -56,10 +56,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution renderingContext.IdGenerator = () => idValue.ToString(); } - var renderer = target.CreateRenderer(renderingContext); - renderingContext.RenderChildren = renderer.VisitDefault; - - renderer.VisitDocument(irDocument); + var documentWriter = target.CreateWriter(renderingContext); + documentWriter.WriteDocument(irDocument); var diagnostics = new List(); diagnostics.AddRange(syntaxTree.Diagnostics); diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/CodeGeneration/DefaultRuntimeTargetTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/CodeGeneration/DefaultRuntimeTargetTest.cs index 0d0cd35073..54a2b77824 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/CodeGeneration/DefaultRuntimeTargetTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/CodeGeneration/DefaultRuntimeTargetTest.cs @@ -28,35 +28,18 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration } [Fact] - public void CreateRenderer_DesignTime_CreatesDesignTimeRenderer() + public void CreateWriter_CreatesDefaultDocumentWriter() { // Arrange var options = RazorParserOptions.CreateDefaultOptions(); - options.DesignTimeMode = true; var target = new DefaultRuntimeTarget(options, Enumerable.Empty()); // Act - var renderer = target.CreateRenderer(new CSharpRenderingContext()); + var writer = target.CreateWriter(new CSharpRenderingContext()); // Assert - Assert.IsType(renderer); - } - - [Fact] - public void CreateRenderer_Runtime_CreatesRuntimeRenderer() - { - // Arrange - var options = RazorParserOptions.CreateDefaultOptions(); - options.DesignTimeMode = false; - - var target = new DefaultRuntimeTarget(options, Enumerable.Empty()); - - // Act - var renderer = target.CreateRenderer(new CSharpRenderingContext()); - - // Assert - Assert.IsType(renderer); + Assert.IsType(writer); } [Fact]