|
| 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; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.CommandLine; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace NuGet.CommandLine.XPlat.Commands.Package.PackageDownload |
| 11 | +{ |
| 12 | + internal class PackageDownloadCommand |
| 13 | + { |
| 14 | + internal static void Register(Command packageCommand, Option<bool> interactiveOption) |
| 15 | + { |
| 16 | + Register(packageCommand, interactiveOption, PackageDownloadRunner.RunAsync); |
| 17 | + } |
| 18 | + |
| 19 | + public static void Register(Command packageCommand, Option<bool> interactiveOption, Func<PackageDownloadArgs, CancellationToken, Task<int>> action) |
| 20 | + { |
| 21 | + var downloadCommand = new DocumentedCommand( |
| 22 | + "download", |
| 23 | + Strings.PackageDownloadCommand_Description, |
| 24 | + "https://aka.ms/dotnet/package/download"); |
| 25 | + |
| 26 | + // Arguments |
| 27 | + var packagesArguments = new Argument<IReadOnlyList<PackageWithNuGetVersion>>("packages") |
| 28 | + { |
| 29 | + Description = Strings.PackageUpdate_PackageArgumentDescription, |
| 30 | + Arity = ArgumentArity.OneOrMore, |
| 31 | + CustomParser = PackageWithNuGetVersion.Parse |
| 32 | + }; |
| 33 | + |
| 34 | + // Options |
| 35 | + var allowInsecureConnections = new Option<bool>("--allow-insecure-connections") |
| 36 | + { |
| 37 | + Description = Strings.PackageDownloadCommand_AllowInsecureConnectionsDescription, |
| 38 | + Arity = ArgumentArity.Zero |
| 39 | + }; |
| 40 | + |
| 41 | + var configFile = new Option<string>("--configfile") |
| 42 | + { |
| 43 | + Description = Strings.Option_ConfigFile, |
| 44 | + Arity = ArgumentArity.ExactlyOne |
| 45 | + }; |
| 46 | + |
| 47 | + var outputDirectory = new Option<string>("--output", "-o") |
| 48 | + { |
| 49 | + Description = Strings.PackageDownloadCommand_OutputDirectoryDescription, |
| 50 | + Arity = ArgumentArity.ExactlyOne |
| 51 | + }; |
| 52 | + |
| 53 | + var prerelease = new Option<bool>("--prerelease") |
| 54 | + { |
| 55 | + Description = Strings.Prerelease_Description, |
| 56 | + Arity = ArgumentArity.Zero |
| 57 | + }; |
| 58 | + |
| 59 | + var sources = new Option<List<string>>("--source", "-s") |
| 60 | + { |
| 61 | + Description = Strings.PackageDownloadCommand_SourcesDescription, |
| 62 | + Arity = ArgumentArity.OneOrMore |
| 63 | + }; |
| 64 | + |
| 65 | + var verbosity = CommonOptions.GetVerbosityOption(); |
| 66 | + |
| 67 | + downloadCommand.Arguments.Add(packagesArguments); |
| 68 | + downloadCommand.Options.Add(allowInsecureConnections); |
| 69 | + downloadCommand.Options.Add(configFile); |
| 70 | + downloadCommand.Options.Add(interactiveOption); |
| 71 | + downloadCommand.Options.Add(outputDirectory); |
| 72 | + downloadCommand.Options.Add(prerelease); |
| 73 | + downloadCommand.Options.Add(sources); |
| 74 | + downloadCommand.Options.Add(verbosity); |
| 75 | + |
| 76 | + downloadCommand.SetAction(async (parserResult, cancellationToken) => |
| 77 | + { |
| 78 | + IReadOnlyList<PackageWithNuGetVersion> packages = parserResult.GetValue(packagesArguments) ?? []; |
| 79 | + var args = new PackageDownloadArgs() |
| 80 | + { |
| 81 | + Packages = packages, |
| 82 | + Sources = parserResult.GetValue(sources), |
| 83 | + OutputDirectory = parserResult.GetValue(outputDirectory), |
| 84 | + IncludePrerelease = parserResult.GetValue(prerelease), |
| 85 | + AllowInsecureConnections = parserResult.GetValue(allowInsecureConnections), |
| 86 | + Interactive = parserResult.GetValue(interactiveOption), |
| 87 | + ConfigFile = parserResult.GetValue(configFile), |
| 88 | + LogLevel = (parserResult.GetValue(verbosity) ?? VerbosityEnum.normal).ToLogLevel() |
| 89 | + }; |
| 90 | + |
| 91 | + return await action(args, cancellationToken); |
| 92 | + }); |
| 93 | + |
| 94 | + packageCommand.Subcommands.Add(downloadCommand); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments