public Assembly compileSourceCode_Sync(string sourceCode)
        {
            if (sourceCode.notValid())
            {
                return(null);
            }
            try
            {
                Environment.CurrentDirectory = PublicDI.config.CurrentExecutableDirectory;
                CompiledAssembly             = null;
                beforeCompile.invoke();
                DebugMode.ifInfo("Compiling Source Code (Size: {0})", sourceCode.size());
                SourceCode = sourceCode;

                if (sourceCode.lines().starting("//CLR_3.5").notEmpty()) // allow setting compilation into 2.0 CLR
                {
                    CompilationVersion = "v3.5";
                }

                var providerOptions = new Dictionary <string, string>().add("CompilerVersion", CompilationVersion);

                var csharpCodeProvider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions);
                var compilerParams     = new CompilerParameters();
                compilerParams.OutputAssembly          = "_o2_Script.dll".tempFile();
                compilerParams.IncludeDebugInformation = generateDebugSymbols;
                compilerParams.GenerateInMemory        = !generateDebugSymbols;

                foreach (var referencedAssembly in ReferencedAssemblies)
                {
                    compilerParams.ReferencedAssemblies.Add(referencedAssembly);
                }

                CompilerResults = (generateDebugSymbols)
                                      ? csharpCodeProvider.CompileAssemblyFromFile(compilerParams,
                                                                                   sourceCode.saveWithExtension(".cs"))
                                      : csharpCodeProvider.CompileAssemblyFromSource(compilerParams, sourceCode);

                if (CompilerResults.Errors.Count > 0 || CompilerResults.CompiledAssembly == null)
                {
                    CompilationErrors = "";
                    foreach (CompilerError error in CompilerResults.Errors)
                    {
                        //CompilationErrors.Add(CompilationErrors.line(error.ToString());
                        var errorMessage = String.Format("{0}::{1}::{2}::{3}::{4}", error.Line,
                                                         error.Column, error.ErrorNumber,
                                                         error.ErrorText, error.FileName);
                        CompilationErrors = CompilationErrors.line(errorMessage);
                        "[CSharp_FastCompiler] Compilation Error: {0}".error(errorMessage);
                    }
                    DebugMode.ifError("Compilation failed");
                    onCompileFail.invoke();
                }
                else
                {
                    CompiledAssembly = CompilerResults.CompiledAssembly;
                    if (CompiledAssembly.Location.fileExists())
                    {
                        CompileEngine.setCachedCompiledAssembly_toMD5(sourceCode, CompiledAssembly);
                    }
                    DebugMode.ifDebug("Compilation was OK");
                    onCompileOK.invoke();
                }
                return(CompiledAssembly);
            }
            catch (Exception ex)
            {
                ex.log("[compileSourceCode_Sync");
                return(null);
            }
        }
示例#2
0
 private void OnCompilationErrors(List <CompilationErrorResultObject> errors) => CompilationErrors?.Invoke(errors);
        public void compileSourceCode()
        {
            O2Thread.mtaThread(
                () =>
            {
                try
                {
                    if (compiling == false && compileStack.Count > 0)
                    {
                        compiling = true;
                        FinishedCompilingCode.Reset();
                        compileExtraSourceCodeReferencesAndUpdateReferencedAssemblies();
                        this.sleep(forceAstBuildDelay, DebugMode);                    // wait a bit to allow more entries to be cleared from the stack
                        var sourceCode = compileStack.Pop();
                        compileStack.Clear();
                        // remove all previous compile requests (since their source code is now out of date

                        //Files.setCurrentDirectoryToExecutableDirectory();
                        Environment.CurrentDirectory = Kernel.PublicDI.config.CurrentExecutableDirectory;
                        ;
                        this.invoke(beforeCompile);
                        DebugMode.ifInfo("Compiling Source Code (Size: {0})", sourceCode.size());
                        SourceCode          = sourceCode;
                        var providerOptions = new Dictionary <string, string>();
                        providerOptions.Add("CompilerVersion", "v3.5");
                        var csharpCodeProvider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions);
                        var compilerParams     = new CompilerParameters
                        {
                            GenerateInMemory        = !generateDebugSymbols,
                            IncludeDebugInformation = generateDebugSymbols
                        };

                        foreach (var referencedAssembly in ReferencedAssemblies)
                        {
                            compilerParams.ReferencedAssemblies.Add(referencedAssembly);
                        }

                        CompilerResults = csharpCodeProvider.CompileAssemblyFromSource(compilerParams, sourceCode);

                        FinishedCompilingCode.Set();

                        if (CompilerResults.Errors.Count > 0 || CompilerResults.CompiledAssembly == null)
                        {
                            CompilationErrors = "";
                            foreach (CompilerError error in CompilerResults.Errors)
                            {
                                //CompilationErrors.Add(CompilationErrors.line(error.ToString());
                                var errorMessage = String.Format("{0}::{1}::{2}::{3}::{4}", error.Line,
                                                                 error.Column, error.ErrorNumber,
                                                                 error.ErrorText, error.FileName);
                                CompilationErrors = CompilationErrors.line(errorMessage);
                                "[CSharp_FastCompiler] Compilation Error: {0}".error(errorMessage);
                            }
                            DebugMode.ifError("Compilation failed");
                            this.invoke(onCompileFail);
                        }
                        else
                        {
                            DebugMode.ifDebug("Compilation was OK");
                            this.invoke(onCompileOK);
                        }
                        compiling = false;
                        compileSourceCode();
                    }
                }
                catch (Exception ex)
                {
                    ex.log("in compileSourceCode");
                    compiling = false;
                    FinishedCompilingCode.Set();
                }
            });
        }