public static bool AddProjectFlavorIfNotExists(Microsoft.Build.Evaluation.Project project, string flavor)
        {
            ProjectPropertyElement property = project.Xml.Properties.FirstOrDefault(
                p => p.Name.Equals("ProjectTypeGuids", StringComparison.CurrentCultureIgnoreCase));

            if (property != null)
            {
                if (property.Value.IndexOf(flavor) == -1)
                {
                    DTEUtil.EnsureFileIsCheckout(project.FullPath);
                    if (string.IsNullOrEmpty(property.Value))
                    {
                        property.Value = string.Format("{0};{1}", flavor, CSharpProjectGUI);
                    }
                    else
                    {
                        property.Value = string.Format("{0};{1}", flavor, property.Value);
                    }
                    return(true); //ProjectTypeGuids updated
                }
                else
                {
                    return(false); //ProjectTypeGuids already has this flavor
                }
            }

            // ProjectTypeGuids not present
            DTEUtil.EnsureFileIsCheckout(project.FullPath);
            project.Xml.AddProperty("ProjectTypeGuids", string.Format("{0};{1}", flavor, CSharpProjectGUI));
            return(true);
        }
        private static bool UpdateImport(Microsoft.Build.Evaluation.Project project, string oldValue, string newValue)
        {
            ProjectImportElement import = project.Xml.Imports.FirstOrDefault(
                p => p.Project.Equals(oldValue, StringComparison.CurrentCultureIgnoreCase));

            if (import != null)
            {
                DTEUtil.EnsureFileIsCheckout(project.FullPath);
                import.Project   = newValue;
                import.Condition = string.Format("Exists('{0}')", newValue);
                return(true);
            }
            return(false);
        }
        private static bool RemoveGlobalProperty(Microsoft.Build.Evaluation.Project project, string name)
        {
            ProjectPropertyGroupElement globals = project.Xml.PropertyGroups.FirstOrDefault(
                p => p.Label.Equals("Globals", StringComparison.CurrentCultureIgnoreCase));
            ProjectPropertyElement property = globals.Properties.FirstOrDefault(
                p => p.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));

            if (property != null)
            {
                DTEUtil.EnsureFileIsCheckout(project.FullPath);
                globals.RemoveChild(property);
                return(true);
            }
            return(false);
        }
        public static bool RemoveProjectFlavorIfExists(Microsoft.Build.Evaluation.Project project, string flavor)
        {
            ProjectPropertyElement property = project.Xml.Properties.FirstOrDefault(
                p => p.Name.Equals("ProjectTypeGuids", StringComparison.CurrentCultureIgnoreCase));

            if (property != null && property.Value.IndexOf(flavor) != -1)
            {
                DTEUtil.EnsureFileIsCheckout(project.FullPath);
                property.Value = property.Value.Replace(flavor, "").Trim(new char[] { ';' });
                if (property.Value.Equals(CSharpProjectGUI, StringComparison.CurrentCultureIgnoreCase))
                {
                    property.Parent.RemoveChild(property);
                }
                return(true); //flavor removed
            }
            return(false);
        }
        public static bool RemoveIceBuilderFromProject(Microsoft.Build.Evaluation.Project project, bool keepProjectFlavor = false)
        {
            bool modified = false;

            if (project != null)
            {
                if (IsCppProject(project))
                {
                    modified = RemoveCppGlobalProperties(project);
                    modified = RemoveImport(project, IceBuilderCppProps) || modified;
                    modified = RemoveImport(project, IceBuilderCppTargets) || modified;
                }
                else if (IsCSharpProject(project))
                {
                    modified = RemoveCsharpGlobalProperties(project);
                    modified = RemoveImport(project, IceBuilderCSharpProps) || modified;
                    modified = RemoveImport(project, IceBuilderCSharpTargets) || modified;
                    if (!keepProjectFlavor)
                    {
                        modified = RemoveProjectFlavorIfExists(project, Package.IceBuilderOldFlavor) || modified;
                    }
                }

                //
                // Remove EnsureIceBuilderImports target
                //
                var target = project.Xml.Targets.FirstOrDefault(
                    t => t.Name.Equals("EnsureIceBuilderImports", StringComparison.CurrentCultureIgnoreCase));
                if (target != null)
                {
                    DTEUtil.EnsureFileIsCheckout(project.FullPath);
                    if (target.Parent != null)
                    {
                        target.Parent.RemoveChild(target);
                    }
                    else
                    {
                        project.Xml.RemoveChild(target);
                    }
                }
            }
            return(modified);
        }
        private static bool RemoveImport(Microsoft.Build.Evaluation.Project project, string import)
        {
            ProjectElement element = project.Xml.Imports.FirstOrDefault(
                p => p.Project.Equals(import, StringComparison.CurrentCultureIgnoreCase));

            if (element != null)
            {
                DTEUtil.EnsureFileIsCheckout(project.FullPath);
                if (element.Parent != null)
                {
                    element.Parent.RemoveChild(element);
                }
                else
                {
                    project.Xml.RemoveChild(element);
                }
            }
            return(element != null);
        }