Skip to content

Commit

Permalink
Added support for nuget range notation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Dec 9, 2024
1 parent 20280f7 commit 8fd17a3
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/NuPU/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,23 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
foreach (var package in packages.Where(p => string.IsNullOrWhiteSpace(updateCommandSettings.Package) || string.Equals(p.Id, updateCommandSettings.Package, StringComparison.OrdinalIgnoreCase)))
{
if (skip) break;
NuGetVersion nugetVersion = null;
NuGetVersion minNuGetVersion = null;
var isMinInclusive = true;
NuGetVersion maxNuGetVersion = null;
var isMaxInclusive = true;

var packageVersion = package.Version ?? GetPackageVersionFromProps(package.Id, csProjFile.Directory, rootDir);

if (VersionRange.TryParse(packageVersion, out VersionRange versionRange))
{
nugetVersion = versionRange.MinVersion;
minNuGetVersion = versionRange.MinVersion;
isMinInclusive = versionRange.IsMinInclusive;
maxNuGetVersion = versionRange.MaxVersion;
isMaxInclusive = versionRange.IsMaxInclusive;
}
else if (NuGetVersion.TryParse(packageVersion, out NuGetVersion parsedVersion))
{
nugetVersion = parsedVersion;
minNuGetVersion = parsedVersion;
}
else
{
Expand All @@ -111,7 +117,19 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();
using var cacheContext = new SourceCacheContext();
var allVersions = await resource.GetAllVersionsAsync(package.Id, cacheContext, NullLogger.Instance, CancellationToken.None);
var newerVersions = allVersions.Where(v => v > nugetVersion).ToList();
var newerVersions = allVersions
.Where(v =>
{
var isAboveMin = minNuGetVersion == null ||
(isMinInclusive ? v >= minNuGetVersion : v > minNuGetVersion);

var isBelowMax = maxNuGetVersion == null ||
(isMaxInclusive ? v <= maxNuGetVersion : v < maxNuGetVersion);

return isAboveMin && isBelowMax;
})
.ToList();

if (newerVersions.Count == 0)
{
continue;
Expand All @@ -120,18 +138,18 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
var stableVersions = newerVersions.Where(v => !v.IsPrerelease);

var versionsToShow = new List<NuGetVersion>();
versionsToShow.AddRange(HighestMajor(stableVersions, nugetVersion));
versionsToShow.AddRange(HighestMinor(stableVersions, nugetVersion));
versionsToShow.AddRange(HighestPatch(stableVersions, nugetVersion));
versionsToShow.AddRange(HighestRevision(stableVersions, nugetVersion));
versionsToShow.AddRange(HighestMajor(stableVersions, minNuGetVersion));
versionsToShow.AddRange(HighestMinor(stableVersions, minNuGetVersion));
versionsToShow.AddRange(HighestPatch(stableVersions, minNuGetVersion));
versionsToShow.AddRange(HighestRevision(stableVersions, minNuGetVersion));

if (updateCommandSettings.IncludePrerelease)
{
var prereleaseVersions = newerVersions.Where(v => v.IsPrerelease);
versionsToShow.AddRange(HighestMajor(prereleaseVersions, nugetVersion));
versionsToShow.AddRange(HighestMinor(prereleaseVersions, nugetVersion));
versionsToShow.AddRange(HighestPatch(prereleaseVersions, nugetVersion));
versionsToShow.AddRange(HighestRevision(prereleaseVersions, nugetVersion));
versionsToShow.AddRange(HighestMajor(prereleaseVersions, minNuGetVersion));
versionsToShow.AddRange(HighestMinor(prereleaseVersions, minNuGetVersion));
versionsToShow.AddRange(HighestPatch(prereleaseVersions, minNuGetVersion));
versionsToShow.AddRange(HighestRevision(prereleaseVersions, minNuGetVersion));
}

if (versionsToShow.Count == 0)
Expand All @@ -140,9 +158,9 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
}

var choices = new List<string>();
var currentVersionString = $"{nugetVersion.OriginalVersion} (current)";
var currentVersionString = $"{minNuGetVersion.OriginalVersion} (current)";
choices.Add(currentVersionString);
choices.AddRange(versionsToShow.OrderBy(v => v).Select(v => Colored(nugetVersion, v)));
choices.AddRange(versionsToShow.OrderBy(v => v).Select(v => Colored(minNuGetVersion, v)));
var skipString = "[grey]Skip project[/]";
choices.Add(skipString);

Expand Down

0 comments on commit 8fd17a3

Please sign in to comment.