Skip to content

Commit fe535b3

Browse files
authored
Invoke dotnet nuget why directly through API (#44614)
1 parent 40318dc commit fe535b3

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/Cli/dotnet/commands/dotnet-help/HelpCommand.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.DotNet.Cli.Utils;
88
using Microsoft.TemplateEngine.Cli.Commands;
99

10+
using NuGetDocumentedCommand = NuGet.CommandLine.XPlat.Commands.DocumentedCommand;
11+
1012
namespace Microsoft.DotNet.Tools.Help
1113
{
1214
public class HelpCommand(string[] helpArgs)
@@ -107,6 +109,11 @@ private bool TryGetDocsLink(string[] command, out string docsLink)
107109
docsLink = dc.DocsLink;
108110
return true;
109111
}
112+
else if (parsedCommand?.CommandResult?.Command is NuGetDocumentedCommand ndc)
113+
{
114+
docsLink = ndc.HelpUrl;
115+
return true;
116+
}
110117
docsLink = null;
111118
return false;
112119
}

src/Cli/dotnet/commands/dotnet-nuget/NuGetCommandParser.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static CliCommand ConstructCommand()
3636
command.Subcommands.Add(GetVerifyCommand());
3737
command.Subcommands.Add(GetTrustCommand());
3838
command.Subcommands.Add(GetSignCommand());
39-
command.Subcommands.Add(GetWhyCommand());
39+
NuGet.CommandLine.XPlat.Commands.Why.WhyCommand.GetWhyCommand(command);
4040

4141
command.SetAction(NuGetCommand.Run);
4242

@@ -218,19 +218,5 @@ private static CliCommand GetSignCommand()
218218

219219
return signCommand;
220220
}
221-
222-
private static CliCommand GetWhyCommand()
223-
{
224-
DocumentedCommand whyCommand = new("why", "https://learn.microsoft.com/dotnet/core/tools/dotnet-nuget-why");
225-
whyCommand.Arguments.Add(new CliArgument<string>("PROJECT|SOLUTION") { Arity = ArgumentArity.ExactlyOne });
226-
whyCommand.Arguments.Add(new CliArgument<string>("PACKAGE") { Arity = ArgumentArity.ExactlyOne });
227-
228-
whyCommand.Options.Add(new ForwardedOption<IEnumerable<string>>("--framework", "-f") { Arity = ArgumentArity.ZeroOrMore }
229-
.ForwardAsManyArgumentsEachPrefixedByOption("--framework")
230-
.AllowSingleArgPerToken());
231-
232-
whyCommand.SetAction(NuGetCommand.Run);
233-
return whyCommand;
234-
}
235221
}
236222
}

src/Cli/dotnet/dotnet.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" />
103103
<PackageReference Include="Microsoft.VisualStudio.SolutionPersistence" />
104104
<PackageReference Include="Newtonsoft.Json" />
105+
<PackageReference Include="NuGet.CommandLine.XPlat" />
105106
<PackageReference Include="Microsoft.ApplicationInsights" />
106107
<PackageReference Include="Microsoft.Build" />
107108
<PackageReference Include="Microsoft.NET.HostModel" />

test/dotnet-nuget.UnitTests/GivenANuGetCommand.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public GivenANuGetCommand(ITestOutputHelper log) : base(log)
7575
"--interactive",
7676
"--verbosity", "detailed",
7777
"--format", "json"}, 0)]
78-
[InlineData(new[] { "why" }, 0)]
79-
[InlineData(new[] { "why", "C:\\path", "Fake.Package" }, 0)]
80-
[InlineData(new[] { "why", "C:\\path", "Fake.Package", "--framework", "net472", "-f", "netcoreapp5.0" }, 0)]
8178

8279
public void ItPassesCommandIfSupported(string[] inputArgs, int result)
8380
{
@@ -110,5 +107,29 @@ public void ItAcceptsPrefixedOption()
110107
.And
111108
.HaveStdErrContaining("Required argument missing for option: '-ss'.");
112109
}
110+
111+
[Fact]
112+
public void ItHasAWhySubcommand()
113+
{
114+
var testAssetName = "NewtonSoftDependentProject";
115+
var testAsset = _testAssetsManager
116+
.CopyTestAsset(testAssetName)
117+
.WithSource();
118+
var projectDirectory = testAsset.Path;
119+
120+
new RestoreCommand(testAsset)
121+
.Execute()
122+
.Should()
123+
.Pass()
124+
.And.NotHaveStdErr();
125+
126+
new DotnetCommand(Log)
127+
.WithWorkingDirectory(projectDirectory)
128+
.Execute("nuget", "why", "newtonsoft.json")
129+
.Should()
130+
.Pass()
131+
.And.NotHaveStdErr()
132+
.And.HaveStdOutContaining("has the following dependency");
133+
}
113134
}
114135
}

0 commit comments

Comments
 (0)