Skip to content

sln-list: Add format options #47618

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

edvilme
Copy link
Contributor

@edvilme edvilme commented Mar 14, 2025

The current output for dotnet sln list might not be easily parseable by other programs.
This adds the --format option (following other commands on the cli) with values:

  • text (Default): Displays a header and a list of projects/folders.
    image

  • raw: Displays no header and a list of projects/folders. This can be useful for piping to other commands.
    image

  • json: Displays a json array with the projects/folders.
    image

@ghost ghost added Area-Infrastructure untriaged Request triage from a team member labels Mar 14, 2025
@edvilme edvilme marked this pull request as ready for review March 14, 2025 23:01
@Copilot Copilot AI review requested due to automatic review settings March 14, 2025 23:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces a new output format option for the "dotnet sln list" command to improve parseability for tools. Key changes include:

  • Adding the SlnListOutputFormat enum with Text, Raw, and Json options.
  • Updating the command parser and help text to incorporate the new --format option.
  • Modifying the list project execution to output data in the selected format.

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

File Description
src/Cli/dotnet/commands/dotnet-sln/list/SlnListOutputFormat.cs Adds a new enum for output formats.
src/Cli/dotnet/commands/dotnet-sln/list/SlnListParser.cs Introduces and adds the new --format CLI option.
test/dotnet-sln.Tests/GivenDotnetSlnList.cs Updates help text to include the --format option details.
src/Cli/dotnet/commands/dotnet-sln/list/Program.cs Implements format-specific output behavior and adjusts project file retrieval.
Comments suppressed due to low confidence (2)

src/Cli/dotnet/commands/dotnet-sln/list/Program.cs:30

  • The removal of the 'includeSolutionFilterFiles: true' parameter may alter solution file detection behavior. Please verify that this change is intentional and update unit tests if needed.
string solutionFileFullPath = SlnFileFactory.GetSolutionFileFullPath(_fileOrDirectory);

src/Cli/dotnet/commands/dotnet-sln/list/Program.cs:52

  • When no projects are found, outputting a plain text message may be inconsistent with the JSON output format. Consider outputting an empty JSON array ("[]") when the --format option is set to Json.
return;

using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hopefully this avoids conflicts with the current namespaces efforts ;)


public enum SlnListOutputFormat
{
Text,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can be renamed Table?

Copy link
Contributor

Choose a reason for hiding this comment

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

No preference from me

@@ -19,8 +19,9 @@ dotnet solution <SLN_FILE> list [options]
<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.
Copy link
Contributor

Choose a reason for hiding this comment

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

tiny nit: sort?


public enum SlnListOutputFormat
{
Text,
Copy link
Contributor

Choose a reason for hiding this comment

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

No preference from me

@@ -27,6 +32,7 @@ private static CliCommand ConstructCommand()
CliCommand command = new("list", LocalizableStrings.ListAppFullName);

command.Options.Add(SolutionFolderOption);
command.Options.Add(OutputFormatOption);
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the user passes in something invalid? And is this case-sensitive or insensitive?

}

public override int Execute()
{
string solutionFileFullPath = SlnFileFactory.GetSolutionFileFullPath(_fileOrDirectory, includeSolutionFilterFiles: true);
string solutionFileFullPath = SlnFileFactory.GetSolutionFileFullPath(_fileOrDirectory);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

.ToArray();
}
string[] paths = _displaySolutionFolders
? solution.SolutionFolders.Select(folder => Path.GetDirectoryName(folder.Path.TrimStart('/'))).ToArray() // VS-SolutionPersistence does not return a path object, so there might be issues with forward/backward slashes on different platforms
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
? solution.SolutionFolders.Select(folder => Path.GetDirectoryName(folder.Path.TrimStart('/'))).ToArray() // VS-SolutionPersistence does not return a path object, so there might be issues with forward/backward slashes on different platforms
// VS-SolutionPersistence does not return a path object, so there might be issues with forward/backward slashes on different platforms
? solution.SolutionFolders.Select(folder => Path.GetDirectoryName(folder.Path.TrimStart('/'))).ToArray()

case SlnListOutputFormat.Json:
Reporter.Output.WriteLine(JsonSerializer.Serialize(paths));
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

default? Same as Text, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Infrastructure untriaged Request triage from a team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants