Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 8d62937

Browse files
committed
Add support for 2-phase compile
1 parent f9623c2 commit 8d62937

File tree

83 files changed

+1986
-419
lines changed

Some content is hidden

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

83 files changed

+1986
-419
lines changed

src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/RazorExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public static void Register(RazorProjectEngineBuilder builder)
2020
InjectDirective.Register(builder);
2121
ModelDirective.Register(builder);
2222

23-
FunctionsDirective.Register(builder);
2423
InheritsDirective.Register(builder);
2524

2625
builder.Features.Add(new DefaultTagHelperDescriptorProvider());

src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/RazorExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public static void Register(RazorProjectEngineBuilder builder)
2222
NamespaceDirective.Register(builder);
2323
PageDirective.Register(builder);
2424

25-
FunctionsDirective.Register(builder);
2625
InheritsDirective.Register(builder);
2726
SectionDirective.Register(builder);
2827

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ExtensionInitializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.Razor.Language;
5+
using Microsoft.AspNetCore.Razor.Language.Components;
56

67
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
78
{

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectDirective.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ internal class Pass : IntermediateNodePassBase, IRazorDirectiveClassifierPass
4444

4545
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
4646
{
47+
if (documentNode.DocumentKind != RazorPageDocumentClassifierPass.RazorPageDocumentKind &&
48+
documentNode.DocumentKind != MvcViewDocumentClassifierPass.MvcViewDocumentKind)
49+
{
50+
// Not a MVC file. Skip.
51+
return;
52+
}
53+
4754
var visitor = new Visitor();
4855
visitor.Visit(documentNode);
4956
var modelType = ModelDirective.GetModelType(documentNode);

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/Microsoft.AspNetCore.Mvc.Razor.Extensions.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<Compile Include="..\Microsoft.AspNetCore.Razor.Language\CodeGeneration\CodeWriterExtensions.cs">
11-
<Link>Shared\CodeWriterExtensions.cs</Link>
12-
</Compile>
10+
<Compile Include="..\Microsoft.AspNetCore.Razor.Language\CodeGeneration\CodeWriterExtensions.cs" Link="Shared\CodeWriterExtensions.cs" />
11+
<Compile Include="..\Microsoft.AspNetCore.Razor.Language\CSharpIdentifier.cs" Link="Shared\CSharpIdentifier.cs" />
12+
<Compile Include="..\Microsoft.AspNetCore.Razor.Language\Checksum.cs" Link="Shared\Checksum.cs" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/MvcViewDocumentClassifierPass.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Text;
65
using Microsoft.AspNetCore.Razor.Language;
76
using Microsoft.AspNetCore.Razor.Language.Intermediate;
87

@@ -31,12 +30,12 @@ protected override void OnDocumentStructureCreated(
3130
{
3231
// It's possible for a Razor document to not have a file path.
3332
// Eg. When we try to generate code for an in memory document like default imports.
34-
var checksum = BytesToString(codeDocument.Source.GetChecksum());
33+
var checksum = Checksum.BytesToString(codeDocument.Source.GetChecksum());
3534
@class.ClassName = $"AspNetCore_{checksum}";
3635
}
3736
else
3837
{
39-
@class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
38+
@class.ClassName = GetClassNameFromPath(filePath);
4039
}
4140

4241
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
@@ -51,21 +50,21 @@ protected override void OnDocumentStructureCreated(
5150
method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";
5251
}
5352

54-
private static string BytesToString(byte[] bytes)
53+
private static string GetClassNameFromPath(string path)
5554
{
56-
if (bytes == null)
55+
const string cshtmlExtension = ".cshtml";
56+
57+
if (string.IsNullOrEmpty(path))
5758
{
58-
throw new ArgumentNullException(nameof(bytes));
59+
return path;
5960
}
6061

61-
var result = new StringBuilder(bytes.Length);
62-
for (var i = 0; i < bytes.Length; i++)
62+
if (path.EndsWith(cshtmlExtension, StringComparison.OrdinalIgnoreCase))
6363
{
64-
// The x2 format means lowercase hex, where each byte is a 2-character string.
65-
result.Append(bytes[i].ToString("x2"));
64+
path = path.Substring(0, path.Length - cshtmlExtension.Length);
6665
}
6766

68-
return result.ToString();
67+
return CSharpIdentifier.SanitizeIdentifier(path);
6968
}
7069
}
7170
}

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ internal static string GetNamespace(string source, DirectiveIntermediateNode dir
115115
for (var i = 0; i < segments.Length - 1; i++)
116116
{
117117
builder.Append('.');
118-
builder.Append(CSharpIdentifier.SanitizeClassName(segments[i]));
118+
builder.Append(CSharpIdentifier.SanitizeIdentifier(segments[i]));
119119
}
120120

121121
return builder.ToString();

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public static void Register(RazorProjectEngineBuilder builder)
2222
NamespaceDirective.Register(builder);
2323
PageDirective.Register(builder);
2424

25-
FunctionsDirective.Register(builder);
2625
InheritsDirective.Register(builder);
2726
SectionDirective.Register(builder);
2827

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/RazorPageDocumentClassifierPass.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Diagnostics;
6-
using System.Text;
76
using Microsoft.AspNetCore.Razor.Language;
87
using Microsoft.AspNetCore.Razor.Language.Extensions;
98
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -58,12 +57,12 @@ protected override void OnDocumentStructureCreated(
5857
{
5958
// It's possible for a Razor document to not have a file path.
6059
// Eg. When we try to generate code for an in memory document like default imports.
61-
var checksum = BytesToString(codeDocument.Source.GetChecksum());
60+
var checksum = Checksum.BytesToString(codeDocument.Source.GetChecksum());
6261
@class.ClassName = $"AspNetCore_{checksum}";
6362
}
6463
else
6564
{
66-
@class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
65+
@class.ClassName = GetClassNameFromPath(filePath);
6766
}
6867

6968
@class.Modifiers.Clear();
@@ -144,21 +143,21 @@ public void Configure(RazorParserOptionsBuilder options)
144143
}
145144
}
146145

147-
private static string BytesToString(byte[] bytes)
146+
private static string GetClassNameFromPath(string path)
148147
{
149-
if (bytes == null)
148+
const string cshtmlExtension = ".cshtml";
149+
150+
if (string.IsNullOrEmpty(path))
150151
{
151-
throw new ArgumentNullException(nameof(bytes));
152+
return path;
152153
}
153154

154-
var result = new StringBuilder(bytes.Length);
155-
for (var i = 0; i < bytes.Length; i++)
155+
if (path.EndsWith(cshtmlExtension, StringComparison.OrdinalIgnoreCase))
156156
{
157-
// The x2 format means lowercase hex, where each byte is a 2-character string.
158-
result.Append(bytes[i].ToString("x2"));
157+
path = path.Substring(0, path.Length - cshtmlExtension.Length);
159158
}
160159

161-
return result.ToString();
160+
return CSharpIdentifier.SanitizeIdentifier(path);
162161
}
163162
}
164163
}

src/Microsoft.AspNetCore.Mvc.Razor.Extensions/CSharpIdentifier.cs renamed to src/Microsoft.AspNetCore.Razor.Language/CSharpIdentifier.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Globalization;
65
using System.Text;
76

8-
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
7+
namespace Microsoft.AspNetCore.Razor.Language
98
{
109
internal static class CSharpIdentifier
1110
{
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-
2911
// CSharp Spec §2.4.2
3012
private static bool IsIdentifierStart(char character)
3113
{
@@ -51,7 +33,7 @@ private static bool IsIdentifierPartByUnicodeCategory(char character)
5133
category == UnicodeCategory.Format; // Cf
5234
}
5335

54-
public static string SanitizeClassName(string inputName)
36+
public static string SanitizeIdentifier(string inputName)
5537
{
5638
if (!IsIdentifierStart(inputName[0]) && IsIdentifierPart(inputName[0]))
5739
{

0 commit comments

Comments
 (0)