Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --format to dotnet sln list #39801

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Cli/dotnet/commands/dotnet-sln/LocalizableStrings.resx
Expand Up @@ -177,4 +177,7 @@
<data name="SolutionFolderHeader" xml:space="preserve">
<value>Solution Folder(s)</value>
</data>
</root>
<data name="CmdFormatDescription" xml:space="preserve">
<value>The desired output format of the command</value>
</data>
</root>
27 changes: 22 additions & 5 deletions src/Cli/dotnet/commands/dotnet-sln/list/Program.cs
Expand Up @@ -2,23 +2,29 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using System.Text.Json;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.Tools.Tool.List;

namespace Microsoft.DotNet.Tools.Sln.List
{
internal class ListProjectsInSolutionCommand : CommandBase
{
private readonly static JsonSerializerOptions s_noEscapeJsonSerializerOptions = new() { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why use this encoder specifically?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got confused and thought that this was what dotnet list package used, but seems I ended up using what dotnet tool list uses. I

s there a different one that is more common/would not pique your curiosity? :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know what they use (the implementation is over in NuGet.Client repo I suppose, I could go look), but if you aligned with there decision I'm 100% ok with using that!


private readonly string _fileOrDirectory;
private readonly bool _displaySolutionFolders;
private readonly SlnListReportOutputFormat _outputFormat;

public ListProjectsInSolutionCommand(
ParseResult parseResult) : base(parseResult)
{
_fileOrDirectory = parseResult.GetValue(SlnCommandParser.SlnArgument);
_displaySolutionFolders = parseResult.GetValue(SlnListParser.SolutionFolderOption);
_outputFormat = parseResult.GetValue(SlnListParser.OutputFormatOption);
}

public override int Execute()
Expand Down Expand Up @@ -50,12 +56,23 @@ public override int Execute()
{
Array.Sort(paths);

string header = _displaySolutionFolders ? LocalizableStrings.SolutionFolderHeader : LocalizableStrings.ProjectsHeader;
Reporter.Output.WriteLine($"{header}");
Reporter.Output.WriteLine(new string('-', header.Length));
foreach (string slnProject in paths)
switch (_outputFormat)
{
Reporter.Output.WriteLine(slnProject);
case SlnListReportOutputFormat.text:
string header = _displaySolutionFolders ? LocalizableStrings.SolutionFolderHeader : LocalizableStrings.ProjectsHeader;
Reporter.Output.WriteLine($"{header}");
Reporter.Output.WriteLine(new string('-', header.Length));

foreach (string slnProject in paths)
{
Reporter.Output.WriteLine(slnProject);
}
break;

case SlnListReportOutputFormat.json:
var jsonArray = JsonSerializer.Serialize(paths, s_noEscapeJsonSerializerOptions);
Reporter.Output.WriteLine(jsonArray);
break;
}
}
return 0;
Expand Down
5 changes: 4 additions & 1 deletion src/Cli/dotnet/commands/dotnet-sln/list/SlnListParser.cs
Expand Up @@ -7,10 +7,12 @@

namespace Microsoft.DotNet.Cli
{
public static class SlnListParser
internal static class SlnListParser
baronfel marked this conversation as resolved.
Show resolved Hide resolved
{
public static readonly CliOption<bool> SolutionFolderOption = new("--solution-folders") { Description = LocalizableStrings.ListSolutionFoldersArgumentDescription };

public static readonly CliOption<SlnListReportOutputFormat> OutputFormatOption = new("--format") { Description = LocalizableStrings.CmdFormatDescription };

private static readonly CliCommand Command = ConstructCommand();

public static CliCommand GetCommand()
Expand All @@ -23,6 +25,7 @@ private static CliCommand ConstructCommand()
CliCommand command = new("list", LocalizableStrings.ListAppFullName);

command.Options.Add(SolutionFolderOption);
command.Options.Add(OutputFormatOption);
command.SetAction((parseResult) => new ListProjectsInSolutionCommand(parseResult).Execute());

return command;
Expand Down
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.DotNet.Tools.Sln.List
{
internal enum SlnListReportOutputFormat
{
text = 0,
json = 1,
Comment on lines +8 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum members should be PascalCase, so Text and Json.

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions test/dotnet-sln.Tests/GivenDotnetSlnList.cs
Expand Up @@ -20,8 +20,9 @@ public class GivenDotnetSlnList : SdkTest
<SLN_FILE> The solution file to operate on. If not specified, the command will search the current directory for one. [default: {PathUtility.EnsureTrailingSlash(defaultVal)}]

Options:
--solution-folders Display solution folder paths.
-?, -h, --help Show command line help.";
--solution-folders Display solution folder paths.
--format <console|console_with_header|json> The desired output format of the command
-?, -h, --help Show command line help";


public GivenDotnetSlnList(ITestOutputHelper log) : base(log)
Expand Down