forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRazorCodeGenerator.cs
More file actions
104 lines (88 loc) · 3.42 KB
/
Copy pathRazorCodeGenerator.cs
File metadata and controls
104 lines (88 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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.CodeDom;
using System.Linq;
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;
using Microsoft.Internal.Web.Utils;
namespace System.Web.Razor.Generator
{
public abstract class RazorCodeGenerator : ParserVisitor
{
private CodeGeneratorContext _context;
protected RazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host)
{
if (String.IsNullOrEmpty(className))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "className");
}
if (rootNamespaceName == null)
{
throw new ArgumentNullException("rootNamespaceName");
}
if (host == null)
{
throw new ArgumentNullException("host");
}
ClassName = className;
RootNamespaceName = rootNamespaceName;
SourceFileName = sourceFileName;
GenerateLinePragmas = String.IsNullOrEmpty(SourceFileName) ? false : true;
Host = host;
}
// Data pulled from constructor
public string ClassName { get; private set; }
public string RootNamespaceName { get; private set; }
public string SourceFileName { get; private set; }
public RazorEngineHost Host { get; private set; }
// Generation settings
public bool GenerateLinePragmas { get; set; }
public bool DesignTimeMode { get; set; }
public CodeGeneratorContext Context
{
get
{
EnsureContextInitialized();
return _context;
}
}
internal virtual Func<CodeWriter> CodeWriterFactory
{
get { return null; }
}
public override void VisitStartBlock(Block block)
{
block.CodeGenerator.GenerateStartBlockCode(block, Context);
}
public override void VisitEndBlock(Block block)
{
block.CodeGenerator.GenerateEndBlockCode(block, Context);
}
public override void VisitSpan(Span span)
{
span.CodeGenerator.GenerateCode(span, Context);
}
public override void OnComplete()
{
Context.FlushBufferedStatement();
}
private void EnsureContextInitialized()
{
if (_context == null)
{
_context = CodeGeneratorContext.Create(Host, CodeWriterFactory, ClassName, RootNamespaceName, SourceFileName, GenerateLinePragmas);
Initialize(_context);
}
}
protected virtual void Initialize(CodeGeneratorContext context)
{
context.Namespace.Imports.AddRange(Host.NamespaceImports.Select(s => new CodeNamespaceImport(s)).ToArray());
if (!String.IsNullOrEmpty(Host.DefaultBaseClass))
{
context.GeneratedClass.BaseTypes.Add(new CodeTypeReference(Host.DefaultBaseClass));
}
// Dev10 Bug 937438: Generate explicit Parameter-less constructor on Razor generated class
context.GeneratedClass.Members.Add(new CodeConstructor() { Attributes = MemberAttributes.Public });
}
}
}