Made `RazorCodeGenerationOptionsBuilder.DesignTime` getter only

- Made RazorCodeGenerationOptions consistent with RazorParserOptions
This commit is contained in:
Ajay Bhargav Baaskaran 2017-07-07 11:30:25 -07:00
parent 9c0a8a5c96
commit b70815e317
14 changed files with 133 additions and 42 deletions

View File

@ -5,7 +5,12 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
internal class DefaultRazorCodeGenerationOptionsBuilder : RazorCodeGenerationOptionsBuilder internal class DefaultRazorCodeGenerationOptionsBuilder : RazorCodeGenerationOptionsBuilder
{ {
public override bool DesignTime { get; set; } public DefaultRazorCodeGenerationOptionsBuilder(bool designTime)
{
DesignTime = designTime;
}
public override bool DesignTime { get; }
public override int IndentSize { get; set; } = 4; public override int IndentSize { get; set; } = 4;

View File

@ -0,0 +1,36 @@
// 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.Linq;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorCodeGenerationOptionsFeature : RazorEngineFeatureBase, IRazorCodeGenerationOptionsFeature
{
private readonly bool _designTime;
private IConfigureRazorCodeGenerationOptionsFeature[] _configureOptions;
public DefaultRazorCodeGenerationOptionsFeature(bool designTime)
{
_designTime = designTime;
}
protected override void OnInitialized()
{
_configureOptions = Engine.Features.OfType<IConfigureRazorCodeGenerationOptionsFeature>().ToArray();
}
public RazorCodeGenerationOptions GetOptions()
{
var builder = new DefaultRazorCodeGenerationOptionsBuilder(_designTime);
for (var i = 0; i < _configureOptions.Length; i++)
{
_configureOptions[i].Configure(builder);
}
var options = builder.Build();
return options;
}
}
}

View File

@ -13,11 +13,11 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
internal class DefaultRazorIntermediateNodeLoweringPhase : RazorEnginePhaseBase, IRazorIntermediateNodeLoweringPhase internal class DefaultRazorIntermediateNodeLoweringPhase : RazorEnginePhaseBase, IRazorIntermediateNodeLoweringPhase
{ {
private IRazorCodeGenerationOptionsFeature[] _optionsCallbacks; private IRazorCodeGenerationOptionsFeature _optionsFeature;
protected override void OnIntialized() protected override void OnIntialized()
{ {
_optionsCallbacks = Engine.Features.OfType<IRazorCodeGenerationOptionsFeature>().OrderBy(f => f.Order).ToArray(); _optionsFeature = GetRequiredFeature<IRazorCodeGenerationOptionsFeature>();
} }
protected override void ExecuteCore(RazorCodeDocument codeDocument) protected override void ExecuteCore(RazorCodeDocument codeDocument)
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var document = new DocumentIntermediateNode(); var document = new DocumentIntermediateNode();
var builder = IntermediateNodeBuilder.Create(document); var builder = IntermediateNodeBuilder.Create(document);
document.Options = CreateCodeGenerationOptions(); document.Options = _optionsFeature.GetOptions();
var namespaces = new Dictionary<string, SourceSpan?>(StringComparer.Ordinal); var namespaces = new Dictionary<string, SourceSpan?>(StringComparer.Ordinal);
@ -146,17 +146,6 @@ namespace Microsoft.AspNetCore.Razor.Language
} }
} }
private RazorCodeGenerationOptions CreateCodeGenerationOptions()
{
var builder = new DefaultRazorCodeGenerationOptionsBuilder();
for (var i = 0; i < _optionsCallbacks.Length; i++)
{
_optionsCallbacks[i].Configure(builder);
}
return builder.Build();
}
private class LoweringVisitor : ParserVisitor private class LoweringVisitor : ParserVisitor
{ {
protected readonly IntermediateNodeBuilder _builder; protected readonly IntermediateNodeBuilder _builder;

View File

@ -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.
namespace Microsoft.AspNetCore.Razor.Language
{
public interface IConfigureRazorCodeGenerationOptionsFeature : IRazorEngineFeature
{
int Order { get; }
void Configure(RazorCodeGenerationOptionsBuilder options);
}
}

View File

@ -5,8 +5,6 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
public interface IRazorCodeGenerationOptionsFeature : IRazorEngineFeature public interface IRazorCodeGenerationOptionsFeature : IRazorEngineFeature
{ {
int Order { get; } RazorCodeGenerationOptions GetOptions();
void Configure(RazorCodeGenerationOptionsBuilder options);
} }
} }

