Skip to content

Add option to RazorPageGenerator for #line preprocessor directives #27765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/Middleware/tools/RazorPageGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public static int Main(string[] args)
{
Console.WriteLine("Invalid argument(s).");
Console.WriteLine(@"Usage:
dotnet razorpagegenerator <root-namespace-of-views> [path]
dotnet razorpagegenerator <root-namespace-of-views> [directory path [#line path prefix]]
Examples:
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews
- processes all views in ""Views"" subfolders of the current directory
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews c:\project
- processes all views in ""Views"" subfolders of c:\project directory
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews
- process all views in ""Views"" subfolders of the current directory; use filename in #line directives
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews c:\project
- process all views in ""Views"" subfolders of c:\project directory; use filename in #line directives
dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews c:\project ../Views/
- process all views in ""Views"" subfolders of c:\project directory; use ""../Views/{filename}"" in line directives
");

return 1;
Expand All @@ -33,7 +35,9 @@ dotnet razorpagegenerator Microsoft.AspNetCore.Diagnostics.RazorViews
var rootNamespace = args[0];
var targetProjectDirectory = args.Length > 1 ? args[1] : Directory.GetCurrentDirectory();
var projectEngine = CreateProjectEngine(rootNamespace, targetProjectDirectory);
var results = MainCore(projectEngine, targetProjectDirectory);

var physicalPathPrefix = args.Length > 2 ? args[2] : string.Empty;
var results = MainCore(projectEngine, targetProjectDirectory, physicalPathPrefix);

foreach (var result in results)
{
Expand Down Expand Up @@ -79,7 +83,10 @@ @using System.Threading.Tasks
return projectEngine;
}

public static IList<RazorPageGeneratorResult> MainCore(RazorProjectEngine projectEngine, string targetProjectDirectory)
public static IList<RazorPageGeneratorResult> MainCore(
RazorProjectEngine projectEngine,
string targetProjectDirectory,
string physicalPathPrefix)
{
var viewDirectories = Directory.EnumerateDirectories(targetProjectDirectory, "Views", SearchOption.AllDirectories);
var fileCount = 0;
Expand All @@ -94,14 +101,14 @@ public static IList<RazorPageGeneratorResult> MainCore(RazorProjectEngine projec

if (!cshtmlFiles.Any())
{
Console.WriteLine(" No .cshtml files were found.");
Console.WriteLine(" No .cshtml or .razor files were found.");
continue;
}

foreach (var item in cshtmlFiles)
{
Console.WriteLine(" Generating code file for view {0}...", item.FileName);
results.Add(GenerateCodeFile(projectEngine, item));
results.Add(GenerateCodeFile(projectEngine, item, physicalPathPrefix));
Console.WriteLine(" Done!");
fileCount++;
}
Expand All @@ -110,9 +117,12 @@ public static IList<RazorPageGeneratorResult> MainCore(RazorProjectEngine projec
return results;
}

private static RazorPageGeneratorResult GenerateCodeFile(RazorProjectEngine projectEngine, RazorProjectItem projectItem)
private static RazorPageGeneratorResult GenerateCodeFile(
RazorProjectEngine projectEngine,
RazorProjectItem projectItem,
string physicalPathPrefix)
{
var projectItemWrapper = new FileSystemRazorProjectItemWrapper(projectItem);
var projectItemWrapper = new FileSystemRazorProjectItemWrapper(projectItem, physicalPathPrefix);
var codeDocument = projectEngine.Process(projectItemWrapper);
var cSharpDocument = codeDocument.GetCSharpDocument();
if (cSharpDocument.Diagnostics.Any())
Expand Down Expand Up @@ -163,17 +173,19 @@ private class FileSystemRazorProjectItemWrapper : RazorProjectItem
{
private readonly RazorProjectItem _source;

public FileSystemRazorProjectItemWrapper(RazorProjectItem item)
public FileSystemRazorProjectItemWrapper(RazorProjectItem item, string physicalPathPrefix)
{
_source = item;

// Mask the full name since we don't want a developer's local file paths to be committed.
PhysicalPath = $"{physicalPathPrefix}{_source.FileName}";
}

public override string BasePath => _source.BasePath;

public override string FilePath => _source.FilePath;

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

public override bool Exists => _source.Exists;

Expand Down Expand Up @@ -213,4 +225,4 @@ private string ProcessFileIncludes()
}
}
}
}
}
Loading