Skip to content

Commit

Permalink
Correct handling for Update-attribute on -r (revert)
Browse files Browse the repository at this point in the history
  • Loading branch information
artnim committed Apr 6, 2024
1 parent b56cc3b commit da4004e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion CentralisedPackageConverter/PackageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ private void RevertProject(FileInfo project, bool dryRun, Encoding encoding, str
if( packageReference.Parent is not null )
{
var condition = GetAttributeValue( packageReference.Parent, "Condition" ) ?? string.Empty;
var package = GetAttributeValue( packageReference, "Include" );
if (!TryGetAttributeValue(packageReference, "Include", out var package))
{
if (!TryGetAttributeValue(packageReference, "Update", out package))
{
Console.WriteLine("Package reference has no Include or Update attribute. Skipping...");
continue;
}
}

if( this.referencesByConditionThenName.TryGetValue( condition, out var packagesByName ) )
{
Expand Down Expand Up @@ -244,6 +251,22 @@ private IEnumerable<XElement> GetDescendants(XDocument xml, string name)
return attr?.Value;
}

/// <summary>
/// Try to get an attribute value from an XML Element
/// </summary>
/// <param name="elem">XML element. If null, result is false.</param>
/// <param name="name">Attribute name, checked namespce- and case-insensitive (pick first)</param>
/// <param name="value">The attribute value.</param>
/// <returns>Boolean whether attribute was picked</returns>
private static bool TryGetAttributeValue(XElement? elem, string name, out string? value)
{
var attr = elem?.Attributes().FirstOrDefault(x =>
string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase));

value = attr?.Value;
return value != null;
}

/// <summary>
/// Safely delete attributes with a name.
/// </summary>
Expand Down

0 comments on commit da4004e

Please sign in to comment.