Skip to content

Commit d546861

Browse files
authored
[release/5.0] Add option to RazorPageGenerator for #line preprocessor directives (#27765) (#29024)
- cherry-picked from 1a241fc in 'master' branch - avoid new CS1504 errors because compiler can't find the ErrorPage.cshtml file - we override Arcade's `$(MicrosoftCodeAnalysisCSharpVersion)` - use new option in GeneratePage.ps1 script - script also did not know `RazorSyntaxGenerator` had moved to this repo - reflect latest `RazorSyntaxGenerator` processing - run GeneratePage.ps1 - update README.md nits: - quote all paths in GeneratePage.ps1 script - remove passive voice in README.md
1 parent 21e56a8 commit d546861

File tree

4 files changed

+137
-122
lines changed

4 files changed

+137
-122
lines changed

src/Middleware/tools/RazorPageGenerator/Program.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public static int Main(string[] args)
1919
{
2020
Console.WriteLine("Invalid argument(s).");
2121
Console.WriteLine(@"Usage:
22-
dotnet razorpagegenerator <root-namespace-of-views> [path]
22+
dotnet razorpagegenerator <root-namespace-of-views> [directory path [#line path prefix]]
2323
Examples:
24-
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews
25-
- processes all views in ""Views"" subfolders of the current directory
26-
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews c:\project
27-
- processes all views in ""Views"" subfolders of c:\project directory
24+
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews
25+
- process all views in ""Views"" subfolders of the current directory; use filename in #line directives
26+
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews c:\project
27+
- process all views in ""Views"" subfolders of c:\project directory; use filename in #line directives
28+
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews c:\project ../Views/
29+
- process all views in ""Views"" subfolders of c:\project directory; use ""../Views/{filename}"" in line directives
2830
");
2931

3032
return 1;
@@ -33,7 +35,9 @@ dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews
3335
var rootNamespace = args[0];
3436
var targetProjectDirectory = args.Length > 1 ? args[1] : Directory.GetCurrentDirectory();
3537
var projectEngine = CreateProjectEngine(rootNamespace, targetProjectDirectory);
36-
var results = MainCore(projectEngine, targetProjectDirectory);
38+
39+
var physicalPathPrefix = args.Length > 2 ? args[2] : string.Empty;
40+
var results = MainCore(projectEngine, targetProjectDirectory, physicalPathPrefix);
3741

3842
foreach (var result in results)
3943
{
@@ -79,7 +83,10 @@ @using System.Threading.Tasks
7983
return projectEngine;
8084
}
8185

82-
public static IList<RazorPageGeneratorResult> MainCore(RazorProjectEngine projectEngine, string targetProjectDirectory)
86+
public static IList<RazorPageGeneratorResult> MainCore(
87+
RazorProjectEngine projectEngine,
88+
string targetProjectDirectory,
89+
string physicalPathPrefix)
8390
{
8491
var viewDirectories = Directory.EnumerateDirectories(targetProjectDirectory, "Views", SearchOption.AllDirectories);
8592
var fileCount = 0;
@@ -94,14 +101,14 @@ public static IList<RazorPageGeneratorResult> MainCore(RazorProjectEngine projec
94101

95102
if (!cshtmlFiles.Any())
96103
{
97-
Console.WriteLine(" No .cshtml files were found.");
104+
Console.WriteLine(" No .cshtml or .razor files were found.");
98105
continue;
99106
}
100107

101108
foreach (var item in cshtmlFiles)
102109
{
103110
Console.WriteLine(" Generating code file for view {0}...", item.FileName);
104-
results.Add(GenerateCodeFile(projectEngine, item));
111+
results.Add(GenerateCodeFile(projectEngine, item, physicalPathPrefix));
105112
Console.WriteLine(" Done!");
106113
fileCount++;
107114
}
@@ -110,9 +117,12 @@ public static IList<RazorPageGeneratorResult> MainCore(RazorProjectEngine projec
110117
return results;
111118
}
112119

113-
private static RazorPageGeneratorResult GenerateCodeFile(RazorProjectEngine projectEngine, RazorProjectItem projectItem)
120+
private static RazorPageGeneratorResult GenerateCodeFile(
121+
RazorProjectEngine projectEngine,
122+
RazorProjectItem projectItem,
123+
string physicalPathPrefix)
114124
{
115-
var projectItemWrapper = new FileSystemRazorProjectItemWrapper(projectItem);
125+
var projectItemWrapper = new FileSystemRazorProjectItemWrapper(projectItem, physicalPathPrefix);
116126
var codeDocument = projectEngine.Process(projectItemWrapper);
117127
var cSharpDocument = codeDocument.GetCSharpDocument();
118128
if (cSharpDocument.Diagnostics.Any())
@@ -163,17 +173,19 @@ private class FileSystemRazorProjectItemWrapper : RazorProjectItem
163173
{
164174
private readonly RazorProjectItem _source;
165175

166-
public FileSystemRazorProjectItemWrapper(RazorProjectItem item)
176+
public FileSystemRazorProjectItemWrapper(RazorProjectItem item, string physicalPathPrefix)
167177
{
168178
_source = item;
179+
180+
// Mask the full name since we don't want a developer's local file paths to be committed.
181+
PhysicalPath = $"{physicalPathPrefix}{_source.FileName}";
169182
}
170183

171184
public override string BasePath => _source.BasePath;
172185

173186
public override string FilePath => _source.FilePath;
174187

175-
// Mask the full name since we don't want a developer's local file paths to be commited.
176-
public override string PhysicalPath => _source.FileName;
188+
public override string PhysicalPath { get; }
177189

178190
public override bool Exists => _source.Exists;
179191

@@ -213,4 +225,4 @@ private string ProcessFileIncludes()
213225
}
214226
}
215227
}
216-
}
228+
}

0 commit comments

Comments
 (0)