View File

@ -1,15 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNetCore.Razor.Language namespace Microsoft.AspNetCore.Razor.Language
{ {
public abstract class RazorCodeGenerationOptions public abstract class RazorCodeGenerationOptions
{ {
public static RazorCodeGenerationOptions Create(bool indentWithTabs, int indentSize, bool designTime, bool suppressChecksum)
{
return new DefaultRazorCodeGenerationOptions(indentWithTabs, indentSize, designTime, suppressChecksum);
}
public static RazorCodeGenerationOptions CreateDefault() public static RazorCodeGenerationOptions CreateDefault()
{ {
return new DefaultRazorCodeGenerationOptions(indentWithTabs: false, indentSize: 4, designTime: false, suppressChecksum: false); return new DefaultRazorCodeGenerationOptions(indentWithTabs: false, indentSize: 4, designTime: false, suppressChecksum: false);
@ -20,6 +17,34 @@ namespace Microsoft.AspNetCore.Razor.Language
return new DefaultRazorCodeGenerationOptions(indentWithTabs: false, indentSize: 4, designTime: true, suppressChecksum: false); return new DefaultRazorCodeGenerationOptions(indentWithTabs: false, indentSize: 4, designTime: true, suppressChecksum: false);
} }
public static RazorCodeGenerationOptions Create(Action<RazorCodeGenerationOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var builder = new DefaultRazorCodeGenerationOptionsBuilder(designTime: false);
configure(builder);
var options = builder.Build();
return options;
}
public static RazorCodeGenerationOptions CreateDesignTime(Action<RazorCodeGenerationOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var builder = new DefaultRazorCodeGenerationOptionsBuilder(designTime: true);
configure(builder);
var options = builder.Build();
return options;
}
public abstract bool DesignTime { get; } public abstract bool DesignTime { get; }
public abstract bool IndentWithTabs { get; } public abstract bool IndentWithTabs { get; }

View File

@ -5,7 +5,7 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
public abstract class RazorCodeGenerationOptionsBuilder public abstract class RazorCodeGenerationOptionsBuilder
{ {
public abstract bool DesignTime { get; set; } public abstract bool DesignTime { get; }
public abstract int IndentSize { get; set; } public abstract int IndentSize { get; set; }

View File

@ -103,6 +103,7 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
// Configure options // Configure options
builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false)); builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false));
builder.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
// Intermediate Node Passes // Intermediate Node Passes
builder.Features.Add(new PreallocatedTagHelperAttributeOptimizationPass()); builder.Features.Add(new PreallocatedTagHelperAttributeOptimizationPass());
@ -116,7 +117,8 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
// Configure options // Configure options
builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: true)); builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: true));
builder.Features.Add(new DesignTimeOptionsFeature()); builder.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: true));
builder.Features.Add(new SuppressChecksumOptionsFeature());
// Intermediate Node Passes // Intermediate Node Passes
builder.Features.Add(new DesignTimeDirectivePass()); builder.Features.Add(new DesignTimeDirectivePass());

View File

@ -5,7 +5,7 @@ using System;
namespace Microsoft.AspNetCore.Razor.Language namespace Microsoft.AspNetCore.Razor.Language
{ {
internal class DesignTimeOptionsFeature : RazorEngineFeatureBase, IRazorCodeGenerationOptionsFeature internal class SuppressChecksumOptionsFeature : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{ {
public int Order { get; set; } public int Order { get; set; }
@ -16,7 +16,6 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
options.DesignTime = true;
options.SuppressChecksum = true; options.SuppressChecksum = true;
} }
} }

View File

