Skip to content

Commit 27439c5

Browse files
committed
dotnet list package restores
1 parent fc6a726 commit 27439c5

25 files changed

+722
-8
lines changed

src/Cli/dotnet/Commands/List/Package/ListPackageReferencesCommandParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private static CliCommand ConstructCommand()
3434
command.Options.Add(PackageListCommandParser.InteractiveOption);
3535
command.Options.Add(PackageListCommandParser.FormatOption);
3636
command.Options.Add(PackageListCommandParser.OutputVersionOption);
37+
command.Options.Add(PackageListCommandParser.NoRestore);
3738

3839
command.SetAction((parseResult) => new ListPackageReferencesCommand(parseResult).Execute());
3940

src/Cli/dotnet/Commands/Package/List/ListPackageReferencesCommand.cs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.DotNet.Cli.Extensions;
77
using Microsoft.DotNet.Cli.Utils;
88
using Microsoft.DotNet.Tools.NuGet;
9+
using System.Globalization;
10+
using Microsoft.DotNet.Tools.MSBuild;
911

1012
namespace Microsoft.DotNet.Tools.Package.List;
1113

@@ -30,7 +32,57 @@ private static string GetAbsolutePath(string currentDirectory, string relativePa
3032

3133
public override int Execute()
3234
{
33-
return NuGetCommand.Run(TransformArgs());
35+
string projectFile = GetProjectOrSolution();
36+
bool noRestore = _parseResult.HasOption(PackageListCommandParser.NoRestore);
37+
int restoreExitCode = 0;
38+
39+
if (!noRestore)
40+
{
41+
ReportOutputFormat formatOption = _parseResult.GetValue((CliOption<ReportOutputFormat>)PackageListCommandParser.FormatOption);
42+
restoreExitCode = RunRestore(projectFile, formatOption);
43+
}
44+
45+
return restoreExitCode == 0
46+
? NuGetCommand.Run(TransformArgs(projectFile))
47+
: restoreExitCode;
48+
}
49+
50+
private int RunRestore(string projectOrSolution, ReportOutputFormat formatOption)
51+
{
52+
MSBuildForwardingApp restoringCommand = new MSBuildForwardingApp(argsToForward: ["-target:restore", projectOrSolution, "-noConsoleLogger"]);
53+
54+
int exitCode = 0;
55+
56+
try
57+
{
58+
exitCode = restoringCommand.Execute();
59+
}
60+
catch (Exception)
61+
{
62+
exitCode = 1;
63+
}
64+
65+
if (exitCode != 0)
66+
{
67+
if (formatOption == ReportOutputFormat.json)
68+
{
69+
string jsonError =
70+
"{\r\n" +
71+
" \"version\": 1,\r\n" +
72+
" \"problems\": [\r\n" +
73+
" {\r\n" +
74+
$" \"text\": \"{String.Format(CultureInfo.CurrentCulture, LocalizableStrings.Error_restore)}\",\r\n" +
75+
" \"level\": \"error\"\r\n" +
76+
" }\r\n" +
77+
" ]\r\n" +
78+
"}";
79+
Console.WriteLine(jsonError);
80+
}
81+
82+
Console.WriteLine(String.Format(CultureInfo.CurrentCulture, LocalizableStrings.Error_restore));
83+
}
84+
85+
return exitCode;
3486
}
3587

3688
internal static void EnforceOptionRules(ParseResult parseResult)
@@ -45,15 +97,15 @@ internal static void EnforceOptionRules(ParseResult parseResult)
4597
}
4698
}
4799

48-
private string[] TransformArgs()
100+
private string[] TransformArgs(string projectOrSolution)
49101
{
50102
var args = new List<string>
51103
{
52104
"package",
53105
"list",
54106
};
55107

56-
args.Add(GetProjectOrSolution());
108+
args.Add(projectOrSolution);
57109

58110
args.AddRange(_parseResult.OptionValuesToBeForwarded(PackageListCommandParser.GetCommand()));
59111

src/Cli/dotnet/Commands/Package/List/LocalizableStrings.resx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@
174174
<data name="CmdOutputVersionDescription" xml:space="preserve">
175175
<value>Specifies the version of machine-readable output. Requires the '--format json' option.</value>
176176
</data>
177+
<data name="Error_restore" xml:space="preserve">
178+
<value>Restore failed. Run `dotnet restore` for more details on the issue.</value>
179+
<comment>Do not translate `dotnet restore`</comment>
180+
</data>
181+
<data name="CmdNoRestoreDescription" xml:space="preserve">
182+
<value>Do not restore before running the command.</value>
183+
</data>
177184
</root>

src/Cli/dotnet/Commands/Package/List/PackageListCommandParser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ internal static class PackageListCommandParser
7474

7575
public static readonly CliOption InteractiveOption = CommonOptions.InteractiveOption().ForwardIfEnabled("--interactive");
7676

77+
public static readonly CliOption NoRestore = new CliOption<bool>("--no-restore")
78+
{
79+
Description = LocalizableStrings.CmdNoRestoreDescription,
80+
Arity = ArgumentArity.Zero
81+
};
82+
7783
public static readonly CliOption VerbosityOption = new ForwardedOption<VerbosityOptions>("--verbosity", "-v")
7884
{
7985
Description = CommonLocalizableStrings.VerbosityOptionDescription,

src/Cli/dotnet/Commands/Package/List/xlf/LocalizableStrings.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/Package/List/xlf/LocalizableStrings.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/Package/List/xlf/LocalizableStrings.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/Package/List/xlf/LocalizableStrings.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/Package/List/xlf/LocalizableStrings.it.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/Package/List/xlf/LocalizableStrings.ja.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)