private string GetGeneratorResult(IEnumerable <string> namespaces, TypeContext context) { #pragma warning disable 612, 618 var razorEngine = RazorEngine.Create(builder => { builder .SetNamespace(DynamicTemplateNamespace) //.SetBaseType("Microsoft.Extensions.RazorViews.BaseView") .SetBaseType(BuildTypeName(context.TemplateType, context.ModelType)) .ConfigureClass((document, @class) => { @class.ClassName = context.ClassName; //if (!str ing.IsNullOrWhiteSpace(document.Source.FilePath)) //{ // @class.ClassName = Path.GetFileNameWithoutExtension(document.Source.FilePath); //} @class.Modifiers.Clear(); @class.Modifiers.Add("internal"); }); builder.Features.Add(new SuppressChecksumOptionsFeature()); context.ConfigureCompilerBuilder?.Invoke(builder); }); string importString = @" @using System @using System.Threading.Tasks "; importString += String.Join("\r\n", namespaces.Select(n => "@using " + n.Trim())) + "\r\n"; using (var reader = context.TemplateContent.GetTemplateReader()) { string path = null; if (string.IsNullOrWhiteSpace(context.TemplateContent.TemplateFile)) { path = Directory.GetCurrentDirectory(); } else { path = Path.GetDirectoryName(context.TemplateContent.TemplateFile); } var razorProject = RazorProjectFileSystem.Create(path); var templateEngine = new Templating.RazorTemplateEngine(razorEngine, razorProject); templateEngine.Options.DefaultImports = RazorSourceDocument.Create(importString, fileName: null); RazorPageGeneratorResult result; if (string.IsNullOrWhiteSpace(context.TemplateContent.TemplateFile)) { var item = RazorSourceDocument.Create(context.TemplateContent.Template, string.Empty); var imports = new List <RazorSourceDocument>(); imports.Add(templateEngine.Options.DefaultImports); var doc = RazorCodeDocument.Create(item, imports); result = GenerateCodeFile(templateEngine, doc); } else { var item = razorProject.GetItem(context.TemplateContent.TemplateFile); result = GenerateCodeFile(templateEngine, item); } return(InspectSource(result, context)); } }
private static RazorPageGeneratorResult GenerateCodeFile(Templating.RazorTemplateEngine templateEngine, RazorCodeDocument document) { var cSharpDocument = templateEngine.GenerateCode(document); if (cSharpDocument.Diagnostics.Any()) { var diagnostics = string.Join(Environment.NewLine, cSharpDocument.Diagnostics); Console.WriteLine($"One or more parse errors encountered. This will not prevent the generator from continuing: {Environment.NewLine}{diagnostics}."); } return(new RazorPageGeneratorResult { FilePath = null, GeneratedCode = cSharpDocument.GeneratedCode, }); }
private static RazorPageGeneratorResult GenerateCodeFile(Templating.RazorTemplateEngine templateEngine, RazorProjectItem projectItem) { var projectItemWrapper = new FileSystemRazorProjectItemWrapper(projectItem); var cSharpDocument = templateEngine.GenerateCode(projectItemWrapper); if (cSharpDocument.Diagnostics.Any()) { var diagnostics = string.Join(Environment.NewLine, cSharpDocument.Diagnostics); Console.WriteLine($"One or more parse errors encountered. This will not prevent the generator from continuing: {Environment.NewLine}{diagnostics}."); } var generatedCodeFilePath = Path.ChangeExtension(projectItem.PhysicalPath, ".Designer.cs"); return(new RazorPageGeneratorResult { FilePath = generatedCodeFilePath, GeneratedCode = cSharpDocument.GeneratedCode, }); }