protected override void Undo(string csprojFile)
        {
            RefrenceItem refrenceItem = RefrenceItems[csprojFile];

            if (refrenceItem == null)
            {
                return;
            }

            XDocument      doc     = XDocument.Load(csprojFile);
            XmlNodeFeature feature = new XmlNodeFeature(refrenceItem.NewElement.Name.LocalName)
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SolutionConfig.DllName.Trim() }
                }
            };

            var selectElement = FineXElement(doc, feature);

            // 添加回原来的节点
            selectElement.Parent.Add(refrenceItem.SourceElement);

            // 删除新的节点
            selectElement.Remove();

            doc.Save(csprojFile);
        }
Exemple #2
0
        protected XElement FineXElement(XDocument doc, XmlNodeFeature feature)
        {
            List <XElement> itemGroups = doc.Root?.Elements().Where(e => e.Name.LocalName == "ItemGroup").ToList();

            if (itemGroups == null)
            {
                return(null);
            }

            List <XElement> refrenceElements = new List <XElement>();

            foreach (XElement itemGroup in itemGroups)
            {
                refrenceElements.AddRange(itemGroup.Elements());
            }

            return(refrenceElements.FirstOrDefault(feature.Match));
        }
        protected override void Change(string csprojFile)
        {
            XDocument doc = XDocument.Load(csprojFile);

            XmlNodeFeature feature1 = new XmlNodeFeature("Reference")
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SolutionConfig.DllName.Trim() }
                }
            };

            XmlNodeFeature feature2 = new XmlNodeFeature("PackageReference")
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SolutionConfig.DllName.Trim() }
                }
            };

            var selectElement = FineXElement(doc, feature1);

            if (selectElement != null)
            {
                XmlElementFactory.ChangeReferenceNode(ref selectElement, SolutionConfig.NewFileAbsolutePath);
                doc.Save(csprojFile);
                return;
            }

            selectElement = FineXElement(doc, feature2);
            if (selectElement != null)
            {
                // 此方法创建出来的节点,没有 版本号 等其它信息,实际使用中会出问题。
                var element = XmlElementFactory.CreateReferenceNode(SolutionConfig.DllName.Trim(), SolutionConfig.NewFileAbsolutePath, doc.Root.Name.NamespaceName);
                selectElement.Parent.Add(element);
                selectElement.Remove();
                doc.Save(csprojFile);
                return;
            }
        }
        protected override void Change(string csprojFile)
        {
            XDocument doc = XDocument.Load(csprojFile);

            XmlNodeFeature feature1 = new XmlNodeFeature("Reference")
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SolutionConfig.DllName.Trim() }
                }
            };

            XmlNodeFeature feature2 = new XmlNodeFeature("PackageReference")
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SolutionConfig.DllName.Trim() }
                }
            };

            XmlNodeFeature feature3 = new XmlNodeFeature("Reference")
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SourceCsprojRootNamespace }
                }
            };

            XmlNodeFeature feature4 = new XmlNodeFeature("PackageReference")
            {
                SimilarAttributeFeature = new Dictionary <string, string>()
                {
                    { "Include", SourceCsprojRootNamespace }
                }
            };

            //var selectElement = FindReferenceItem(doc);
            var selectElement = FineXElement(doc, feature1) ?? FineXElement(doc, feature2);

            if (selectElement == null && !string.IsNullOrWhiteSpace(SourceCsprojRootNamespace))
            {
                selectElement = FineXElement(doc, feature3) ?? FineXElement(doc, feature4);
            }

            if (selectElement == null)
            {
                return;
            }

            // 添加新节点。
            XElement newElement;

            //if (string.IsNullOrWhiteSpace(SourceCsprojGuid) || string.IsNullOrWhiteSpace(SourceCsprojName))
            //{
            //    newElement = XmlElementFactory.CreateProjectReferenceNode(SourceCsprojFile, doc.Root.Name.NamespaceName);
            //}
            //else
            //{
            //    newElement = XmlElementFactory.CreateProjectReferenceNode(SourceCsprojFile, SourceCsprojGuid, SourceCsprojName,
            //            doc.Root.Name.NamespaceName);
            //}

            newElement = XmlElementFactory.CreateProjectReferenceNode(SourceCsprojFile, doc.Root.Name.NamespaceName);
            selectElement.Parent.Add(newElement);

            // 删除此节点。
            selectElement.Remove();

            doc.Save(csprojFile);

            RefrenceItems.Add(csprojFile, new RefrenceItem(csprojFile, selectElement, newElement));
        }