Skip to content

Commit

Permalink
Add version range support
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Apr 29, 2020
1 parent 71c22e4 commit 203dbae
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 35 deletions.
5 changes: 2 additions & 3 deletions src/NuGet.Updater.Tests/ConsoleArgsParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ConsoleArgsParserTests
private const string SomePublicFeed = "https://pkgs.dev.azure.com/qwe/_packaging/asd/nuget/v3/index.json";
private const string SomePrivateFeed = "https://pkgs.dev.azure.com/qwe/_packaging/asd/nuget/v3/index.json|hunter2";
private const string PinnedVersionJsonPath = @"Resources\version_overrides.json";

[TestMethod]
public void Given_HelpArgument_ContextIsHelp()
{
Expand Down Expand Up @@ -167,8 +167,7 @@ public void Given_UpdaterParametersArgument_ContextTargetVersionIsSet()

Assert.IsFalse(context.HasError);

var actualValues = context.Parameters.VersionOverrides
.ToDictionary(x => x.Key, x => x.Value);
var actualValues = (ICollection)context.Parameters.VersionOverrides;
var expectedValues = ConsoleArgsContext.LoadManualOperations(PinnedVersionJsonPath);

CollectionAssert.AreEqual(expectedValues, actualValues);
Expand Down
49 changes: 48 additions & 1 deletion src/NuGet.Updater.Tests/PackageReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PackageReferenceTests
private static readonly Dictionary<string, string[]> TestPackages = new Dictionary<string, string[]>
{
{"nventive.NuGet.Updater", new[] { "1.0-beta.1" } },
{"Uno.UI", new[] { "2.1.39", "2.2.0", "2.3.0-dev.44", "2.3.0-dev.48", "2.3.0-dev.58" } },
};

private static readonly TestPackageFeed TestFeed = new TestPackageFeed(TestFeedUri, TestPackages);
Expand Down Expand Up @@ -72,13 +73,59 @@ public async Task GivenManualUpdates_AndVersionNotInFeed_ManualVersionIsFound()
Feeds = { TestFeed },
VersionOverrides =
{
{ reference.Identity.Id, reference.Identity.Version },
{ reference.Identity.Id, (true, new VersionRange(reference.Identity.Version, true, reference.Identity.Version, true)) },
},
};

var version = await reference.GetLatestVersion(CancellationToken.None, parameters);

Assert.AreEqual(version.Version, reference.Identity.Version);
}

[TestMethod]
public async Task GivenRangeOverrides_CorrectVersionsAreResolved()
{
var reference = new PackageReference("Uno.UI", "2.1.39");

var parameters = new UpdaterParameters
{
TargetVersions = { "dev", "stable" },
Feeds = { TestFeed },
VersionOverrides =
{
{ reference.Identity.Id, (false, VersionRange.Parse("(,2.3.0-dev.48]")) },
},
};

var version = await reference.GetLatestVersion(CancellationToken.None, parameters);

Assert.AreEqual(NuGetVersion.Parse("2.3.0-dev.48"), version.Version);

parameters.VersionOverrides["Uno.UI"] = (false, VersionRange.Parse("(,2.3.0-dev.48)"));

version = await reference.GetLatestVersion(CancellationToken.None, parameters);

Assert.AreEqual(NuGetVersion.Parse("2.3.0-dev.44"), version.Version);
}

[TestMethod]
public async Task GivenRangeOverrides_CorrectVersionsAreResolved_AndTargetVersionIsHonored()
{
var reference = new PackageReference("Uno.UI", "2.1.39");

var parameters = new UpdaterParameters
{
TargetVersions = { "stable" },
Feeds = { TestFeed },
VersionOverrides =
{
{ reference.Identity.Id, (false, VersionRange.Parse("(,2.3.0-dev.48]")) },
},
};

var version = await reference.GetLatestVersion(CancellationToken.None, parameters);

Assert.AreEqual(NuGetVersion.Parse("2.2.0"), version.Version);
}
}
}
24 changes: 0 additions & 24 deletions src/NuGet.Updater.Tests/SolutionHelperTests.cs

This file was deleted.

14 changes: 12 additions & 2 deletions src/NuGet.Updater.Tool/Arguments/ConsoleArgsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,24 @@ Action<string> TryParseAndSet<T>(Func<string, T> parse, Action<T> set)

