public void AddToDoc(XDocument xdoc, XNamespace ns) { ConsoleHelper.WriteLine($" Adding assembly ref: '{Include}", true); XElement newref; if (Hintpath == null) { newref = new XElement(ns + "Reference", new XAttribute("Include", Include), new XElement(ns + "SpecificVersion", "False") ); } else { newref = new XElement(ns + "Reference", new XAttribute("Include", Include), new XElement(ns + "SpecificVersion", "False"), new XElement(ns + "HintPath", Hintpath) ); } // Sort insert var groups = from el in xdoc.Element(ns + "Project").Elements(ns + "ItemGroup") where el.Element(ns + "Reference") != null select el; if (groups.Count() == 0) { ConsoleHelper.ColorWrite(ConsoleColor.Yellow, $"NotImplementedException: Cannot insert reference!"); return; } var refs = from el in groups.ElementAt(0).Elements(ns + "Reference") where el.Attribute("Include") != null orderby el.Attribute("Include").Value select el; if (Include.CompareTo(refs.First().Attribute("Include").Value) < 0) { groups.ElementAt(0).AddFirst(newref); } else if (Include.CompareTo(refs.Last().Attribute("Include").Value) > 0) { refs.Last().AddAfterSelf(newref); } else { for (int i = 0; i < refs.Count() - 1; i++) { string inc1 = refs.ElementAt(i).Attribute("Include").Value; string inc2 = refs.ElementAt(i + 1).Attribute("Include").Value; if (Include.CompareTo(inc1) > 0 && Include.CompareTo(inc2) < 0) { refs.ElementAt(i).AddAfterSelf(newref); } } } }
public override int CompareTo(object obj) { var target = obj as ProjectReference; if (target == null) { throw new InvalidCastException("Must compare to same type."); } return(Include.CompareTo(target.Include)); }
public void AddToDoc(XDocument xdoc, XNamespace ns) { ConsoleHelper.WriteLine($" Adding proj ref: '{Include}'", true); XElement newref = new XElement(ns + "ProjectReference", new XAttribute("Include", Include), new XElement(ns + "Project", Project), new XElement(ns + "Name", Name) ); // Sort insert var groups = from el in xdoc.Element(ns + "Project").Elements(ns + "ItemGroup") where el.Element(ns + "ProjectReference") != null select el; if (groups.Count() == 0) { groups = from el in xdoc.Element(ns + "Project").Elements(ns + "ItemGroup") select el; XElement newgroup = new XElement(ns + "ItemGroup", newref); groups.Last().AddAfterSelf(newgroup); } else { var refs = from el in groups.Elements(ns + "ProjectReference") where el.Attribute("Include") != null orderby el.Attribute("Include").Value select el; if (Include.CompareTo(refs.First().Attribute("Include").Value) < 0) { groups.ElementAt(0).AddFirst(newref); } else if (Include.CompareTo(refs.Last().Attribute("Include").Value) > 0) { refs.Last().AddAfterSelf(newref); } else { for (int i = 0; i < refs.Count() - 1; i++) { string inc1 = refs.ElementAt(i).Attribute("Include").Value; string inc2 = refs.ElementAt(i + 1).Attribute("Include").Value; if (Include.CompareTo(inc1) > 0 && Include.CompareTo(inc2) < 0) { refs.ElementAt(i).AddAfterSelf(newref); } } } } }