@ -108,7 +108,7 @@ namespace RazorPageGenerator
}; };
} }
private class SuppressChecksumOptionsFeature : RazorEngineFeatureBase, IRazorCodeGenerationOptionsFeature private class SuppressChecksumOptionsFeature : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{ {
public int Order { get; set; } public int Order { get; set; }

View File

@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
var document = new DocumentIntermediateNode(); var document = new DocumentIntermediateNode();
var codeDocument = TestRazorCodeDocument.CreateEmpty(); var codeDocument = TestRazorCodeDocument.CreateEmpty();
var optionsBuilder = new DefaultRazorCodeGenerationOptionsBuilder() var optionsBuilder = new DefaultRazorCodeGenerationOptionsBuilder(designTime: false)
{ {
SuppressChecksum = true SuppressChecksum = true
}; };

View File

@ -36,22 +36,24 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange // Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty(); var codeDocument = TestRazorCodeDocument.CreateEmpty();
var callback = new Mock<IRazorCodeGenerationOptionsFeature>(); var callback = new Mock<IConfigureRazorCodeGenerationOptionsFeature>();
callback callback
.Setup(c => c.Configure(It.IsAny<RazorCodeGenerationOptionsBuilder>())) .Setup(c => c.Configure(It.IsAny<RazorCodeGenerationOptionsBuilder>()))
.Callback<RazorCodeGenerationOptionsBuilder>(o => .Callback<RazorCodeGenerationOptionsBuilder>(o =>
{ {
o.DesignTime = true;
o.IndentSize = 17; o.IndentSize = 17;
o.IndentWithTabs = true; o.IndentWithTabs = true;
o.SuppressChecksum = true; o.SuppressChecksum = true;
}); });
// Act // Act
var documentNode = Lower(codeDocument, builder: b => var documentNode = Lower(
{ codeDocument,
b.Features.Add(callback.Object); builder: b =>
}); {
b.Features.Add(callback.Object);
},
designTime: true);
// Assert // Assert
Assert.NotNull(documentNode.Options); Assert.NotNull(documentNode.Options);
@ -431,18 +433,21 @@ namespace Microsoft.AspNetCore.Razor.Language
private DocumentIntermediateNode Lower( private DocumentIntermediateNode Lower(
RazorCodeDocument codeDocument, RazorCodeDocument codeDocument,
Action<IRazorEngineBuilder> builder = null, Action<IRazorEngineBuilder> builder = null,
IEnumerable<TagHelperDescriptor> tagHelpers = null) IEnumerable<TagHelperDescriptor> tagHelpers = null,
bool designTime = false)
{ {
tagHelpers = tagHelpers ?? new TagHelperDescriptor[0]; tagHelpers = tagHelpers ?? new TagHelperDescriptor[0];
var engine = RazorEngine.Create(b => Action<IRazorEngineBuilder> configureEngine = b =>
{ {
builder?.Invoke(b); builder?.Invoke(b);
FunctionsDirective.Register(b); FunctionsDirective.Register(b);
SectionDirective.Register(b); SectionDirective.Register(b);
b.AddTagHelpers(tagHelpers); b.AddTagHelpers(tagHelpers);
}); };
var engine = designTime ? RazorEngine.CreateDesignTime(configureEngine) : RazorEngine.Create(configureEngine);
for (var i = 0; i < engine.Phases.Count; i++) for (var i = 0; i < engine.Phases.Count; i++)
{ {

View File

@ -26,6 +26,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => var engine = RazorEngine.CreateEmpty(b =>
{ {
b.Phases.Add(phase); b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
b.AddDirective(directive); b.AddDirective(directive);
}); });
var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive)); var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive));
@ -60,6 +61,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => var engine = RazorEngine.CreateEmpty(b =>
{ {
b.Phases.Add(phase); b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
b.AddDirective(directive); b.AddDirective(directive);
}); });
var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive)); var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive));
@ -94,6 +96,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => var engine = RazorEngine.CreateEmpty(b =>
{ {
b.Phases.Add(phase); b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
b.AddDirective(directive); b.AddDirective(directive);
}); });
var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive)); var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive));
@ -124,6 +127,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => var engine = RazorEngine.CreateEmpty(b =>
{ {
b.Phases.Add(phase); b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
b.AddDirective(codeBlockDirective); b.AddDirective(codeBlockDirective);
b.AddDirective(razorBlockDirective); b.AddDirective(razorBlockDirective);
}); });
@ -158,6 +162,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => var engine = RazorEngine.CreateEmpty(b =>
{ {
b.Phases.Add(phase); b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
b.AddDirective(directive); b.AddDirective(directive);
}); });
var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive)); var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive));
@ -187,6 +192,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => var engine = RazorEngine.CreateEmpty(b =>
{ {
b.Phases.Add(phase); b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
b.AddDirective(directive); b.AddDirective(directive);
}); });
var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive)); var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive));
@ -213,7 +219,11 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange // Arrange
var phase = new DefaultRazorIntermediateNodeLoweringPhase(); var phase = new DefaultRazorIntermediateNodeLoweringPhase();
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase)); var engine = RazorEngine.CreateEmpty(b =>
{
b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
});
var codeDocument = TestRazorCodeDocument.CreateEmpty(); var codeDocument = TestRazorCodeDocument.CreateEmpty();
@ -229,7 +239,11 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
// Arrange // Arrange
var phase = new DefaultRazorIntermediateNodeLoweringPhase(); var phase = new DefaultRazorIntermediateNodeLoweringPhase();
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase)); var engine = RazorEngine.CreateEmpty(b =>
{
b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
});
var codeDocument = TestRazorCodeDocument.Create("<p class=@("); var codeDocument = TestRazorCodeDocument.Create("<p class=@(");
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source)); codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source));
@ -248,7 +262,11 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
// Arrange // Arrange
var phase = new DefaultRazorIntermediateNodeLoweringPhase(); var phase = new DefaultRazorIntermediateNodeLoweringPhase();
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase)); var engine = RazorEngine.CreateEmpty(b =>
{
b.Phases.Add(phase);
b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
});
var codeDocument = TestRazorCodeDocument.CreateEmpty(); var codeDocument = TestRazorCodeDocument.CreateEmpty();
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source)); codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source));

