Skip to content

Commit

Permalink
feat: Support for *Version properties update
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Apr 3, 2024
1 parent 10a7f1e commit 964b56d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/NvGet.Tools.Updater/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ properties.json example:
]
```
In this case the `UnoVersion` property will be updated to the latest version of `Uno.UI` found in the solution.

## Supported types of updates

The nuget updater supports updating:
- `.csproj`, `Directory.Build.props`, `Directory.Build.targets` and `Directory.Packages.props`
For this type of files, the tool will update:
- `PackageReference` and `PackageVersion` items
- MSBuild properties using named this way `UnoWinUIVersion` or `UnoExtensionsNavigationVersion`
- `.nuspec`
For this type of files, the tool will update `reference` entries.
- `global.json`
For this type of files, the tool will update `msbuild-sdk` entries
29 changes: 29 additions & 0 deletions src/NvGet/Extensions/XmlDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ public static PackageIdentity[] GetPackageReferences(this XmlDocument document)
}
}

// find all nodes inside a PropertyGroup node that for which the name is ending by Version
var propertyGroupVersionReferences = document
.SelectElements("PropertyGroup")
.SelectMany(pg => pg.SelectNodes("*").OfType<XmlElement>())
.Where(e => e.LocalName.EndsWith("Version", StringComparison.OrdinalIgnoreCase));

foreach(var versionProperty in propertyGroupVersionReferences)
{
var originalTrimmedName = versionProperty
.LocalName
.TrimEnd("Version");

var nameParts =
originalTrimmedName
.Select((c, i) =>
i > 0
&& char.IsUpper(c)
&& !char.IsUpper(originalTrimmedName[i - 1])
? "." + c
: c.ToString());

var packageName = string.Concat(nameParts).ToLowerInvariant();

if(NuGetVersion.TryParse(versionProperty.InnerText, out var nugetVersion))
{
references.Add(CreatePackageIdentity(packageName, versionProperty.InnerText));
}
}

return references
.Trim()
.ToArray();
Expand Down
11 changes: 10 additions & 1 deletion src/NvGet/Helpers/SolutionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ private static async Task<string[]> GetDirectoryFiles(CancellationToken ct, stri
else
{
var solutionFolder = Path.GetDirectoryName(solutionPath);
file = Path.Combine(solutionFolder, target.GetDescription());

if(target is FileType.DirectoryProps or FileType.DirectoryTargets or FileType.GlobalJson or FileType.CentralPackageManagement)
{
var matchingFiles = await FileHelper.GetFiles(ct, solutionFolder, nameFilter: target.GetDescription());
return matchingFiles.ToArray();
}
else
{
file = Path.Combine(solutionFolder, target.GetDescription());
}
}

if(file.HasValue() && await FileHelper.Exists(file))
Expand Down
18 changes: 18 additions & 0 deletions src/NvGet/Tools/Updater/Extensions/XmlDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Xml;
using Newtonsoft.Json;
using NuGet.Versioning;
using NvGet.Extensions;
using NvGet.Helpers;
using NvGet.Tools.Updater.Log;
Expand Down Expand Up @@ -95,6 +96,23 @@ UpdateOperation operation
}
}

var propertyGroupVersionReferences = document
.SelectElements("PropertyGroup")
.SelectMany(pg => pg.SelectNodes(packageId.Replace(".", "") + "Version").OfType<XmlElement>());

foreach(var versionProperty in propertyGroupVersionReferences)
{
if(NuGetVersion.TryParse(versionProperty.InnerText, out var previousVersion))
{
var currentOperation = operation.WithPreviousVersion(versionProperty.InnerText);

if(currentOperation.ShouldProceed())
{
versionProperty.InnerText = currentOperation.UpdatedVersion.ToString();
}
}
}

return operations;
}

Expand Down

0 comments on commit 964b56d

Please sign in to comment.