Skip to content

Commit 8471913

Browse files
authored
Accept version ranges via package@version syntax (#49555)
1 parent 3c5e873 commit 8471913

14 files changed

+70
-54
lines changed

src/Cli/dotnet/Commands/Package/Add/PackageAddCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.DotNet.Cli.Commands.NuGet;
99
using Microsoft.DotNet.Cli.Extensions;
1010
using Microsoft.DotNet.Cli.Utils;
11-
using NuGet.Packaging.Core;
1211

1312
namespace Microsoft.DotNet.Cli.Commands.Package.Add;
1413

@@ -19,7 +18,7 @@ namespace Microsoft.DotNet.Cli.Commands.Package.Add;
1918
/// </param>
2019
internal class PackageAddCommand(ParseResult parseResult, string fileOrDirectory) : CommandBase(parseResult)
2120
{
22-
private readonly PackageIdentity _packageId = parseResult.GetValue(PackageAddCommandParser.CmdPackageArgument);
21+
private readonly PackageIdentityWithRange _packageId = parseResult.GetValue(PackageAddCommandParser.CmdPackageArgument);
2322

2423
public override int Execute()
2524
{
@@ -101,7 +100,7 @@ private static void DisposeTemporaryFile(string filePath)
101100
}
102101
}
103102

104-
private string[] TransformArgs(PackageIdentity packageId, string tempDgFilePath, string projectFilePath)
103+
private string[] TransformArgs(PackageIdentityWithRange packageId, string tempDgFilePath, string projectFilePath)
105104
{
106105
List<string> args = [
107106
"package",
@@ -111,11 +110,11 @@ private string[] TransformArgs(PackageIdentity packageId, string tempDgFilePath,
111110
"--project",
112111
projectFilePath
113112
];
114-
113+
115114
if (packageId.HasVersion)
116115
{
117116
args.Add("--version");
118-
args.Add(packageId.Version.ToString());
117+
args.Add(packageId.VersionRange.OriginalString);
119118
}
120119

121120
args.AddRange(_parseResult

src/Cli/dotnet/Commands/Package/Add/PackageAddCommandParser.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66
using System.CommandLine;
77
using System.CommandLine.Completions;
8+
using System.CommandLine.Parsing;
9+
using Microsoft.DotNet.Cli.Extensions;
810
using Microsoft.Extensions.EnvironmentAbstractions;
911
using NuGet.Versioning;
10-
using Microsoft.DotNet.Cli.Extensions;
11-
using System.CommandLine.Parsing;
12-
using NuGet.Packaging.Core;
1312

1413
namespace Microsoft.DotNet.Cli.Commands.Package.Add;
1514

1615
internal static class PackageAddCommandParser
1716
{
18-
public static readonly Argument<PackageIdentity> CmdPackageArgument = CommonArguments.RequiredPackageIdentityArgument()
17+
public static readonly Argument<PackageIdentityWithRange> CmdPackageArgument = CommonArguments.RequiredPackageIdentityArgument()
1918
.AddCompletions((context) =>
2019
{
2120
// we should take --prerelease flags into account for version completion
@@ -31,7 +30,7 @@ internal static class PackageAddCommandParser
3130
.AddCompletions((context) =>
3231
{
3332
// we can only do version completion if we have a package id
34-
if (context.ParseResult.GetValue(CmdPackageArgument) is PackageIdentity packageId && !packageId.HasVersion)
33+
if (context.ParseResult.GetValue(CmdPackageArgument) is { HasVersion: false } packageId)
3534
{
3635
// we should take --prerelease flags into account for version completion
3736
var allowPrerelease = context.ParseResult.GetValue(PrereleaseOption);
@@ -106,7 +105,7 @@ private static Command ConstructCommand()
106105

107106
private static void DisallowVersionIfPackageIdentityHasVersionValidator(OptionResult result)
108107
{
109-
if (result.Parent.GetValue(CmdPackageArgument) is PackageIdentity identity && identity.HasVersion)
108+
if (result.Parent.GetValue(CmdPackageArgument).HasVersion)
110109
{
111110
result.AddError(CliCommandStrings.ValidationFailedDuplicateVersion);
112111
}

src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,22 @@
77
using Microsoft.DotNet.Cli.Commands.Tool.Install;
88
using Microsoft.DotNet.Cli.Commands.Tool.Restore;
99
using Microsoft.DotNet.Cli.Commands.Tool.Run;
10-
using Microsoft.DotNet.Cli.Extensions;
1110
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
1211
using Microsoft.DotNet.Cli.ToolManifest;
1312
using Microsoft.DotNet.Cli.ToolPackage;
1413
using Microsoft.DotNet.Cli.Utils;
1514
using Microsoft.DotNet.Cli.Utils.Extensions;
16-
1715
using Microsoft.Extensions.EnvironmentAbstractions;
18-
using NuGet.Common;
1916
using NuGet.Configuration;
20-
using NuGet.Packaging.Core;
2117
using NuGet.Versioning;
2218

23-
2419
namespace Microsoft.DotNet.Cli.Commands.Tool.Execute;
2520

2621
internal class ToolExecuteCommand(ParseResult result, ToolManifestFinder? toolManifestFinder = null, string? currentWorkingDirectory = null) : CommandBase(result)
2722
{
2823
const int ERROR_CANCELLED = 1223; // Windows error code for "Operation canceled by user"
2924

30-
private readonly PackageIdentity _packageToolIdentityArgument = result.GetRequiredValue(ToolExecuteCommandParser.PackageIdentityArgument);
25+
private readonly PackageIdentityWithRange _packageToolIdentityArgument = result.GetRequiredValue(ToolExecuteCommandParser.PackageIdentityArgument);
3126
private readonly IEnumerable<string> _forwardArguments = result.GetValue(ToolExecuteCommandParser.CommandArgument) ?? Enumerable.Empty<string>();
3227
private readonly bool _allowRollForward = result.GetValue(ToolExecuteCommandParser.RollForwardOption);
3328
private readonly string? _configFile = result.GetValue(ToolExecuteCommandParser.ConfigOption);

src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommandParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
using System.CommandLine;
55
using Microsoft.DotNet.Cli.Commands.Tool.Install;
6-
using NuGet.Packaging.Core;
76

87
namespace Microsoft.DotNet.Cli.Commands.Tool.Execute;
98

109
internal static class ToolExecuteCommandParser
1110

1211
{
13-
public static readonly Argument<PackageIdentity> PackageIdentityArgument = ToolInstallCommandParser.PackageIdentityArgument;
12+
public static readonly Argument<PackageIdentityWithRange> PackageIdentityArgument = ToolInstallCommandParser.PackageIdentityArgument;
1413

1514
public static readonly Argument<IEnumerable<string>> CommandArgument = new("commandArguments")
1615
{

src/Cli/dotnet/Commands/Tool/Install/ParseResultExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class ParseResultExtension
1313
{
1414
public static VersionRange GetVersionRange(this ParseResult parseResult)
1515
{
16-
var packageVersionFromIdentityArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument)?.Version?.ToString();
16+
var packageVersionFromIdentityArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument).VersionRange?.OriginalString;
1717
var packageVersionFromVersionOption = parseResult.GetValue(ToolInstallCommandParser.VersionOption);
1818

1919
// Check that only one of these has a value

src/Cli/dotnet/Commands/Tool/Install/ToolInstallCommandParser.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.CommandLine;
5-
using System.CommandLine.Parsing;
65
using Microsoft.DotNet.Cli.Commands.Tool.Common;
76
using Microsoft.DotNet.Cli.Commands.Tool.Search;
87
using Microsoft.DotNet.Cli.Extensions;
9-
using NuGet.Packaging.Core;
108

119
namespace Microsoft.DotNet.Cli.Commands.Tool.Install;
1210

1311
internal static class ToolInstallCommandParser
1412
{
15-
public static readonly Argument<PackageIdentity> PackageIdentityArgument = CommonArguments.RequiredPackageIdentityArgument();
13+
public static readonly Argument<PackageIdentityWithRange> PackageIdentityArgument = CommonArguments.RequiredPackageIdentityArgument();
1614

1715
public static readonly Option<string> VersionOption = new("--version")
1816
{

src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public ToolInstallGlobalOrToolPathCommand(
7171
_verifySignatures = verifySignatures;
7272
_currentWorkingDirectory = currentWorkingDirectory;
7373

74-
var packageIdArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument)?.Id;
74+
var packageIdArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument).Id;
7575

7676
_packageId = packageId ?? (packageIdArgument is not null ? new PackageId(packageIdArgument) : null);
7777
_configFilePath = parseResult.GetValue(ToolInstallCommandParser.ConfigOption);

src/Cli/dotnet/Commands/Tool/Install/ToolInstallLocalCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ToolInstallLocalCommand(
4646
: base(parseResult)
4747
{
4848
_updateAll = parseResult.GetValue(ToolUpdateCommandParser.UpdateAllOption);
49-
var packageIdArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument)?.Id;
49+
var packageIdArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument).Id;
5050
_packageId = packageId ?? (packageIdArgument is not null ? new PackageId(packageIdArgument) : null);
5151
_explicitManifestFile = parseResult.GetValue(ToolInstallCommandParser.ToolManifestOption);
5252

src/Cli/dotnet/Commands/Tool/Update/ToolUpdateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal static void EnsureEitherUpdateAllOrUpdateOption(
8282

8383
internal static void EnsureNoConflictPackageIdentityVersionOption(ParseResult parseResult)
8484
{
85-
if (!string.IsNullOrEmpty(parseResult.GetValue(ToolUpdateCommandParser.PackageIdentityArgument)?.Version?.ToString()) &&
85+
if (!string.IsNullOrEmpty(parseResult.GetValue(ToolUpdateCommandParser.PackageIdentityArgument)?.VersionRange?.OriginalString) &&
8686
!string.IsNullOrEmpty(parseResult.GetValue(ToolAppliedOption.VersionOption)))
8787
{
8888
throw new GracefulException(CliStrings.PackageIdentityArgumentVersionOptionConflict);

src/Cli/dotnet/Commands/Tool/Update/ToolUpdateCommandParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
using System.CommandLine;
55
using Microsoft.DotNet.Cli.Commands.Tool.Common;
66
using Microsoft.DotNet.Cli.Commands.Tool.Install;
7-
using NuGet.Packaging.Core;
87

98
namespace Microsoft.DotNet.Cli.Commands.Tool.Update;
109

1110
internal static class ToolUpdateCommandParser
1211
{
13-
public static readonly Argument<PackageIdentity?> PackageIdentityArgument = CommonArguments.OptionalPackageIdentityArgument();
12+
public static readonly Argument<PackageIdentityWithRange?> PackageIdentityArgument = CommonArguments.OptionalPackageIdentityArgument();
1413

1514
public static readonly Option<bool> UpdateAllOption = ToolAppliedOption.UpdateAllOption;
1615

0 commit comments

Comments
 (0)