CSScriptCompilers.CSCompiler.CompileAssemblyFromFileBatchImpl C# (CSharp) Méthode

CompileAssemblyFromFileBatchImpl() private méthode

private CompileAssemblyFromFileBatchImpl ( CompilerParameters options, string fileNames ) : CompilerResults
options System.CodeDom.Compiler.CompilerParameters
fileNames string
Résultat System.CodeDom.Compiler.CompilerResults
        CompilerResults CompileAssemblyFromFileBatchImpl(CompilerParameters options, string[] fileNames)
        {
            //System.Diagnostics.Debug.Assert(false);
            CompilerResults retval = new CompilerResults(new TempFileCollection());

            string outputName = Path.GetFileNameWithoutExtension(options.OutputAssembly);
            string tempDir = Path.Combine(Path.GetTempPath(), "CSSCRIPT\\CPP\\" + System.Guid.NewGuid().ToString());
            string tempProj = Path.Combine(tempDir, outputName + ".csproj");
            string outputFile = Path.Combine(tempDir, outputName + (options.GenerateExecutable ? ".exe" : ".dll"));

            if (Directory.Exists(tempDir))
                Directory.Delete(tempDir, true);

            Directory.CreateDirectory(tempDir);

            using (StreamReader sr = new StreamReader(ProjTemplateFile))
            using (StreamWriter sw = new StreamWriter(tempProj))
            {
                string content = sr.ReadToEnd();
                content = content.Replace("$NAME$", outputName);
                content = content.Replace("$DEBUG_TYPE$", options.IncludeDebugInformation ? "<DebugType>full</DebugType>" : "");
                content = content.Replace("$OPTIMIZE$", options.IncludeDebugInformation ? "false" : "true");
                content = content.Replace("$DEBUG_CONST$", options.IncludeDebugInformation ? "DEBUG;" : "");
                content = content.Replace("$DEBUG$", options.IncludeDebugInformation ? "true" : "false");
                content = content.Replace("$OUPTUT_DIR$", tempDir);

                //Exe/WinExe/Library
                if (options.GenerateExecutable) //exe
                {
                    if (options.CompilerOptions != null && options.CompilerOptions.IndexOf("/target:winexe") != -1)
                    {
                        content = content.Replace("$TYPE$", "WinExe");	//WinForm
                    }
                    else
                    {
                        content = content.Replace("$TYPE$", "Exe");	//console
                    }
                }
                else //dll
                {
                    content = content.Replace("$TYPE$", "Library");	//dll
                }

                string references = "";
                foreach (string file in options.ReferencedAssemblies)
                    references += string.Format(reference_template, Path.GetFileName(file), file);
                content = content.Replace("$REFERENCES$", references);

                content = content.Replace("$MIN_CLR_VER$", "<MinFrameworkVersionRequired>3.0</MinFrameworkVersionRequired>");
                content = content.Replace("$IMPORT_PROJECT$", "<Import Project=\"$(MSBuildBinPath)\\Microsoft.WinFX.targets\" />");

                string sources = "";

                foreach (string file in fileNames)
                {
                    if (file.ToLower().EndsWith(".xaml"))
                    {
                        if (IsAppXAML(file))
                            sources += "<ApplicationDefinition Include=\"" + file + "\" />\n";
                        else
                            sources += "<Page Include=\"" + file + "\" />\n";
                    }
                    else
                        sources += string.Format(include_template, file);
                }
                content = content.Replace("$SOURCE_FILES$", sources);

                sw.Write(content);
            }

            string compileLog = "";
            //Stopwatch sw1 = new Stopwatch();
            //sw1.Start();

            string msbuild = Path.Combine(Path.GetDirectoryName("".GetType().Assembly.Location), "MSBuild.exe");

            compileLog = RunApp(Path.GetDirectoryName(tempProj), msbuild, "\"" + tempProj + "\" /p:Configuration=\"CSSBuild\" /nologo /verbosity:m").Trim();

            //sw1.Stop();

            if (compileLog.EndsWith("-- FAILED."))
            {
                using (StringReader sr = new StringReader(compileLog))
                {
                    string line = "";
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Trim();

                        if (line == "")
                            continue;

                        if (line.EndsWith("Done building project"))
                            break;

                        int lineNumber = 0;
                        int colNumber = 0;
                        string fileName = "";
                        string errorNumber = "";
                        string errorText = "";
                        bool isWarning = false;

                        int fileEnd = line.IndexOf(": warning ");
                        if (fileEnd == -1)
                            fileEnd = line.IndexOf(": error ");
                        else
                            isWarning = true;

                        if (fileEnd == -1)
                            continue;

                        string filePart = line.Substring(0, fileEnd);
                        string errorPart = line.Substring(fileEnd + 2); //" :" == 2
                        int lineNumberStart = filePart.LastIndexOf("(");
                        int errorDescrStart = errorPart.IndexOf(":");

                        string[] erorLocation = filePart.Substring(lineNumberStart).Replace("(", "").Replace(")", "").Split(',');
                        lineNumber = filePart.EndsWith(")") ? int.Parse(erorLocation[0]) : -1;
                        colNumber = filePart.EndsWith(")") ? int.Parse(erorLocation[1]) : -1;
                        fileName = Path.GetFullPath(lineNumber == -1 ? filePart : filePart.Substring(0, lineNumberStart).Trim());
                        errorNumber = errorPart.Substring(0, errorDescrStart).Trim();
                        errorText = errorPart.Substring(errorDescrStart + 1).Trim();

                        CompilerError error = new CompilerError(fileName, lineNumber, colNumber, errorNumber, errorText);
                        error.IsWarning = isWarning;

                        retval.Errors.Add(error);
                    }
                }
            }

            if (File.Exists(outputFile))
            {
                if (File.Exists(options.OutputAssembly))
                    File.Copy(outputFile, options.OutputAssembly, true);
                else
                    File.Move(outputFile, options.OutputAssembly);
                try
                {
                    Directory.Delete(tempDir, true);
                }
                catch { }
            }

            if (options.IncludeDebugInformation)
            {
                string pdbSrcFile = Path.ChangeExtension(outputFile, ".pdb");
                string pdbDestFile = Path.ChangeExtension(options.OutputAssembly, ".pdb");
                if (File.Exists(pdbSrcFile))
                {
                    if (File.Exists(pdbDestFile))
                        File.Copy(pdbSrcFile, pdbDestFile, true);
                    else
                        File.Move(pdbSrcFile, pdbDestFile);
                }
            }
            return retval;
        }