forked from aspnet/Mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMvcCSharpCodeGenerator.cs
142 lines (117 loc) · 4.78 KB
/
MvcCSharpCodeGenerator.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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.IO;
using Microsoft.AspNetCore.Mvc.Razor.Directives;
using Microsoft.AspNetCore.Mvc.Razor.Host;
using Microsoft.AspNetCore.Razor.Chunks;
using Microsoft.AspNetCore.Razor.CodeGenerators;
using Microsoft.AspNetCore.Razor.CodeGenerators.Visitors;
namespace Microsoft.AspNetCore.Mvc.Razor
{
public class MvcCSharpCodeGenerator : CSharpCodeGenerator
{
private readonly GeneratedTagHelperAttributeContext _tagHelperAttributeContext;
private readonly string _defaultModel;
private readonly string _injectAttribute;
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;
}
protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
{
if (Context.Host.DesignTimeMode &&
string.Equals(
Path.GetFileName(Context.SourceFile),
ViewHierarchyUtility.ViewImportsFileName,
StringComparison.OrdinalIgnoreCase))
{
// Write a using TModel = System.Object; token during design time to make intellisense work
writer.WriteLine($"using {ChunkHelper.TModelToken} = {typeof(object).FullName};");
}
return base.BuildClassDeclaration(writer);
}
protected override void BuildAfterExecuteContent(CSharpCodeWriter writer, IList<Chunk> chunks)
{
new ViewComponentTagHelperChunkVisitor(writer, Context).Accept(chunks);
}
protected override void DecorateChunks(CodeGeneratorContext context, IList<Chunk> chunks)
{
var descriptorDecorator = new TagHelperChunkVisitor(context);
descriptorDecorator.Accept(chunks);
}
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;
}
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);
}
protected override void BuildConstructor(CSharpCodeWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
base.BuildConstructor(writer);
writer.WriteLineHiddenDirective();
var injectVisitor = new InjectChunkVisitor(writer, Context, _injectAttribute);
injectVisitor.Accept(Context.ChunkTreeBuilder.Root.Children);
writer.WriteLine();
writer.WriteLineHiddenDirective();
}
}
}