private int RemoveProjectToProjectReferenceAlternatives(string framework, string reference) { int numberOfRemovedRefs = 0; foreach (var r in GetIncludeAlternativesForRemoval(reference)) { foreach (var existingItem in ProjectRootElement.FindExistingItemsWithCondition(framework, r)) { ProjectElementContainer itemGroup = existingItem.Parent; itemGroup.RemoveChild(existingItem); if (itemGroup.Children.Count == 0) { itemGroup.Parent.RemoveChild(itemGroup); } numberOfRemovedRefs++; Reporter.Output.WriteLine(string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, r)); } } if (numberOfRemovedRefs == 0) { Reporter.Output.WriteLine(string.Format( CommonLocalizableStrings.ProjectReferenceCouldNotBeFound, reference)); } return(numberOfRemovedRefs); }
private void AddProperties(ProjectElementContainer parent, PropertyValue value, bool includeAddtionalProperties, string parsedValuePrefix = null, bool includePropertyValue = true) { var propertyGroup = parent.ContainingProject.CreatePropertyGroupElement(); parent.AppendChild(propertyGroup); if (includePropertyValue) { propertyGroup.AddProperty(value.Property.Name, value.Value); } if (!String.IsNullOrEmpty(parsedValuePrefix)) { propertyGroup.AddProperty(parsedValuePrefix + value.Property.Name, value.Value); } if (includeAddtionalProperties) { foreach (var additionalProperty in value.AdditionalProperties) { propertyGroup.AddProperty(additionalProperty.Key, additionalProperty.Value); } } if (propertyGroup.Children.Count == 0) { parent.RemoveChild(propertyGroup); } }
private static bool TryMoveElements(ImmutableArray <ProjectItemElement> elements, ProjectItemElement referenceElement, MoveAction moveAction) { Requires.NotNull(referenceElement, nameof(referenceElement)); ProjectElementContainer parent = referenceElement.Parent; if (parent == null || !elements.Any()) { return(false); } // Sanity check bool didAllElementsMove = true; switch (moveAction) { case MoveAction.Above: foreach (ProjectItemElement element in elements) { ProjectElementContainer elementParent = element.Parent; if (elementParent != null) { elementParent.RemoveChild(element); parent.InsertBeforeChild(element, referenceElement); } else { didAllElementsMove = false; } } break; case MoveAction.Below: // Iterate in reverse order when we are wanting to move elements down. // If we didn't do this, the end result would be the moved elements are reversed. for (int i = elements.Length - 1; i >= 0; --i) { ProjectItemElement element = elements[i]; ProjectElementContainer elementParent = element.Parent; if (elementParent != null) { elementParent.RemoveChild(element); parent.InsertAfterChild(element, referenceElement); } else { didAllElementsMove = false; } } break; } return(didAllElementsMove); }
public static void Remove(this ProjectElement element) { if (element.Parent != null) { ProjectElementContainer parent = element.Parent; parent.RemoveChild(element); if (parent.Count == 0) { parent?.Parent.RemoveChild(parent); } } }
public override bool Execute() { foreach (string projectPath in Projects.Select(p => p.ItemSpec)) { var projectRoot = ProjectRootElement.Open(projectPath); var packageIdentities = GetPackageIdentityCollection(NuGetPackages); Dictionary <ProjectItemElement, string> updatedItems = new Dictionary <ProjectItemElement, string>(); ProjectElementContainer parent = null; foreach (var item in projectRoot.Items) { if (item.ItemType == "PackageReference") { PackageIdentity identity = packageIdentities.FirstOrDefault(p => p.Id == item.Include); if (identity != null) { // Save the PackageReference parent ItemGroup so we can append to it if (parent == null) { parent = item.Parent; } parent.RemoveChild(item); ProjectItemElement newItem = projectRoot.CreateItemElement("PackageReference", identity.Id); updatedItems.Add(newItem, identity.Version.ToNormalizedString()); } } } foreach (var item in updatedItems) { var appendItem = item.Key; // we must "root" an item to a parent before we can update its metadata. parent.AppendChild(appendItem); appendItem.AddMetadata("Version", item.Value, true); } projectRoot.Save(); } return(!Log.HasLoggedErrors); }
public void RemoveItem(VSProjectItem item) { ProjectElementContainer parent = ((ProjectElement)item.InternalProjectItem).Parent; parent.RemoveChild((ProjectElement)item.InternalProjectItem); }