public void WriteOptionDescriptions(TextWriter writer) => CreateOptionsFor(default).WriteOptionDescriptions(writer);

internal static Dictionary<string, NuGetVersion> LoadManualOperations(string inputFilePath)
internal static Dictionary<string, (bool, VersionRange)> LoadManualOperations(string inputFilePath)
{
using(var fileReader = File.OpenText(inputFilePath))
using(var jsonReader = new JsonTextReader(fileReader))
{
var result = JsonSerializer.CreateDefault().Deserialize<IEnumerable<UpdateResult>>(jsonReader);

return result.ToDictionary(r => r.PackageId, r => new NuGetVersion(r.UpdatedVersion));
return result.ToDictionary(
r => r.PackageId,
r => NuGetVersion.TryParse(r.UpdatedVersion, out var version) ?
(true, new VersionRange(
minVersion: version,
includeMinVersion: true,
maxVersion: version,
includeMaxVersion: true,
floatRange: null,
originalString: null)) :
(false, VersionRange.Parse(r.UpdatedVersion)));
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/NuGet.Updater.Tool/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ nugetupdater -s=MySolution.sln -n -f=https://pkgs.dev.azure.com/account/_packagi
```
nugetupdater -s=MySolution.sln -n --allowDowngrade
```

- Update packages to specific versions (forcefully and/or with nuget version ranges). See : https://docs.microsoft.com/en-us/nuget/concepts/package-versioning#version-ranges
```
nugetupdater -s=MySolution.sln -n -v=dev -v=stable --allowDowngrade --versionOverrides=versions.json
```
Versions.json example:
```
[
{
"PackageId": "Uno.UI",
"UpdatedVersion": "2.3.0-dev.76" -> Force 2.3.0-dev.76
},
{
"PackageId": "Uno.Wasm.Bootstrap",
"UpdatedVersion": "(,1.2.0-dev.18]" -> Resolves 1.2.0-dev.18 (-v=dev + -v=stable), 1.0.10 (-v=stable)
}
]
```
2 changes: 2 additions & 0 deletions src/NuGet.Updater.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NuGet.Downloader.Tests", "N
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
NuGet.Shared\NuGet.Shared.projitems*{29277270-8efe-41a3-8cd8-d54ebf514c41}*SharedItemsImports = 5
NuGet.Shared\NuGet.Shared.projitems*{3890cca3-b7cf-450b-a6cd-4a4106b0008c}*SharedItemsImports = 13
NuGet.Shared\NuGet.Shared.projitems*{912018bd-d988-4c90-b4ec-ba1c171207c3}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Updater/Entities/UpdaterParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public class UpdaterParameters
public string PackageAuthor { get; set; }

/// <summary>
/// Gets the version to set for specific packages.
/// Gets the version range overrides for specific packages.
/// </summary>
public IDictionary<string, NuGetVersion> VersionOverrides { get; } = new Dictionary<string, NuGetVersion>();
public IDictionary<string, (bool forceVersion, VersionRange range)> VersionOverrides { get; } = new Dictionary<string, (bool, VersionRange)>();

/// <summary>
/// Gets or sets a value indicating whether to actually write the updates to the files.
Expand Down
7 changes: 4 additions & 3 deletions src/NuGet.Updater/Extensions/PackageReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ public static async Task<FeedVersion> GetLatestVersion(
UpdaterParameters parameters
)
{
if(parameters.VersionOverrides.TryGetValue(reference.Identity.Id, out var manualVersion))
if(parameters.VersionOverrides.TryGetValue(reference.Identity.Id, out var manualVersion) && manualVersion.forceVersion)
{
PackageFeed.Logger.LogInformation($"Overriding version for {reference.Identity.Id}");
return new FeedVersion(manualVersion);
return new FeedVersion(manualVersion.range.MinVersion);
}

var availableVersions = await Task.WhenAll(parameters
.Feeds
.Select(f => f.GetPackageVersions(ct, reference, parameters.PackageAuthor))
);

var versionsPerTarget = availableVersions
.SelectMany(x => x)
.Where(v => manualVersion.range?.Satisfies(v.Version) ?? true)
.OrderByDescending(v => v)
.GroupBy(version => parameters.TargetVersions.FirstOrDefault(t => version.IsMatchingVersion(t, parameters.Strict)))
.Where(g => g.Key.HasValue());
Expand Down

0 comments on commit 203dbae

Please sign in to comment.