Skip to content

Commit 5fc2a5c

Browse files
committed
More tests
1 parent 3e53d57 commit 5fc2a5c

26 files changed

Lines changed: 1078 additions & 841 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using NuGet.Common;
6+
7+
namespace NuGet.CommandLine.XPlat.Commands.Package.PackageDownload;
8+
9+
internal class PackageDownloadArgs
10+
{
11+
public IReadOnlyList<Package> Packages { get; set; }
12+
public IList<string> Sources { get; set; }
13+
public string OutputDirectory { get; set; }
14+
public string ConfigFile { get; set; }
15+
16+
public bool IncludePrerelease { get; set; }
17+
public bool DownloadOnly { get; set; }
18+
public bool AllowInsecureConnections { get; set; }
19+
public bool Interactive { get; set; }
20+
public LogLevel LogLevel { get; set; }
21+
}
22+
23+
internal enum Verbosity { Quiet, Normal, Detailed }

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageDownload/PackageDownloadCommand.cs renamed to src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Download/PackageDownloadCommand.cs

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@
55
using System.Collections.Generic;
66
using System.CommandLine;
77
using System.CommandLine.Help;
8-
using NuGet.CommandLine.XPlat.Commands;
9-
using NuGet.CommandLine.XPlat.Commands.PackageDownload;
8+
using System.Threading;
9+
using System.Threading.Tasks;
1010

11-
namespace NuGet.CommandLine.XPlat
11+
namespace NuGet.CommandLine.XPlat.Commands.Package.PackageDownload
1212
{
1313
internal class PackageDownloadCommand
1414
{
15-
public static void Register(Command rootCommand, Func<ILoggerWithColor> getLogger)
15+
internal static void Register(Command packageCommand, Option<bool> interactiveOption)
16+
{
17+
Register(packageCommand, interactiveOption, PackageDownloadRunner.RunAsync);
18+
}
19+
public static void Register(Command packageCommand, Option<bool> interactiveOption, Func<PackageDownloadArgs, CancellationToken, Task<int>> action)
1620
{
1721
var downloadCommand = new DocumentedCommand(
1822
"download",
1923
Strings.pkgDownload_descritpion,
2024
"https://aka.ms/dotnet/package/download");
2125

2226
// Arguments
23-
var packageId = new Argument<string>("PackageId")
27+
var packagesArguments = new Argument<IReadOnlyList<Package>>("packages")
2428
{
25-
Description = Strings.pkgDownload_packageIdDescription,
26-
Arity = ArgumentArity.ExactlyOne,
29+
Description = Strings.PackageUpdate_PackageArgumentDescription,
30+
Arity = ArgumentArity.ZeroOrMore,
31+
CustomParser = Package.Parse
2732
};
2833

2934
// Options
@@ -50,12 +55,6 @@ public static void Register(Command rootCommand, Func<ILoggerWithColor> getLogge
5055
Arity = ArgumentArity.Zero
5156
};
5257

53-
var interactive = new Option<bool>("--interactive")
54-
{
55-
Description = Strings.pkgDownload_interactiveDecription,
56-
Arity = ArgumentArity.Zero
57-
};
58-
5958
var outputDirectory = new Option<string>("--output-directory")
6059
{
6160
Description = Strings.pkgDownload_OutputDirectoryDescription,
@@ -74,59 +73,40 @@ public static void Register(Command rootCommand, Func<ILoggerWithColor> getLogge
7473
Arity = ArgumentArity.OneOrMore
7574
};
7675

77-
var verbosity = new Option<string>("--verbosity")
78-
{
79-
Description = Strings.pkgDownload_verbosityDescription,
80-
Arity = ArgumentArity.ExactlyOne
81-
};
76+
var verbosity = CommonOptions.GetVerbosityOption();
8277

83-
var version = new Option<string>("--version")
84-
{
85-
Description = Strings.pkgDownload_versionDescription,
86-
Arity = ArgumentArity.ExactlyOne
87-
};
8878

89-
90-
downloadCommand.Arguments.Add(packageId);
79+
downloadCommand.Arguments.Add(packagesArguments);
9180
downloadCommand.Options.Add(allowInsecureConnections);
9281
downloadCommand.Options.Add(configFile);
9382
downloadCommand.Options.Add(downloadOnly);
9483
downloadCommand.Options.Add(help);
95-
downloadCommand.Options.Add(interactive);
84+
downloadCommand.Options.Add(interactiveOption);
9685
downloadCommand.Options.Add(outputDirectory);
9786
downloadCommand.Options.Add(prerelease);
9887
downloadCommand.Options.Add(sources);
9988
downloadCommand.Options.Add(verbosity);
100-
downloadCommand.Options.Add(version);
10189

10290
downloadCommand.SetAction(async (parserResult, cancellationToken) =>
10391
{
104-
ILoggerWithColor logger = getLogger();
105-
106-
try
107-
{
108-
var args = new PackageDownloadArgs(parserResult.GetValue(packageId), parserResult.GetValue(sources), parserResult.GetValue(outputDirectory), logger)
109-
{
110-
Version = parserResult.GetValue(version),
111-
IncludePrerelease = parserResult.GetValue(prerelease),
112-
DownloadOnly = parserResult.GetValue(downloadOnly),
113-
AllowInsecureConnections = parserResult.GetValue(allowInsecureConnections),
114-
Interactive = parserResult.GetValue(interactive),
115-
ConfigFile = parserResult.GetValue(configFile)
116-
};
117-
118-
args.SetVerbosity(parserResult.GetValue(verbosity));
119-
120-
return await PackageDownloadRunner.RunAsync(args, cancellationToken);
121-
}
122-
catch (ArgumentException ex)
92+
IReadOnlyList<Package> packages = parserResult.GetValue(packagesArguments) ?? [];
93+
var args = new PackageDownloadArgs()
12394
{
124-
logger.LogError(ex.Message);
125-
return ExitCodes.InvalidArguments;
126-
}
95+
Packages = packages,
96+
Sources = parserResult.GetValue(sources),
97+
OutputDirectory = parserResult.GetValue(outputDirectory),
98+
IncludePrerelease = parserResult.GetValue(prerelease),
99+
DownloadOnly = parserResult.GetValue(downloadOnly),
100+
AllowInsecureConnections = parserResult.GetValue(allowInsecureConnections),
101+
Interactive = parserResult.GetValue(interactiveOption),
102+
ConfigFile = parserResult.GetValue(configFile),
103+
LogLevel = (parserResult.GetValue(verbosity) ?? VerbosityEnum.normal).ToLogLevel()
104+
};
105+
106+
return await action(args, cancellationToken);
127107
});
128108

129-
rootCommand.Subcommands.Add(downloadCommand);
109+
packageCommand.Subcommands.Add(downloadCommand);
130110
}
131111
}
132112
}

0 commit comments

Comments
 (0)