Exemple #1
0
        public async Task <Type> CompileSourceCode(string code)
        {
            Console.WriteLine("Compile assembly");
            var assembly = await Compile(code);

            if (assembly != null)
            {
                CompileLog.Add("Searching for first exported type.");
                return(assembly.GetExportedTypes().FirstOrDefault());
            }
            return(null);
        }
        public async Task <Assembly> Compile(string code)
        {
            await Init();

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Preview));

            foreach (var diagnostic in syntaxTree.GetDiagnostics())
            {
                CompileLog.Add(diagnostic.ToString());
            }

            if (syntaxTree.GetDiagnostics().Any(i => i.Severity == DiagnosticSeverity.Error))
            {
                CompileLog.Add("Parse SyntaxTree Error!");
                return(null);
            }

            CompileLog.Add("Parse SyntaxTree Success");

            CSharpCompilation compilation = CSharpCompilation.Create("CompileBlazorInBlazor.Demo", new[] { syntaxTree },
                                                                     references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (MemoryStream stream = new MemoryStream())
            {
                EmitResult result = compilation.Emit(stream);

                foreach (var diagnostic in result.Diagnostics)
                {
                    CompileLog.Add(diagnostic.ToString());
                }

                if (!result.Success)
                {
                    CompileLog.Add("Compilation error");
                    return(null);
                }

                CompileLog.Add("Compilation success!");

                stream.Seek(0, SeekOrigin.Begin);

//                var context = new CollectibleAssemblyLoadContext();
                Assembly assemby = AppDomain.CurrentDomain.Load(stream.ToArray());
                return(assemby);
            }

            return(null);
        }
        public async Task <Type> CompileBlazor(string code)
        {
            CompileLog.Add("Create fileSystem");

            var fileSystem = new EmptyRazorProjectFileSystem();

            CompileLog.Add("Create engine");
//            Microsoft.AspNetCore.Blazor.Build.
            var engine = RazorProjectEngine.Create(RazorConfiguration.Create(RazorLanguageVersion.Version_3_0, "Blazor", new RazorExtension[0]), fileSystem, b =>
            {
                //BlazorExtensionInitializer.Register(b);
            });


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

            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(ComponentBase))));
            }

            return(null);
        }
Exemple #4
0
        public async Task <Assembly> Compile(string code)
        {
            await Init();

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Latest));

            foreach (var diagnostic in syntaxTree.GetDiagnostics())
            {
                CompileLog.Add(diagnostic.ToString());
            }

            if (syntaxTree.GetDiagnostics().Any(i => i.Severity == DiagnosticSeverity.Error))
            {
                CompileLog.Add("Parse SyntaxTree Error!");
                return(null);
            }

            CompileLog.Add("Parse SyntaxTree Success");

            //https://stackoverflow.com/questions/35711461/how-to-get-roslyn-to-compile-when-referenced-assemblies-have-references-to-both
            var op = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            op = op.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);
            CSharpCompilation compilation = CSharpCompilation.Create("WasmRoslyn.Demo", new[] { syntaxTree },
                                                                     references, op);

            using (MemoryStream stream = new MemoryStream())
            {
                EmitResult result = compilation.Emit(stream);

                foreach (var diagnostic in result.Diagnostics)
                {
                    CompileLog.Add(diagnostic.ToString());
                }

                if (!result.Success)
                {
                    CompileLog.Add("Compilation error");
                    return(null);
                }

                CompileLog.Add("Compilation success!");
                Assembly assemby = AppDomain.CurrentDomain.Load(stream.ToArray());
                return(assemby);
            }
        }
Exemple #5
0
        public async Task <Type> CompileBlazor(string code)
        {
            CompileLog.Add("Create fileSystem");

            var fileSystem = new EmptyRazorProjectFileSystem();

            CompileLog.Add("Create engine");
            //            Microsoft.AspNetCore.Blazor.Build.

            var engine = RazorProjectEngine.Create(RazorConfiguration.Create(RazorLanguageVersion.Version_3_0, "Blazor", new RazorExtension[0]), fileSystem, b =>
            {
                //                RazorExtensions.Register(b);


//                b.SetRootNamespace(DefaultRootNamespace);

                // Turn off checksums, we're testing code generation.
//                b.Features.Add(new SuppressChecksum());

//                if (LineEnding != null)
//                {
//                    b.Phases.Insert(0, new ForceLineEndingPhase(LineEnding));
//                }

                // Including MVC here so that we can find any issues that arise from mixed MVC + Components.
//                Microsoft.AspNetCore.Mvc.Razor.Extensions.RazorExtensions.Register(b);
//
//                // Features that use Roslyn are mandatory for components
//                Microsoft.CodeAnalysis.Razor.CompilerFeatures.Register(b);
//
//                b.Features.Add(new CompilationTagHelperFeature());
//                b.Features.Add(new DefaultMetadataReferenceFeature()
//                {
//                    References = references,
//                });
            });


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

            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(ComponentBase))));
            }

            return(null);
        }