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 void AddPackageReference(ProjectElementContainer itemGroup, PackageReference packageReference) { var centralPackageItem = itemGroup.ContainingProject.CreateItemElement(PackageReferenceTypeTag); centralPackageItem.Update = packageReference.Name; itemGroup.AppendChild(centralPackageItem); centralPackageItem.AddMetadataAsAttribute(VersionMetadataAttribute, packageReference.Version); }
internal ProjectPropertyElement ToProjectPropertyElement(ProjectElementContainer parent) { ProjectPropertyElement property = parent.ContainingProject.CreatePropertyElement(Name); property.Value = EvaluatedValue; parent.AppendChild(property); return(property); }
/// <summary> /// Adds an <ItemGroup /> element to the specifed parent. /// </summary> /// <param name="parent">A parent <see cref="ProjectElementContainer" /> to add the item group to.</param> /// <param name="condition">An optional condition to add to the item group.</param> /// <param name="label">An optional label to add to the item group.</param> /// <returns>The current <see cref="ProjectCreator" />.</returns> protected ProjectItemGroupElement ItemGroup(ProjectElementContainer parent, string?condition = null, string?label = null) { ProjectItemGroupElement itemGroup = RootElement.CreateItemGroupElement(); parent.AppendChild(itemGroup); itemGroup.Condition = condition; itemGroup.Label = label; return(itemGroup); }
/// <summary> /// Creates a ProjectItemDefinitionElement representing this instance. /// </summary> internal ProjectItemDefinitionElement ToProjectItemDefinitionElement(ProjectElementContainer parent) { ProjectItemDefinitionElement element = parent.ContainingProject.CreateItemDefinitionElement(ItemType); parent.AppendChild(element); foreach (ProjectMetadataInstance metadataInstance in _metadata) { element.AddMetadata(metadataInstance.Name, metadataInstance.EvaluatedValue); } return(element); }
/// <summary> /// Adds a <PropertyGroup /> element to the specified <see cref="ProjectElementContainer" />. /// </summary> /// <param name="parent">The parent <see cref="ProjectElementContainer" /> to add the property group to.</param> /// <param name="condition">An optional condition to add to the property group.</param> /// <param name="label">An optional label to add to the property group.</param> /// <returns>The <see cref="ProjectElementContainer" /> that was added.</returns> protected ProjectPropertyGroupElement PropertyGroup(ProjectElementContainer parent, string?condition = null, string?label = null) { ProjectPropertyGroupElement propertyGroup = RootElement.CreatePropertyGroupElement(); parent.AppendChild(propertyGroup); propertyGroup.Condition = condition; propertyGroup.Label = label; return(propertyGroup); }
private void AddProperties(ProjectElementContainer parent, PropertyValue value, bool includeAddtionalProperties) { var propertyGroup = parent.ContainingProject.CreatePropertyGroupElement(); parent.AppendChild(propertyGroup); propertyGroup.AddProperty(value.Property.Name, value.Value); if (includeAddtionalProperties) { foreach (var additionalProperty in value.AdditionalProperties) { propertyGroup.AddProperty(additionalProperty.Key, additionalProperty.Value); } } }
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); }