Пример #1
0
        private bool CompileHere()
        {
            if (OnlyOnce && (File.Exists("delivery/bin/" + Path.GetFileName(FCSProjFile).Replace(".csproj", ".dll")) ||
                             File.Exists("delivery/bin/" + Path.GetFileName(FCSProjFile).Replace(".csproj", ".exe"))))
            {
                Console.WriteLine("Skipping " + FCSProjFile);
                return(true);
            }

            Console.WriteLine("Compiling " + FCSProjFile);

            XmlDocument doc = new XmlDocument();

            doc.Load(FCSProjFile);

            XmlNode propertyGroup = doc.DocumentElement.FirstChild;
            Dictionary <string, string> mainProperties = new Dictionary <string, string>();

            List <String> src = new List <string>();

            foreach (XmlNode propNode in propertyGroup.ChildNodes)
            {
                mainProperties.Add(propNode.Name, propNode.InnerText);
            }

            CSharpCodeProvider csc = new CSharpCodeProvider(
                new Dictionary <string, string>()
            {
                { "CompilerVersion", "v4.0" }
            });
            CompilerParameters parameters = new CompilerParameters();

            parameters.GenerateInMemory = false;
            parameters.CompilerOptions  = string.Empty;

            string OutputFile = Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" + mainProperties["OutputPath"].Replace("\\", "/"));

            OutputFile += "/" + mainProperties["AssemblyName"];

            if (mainProperties["OutputType"].ToLower() == "library")
            {
                parameters.GenerateExecutable = false;
                OutputFile += ".dll";
            }
            else if (mainProperties["OutputType"].ToLower() == "winexe")
            {
                parameters.GenerateExecutable = true;
                parameters.CompilerOptions   += " /target:winexe";
                OutputFile += ".exe";
            }
            else
            {
                parameters.GenerateExecutable = true;
                OutputFile += ".exe";
            }

            if (File.Exists(OutputFile))
            {
                // if compilation fails, we want compileProject -D:onlyonce=true to pick up this project
                File.Delete(OutputFile);
            }

            parameters.OutputAssembly = OutputFile;
            parameters.WarningLevel   = 4;

            if (mainProperties.ContainsKey("ApplicationManifest") && (mainProperties["ApplicationManifest"].Length > 0))
            {
                if (!this.Project.RuntimeFramework.Name.StartsWith("mono"))
                {
                    // we cannot include the manifest when compiling on Mono
                    parameters.CompilerOptions += " /win32manifest:\"APPMANIFEST\"";
                }
            }

            parameters.CompilerOptions += " /define:DEBUGMODE /doc:\"XMLOUTPUTFILE.xml\"";

            if (this.Project.PlatformName == "unix")
            {
                // command line options use - instead of /, eg. /define or -define
                parameters.CompilerOptions = parameters.CompilerOptions.Replace("/", "-");
            }

            // insert the path to the xml output file after the command line options thing has been done
            parameters.CompilerOptions = parameters.CompilerOptions.Replace("XMLOUTPUTFILE", OutputFile.Replace("\\", "/"));

            if (mainProperties.ContainsKey("ApplicationManifest") && (mainProperties["ApplicationManifest"].Length > 0))
            {
                parameters.CompilerOptions =
                    parameters.CompilerOptions.Replace("APPMANIFEST",
                                                       Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" + mainProperties["ApplicationManifest"].Replace("\\", "/")));
            }

            String FrameworkDLLPath = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(System.Type)).Location);

            foreach (XmlNode ProjectNodeChild in doc.DocumentElement)
            {
                if (ProjectNodeChild.Name == "ItemGroup")
                {
                    foreach (XmlNode ItemNode in ProjectNodeChild)
                    {
                        if (ItemNode.Name == "Reference")
                        {
                            if (ItemNode.HasChildNodes && (ItemNode.ChildNodes[0].Name == "HintPath"))
                            {
                                parameters.ReferencedAssemblies.Add(Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                                                     ItemNode.ChildNodes[0].InnerText.Replace("\\", "/")));
                            }
                            else
                            {
                                // .net dlls
                                parameters.ReferencedAssemblies.Add(
                                    FrameworkDLLPath + Path.DirectorySeparatorChar +
                                    ItemNode.Attributes["Include"].Value + ".dll");
                            }
                        }
                        else if (ItemNode.Name == "ProjectReference")
                        {
                            string ReferencedProjectName = ItemNode.ChildNodes[1].InnerText;
                            parameters.ReferencedAssemblies.Add(
                                Path.GetFullPath(Path.GetDirectoryName(OutputFile) + "/" +
                                                 ReferencedProjectName.Replace("\\", "/") + ".dll"));
                        }
                        else if (ItemNode.Name == "Compile")
                        {
                            src.Add(Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                     ItemNode.Attributes["Include"].Value.Replace("\\", "/")));
                        }
                        else if (ItemNode.Name == "EmbeddedResource")
                        {
                            string ResourceXFile = ItemNode.Attributes["Include"].Value;

                            if (ResourceXFile.StartsWith(".."))
                            {
                                ResourceXFile = Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                                 ResourceXFile.Replace("\\", "/"));
                            }

                            parameters.EmbeddedResources.Add(ResourceXFile);
                        }
                    }
                }
            }

            CompilerResults results = csc.CompileAssemblyFromFile(parameters, src.ToArray());

            bool result = true;

            foreach (CompilerError error in results.Errors)
            {
                Console.WriteLine(error.ToString());

                if (!error.IsWarning)
                {
                    result = false;
                }
            }

            if (!result)
            {
                FailTask fail = new FailTask();
                this.CopyTo(fail);
                fail.Message = "compiler error(s)";
                fail.Execute();
            }

            return(result);
        }
