internal RazorProjectEngine CreateProjectEngine(RazorConfiguration configuration, MetadataReference[] references)
        {
            return(RazorProjectEngine.Create(configuration, FileSystem, b =>
            {
                // Turn off checksums, we're testing code generation.
                b.Features.Add(new SuppressChecksum());

                BlazorExtensionInitializer.Register(b);

                b.Features.Add(new CompilationTagHelperFeature());
                b.Features.Add(new DefaultMetadataReferenceFeature()
                {
                    References = references,
                });
            }));
        }
Example #2
0
        public async Task <Type> CompileBlazor(string code)
        {
            CompileLog.Add("Create fileSystem");

            var fileSystem = new EmptyRazorProjectFileSystem();

            CompileLog.Add("Create engine");
            var engine = RazorProjectEngine.Create(BlazorExtensionInitializer.DefaultConfiguration, fileSystem, b =>
            {
                BlazorExtensionInitializer.Register(b);
            });


            CompileLog.Add("Create file");
            var file = new MemoryRazorProjectItem(code, true, "/App", "/App/App.cshtml");

            CompileLog.Add("File process and GetCSharpDocument");
            var doc = engine.Process(file).GetCSharpDocument();

            CompileLog.Add("Get GeneratedCode");
            var csCode = doc.GeneratedCode;

            CompileLog.Add("Read Diagnostics");
            foreach (var diagnostic in doc.Diagnostics)
            {
                CompileLog.Add(diagnostic.ToString());
            }

            if (doc.Diagnostics.Any(i => i.Severity == RazorDiagnosticSeverity.Error))
            {
                return(null);
            }

            CompileLog.Add(csCode);

            CompileLog.Add("Compile assembly");
            var assembly = await Compile(csCode);

            if (assembly != null)
            {
                CompileLog.Add("Search Blazor component");
                return(assembly.GetExportedTypes().FirstOrDefault(i => i.IsSubclassOf(typeof(BlazorComponent))));
            }

            return(null);
        }