Skip to content

Commit c565c2a

Browse files
author
John Luo
committed
Merge branch 'master' of ..\AspNetCore-Tooling\ into johluo/tooling-soncolidation-source-2
2 parents ff773bd + 323b4ee commit c565c2a

File tree

6,292 files changed

+345024
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,292 files changed

+345024
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Globalization;
6+
using System.Text;
7+
8+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
9+
{
10+
internal static class CSharpIdentifier
11+
{
12+
private const string CshtmlExtension = ".cshtml";
13+
14+
public static string GetClassNameFromPath(string path)
15+
{
16+
if (string.IsNullOrEmpty(path))
17+
{
18+
return path;
19+
}
20+
21+
if (path.EndsWith(CshtmlExtension, StringComparison.OrdinalIgnoreCase))
22+
{
23+
path = path.Substring(0, path.Length - CshtmlExtension.Length);
24+
}
25+
26+
return SanitizeClassName(path);
27+
}
28+
29+
// CSharp Spec §2.4.2
30+
private static bool IsIdentifierStart(char character)
31+
{
32+
return char.IsLetter(character) ||
33+
character == '_' ||
34+
CharUnicodeInfo.GetUnicodeCategory(character) == UnicodeCategory.LetterNumber;
35+
}
36+
37+
public static bool IsIdentifierPart(char character)
38+
{
39+
return char.IsDigit(character) ||
40+
IsIdentifierStart(character) ||
41+
IsIdentifierPartByUnicodeCategory(character);
42+
}
43+
44+
private static bool IsIdentifierPartByUnicodeCategory(char character)
45+
{
46+
var category = CharUnicodeInfo.GetUnicodeCategory(character);
47+
48+
return category == UnicodeCategory.NonSpacingMark || // Mn
49+
category == UnicodeCategory.SpacingCombiningMark || // Mc
50+
category == UnicodeCategory.ConnectorPunctuation || // Pc
51+
category == UnicodeCategory.Format; // Cf
52+
}
53+
54+
public static string SanitizeClassName(string inputName)
55+
{
56+
if (string.IsNullOrEmpty(inputName))
57+
{
58+
return inputName;
59+
}
60+
61+
if (!IsIdentifierStart(inputName[0]) && IsIdentifierPart(inputName[0]))
62+
{
63+
inputName = "_" + inputName;
64+
}
65+
66+
var builder = new StringBuilder(inputName.Length);
67+
for (var i = 0; i < inputName.Length; i++)
68+
{
69+
var ch = inputName[i];
70+
builder.Append(IsIdentifierPart(ch) ? ch : '_');
71+
}
72+
73+
return builder.ToString();
74+
}
75+
}
76+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Razor.Language;
6+
7+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
8+
{
9+
internal class ExtensionInitializer : RazorExtensionInitializer
10+
{
11+
public override void Initialize(RazorProjectEngineBuilder builder)
12+
{
13+
if (builder == null)
14+
{
15+
throw new ArgumentNullException(nameof(builder));
16+
}
17+
18+
if (builder.Configuration.ConfigurationName == "MVC-1.0")
19+
{
20+
RazorExtensions.Register(builder);
21+
}
22+
else if (builder.Configuration.ConfigurationName == "MVC-1.1")
23+
{
24+
RazorExtensions.Register(builder);
25+
RazorExtensions.RegisterViewComponentTagHelpers(builder);
26+
}
27+
}
28+
}
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
5+
6+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
7+
{
8+
public interface IInjectTargetExtension : ICodeTargetExtension
9+
{
10+
void WriteInjectProperty(CodeRenderingContext context, InjectIntermediateNode node);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
5+
6+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
7+
{
8+
public interface IViewComponentTagHelperTargetExtension : ICodeTargetExtension
9+
{
10+
void WriteViewComponentTagHelper(CodeRenderingContext context, ViewComponentTagHelperIntermediateNode node);
11+
}
12+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.AspNetCore.Razor.Language.Intermediate;
9+
10+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
11+
{
12+
public static class InjectDirective
13+
{
14+
public static readonly DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective(
15+
"inject",
16+
DirectiveKind.SingleLine,
17+
builder =>
18+
{
19+
builder
20+
.AddTypeToken(Resources.InjectDirective_TypeToken_Name, Resources.InjectDirective_TypeToken_Description)
21+
.AddMemberToken(Resources.InjectDirective_MemberToken_Name, Resources.InjectDirective_MemberToken_Description);
22+
23+
builder.Usage = DirectiveUsage.FileScopedMultipleOccurring;
24+
builder.Description = Resources.InjectDirective_Description;
25+
});
26+
27+
public static RazorProjectEngineBuilder Register(RazorProjectEngineBuilder builder)
28+
{
29+
if (builder == null)
30+
{
31+
throw new ArgumentNullException(nameof(builder));
32+
}
33+
34+
builder.AddDirective(Directive);
35+
builder.Features.Add(new Pass());
36+
builder.AddTargetExtension(new InjectTargetExtension());
37+
return builder;
38+
}
39+
40+
internal class Pass : IntermediateNodePassBase, IRazorDirectiveClassifierPass
41+
{
42+
// Runs after the @model and @namespace directives
43+
public override int Order => 10;
44+
45+
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
46+
{
47+
var visitor = new Visitor();
48+
visitor.Visit(documentNode);
49+
var modelType = ModelDirective.GetModelType(documentNode);
50+
51+
var properties = new HashSet<string>(StringComparer.Ordinal);
52+
53+
for (var i = visitor.Directives.Count - 1; i >= 0; i--)
54+
{
55+
var directive = visitor.Directives[i];
56+
var tokens = directive.Tokens.ToArray();
57+
if (tokens.Length < 2)
58+
{
59+
continue;
60+
}
61+
62+
var typeName = tokens[0].Content;
63+
var memberName = tokens[1].Content;
64+
65+
if (!properties.Add(memberName))
66+
{
67+
continue;
68+
}
69+
70+
typeName = typeName.Replace("<TModel>", "<" + modelType + ">");
71+
72+
var injectNode = new InjectIntermediateNode()
73+
{
74+
TypeName = typeName,
75+
MemberName = memberName,
76+
};
77+
78+
visitor.Class.Children.Add(injectNode);
79+
}
80+
}
81+
}
82+
83+
private class Visitor : IntermediateNodeWalker
84+
{
85+
public ClassDeclarationIntermediateNode Class { get; private set; }
86+
87+
public IList<DirectiveIntermediateNode> Directives { get; } = new List<DirectiveIntermediateNode>();
88+
89+
public override void VisitClassDeclaration(ClassDeclarationIntermediateNode node)
90+
{
91+
if (Class == null)
92+
{
93+
Class = node;
94+
}
95+
96+
base.VisitClassDeclaration(node);
97+
}
98+
99+
public override void VisitDirective(DirectiveIntermediateNode node)
100+
{
101+
if (node.Directive == Directive)
102+
{
103+
Directives.Add(node);
104+
}
105+
}
106+
}
107+
108+
#region Obsolete
109+
[Obsolete("This method is obsolete and will be removed in a future version.")]
110+
public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
111+
{
112+
if (builder == null)
113+
{
114+
throw new ArgumentNullException(nameof(builder));
115+
}
116+
117+
builder.AddDirective(Directive);
118+
builder.Features.Add(new Pass());
119+
builder.AddTargetExtension(new InjectTargetExtension());
120+
return builder;
121+
}
122+
#endregion
123+
}
124+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Razor.Language;
6+
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
7+
using Microsoft.AspNetCore.Razor.Language.Intermediate;
8+
9+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
10+
{
11+
public class InjectIntermediateNode : ExtensionIntermediateNode
12+
{
13+
public string TypeName { get; set; }
14+
15+
public string MemberName { get; set; }
16+
17+
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly;
18+
19+
public override void Accept(IntermediateNodeVisitor visitor)
20+
{
21+
if (visitor == null)
22+
{
23+
throw new ArgumentNullException(nameof(visitor));
24+
}
25+
26+
AcceptExtensionNode<InjectIntermediateNode>(this, visitor);
27+
}
28+
29+
public override void WriteNode(CodeTarget target, CodeRenderingContext context)
30+
{
31+
if (target == null)
32+
{
33+
throw new ArgumentNullException(nameof(target));
34+
}
35+
36+
if (context == null)
37+
{
38+
throw new ArgumentNullException(nameof(context));
39+
}
40+
41+
var extension = target.GetExtension<IInjectTargetExtension>();
42+
if (extension == null)
43+
{
44+
ReportMissingCodeTargetExtension<IInjectTargetExtension>(context);
45+
return;
46+
}
47+
48+
extension.WriteInjectProperty(context, this);
49+
}
50+
51+
public override void FormatNode(IntermediateNodeFormatter formatter)
52+
{
53+
formatter.WriteContent(MemberName);
54+
55+
formatter.WriteProperty(nameof(MemberName), MemberName);
56+
formatter.WriteProperty(nameof(TypeName), TypeName);
57+
}
58+
}
59+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
6+
7+
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
8+
{
9+
public class InjectTargetExtension : IInjectTargetExtension
10+
{
11+
private const string RazorInjectAttribute = "[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]";
12+
13+
public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediateNode node)
14+
{
15+
if (context == null)
16+
{
17+
throw new ArgumentNullException(nameof(context));
18+
}
19+
20+
if (node == null)
21+
{
22+
throw new ArgumentNullException(nameof(node));
23+
}
24+
25+
var property = $"public {node.TypeName} {node.MemberName} {{ get; private set; }}";
26+
27+
if (node.Source.HasValue)
28+
{
29+
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
30+
{
31+
context.CodeWriter
32+
.WriteLine(RazorInjectAttribute)
33+
.WriteLine(property);
34+
}
35+
}
36+
else
37+
{
38+
context.CodeWriter
39+
.WriteLine(RazorInjectAttribute)
40+
.WriteLine(property);
41+
}
42+
}
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
5+
6+
namespace Microsoft.AspNetCore.Razor.Language.Extensions
7+
{
8+
internal class LegacySectionTargetExtension : ISectionTargetExtension
9+
{
10+
private static readonly string DefaultWriterName = "__razor_section_writer";
11+
12+
public static readonly string DefaultSectionMethodName = "DefineSection";
13+
14+
public string SectionMethodName { get; set; } = DefaultSectionMethodName;
15+
16+
public void WriteSection(CodeRenderingContext context, SectionIntermediateNode node)
17+
{
18+
context.CodeWriter
19+
.WriteStartMethodInvocation(SectionMethodName)
20+
.Write("\"")
21+
.Write(node.SectionName)
22+
.Write("\", ");
23+
24+
using (context.CodeWriter.BuildAsyncLambda(DefaultWriterName))
25+
{
26+
context.RenderChildren(node);
27+
}
28+
29+
context.CodeWriter.WriteEndMethodInvocation(endLine: true);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)