コード例 #1
0
        //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);
        }
コード例 #2
0
        //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);
                }
            }
        }