public static string getCachedCompiledAssembly(string scriptOrFile, bool compileIfNotFound) { if (CachedCompiledAssemblies.hasKey(scriptOrFile)) { var pathToDll = CachedCompiledAssemblies[scriptOrFile]; if (scriptOrFile.isFile()) { "in getCachedCompiledAssembly, mapped file '{0}' to cached assembly '{1}'".debug(scriptOrFile, pathToDll); } else { "in getCachedCompiledAssembly, found cached assembly for script/md5hash with size '{0}' to cached assembly '{1}'".debug(scriptOrFile.size(), pathToDll); } return(pathToDll); } if (compileIfNotFound.isFalse()) { return(null); } var mappedFile = CompileEngine.findScriptOnLocalScriptFolder(scriptOrFile); //var sourceCode = mappedFile.fileContents(); //if (sourceCode.contains("//generateDebugSymbols").isFalse()) //sourceCode += "//generateDebugSymbols".lineBefore(); var assembly = new CompileEngine().compileSourceFile(mappedFile); if (assembly != null && assembly.Location.fileExists()) { var pathToDll = assembly.Location; CachedCompiledAssemblies.add(scriptOrFile, pathToDll); "in getCachedCompiledAssembly, compiled file '{0}' to assembly '{1}' (and added it to CachedCompiledAssembly)".debug(scriptOrFile, pathToDll); return(assembly.Location); } return(""); }
static void Main(string[] args) { if (Control.ModifierKeys == Keys.Shift) showLogViewer().parentForm().width(1000).height(400); var firstScript = "O2_1st_Script.cs"; Console.WriteLine("Welcome to the O2 Platform v2.0"); "Current AppDomain: {0}".info(AppDomain.CurrentDomain.BaseDirectory); CompileEngine.lsGACExtraReferencesToAdd.Clear(); var assembly = new CompileEngine().compileSourceFile(firstScript); if (assembly.notNull()) { Console.WriteLine("Executing script {0} from location {1}".info(firstScript, assembly.Location)); if (assembly.methods().size()>0) { assembly.methods()[0].invoke(); Console.WriteLine("Invocation complete"); } else Console.WriteLine("Error: there were no methods in compiled assembly"); } else Console.WriteLine("Error: could not find, compile or execute first script ({0})".format(firstScript)); }
//public ascx_ScriptsFolder scriptsFolder; /* public void addCurrentAppDomainsDllsAsReferences() { foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies()) { var assemblyName = Path.GetFileName(assembly.Location); if (false == lsExtraReferenceAssembliesToAdd.Contains(assemblyName)) if (assemblyName.IndexOf("JetBrains")==-1 && assemblyName.IndexOf("VisualStudio") == -1 && assemblyName.IndexOf("SMDiagnostics") == -1) // don't add these assemblies lsExtraReferenceAssembliesToAdd.Add(assemblyName); } }*/ public void compileSourceCode() { if (sourceCodeEditor.partialFileViewMode == false) if (this.okThread(delegate { compileSourceCode(); })) { var lsExtraReferencesToAdd = new List<string>(); //lsExtraReferenceAssembliesToAdd.ToArray()); String sErrorMessages = ""; var compileEngine = new CompileEngine(); Assembly aCompiledAssembly = null; if (tbExtraReferencesToAdd.Text != "") lsExtraReferencesToAdd.AddRange(tbExtraReferencesToAdd.Text.Split(new[] {Environment.NewLine}, StringSplitOptions. RemoveEmptyEntries)); lbSourceCode_CompilationResult.Items.Clear(); var exeMainClass = (rbCreateExe.Checked) ? tbMainClass.Text : ""; var outputAssemblyName = ""; // todo expose outputAssemblyName on GUI //DI.config.addPathToCurrentExecutableEnvironmentPathVariable(DI.config.O2TempDir); sourceCodeEditor.saveSourceCode(); var filesToCompile = new List<String> {sourceCodeEditor.sPathToFileLoaded}; if (compileEngine.compileSourceFiles(filesToCompile, lsExtraReferencesToAdd.ToArray(), ref aCompiledAssembly, ref sErrorMessages, false /*verbose*/, exeMainClass, outputAssemblyName)) { // if we only have 1 class in the completed code, set tbMainClass.Text to it if (aCompiledAssembly.GetTypes().Length == 1) tbMainClass.Text = aCompiledAssembly.GetTypes()[0].FullName; lbSourceCode_CompilationResult.ForeColor = Color.Black; lbSourceCode_CompilationResult.Items.Add("No errors"); O2Messages.dotNetAssemblyAvailable(aCompiledAssembly.Location); //if (assemblyInvoke != null) // assemblyInvoke.loadAssembly(aCompiledAssembly, cbAutoExecuteOnMethodCompile.Checked); } else { //assemblyInvoke.setControlsEnableState(false); lbSourceCode_CompilationResult.ForeColor = Color.Red; compileEngine.addErrorsListToListBox(sErrorMessages, lbSourceCode_CompilationResult); } if (cbAutoSaveOnCompile.Checked) sourceCodeEditor.saveSourceCode(); } }
public static bool isFileAReferenceARequestToUseThePrevioulsyCompiledVersion(string fileToResolve, List <string> ReferencedAssemblies) { if (fileToResolve.starts("Ref:")) { fileToResolve = fileToResolve.remove("Ref:"); var fileRef = CompileEngine.getCachedCompiledAssembly(fileToResolve); if (fileRef.valid() && fileRef.fileExists()) { if (ReferencedAssemblies.contains(fileRef).isFalse()) { ReferencedAssemblies.add(fileRef); } } return(true); } else { return(false); } }
private static string getMethodName(string arg0) { string[] splittedArg0 = arg0.Split('!'); switch (splittedArg0.Length) { case 2: string file = splittedArg0[0]; string methodName = splittedArg0[1]; O2Cmd.log.write("Trying to load file {0} and execute method {1}", file, methodName); switch (Path.GetExtension(file)) { case ".cs": O2Cmd.log.write("\n*.cs file provided, compiling code\n"); Assembly assembly = new CompileEngine().compileSourceFile(file); if (assembly == null) O2Cmd.log.write("Error: could not compile provided code"); else { O2Cmd.log.write("Source code successfully compiled, loading types\n"); foreach (Type typeToLoad in assembly.GetTypes()) O2CmdApi.typesWithCommands.Add(typeToLoad); O2Cmd.log.write("\nHere is the updated list of available command line methods\n"); O2Cmd.help(); O2Cmd.log.write("\nExecuting method:{0}\n", methodName); return methodName; } break; case ".dll": break; default: O2Cmd.log.error("ERROR: unsupported file extension (it must be *.cs or *.dll"); break; } return ""; default: return arg0; } //return arg0; }
// we need to use CompileEngine (which is slower but supports multiple file compilation public void compileExtraSourceCodeReferencesAndUpdateReferencedAssemblies() { if (ExtraSourceCodeFilesToCompile.size() > 0) { var assembly = new CompileEngine().compileSourceFiles(ExtraSourceCodeFilesToCompile); if (assembly != null) { ReferencedAssemblies.Add(assembly.Location); generateDebugSymbols = true; // if there are extra assemblies we can't generate the assembly in memory } } }
public static string getCachedCompiledAssembly(string scriptOrFile, bool compileIfNotFound) { if (CachedCompiledAssemblies.hasKey(scriptOrFile)) { var pathToDll = CachedCompiledAssemblies[scriptOrFile]; if (scriptOrFile.isFile()) "in getCachedCompiledAssembly, mapped file '{0}' to cached assembly '{1}'".debug(scriptOrFile, pathToDll); else "in getCachedCompiledAssembly, found cached assembly for script/md5hash with size '{0}' to cached assembly '{1}'".debug(scriptOrFile.size(), pathToDll); return pathToDll; } if (compileIfNotFound.isFalse()) return null; var mappedFile = CompileEngine.findScriptOnLocalScriptFolder(scriptOrFile); //var sourceCode = mappedFile.fileContents(); //if (sourceCode.contains("//generateDebugSymbols").isFalse()) //sourceCode += "//generateDebugSymbols".lineBefore(); var assembly = new CompileEngine().compileSourceFile(mappedFile); if (assembly != null && assembly.Location.fileExists()) { var pathToDll = assembly.Location; CachedCompiledAssemblies.add(scriptOrFile, pathToDll); "in getCachedCompiledAssembly, compiled file '{0}' to assembly '{1}' (and added it to CachedCompiledAssembly)".debug(scriptOrFile, pathToDll); return assembly.Location; } return ""; }
public Assembly compileCSSharpFile() { Assembly compiledAssembly = null; var compileEngine = new CompileEngine(); if (getSourceCode() != "") { saveSourceCode(); // always save before compiling compileEngine.compileSourceFile(sPathToFileLoaded); compiledAssembly = compileEngine.compiledAssembly ?? null; if (compiledAssembly.notNull() && o2CodeCompletion.notNull() && compileEngine.cpCompilerParameters.notNull()) o2CodeCompletion.addReferences(compileEngine.cpCompilerParameters.ReferencedAssemblies.toList()); } var state = compiledAssembly == null && compileEngine.sbErrorMessage != null; //btShowHideCompilationErrors.visible(state); btShowHideCompilationErrors.prop("Visible",state); tvCompilationErrors.visible(state); lbCompilationErrors.prop("Visible", state); clearBookmarksAndMarkers(); // if there isn't a compiledAssembly, show errors if (compiledAssembly == null) { compileEngine.addErrorsListToTreeView(tvCompilationErrors); showErrorsInSourceCodeEditor(compileEngine.sbErrorMessage.str()); } else { if (compiledFileAstData.notNull()) compiledFileAstData.Dispose(); compiledFileAstData = new O2MappedAstData(sPathToFileLoaded); } return compiledAssembly; }
private object GetCurrentCSharpObjectModel() { var timer = new O2Timer("Calculated O2 Object Model for referencedAssesmblies").start(); var signatures = new List<string>(); var referencedAssemblies = new CompileEngine().getListOfReferencedAssembliesToUse(); //compileEngine.lsGACExtraReferencesToAdd(); foreach (var referencedAssesmbly in referencedAssemblies) if (File.Exists(referencedAssesmbly)) foreach (var method in PublicDI.reflection.getMethods(referencedAssesmbly)) signatures.Add(new FilteredSignature(method).sSignature); timer.stop(); return signatures; }
public static void addSourceFileOrFolderIncludedInSourceCode(List <string> sourceCodeFiles, List <string> referencedAssemblies) { var currentSourceDirectories = new List <string>(); // in case we need to resolve file names below foreach (var file in sourceCodeFiles) { if (file.valid()) { var directory = Path.GetDirectoryName(file); if (false == currentSourceDirectories.Contains(directory)) { currentSourceDirectories.Add(directory); } } } var filesToAdd = new List <string>(); // find the extra files to add foreach (var sourceCodeFile in sourceCodeFiles) { if (sourceCodeFile.valid() && sourceCodeFile.extension(".h2").isFalse()) { var fileLines = Files.getFileLines(sourceCodeFile); foreach (var fileLine in fileLines) { var match = StringsAndLists.TextStartsWithStringListItem(fileLine, specialO2Tag_ExtraSourceFile); if (match != "") { // var file = fileLine.Replace(specialO2Tag_ExtraSourceFile, "").Trim(); var file = fileLine.Replace(match, "").Trim(); if (false == sourceCodeFiles.Contains(file) && false == filesToAdd.Contains(file)) { //handle the File:xxx:Ref:xxx case if (CompileEngine.isFileAReferenceARequestToUseThePrevioulsyCompiledVersion(file, referencedAssemblies) .isFalse()) { filesToAdd.Add(file); } } } //else if (fileLine.StartsWith(specialO2Tag_ExtraFolder)) else { match = StringsAndLists.TextStartsWithStringListItem(fileLine, specialO2Tag_ExtraFolder); if (match != "") { var folder = fileLine.Replace(match, "").Trim(); if (false == Directory.Exists(folder)) { foreach (var path in currentSourceDirectories) { if (Directory.Exists(Path.Combine(path, folder))) { folder = Path.Combine(path, folder); break; } } } foreach (var file in Files.getFilesFromDir_returnFullPath(folder, "*.cs", true)) { if (false == sourceCodeFiles.Contains(file) && false == filesToAdd.Contains(file)) { filesToAdd.Add(file); } } } else { match = StringsAndLists.TextStartsWithStringListItem(fileLine, specialO2Tag_PathMapping); if (match != "") { addCompilationPathMappings(fileLine.remove(match)); } } } } } } applyCompilationPathMappings(filesToAdd); // add them to the list (checking if the file exist) if (filesToAdd.Count > 0) { PublicDI.log.info("There are {0} extra files to add to the list of source code files to compile: {0}", filesToAdd.Count); foreach (var file in filesToAdd) { var filePath = ""; if (File.Exists(file)) { filePath = file; } else { // try to find the file in the current sourceCodeFiles directories foreach (var directory in currentSourceDirectories) { if (File.Exists(Path.Combine(directory, file))) { filePath = Path.Combine(directory, file); break; } } if (filePath.fileExists().isFalse()) { filePath = findScriptOnLocalScriptFolder(file); } if (filePath == "") { PublicDI.log.error("in addSourceFileOrFolderIncludedInSourceCode, could not file file to add: {0}", file); } } if (filePath != "") { filePath = Path.GetFullPath(filePath); if (false == sourceCodeFiles.lower().Contains(filePath.lower())) { sourceCodeFiles.Add(filePath); addSourceFileOrFolderIncludedInSourceCode(sourceCodeFiles, referencedAssemblies); // we need to recursively add new new dependencies } } } } }