public void compileSnippet() { O2Thread.mtaThread( () => { if (creatingAst == false && createAstStack.Count > 0) { creatingAst = true; var codeSnippet = createAstStack.Pop(); this.sleep(forceAstBuildDelay, DebugMode); // wait a bit to allow more entries to be cleared from the stack if (createAstStack.Count > 0) { codeSnippet = createAstStack.Pop(); } createAstStack.Clear(); InvocationParameters = getDefaultInvocationParameters(); this.invoke(beforeSnippetAst); DebugMode.ifInfo("Compiling Source Snippet (Size: {0})", codeSnippet.size()); var sourceCode = createCSharpCodeWith_Class_Method_WithMethodText(codeSnippet); if (sourceCode != null) { compileSourceCode(sourceCode, CreatedFromSnipptet); } else { FinishedCompilingCode.Set(); } creatingAst = false; compileSnippet(); } }); }
public void compileSnippet() { O2Thread.mtaThread( () => { if (_creatingAst == false && _createAstStack.Count > 0) { _creatingAst = true; var codeSnippet = _createAstStack.Pop(); this.sleep(forceAstBuildDelay, DebugMode); // wait a bit to allow more entries to be cleared from the stack if (_createAstStack.Count > 0) { codeSnippet = _createAstStack.Pop(); } _createAstStack.Clear(); InvocationParameters = getDefaultInvocationParameters(); beforeSnippetAst.invoke(); DebugMode.ifInfo("Compiling Source Snippet (Size: {0})", codeSnippet.size()); var sourceCode = createCSharpCodeWith_Class_Method_WithMethodText(codeSnippet); if (UseCachedAssemblyIfAvailable && getCachedAssemblyForCode_and_RaiseEvents(sourceCode)) // see if we have already compiled this snippet before { return; } if (sourceCode != null) { compileSourceCode(sourceCode, CreatedFromSnipptet); } else { FinishedCompilingCode.Set(); } _creatingAst = false; //compileSnippet(); // this was there to try to see if the current in the editor was compiled (this should be detected in a different way) } }); }
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); } }
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(); } }); }