private ProjectCompiler RunCode_CompileCode(CompilerProjectReader projectReader, string assemblyFilename, string sourceFile) { ProjectCompiler compiler = new ProjectCompiler(CompilerManager.Current.Win32ResourceCompiler, CompilerManager.Current.ResourceCompiler); //try //{ compiler.SetOutputAssembly(assemblyFilename + ".dll"); //compiler.AddSource(new CompilerFile(sourceFile)); compiler.AddSource(new CompilerFile { File = sourceFile }); //if (projectReader != null) // compiler.SetProjectCompilerFile(projectReader.GetProjectCompilerFile()); // CompilerDefaultValues from runsource.runsource.config.xml runsource.runsource.config.local.xml //compiler.SetParameters(GetRunSourceConfigCompilerDefaultValues(), runCode: true); compiler.SetParameters(projectReader, runCode: true); compiler.SetTarget("library"); CompilerManager.Current.UpdateAssemblies(compiler.Assemblies.Values); compiler.Compile(); return(compiler); }
public static ProjectCompiler Create(string projectFile, Win32ResourceCompiler win32ResourceCompiler, ResourceCompiler resourceCompiler = null) { ProjectCompiler compiler = new ProjectCompiler(win32ResourceCompiler, resourceCompiler); CompilerProjectReader projectReader = CompilerProjectReader.Create(new XmlConfig(projectFile).GetConfigElementExplicit("/AssemblyProject")); compiler.SetParameters(projectReader); compiler.SetProjectCompilerFile(projectReader.GetProjectCompilerFile()); return compiler; }
public static ProjectCompiler Create(string projectFile, Win32ResourceCompiler win32ResourceCompiler, ResourceCompiler resourceCompiler = null) { ProjectCompiler compiler = new ProjectCompiler(win32ResourceCompiler, resourceCompiler); CompilerProjectReader projectReader = CompilerProjectReader.Create(new XmlConfig(projectFile).GetConfigElementExplicit("/AssemblyProject")); compiler.SetParameters(projectReader); //compiler.SetProjectCompilerFile(projectReader.GetProjectCompilerFile()); return(compiler); }
public GenerateAndExecute(string assemblyFilename) { _assemblyFilename = assemblyFilename; //_nameSpace = __defaultNameSpace; _className = __defaultClassName; _runMethodName = __defaultRunMethodName; _initMethodName = __defaultInitMethodName; _endMethodName = __defaultEndMethodName; _compiler = new ProjectCompiler(); _compiler.SetOutputAssembly(assemblyFilename); }
//public void CopyProjectSourceFiles(string projectName, string destinationDirectory) //{ // string pathProject = GetPathProject(projectName); // Trace.WriteLine("Copy project source files \"{0}\" to \"{1}\"", pathProject, destinationDirectory); // Compiler compiler = CreateProjectCompiler(pathProject); // compiler.CopySourceFiles(destinationDirectory.zRootPath(zPath.GetDirectoryName(pathProject))); //} //public void ZipProjectSourceFiles(string projectName, string zipFile) //{ // string pathProject = GetPathProject(projectName); // Trace.WriteLine("Zip project source files \"{0}\" to \"{1}\"", pathProject, zipFile); // Compiler compiler = CreateProjectCompiler(pathProject); // compiler.ZipSourceFiles(zipFile.zRootPath(zPath.GetDirectoryName(pathProject))); //} // not used le 04/12/2016, it is used from runsource to compile a project public IProjectCompiler CompileProject(string projectName) { // - compile assembly project (like runsource.dll.project.xml) and runsource project (like download.project.xml) // - for assembly project use CompilerDefaultValues from runsource.runsource.config.xml runsource.runsource.config.local.xml // - for runsource project use CompilerDefaultValues from runsource.runsource.config.xml runsource.runsource.config.local.xml //Compiler compiler = new Compiler(); //projectName = GetProjectVariableValue(projectName, throwError: true); //string pathProject = GetFilePath(projectName); //compiler.DefaultDir = zPath.GetDirectoryName(pathProject); //// CompilerDefaultValues from runsource.runsource.config.xml runsource.runsource.config.local.xml //compiler.SetParameters(GetRunSourceConfigCompilerDefaultValues(), dontSetOutput: true); //Trace.WriteLine("Compile project \"{0}\"", pathProject); //compiler.SetParameters(CompilerProject.Create(new XmlConfig(pathProject).GetConfigElementExplicit("/AssemblyProject"))); string pathProject = GetPathProject(projectName); Trace.WriteLine("Compile project \"{0}\"", pathProject); //ProjectCompiler compiler = CreateProjectCompiler(pathProject); ProjectCompiler compiler = ProjectCompiler.Create(pathProject, CompilerManager.Current.Win32ResourceCompiler, CompilerManager.Current.ResourceCompiler); //compiler.RunsourceSourceDirectory = GetRunSourceConfig().Get("UpdateRunSource/UpdateDirectory").zRootPath(zapp.GetEntryAssemblyDirectory()); compiler.RunsourceSourceDirectory = GetRunSourceConfig().Get("RunsourceSourceDirectory").zRootPath(zapp.GetEntryAssemblyDirectory()); compiler.Compile(); string s = null; if (!compiler.Success) { SetResult(compiler.GetCompilerMessagesDataTable()); s = " with error(s)"; } else { // trace warning compiler.TraceMessages(); //if (compiler.CopyRunSourceSourceFiles) //{ // string runsourceDirectory = GetRunSourceConfig().Get("UpdateRunSource/UpdateDirectory").zRootPath(zapp.GetEntryAssemblyDirectory()); // if (runsourceDirectory != null) // { // foreach (string directory in compiler.CopyOutputDirectories) // { // Trace.WriteLine(" copy runsource source files from \"{0}\" to \"{1}\"", runsourceDirectory, directory); // foreach (string file in zDirectory.EnumerateFiles(runsourceDirectory, "*" + ProjectCompiler.ZipSourceFilename)) // zfile.CopyFileToDirectory(file, directory, options: CopyFileOptions.OverwriteReadOnly | CopyFileOptions.CopyOnlyIfNewer); // } // } //} } Trace.WriteLine(" compiled{0} : {1}", s, compiler.OutputAssembly); return(compiler); }
public static bool CompileProjects(string projectsFile, Win32ResourceCompiler win32ResourceCompiler, ResourceCompiler resourceCompiler = null, string runsourceSourceDirectory = null, Action <IProjectCompiler> onCompiled = null) { Chrono chrono = new Chrono(); chrono.Start(); int nbProject = 0; try { if (!zFile.Exists(projectsFile)) { throw new PBException("projects file dont exists \"{0}\"", projectsFile); } XmlConfig projects = new XmlConfig(projectsFile); string projectsDirectory = zPath.GetDirectoryName(projectsFile); //string updateDir = _config.GetExplicit("UpdateRunSource/UpdateDirectory").zRootPath(zapp.GetEntryAssemblyDirectory()); string updateDirectory = projects.Get("UpdateDirectory"); foreach (XElement project in projects.GetElements("Project")) { string projectFile = project.zExplicitAttribValue("value").zRootPath(projectsDirectory); Trace.WriteLine("Compile project \"{0}\"", projectFile); ProjectCompiler compiler = ProjectCompiler.Create(projectFile, win32ResourceCompiler, resourceCompiler); compiler.RunsourceSourceDirectory = runsourceSourceDirectory; compiler.Compile(); compiler.TraceMessages(); //if (onCompiled != null) // onCompiled(compiler); onCompiled?.Invoke(compiler); if (!compiler.Success) { return(false); } string copyOutput = project.zAttribValue("copyOutput").zRootPath(zapp.GetEntryAssemblyDirectory()); if (copyOutput != null) { //Trace.WriteLine(" copy result files to directory \"{0}\"", copyOutput); compiler.CopyResultFilesToDirectory(copyOutput); } if (project.zAttribValue("copyToUpdateDirectory").zTryParseAs(false)) { if (updateDirectory == null) { throw new PBException("update directory is not defined"); } //Trace.WriteLine(" copy result files to directory \"{0}\"", updateDirectory); compiler.CopyResultFilesToDirectory(updateDirectory); } nbProject++; } } catch (ProjectCompilerException ex) { Error.WriteMessage(ErrorOptions.TraceError, ex.Message); } finally { chrono.Stop(); Trace.WriteLine("{0} project(s) compiled", nbProject); Trace.WriteLine("Process completed {0}", chrono.TotalTimeString); } return(true); }
// RunCode_ExecuteCode must throw an exception if he can't execute run method // if no error thrown RunCode_ExecuteCode must call RunCode_EndRun() //private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProject compilerProject, Compiler compiler, bool useNewThread) private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProjectReader compilerProject, ProjectCompiler compiler, bool runOnMainThread, bool callInit) { RunCode runCode = new RunCode(++_runCodeId); runCode.RunAssembly = assembly; //runCode.CompilerAssemblies = compiler.Assemblies; runCode.RunMethodName = codeResult.GetFullRunMethodName(); runCode.EndRun += error => RunCode_EndRun(runCode, error); //if (forceCallInit) // _runSourceInitEndMethods.CallInit = true; _executionAborted = false; foreach (CompilerAssembly compilerAssembly in compiler.Assemblies.Values) { //WriteLine(2, " Assembly \"{0}\" resolve {1}", assembly.File, assembly.Resolve); if (compilerAssembly.Resolve) AssemblyResolve.Add(compilerAssembly.File, compilerAssembly.ResolveName); } //_runSourceInitEndMethods.CallInit = callInit; //if (callInit) //_runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => runCode.GetMethod(methodName)); _runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => zReflection.GetMethod(methodName, assembly, ErrorOptions.TraceWarning)); // add runCode to _runCodes after call init, if call init fail runCode is not in _runCodes if (!_runCodes.TryAdd(runCode.Id, runCode)) throw new PBException("unable to add RunCode id {0} to ConcurrentDictionary", runCode.Id); runCode.Run(runOnMainThread); // problem with AssemblyResolve.Clear() end method may need to resolve assembly //AssemblyResolve.Clear(); }
private ProjectCompiler RunCode_CompileCode(CompilerProjectReader compilerProject, string assemblyFilename, string sourceFile) { ProjectCompiler compiler = new ProjectCompiler(CompilerManager.Current.Win32ResourceCompiler, CompilerManager.Current.ResourceCompiler); //try //{ compiler.SetOutputAssembly(assemblyFilename + ".dll"); compiler.AddSource(new CompilerFile(sourceFile)); if (compilerProject != null) compiler.SetProjectCompilerFile(compilerProject.GetProjectCompilerFile()); // CompilerDefaultValues from runsource.runsource.config.xml runsource.runsource.config.local.xml //compiler.SetParameters(GetRunSourceConfigCompilerDefaultValues(), runCode: true); compiler.SetParameters(compilerProject, runCode: true); compiler.SetTarget("library"); CompilerManager.Current.UpdateAssemblies(compiler.Assemblies.Values); compiler.Compile(); return compiler; }
// RunCode_ExecuteCode must throw an exception if he can't execute run method // if no error thrown RunCode_ExecuteCode must call RunCode_EndRun() //private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProjectReader compilerProject, ProjectCompiler compiler, bool runOnMainThread, bool callInit) private async Task RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProjectReader compilerProject, ProjectCompiler compiler, bool runOnMainThread, bool callInit) { RunCode runCode = new RunCode(++_runCodeId); runCode.RunAssembly = assembly; //runCode.CompilerAssemblies = compiler.Assemblies; runCode.RunMethodName = codeResult.GetFullRunMethodName(); runCode.EndRun += error => RunCode_EndRun(runCode, error); //if (forceCallInit) // _runSourceInitEndMethods.CallInit = true; _executionAborted = false; foreach (CompilerAssembly compilerAssembly in compiler.Assemblies.Values) { //WriteLine(2, " Assembly \"{0}\" resolve {1}", assembly.File, assembly.Resolve); if (compilerAssembly.Resolve) { AssemblyResolve.Add(compilerAssembly.File, compilerAssembly.ResolveName); } } //_runSourceInitEndMethods.CallInit = callInit; //if (callInit) //_runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => runCode.GetMethod(methodName)); _runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => zReflection.GetMethod(methodName, assembly, ErrorOptions.TraceWarning)); // add runCode to _runCodes after call init, if call init fail runCode is not in _runCodes if (!_runCodes.TryAdd(runCode.Id, runCode)) { throw new PBException("unable to add RunCode id {0} to ConcurrentDictionary", runCode.Id); } //runCode.Run(runOnMainThread); await runCode.Run_v2(runOnMainThread); // problem with AssemblyResolve.Clear() end method may need to resolve assembly //AssemblyResolve.Clear(); }
//private CompilerProject GetRunSourceConfigCompilerDefaultValues() //{ // return CompilerProject.Create(GetRunSourceConfig().zGetConfigElement("CompilerDefaultValues")); //} //private void _RunCode(string code, bool runOnMainThread = false, bool compileWithoutProject = false, bool allowMultipleRun = false, bool dontRunCode = false, bool callInit = false) private async Task _RunCode(string code, bool runOnMainThread = false, bool compileWithoutProject = false, bool allowMultipleRun = false, bool dontRunCode = false, bool callInit = false) { if (code == "") { return; } //if (!dontRunCode && _runCode != null) // throw new PBException("error program already running"); //if (!dontRunCode && _runCodes.Count > 0 && !_allowMultipleExecution) if (!dontRunCode && _runCodes.Count > 0 && !allowMultipleRun) { throw new PBException("error program already running and multiple execution is not allowed"); } bool error = false; bool doEndRun = true; _refreshRunSourceConfig = true; _refreshProjectConfig = true; try { CompilerProjectReader projectReader = null; if (!compileWithoutProject) { projectReader = GetProjectCompilerProject(); } if (projectReader == null) { projectReader = GetDefaultProject(); //Trace.WriteLine($"compile with default project \"{projectReader.GetProjectCompilerFile().File}\""); Trace.WriteLine($"compile with default project \"{projectReader.ProjectFile}\""); } string assemblyFile = GetGenerateAssembly().GetNewAssemblyFile(); GenerateCSharpCodeResult codeResult = RunCode_GenerateCode(code, projectReader, assemblyFile); ProjectCompiler compiler = RunCode_CompileCode(projectReader, assemblyFile, codeResult.SourceFile); //if (compiler.HasError()) if (!compiler.Success) { SetResult(compiler.GetCompilerMessagesDataTable()); } else { // trace warning //compiler.TraceMessages(_messageFilter); compiler.TraceMessages(message => _messageFilter(message) || message.FileName != codeResult.SourceFile); if (!dontRunCode) { //RunCode_ExecuteCode(compiler.Results.LoadAssembly(), codeResult, projectReader, compiler, runOnMainThread, callInit); await RunCode_ExecuteCode(compiler.Results.LoadAssembly(), codeResult, projectReader, compiler, runOnMainThread, callInit); doEndRun = false; } } } //catch //{ // error = true; // throw; //} catch (Exception ex) { error = true; if (ex is ProjectCompilerException) { Error.WriteMessage(ErrorOptions.TraceError, ex.Message); } else { throw; } } finally { if (doEndRun) { RunCode_EndRun(null, error); } } }