1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- #nullable disable
5
-
6
4
using System . CommandLine ;
7
5
using System . CommandLine . Completions ;
8
6
using System . CommandLine . Parsing ;
9
- using Microsoft . DotNet . Cli . Configuration ;
10
7
using Microsoft . DotNet . Cli . Extensions ;
11
- using Microsoft . Extensions . Configuration . DotnetCli . Services ;
8
+ using Microsoft . Extensions . Configuration . DotnetCli ;
12
9
using Microsoft . Extensions . EnvironmentAbstractions ;
13
10
using NuGet . Versioning ;
14
11
15
12
namespace Microsoft . DotNet . Cli . Commands . Package . Add ;
16
13
17
14
public static class PackageAddCommandParser
18
15
{
19
- public static readonly Argument < PackageIdentityWithRange > CmdPackageArgument = CommonArguments . RequiredPackageIdentityArgument ( )
20
- . AddCompletions ( ( context ) =>
21
- {
22
- // we should take --prerelease flags into account for version completion
23
- var allowPrerelease = context . ParseResult . GetValue ( PrereleaseOption ) ;
24
- return QueryNuGet ( context . WordToComplete , allowPrerelease , CancellationToken . None ) . Result . Select ( packageId => new CompletionItem ( packageId ) ) ;
25
- } ) ;
26
-
27
- public static readonly Option < string > VersionOption = new DynamicForwardedOption < string > ( "--version" , "-v" )
28
- {
29
- Description = CliCommandStrings . CmdVersionDescription ,
30
- HelpName = CliCommandStrings . CmdVersion
31
- } . ForwardAsSingle ( o => $ "--version { o } ")
32
- . AddCompletions ( ( context ) =>
33
- {
34
- // we can only do version completion if we have a package id
35
- if ( context . ParseResult . GetValue ( CmdPackageArgument ) is { HasVersion : false } packageId )
36
- {
37
- // we should take --prerelease flags into account for version completion
38
- var allowPrerelease = context . ParseResult . GetValue ( PrereleaseOption ) ;
39
- return QueryVersionsForPackage ( packageId . Id , context . WordToComplete , allowPrerelease , CancellationToken . None )
40
- . Result
41
- . Select ( version => new CompletionItem ( version . ToNormalizedString ( ) ) ) ;
42
- }
43
- else
44
- {
45
- return [ ] ;
46
- }
47
- } ) ;
48
-
49
16
public static readonly Option < string > FrameworkOption = new ForwardedOption < string > ( "--framework" , "-f" )
50
17
{
51
18
Description = CliCommandStrings . PackageAddCmdFrameworkDescription ,
@@ -78,6 +45,36 @@ public static class PackageAddCommandParser
78
45
Arity = ArgumentArity . Zero
79
46
} . ForwardAs ( "--prerelease" ) ;
80
47
48
+ public static readonly Argument < PackageIdentityWithRange > CmdPackageArgument = CommonArguments . RequiredPackageIdentityArgument ( )
49
+ . AddCompletions ( ( context ) =>
50
+ {
51
+ // we should take --prerelease flags into account for version completion
52
+ var allowPrerelease = context . ParseResult . GetValue ( PrereleaseOption ) ;
53
+ return QueryNuGet ( context . WordToComplete , allowPrerelease , CancellationToken . None ) . Result . Select ( packageId => new CompletionItem ( packageId ) ) ;
54
+ } ) ;
55
+
56
+ public static readonly Option < string > VersionOption = new DynamicForwardedOption < string > ( "--version" , "-v" )
57
+ {
58
+ Description = CliCommandStrings . CmdVersionDescription ,
59
+ HelpName = CliCommandStrings . CmdVersion
60
+ } . ForwardAsSingle ( o => $ "--version { o } ")
61
+ . AddCompletions ( ( context ) =>
62
+ {
63
+ // we can only do version completion if we have a package id
64
+ if ( context . ParseResult . GetValue ( CmdPackageArgument ) is { HasVersion : false } packageId )
65
+ {
66
+ // we should take --prerelease flags into account for version completion
67
+ var allowPrerelease = context . ParseResult . GetValue ( PrereleaseOption ) ;
68
+ return QueryVersionsForPackage ( packageId . Id , context . WordToComplete , allowPrerelease , CancellationToken . None )
69
+ . Result
70
+ . Select ( version => new CompletionItem ( version . ToNormalizedString ( ) ) ) ;
71
+ }
72
+ else
73
+ {
74
+ return [ ] ;
75
+ }
76
+ } ) ;
77
+
81
78
private static readonly Command Command = ConstructCommand ( ) ;
82
79
83
80
public static Command GetCommand ( )
@@ -107,17 +104,18 @@ private static Command ConstructCommand()
107
104
108
105
private static void DisallowVersionIfPackageIdentityHasVersionValidator ( OptionResult result )
109
106
{
110
- if ( result . Parent . GetValue ( CmdPackageArgument ) . HasVersion )
107
+ if ( result . Parent ! . GetValue ( CmdPackageArgument ) . HasVersion )
111
108
{
112
109
result . AddError ( CliCommandStrings . ValidationFailedDuplicateVersion ) ;
113
110
}
114
111
}
115
112
116
- public static async Task < IEnumerable < string > > QueryNuGet ( string packageStem , bool allowPrerelease , CancellationToken cancellationToken )
113
+ // Only called during tab-completions, so this is allowed can hack/create singleton members like the configuration/downloader
114
+ internal static async Task < IEnumerable < string > > QueryNuGet ( string packageStem , bool allowPrerelease , CancellationToken cancellationToken )
117
115
{
118
116
try
119
117
{
120
- var downloader = new NuGetPackageDownloader . NuGetPackageDownloader ( packageInstallDir : new DirectoryPath ( ) , configurationService : DotNetConfigurationFactory . Create ( ) ) ;
118
+ var downloader = new NuGetPackageDownloader . NuGetPackageDownloader ( packageInstallDir : new DirectoryPath ( ) ) ;
121
119
var versions = await downloader . GetPackageIdsAsync ( packageStem , allowPrerelease , cancellationToken : cancellationToken ) ;
122
120
return versions ;
123
121
}
@@ -127,11 +125,12 @@ public static async Task<IEnumerable<string>> QueryNuGet(string packageStem, boo
127
125
}
128
126
}
129
127
128
+ // Only called during tab-completions, so this is allowed can hack/create singleton members like the configuration/downloader
130
129
internal static async Task < IEnumerable < NuGetVersion > > QueryVersionsForPackage ( string packageId , string versionFragment , bool allowPrerelease , CancellationToken cancellationToken )
131
130
{
132
131
try
133
132
{
134
- var downloader = new NuGetPackageDownloader . NuGetPackageDownloader ( packageInstallDir : new DirectoryPath ( ) , configurationService : DotNetConfigurationFactory . Create ( ) ) ;
133
+ var downloader = new NuGetPackageDownloader . NuGetPackageDownloader ( packageInstallDir : new DirectoryPath ( ) ) ;
135
134
var versions = await downloader . GetPackageVersionsAsync ( new ( packageId ) , versionFragment , allowPrerelease , cancellationToken : cancellationToken ) ;
136
135
return versions ;
137
136
}
0 commit comments