Giter VIP home page Giter VIP logo

compileblazorinblazor's People

Contributors

samprof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

compileblazorinblazor's Issues

Upgrade to 3.0.0-preview4-19216-03

Hi

I try to update this project to 3.0.0-preview4-19216-03 but I have faced this error:
var engine = RazorProjectEngine.Create(BlazorExtensionInitializer.DefaultConfiguration, fileSystem, b =>
{
BlazorExtensionInitializer.Register(b);
});

BlazorExtensionInitializer is gone!!! :-\

and I dont know how can I replace it with new version.

Issues as of 2023-06 .NET 7.0

First of all, great project and job. I'd like to share my sincere appreciation.

Secondly as of 06.2023 there are some issues with the code for .NET 7.0 (and I'm talking only about .NET 7.0, I did not test it with other versions).

  1. Basically the project doesn't work with .NET-7.0 unless changes are made,
  2. You are missing instantiation of public List CompileLog { get; set; } in lin 18 in CompileService.cs. The new() operator is needed or NullExceptions are thrown for newer .NET versions.
  3. .NET 7.0 has troubles with loading resource.dlls in Blazor (the default xxx.resources.dll is missing from output _framework directory, only localized ones are present under eg. fr or es directories, therefore an exception is thrown near this line: references.Add(MetadataReference.CreateFromStream(await this._http.GetStreamAsync(_uriHelper.BaseUri + "_framework/" + name)));)
  4. For the same line as aforementioned one, there should be "_framework/" instead of: "/_framework/_bin/"
  5. The function below seems to have some issues. The most concerning is that SyntaxTree in First part returns no diagnostic issues, and they are null, therefore the process crashes.
 public async Task<Assembly> Compile(string code)
        {
            await Init();

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Preview));
            foreach (var diagnostic in syntaxTree.GetDiagnostics()) //first place where nullexception is thrown
            {
                CompileLog.Add(diagnostic.ToString()); //another because CompileLog has no new() operator
            }

            if (syntaxTree.GetDiagnostics().Any(i => i.Severity == DiagnosticSeverity.Error)) //again null vulnerability
            {
                CompileLog.Add("Parse SyntaxTree Error!"); //again null vulnerability
                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) //first place where the diagnostics actually catch the errors
                {
                    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;
        }

I improved that function as follows:

public async Task<Assembly> Compile(string code)
        {
            await Init();
            try
            {
                SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Preview));
                var diags = syntaxTree.GetDiagnostics();
                for (int i = 0; i < diags.Count(); i++)
                {
                    var diagnostic = diags.ElementAt(i);
                    CompileLog.Add(diagnostic.ToString());
                }

                if (syntaxTree.GetDiagnostics()?.Any(i => i.Severity == DiagnosticSeverity.Error) ?? true)
                {
                    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);

                    Assembly assemby = AppDomain.CurrentDomain.Load(stream.ToArray());
                    return assemby;
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
                return null;
            }
            return null;
        }

PS Again, thanks for sharing your project. Some serious gourmet code! Great work!

PS2 This fork resolves some of the issues that I mentioned: https://github.com/BlazorComponents/CompileBlazorInBlazor

Best regards,
TS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.