Пример #2
0
        private bool CompileHere()
        {
            Console.WriteLine("Compiling " + FCSProjFile);

            XmlDocument doc = new XmlDocument();

            doc.Load(FCSProjFile);

            XmlNode propertyGroup = doc.DocumentElement.FirstChild;
            Dictionary <string, string> mainProperties = new Dictionary <string, string>();

            List <String> src = new List <string>();

            foreach (XmlNode propNode in propertyGroup.ChildNodes)
            {
                mainProperties.Add(propNode.Name, propNode.InnerText);
            }

            CSharpCodeProvider csc = new CSharpCodeProvider(
                new Dictionary <string, string>()
            {
                { "CompilerVersion", "v4.0" }
            });
            CompilerParameters parameters = new CompilerParameters();

            parameters.GenerateInMemory = false;
            parameters.CompilerOptions  = string.Empty;

            string OutputFile = Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" + mainProperties["OutputPath"].Replace("\\", "/"));

            OutputFile += "/" + mainProperties["AssemblyName"];

            if (mainProperties["OutputType"].ToLower() == "library")
            {
                parameters.GenerateExecutable = false;
                OutputFile += ".dll";
            }
            else if (mainProperties["OutputType"].ToLower() == "winexe")
            {
                parameters.GenerateExecutable = true;
                parameters.CompilerOptions   += " /target:winexe";
                OutputFile += ".exe";
            }
            else
            {
                parameters.GenerateExecutable = true;
                OutputFile += ".exe";
            }

            // needed because of sqlite3.dll, when compiling on Linux for Windows
            if (this.Project.PlatformName == "unix")
            {
                parameters.CompilerOptions += " /platform:x86";
            }

            parameters.OutputAssembly = OutputFile;
            parameters.WarningLevel   = 4;

            if (mainProperties.ContainsKey("ApplicationManifest") && (mainProperties["ApplicationManifest"].Length > 0))
            {
                if (!this.Project.RuntimeFramework.Name.StartsWith("mono"))
                {
                    // we cannot include the manifest when compiling on Mono
                    parameters.CompilerOptions += " /win32manifest:\"APPMANIFEST\"";
                }
            }

            parameters.CompilerOptions += " /define:DEBUGMODE /doc:\"XMLOUTPUTFILE.xml\"";

            if (this.Project.PlatformName == "unix")
            {
                // command line options use - instead of /, eg. /define or -define
                parameters.CompilerOptions = parameters.CompilerOptions.Replace("/", "-");
            }

            // insert the path to the xml output file after the command line options thing has been done
            parameters.CompilerOptions = parameters.CompilerOptions.Replace("XMLOUTPUTFILE", OutputFile.Replace("\\", "/"));

            if (mainProperties.ContainsKey("ApplicationManifest") && (mainProperties["ApplicationManifest"].Length > 0))
            {
                parameters.CompilerOptions =
                    parameters.CompilerOptions.Replace("APPMANIFEST",
                                                       Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" + mainProperties["ApplicationManifest"].Replace("\\", "/")));
            }

            String FrameworkDLLPath = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(System.Type)).Location);

            foreach (XmlNode ProjectNodeChild in doc.DocumentElement)
            {
                if (ProjectNodeChild.Name == "ItemGroup")
                {
                    foreach (XmlNode ItemNode in ProjectNodeChild)
                    {
                        if (ItemNode.Name == "Reference")
                        {
                            if (ItemNode.HasChildNodes && (ItemNode.ChildNodes[0].Name == "HintPath"))
                            {
                                if (ItemNode.ChildNodes[0].InnerText.Contains(".."))
                                {
                                    parameters.ReferencedAssemblies.Add(Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                                                         ItemNode.ChildNodes[0].InnerText.Replace("\\", "/")));
                                }
                                else
                                {
                                    parameters.ReferencedAssemblies.Add(ItemNode.ChildNodes[0].InnerText);
                                }
                            }
                            else
                            {
                                // .net dlls
                                parameters.ReferencedAssemblies.Add(
                                    FrameworkDLLPath + Path.DirectorySeparatorChar +
                                    ItemNode.Attributes["Include"].Value + ".dll");
                            }
                        }
                        else if (ItemNode.Name == "ProjectReference")
                        {
                            string ReferencedProjectName = ItemNode.ChildNodes[1].InnerText;
                            parameters.ReferencedAssemblies.Add(
                                Path.GetFullPath(Path.GetDirectoryName(OutputFile) + "/" +
                                                 ReferencedProjectName.Replace("\\", "/") + ".dll"));
                        }
                        else if (ItemNode.Name == "Compile")
                        {
                            if (ItemNode.Attributes["Include"].Value.Contains(".."))
                            {
                                src.Add(Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                         ItemNode.Attributes["Include"].Value.Replace("\\", "/")));
                            }
                            else
                            {
                                src.Add(ItemNode.Attributes["Include"].Value);
                            }
                        }
                        else if (ItemNode.Name == "EmbeddedResource")
                        {
                            string ResourceXFile = ItemNode.Attributes["Include"].Value;

                            if (ResourceXFile.StartsWith(".."))
                            {
                                ResourceXFile = Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                                 ResourceXFile.Replace("\\", "/"));
                            }

                            if (ResourceXFile.EndsWith(".resx"))
                            {
                                string NamespaceAndClass = Path.GetFileNameWithoutExtension(ResourceXFile);

                                if (ItemNode.HasChildNodes && (ItemNode.FirstChild.Name == "DependentUpon"))
                                {
                                    string CSFile = ItemNode.FirstChild.InnerText;

                                    if (CSFile.StartsWith(".."))
                                    {
                                        CSFile = Path.GetFullPath(Path.GetDirectoryName(FCSProjFile) + "/" +
                                                                  CSFile.Replace("\\", "/"));
                                    }
                                    else if (!Path.IsPathRooted(CSFile))
                                    {
                                        CSFile = Path.GetFullPath(Path.GetDirectoryName(ResourceXFile) + "/" +
                                                                  CSFile);
                                    }

                                    NamespaceAndClass = GetNamespaceAndClass(CSFile);
                                }

                                //"../../../../tmp/" +
                                string ResourcesFile = NamespaceAndClass + ".resources";

                                if (File.Exists(ResourceXFile))
                                {
                                    Environment.CurrentDirectory = Path.GetDirectoryName(ResourceXFile);

                                    ResXResourceReader ResXReader = new ResXResourceReader(ResourceXFile);
                                    FileStream         fs         = new FileStream(ResourcesFile, FileMode.OpenOrCreate, FileAccess.Write);
                                    IResourceWriter    writer     = new ResourceWriter(fs);

                                    foreach (DictionaryEntry d in ResXReader)
                                    {
                                        writer.AddResource(d.Key.ToString(), d.Value);
                                    }

                                    writer.Close();

                                    parameters.EmbeddedResources.Add(ResourcesFile);
                                }
                                else
                                {
                                    Console.WriteLine("Warning: cannot find resource file " + ResourceXFile);
                                }
                            }
                            else
                            {
                                parameters.EmbeddedResources.Add(ResourceXFile);
                            }
                        }
                    }
                }
            }

            CompilerResults results = csc.CompileAssemblyFromFile(parameters, src.ToArray());

            bool result = true;

            foreach (CompilerError error in results.Errors)
            {
                Console.WriteLine(error.ToString());

                if (!error.IsWarning)
                {
                    result = false;
                }
            }

            if (!result)
            {
                FailTask fail = new FailTask();
                this.CopyTo(fail);
                fail.Message = "compiler error(s)";
                fail.Execute();
            }

            return(result);
        }