Example #1
0
        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());
            });

            string importString = @"
@using System
@using System.Threading.Tasks
";
            importString += String.Join("\r\n", namespaces.Select(n => "@using " + n.Trim())) + "\r\n";
            var result = new RazorPageGeneratorResult();
            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);
                if (string.IsNullOrWhiteSpace(context.TemplateContent.TemplateFile))
                {
                    var razorSource  = RazorSourceDocument.Create(context.TemplateContent.Template, string.Empty);
                    var codeDocument = RazorCodeDocument.Create(razorSource);
                    razorEngine.Process(codeDocument);

                    var cSharpDocument = codeDocument.GetCSharpDocument();
                    var generatedCode  = cSharpDocument.GeneratedCode;
                    var projectItem    = razorProject.EnumerateItems(path);
                    result = GenerateCodeFile(generatedCode, projectItem.FirstOrDefault());
                }

                return(InspectSource(result, context));
            }
        }
Example #2
0
 public virtual string InspectSource(RazorPageGeneratorResult results, TypeContext context)
 {
     return(results.GeneratedCode);
 }