View File

@ -161,6 +161,7 @@ namespace Microsoft.AspNetCore.Razor.Language
feature => Assert.IsType<DefaultTagHelperOptimizationPass>(feature), feature => Assert.IsType<DefaultTagHelperOptimizationPass>(feature),
feature => Assert.IsType<DefaultDocumentClassifierPassFeature>(feature), feature => Assert.IsType<DefaultDocumentClassifierPassFeature>(feature),
feature => Assert.IsType<DefaultRazorParserOptionsFeature>(feature), feature => Assert.IsType<DefaultRazorParserOptionsFeature>(feature),
feature => Assert.IsType<DefaultRazorCodeGenerationOptionsFeature>(feature),
feature => Assert.IsType<PreallocatedTagHelperAttributeOptimizationPass>(feature)); feature => Assert.IsType<PreallocatedTagHelperAttributeOptimizationPass>(feature));
} }
@ -202,7 +203,8 @@ namespace Microsoft.AspNetCore.Razor.Language
feature => Assert.IsType<DefaultTagHelperOptimizationPass>(feature), feature => Assert.IsType<DefaultTagHelperOptimizationPass>(feature),
feature => Assert.IsType<DefaultDocumentClassifierPassFeature>(feature), feature => Assert.IsType<DefaultDocumentClassifierPassFeature>(feature),
feature => Assert.IsType<DefaultRazorParserOptionsFeature>(feature), feature => Assert.IsType<DefaultRazorParserOptionsFeature>(feature),
feature => Assert.IsType<DesignTimeOptionsFeature>(feature), feature => Assert.IsType<DefaultRazorCodeGenerationOptionsFeature>(feature),
feature => Assert.IsType<SuppressChecksumOptionsFeature>(feature),
feature => Assert.IsType<DesignTimeDirectivePass>(feature)); feature => Assert.IsType<DesignTimeDirectivePass>(feature));
} }