/// <summary> /// Called during Razor's code generation process to generate code that instantiates the value of the tag /// helper's property. Last value written should not be or end with a semicolon. /// </summary> /// <param name="attributeDescriptor"> /// The <see cref="TagHelperAttributeDescriptor"/> to generate code for. /// </param> /// <param name="writer">The <see cref="CSharpCodeWriter"/> that's used to write code.</param> /// <param name="context">A <see cref="Chunks.Generators.ChunkGeneratorContext"/> instance that contains /// information about the current code generation process.</param> /// <param name="renderAttributeValue"> /// <see cref="Action"/> that renders the raw value of the HTML attribute. /// </param> /// <param name="complexValue"> /// Indicates whether or not the source attribute value contains more than simple text. <c>false</c> for plain /// C# expressions e.g. <c>"PropertyName"</c>. <c>true</c> if the attribute value contain at least one in-line /// Razor construct e.g. <c>"@(@readonly)"</c>. /// </param> public virtual void RenderAttributeValue( TagHelperAttributeDescriptor attributeDescriptor, CSharpCodeWriter writer, CodeGeneratorContext context, Action<CSharpCodeWriter> renderAttributeValue, bool complexValue) { if (attributeDescriptor == null) { throw new ArgumentNullException(nameof(attributeDescriptor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (renderAttributeValue == null) { throw new ArgumentNullException(nameof(renderAttributeValue)); } renderAttributeValue(writer); }
public MvcCSharpCodeGenerator( CodeGeneratorContext context, string defaultModel, string injectAttribute, GeneratedTagHelperAttributeContext tagHelperAttributeContext) : base(context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (defaultModel == null) { throw new ArgumentNullException(nameof(defaultModel)); } if (injectAttribute == null) { throw new ArgumentNullException(nameof(injectAttribute)); } if (tagHelperAttributeContext == null) { throw new ArgumentNullException(nameof(tagHelperAttributeContext)); } _tagHelperAttributeContext = tagHelperAttributeContext; _defaultModel = defaultModel; _injectAttribute = injectAttribute; }
/// <summary> /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>. /// </summary> /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param> /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param> /// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about /// the current code generation process.</param> public CSharpTagHelperCodeRenderer( IChunkVisitor bodyVisitor, CSharpCodeWriter writer, CodeGeneratorContext context) { if (bodyVisitor == null) { throw new ArgumentNullException(nameof(bodyVisitor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } _bodyVisitor = bodyVisitor; _writer = writer; _context = context; _tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext; _designTimeMode = context.Host.DesignTimeMode; _literalBodyVisitor = new CSharpLiteralCodeVisitor(this, writer, context); _attributeCodeVisitor = new TagHelperAttributeCodeVisitor(writer, context); AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer(); }
public override CodeGenerator DecorateCodeGenerator(CodeGenerator generator, CodeGeneratorContext context) { String type = ChunkHelper.GetModelTypeName(context.ChunkTreeBuilder.Root, "dynamic"); DefaultBaseClass = $"Genny.GennyTemplate<{type}>"; return base.DecorateCodeGenerator(generator, context); }
protected override CSharpCodeVisitor CreateCSharpCodeVisitor( CSharpCodeWriter writer, CodeGeneratorContext context) { var visitor = base.CreateCSharpCodeVisitor(writer, context); visitor.TagHelperRenderer = new NoUniqueIdsTagHelperCodeRenderer(visitor, writer, context); return visitor; }
/// <inheritdoc /> /// <remarks>If the attribute being rendered is of the type /// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/>, then a model expression will be /// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>. /// </remarks> public override void RenderAttributeValue( TagHelperAttributeDescriptor attributeDescriptor, CSharpCodeWriter writer, CodeGeneratorContext codeGeneratorContext, Action<CSharpCodeWriter> renderAttributeValue, bool complexValue) { if (attributeDescriptor == null) { throw new ArgumentNullException(nameof(attributeDescriptor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (codeGeneratorContext == null) { throw new ArgumentNullException(nameof(codeGeneratorContext)); } if (renderAttributeValue == null) { throw new ArgumentNullException(nameof(renderAttributeValue)); } if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal)) { writer .WriteStartInstanceMethodInvocation(_context.ModelExpressionProviderPropertyName, _context.CreateModelExpressionMethodName) .Write(_context.ViewDataPropertyName) .WriteParameterSeparator() .Write(ModelLambdaVariableName) .Write(" => "); if (!complexValue) { writer .Write(ModelLambdaVariableName) .Write("."); } renderAttributeValue(writer); writer.WriteEndMethodInvocation(endLine: false); } else { base.RenderAttributeValue( attributeDescriptor, writer, codeGeneratorContext, renderAttributeValue, complexValue); } }
public override CodeGenerator DecorateCodeGenerator(CodeGenerator incomingBuilder, CodeGeneratorContext context) { if (incomingBuilder is CodeGenTestCodeGenerator) { return incomingBuilder; } else { return new CodeGenTestCodeGenerator(context); } }
public MvcCSharpCodeVisitor( CSharpCodeWriter writer, CodeGeneratorContext context) : base(writer, context) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } }
public void ChunkTreeWithUsings() { var syntaxTreeNode = new Mock<Span>(new SpanBuilder()); var language = new CSharpRazorCodeLanguage(); var host = new CodeGenTestHost(language); var codeGeneratorContext = new CodeGeneratorContext( host, "TestClass", "TestNamespace", "Foo.cs", shouldGenerateLinePragmas: false, errorSink: new ErrorSink()); codeGeneratorContext.ChunkTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode.Object); codeGeneratorContext.ChunkTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode.Object); var codeGenerator = new CodeGenTestCodeGenerator(codeGeneratorContext); var testFile = TestFile.Create("TestFiles/CodeGenerator/Output/CSharpCodeGenerator.cs"); string expectedOutput; #if GENERATE_BASELINES if (testFile.Exists()) { expectedOutput = testFile.ReadAllText(); } else { expectedOutput = null; } #else expectedOutput = testFile.ReadAllText(); #endif // Act var result = codeGenerator.Generate(); // Assert #if GENERATE_BASELINES // Update baseline files if files do not already match. if (!string.Equals(expectedOutput, result.Code, StringComparison.Ordinal)) { BaselineWriter.WriteBaseline( @"test\Microsoft.AspNetCore.Razor.Test\TestFiles\CodeGenerator\Output\CSharpCodeGenerator.cs", result.Code); } #else Assert.Equal(expectedOutput, result.Code); #endif }
public void CreateCodeGenerator_ReturnsNewCSharpCodeGenerator() { // Arrange var language = new CSharpRazorCodeLanguage(); var host = new RazorEngineHost(language); var codeGeneratorContext = new CodeGeneratorContext( host, "myclass", "myns", string.Empty, shouldGenerateLinePragmas: false, errorSink: new ErrorSink()); // Act var generator = language.CreateCodeGenerator(codeGeneratorContext); // Assert Assert.IsType<CSharpCodeGenerator>(generator); }
public void RenderAttributeValue_RendersModelExpressionsCorrectly( string modelExpressionType, string propertyType, string expectedValue) { // Arrange var renderer = new MvcTagHelperAttributeValueCodeRenderer( new GeneratedTagHelperAttributeContext { ModelExpressionTypeName = modelExpressionType, CreateModelExpressionMethodName = "SomeMethod", ModelExpressionProviderPropertyName = "Provider", ViewDataPropertyName = "ViewData" }); var attributeDescriptor = new TagHelperAttributeDescriptor { Name = "MyAttribute", PropertyName = "SomeProperty", TypeName = propertyType, }; var writer = new CSharpCodeWriter(); var generatorContext = new ChunkGeneratorContext( host: null, className: string.Empty, rootNamespace: string.Empty, sourceFile: string.Empty, shouldGenerateLinePragmas: true); var errorSink = new ErrorSink(); var context = new CodeGeneratorContext(generatorContext, errorSink); // Act renderer.RenderAttributeValue(attributeDescriptor, writer, context, (codeWriter) => { codeWriter.Write("MyValue"); }, complexValue: false); // Assert Assert.Equal(expectedValue, writer.GenerateCode()); }
public InjectChunkVisitor( CSharpCodeWriter writer, CodeGeneratorContext context, string injectAttributeName) : base(writer, context) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (injectAttributeName == null) { throw new ArgumentNullException(nameof(injectAttributeName)); } _injectAttribute = "[" + injectAttributeName + "]"; }
public CSharpCodeGenerator(CodeGeneratorContext context) : base(context) { }
protected internal override CodeGenerator CreateCodeGenerator(CodeGeneratorContext context) { return Host.DecorateCodeGenerator(new TestCSharpCodeGenerator(context), context); }
public TestCSharpCodeGenerator(CodeGeneratorContext context) : base(context) { }
public CodeGenTestCodeGenerator(CodeGeneratorContext context) : base(context) { }
public TestCSharpCodeGenerator( CodeGeneratorContext context, string defaultModel, string activateAttribute, GeneratedTagHelperAttributeContext tagHelperAttributeContext) : base(context, defaultModel, activateAttribute, tagHelperAttributeContext) { _tagHelperAttributeContext = tagHelperAttributeContext; }
/// <inheritdoc /> public override CodeGenerator DecorateCodeGenerator( CodeGenerator incomingGenerator, CodeGeneratorContext context) { if (incomingGenerator == null) { throw new ArgumentNullException(nameof(incomingGenerator)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } var inheritedChunkTrees = GetInheritedChunkTrees(context.SourceFile); ChunkInheritanceUtility.MergeInheritedChunkTrees( context.ChunkTreeBuilder.Root, inheritedChunkTrees, DefaultModel); return new MvcCSharpCodeGenerator( context, DefaultModel, InjectAttribute, new GeneratedTagHelperAttributeContext { ModelExpressionTypeName = ModelExpressionType, CreateModelExpressionMethodName = CreateModelExpressionMethod, ModelExpressionProviderPropertyName = ModelExpressionProviderProperty, ViewDataPropertyName = ViewDataProperty, }); }
public TagHelperAttributeCodeVisitor( CSharpCodeWriter writer, CodeGeneratorContext context) : base(writer, context) { }
public override CodeGenerator DecorateCodeGenerator( CodeGenerator incomingBuilder, CodeGeneratorContext context) { base.DecorateCodeGenerator(incomingBuilder, context); return new TestCSharpCodeGenerator( context, DefaultModel, "Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute", new GeneratedTagHelperAttributeContext { ModelExpressionTypeName = ModelExpressionType, CreateModelExpressionMethodName = CreateModelExpressionMethod }); }
protected override CSharpDesignTimeCodeVisitor CreateCSharpDesignTimeCodeVisitor( CSharpCodeVisitor csharpCodeVisitor, CSharpCodeWriter writer, CodeGeneratorContext context) { if (csharpCodeVisitor == null) { throw new ArgumentNullException(nameof(csharpCodeVisitor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } return new MvcCSharpDesignTimeCodeVisitor(csharpCodeVisitor, writer, context); }
public abstract CodeGenerator CreateCodeGenerator(CodeGeneratorContext chunkGeneratorContext);
protected internal virtual GeneratorResults GenerateCodeCore( ITextDocument input, string className, string rootNamespace, string sourceFileName, string checksum, CancellationToken? cancelToken) { if (input == null) { throw new ArgumentNullException(nameof(input)); } className = (className ?? Host.DefaultClassName) ?? DefaultClassName; rootNamespace = (rootNamespace ?? Host.DefaultNamespace) ?? DefaultNamespace; // Run the parser var parser = CreateParser(sourceFileName); Debug.Assert(parser != null); var results = parser.Parse(input); // Generate code var chunkGenerator = CreateChunkGenerator(className, rootNamespace, sourceFileName); chunkGenerator.DesignTimeMode = Host.DesignTimeMode; chunkGenerator.Visit(results); var codeGeneratorContext = new CodeGeneratorContext(chunkGenerator.Context, results.ErrorSink); codeGeneratorContext.Checksum = checksum; var codeGenerator = CreateCodeGenerator(codeGeneratorContext); var codeGeneratorResult = codeGenerator.Generate(); // Collect results and return return new GeneratorResults(results, codeGeneratorResult, codeGeneratorContext.ChunkTreeBuilder.Root); }
public void DecorateCodeGenerator_DesignTimeRazorPathNormalizer_NormalizesChunkInheritanceUtilityPaths( string rootPrefix) { // Arrange var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/"; var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml"; var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider()); using (var host = new MvcRazorHost( chunkTreeCache, pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath))) { var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache); var codeGeneratorContext = new CodeGeneratorContext( new ChunkGeneratorContext( host, host.DefaultClassName, host.DefaultNamespace, rootedFilePath, shouldGenerateLinePragmas: true), new ErrorSink()); var codeGenerator = new CSharpCodeGenerator(codeGeneratorContext); host.ChunkInheritanceUtility = chunkInheritanceUtility; // Act host.DecorateCodeGenerator(codeGenerator, codeGeneratorContext); // Assert Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); } }
/// <summary> /// Gets an instance of the code generator and is provided an opportunity to decorate or replace it /// </summary> /// <param name="incomingBuilder">The <see cref="CodeGenerator"/>.</param> /// <param name="context">The <see cref="CodeGeneratorContext"/>.</param> /// <returns>Either the same code generator, after modifications, or a different code generator.</returns> public virtual CodeGenerator DecorateCodeGenerator( CodeGenerator incomingBuilder, CodeGeneratorContext context) { if (incomingBuilder == null) { throw new ArgumentNullException(nameof(incomingBuilder)); } return incomingBuilder; }
public NoUniqueIdsTagHelperCodeRenderer(IChunkVisitor bodyVisitor, CSharpCodeWriter writer, CodeGeneratorContext context) : base(bodyVisitor, writer, context) { }
public CSharpLiteralCodeVisitor( CSharpTagHelperCodeRenderer tagHelperRenderer, CSharpCodeWriter writer, CodeGeneratorContext context) : base(writer, context) { // Ensure that no matter how this class is used, we don't create numerous CSharpTagHelperCodeRenderer // instances. TagHelperRenderer = tagHelperRenderer; }
public override CodeGenerator CreateCodeGenerator(CodeGeneratorContext chunkGeneratorContext) { return new CSharpCodeGenerator(chunkGeneratorContext); }
public void CreateCodeGenerator_PassesChunkGeneratorThroughDecorateMethodOnHost() { // Arrange var mockHost = new Mock<RazorEngineHost>(new CSharpRazorCodeLanguage()) { CallBase = true }; var codeGeneratorContext = new CodeGeneratorContext( mockHost.Object, "different-class", "different-ns", string.Empty, shouldGenerateLinePragmas: true, errorSink: new ErrorSink()); var expected = new CSharpCodeGenerator(codeGeneratorContext); mockHost .Setup(h => h.DecorateCodeGenerator(It.IsAny<CSharpCodeGenerator>(), codeGeneratorContext)) .Returns(expected); var engine = new RazorTemplateEngine(mockHost.Object); // Act var actual = engine.CreateCodeGenerator(codeGeneratorContext); // Assert Assert.Equal(expected, actual); }
protected internal virtual CodeGenerator CreateCodeGenerator(CodeGeneratorContext context) { return Host.DecorateCodeGenerator(Host.CodeLanguage.CreateCodeGenerator(context), context); }
protected override CSharpCodeVisitor CreateCSharpCodeVisitor( CSharpCodeWriter writer, CodeGeneratorContext context) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } var csharpCodeVisitor = base.CreateCSharpCodeVisitor(writer, context); csharpCodeVisitor.TagHelperRenderer.AttributeValueCodeRenderer = new MvcTagHelperAttributeValueCodeRenderer(_tagHelperAttributeContext); return csharpCodeVisitor; }