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

[Draft] Adding --format support to dotnet new list #44140

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,7 @@ If command is specified without the argument, it lists all the template packages
<data name="TemplateCommand_Option_Type" xml:space="preserve">
<value>Specifies the template type to instantiate.</value>
</data>
<data name="ListCommand_Option_Format" xml:space="preserve">
<value>Specifies the output format type for the list packages command</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal BaseListCommand(

Arguments.Add(NameArgument);
Options.Add(IgnoreConstraintsOption);
Options.Add(FormatOption);
Options.Add(SharedOptions.OutputOption);
Options.Add(SharedOptions.ProjectPathOption);
SetupTabularOutputOptions(this);
Expand All @@ -46,6 +47,13 @@ internal BaseListCommand(
Arity = new ArgumentArity(0, 1)
};

internal static CliOption<FormatOptions> FormatOption { get; } = new("--format")
{
AllowMultipleArgumentsPerToken = true,
Description = SymbolStrings.ListCommand_Option_Format,
Arity = new ArgumentArity(1, 1),
Copy link
Member

Choose a reason for hiding this comment

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

nit: consider using ArgumentArity.ExactlyOne here since it's the same as ArgumentArity(1,1).

};

internal static CliArgument<string> NameArgument { get; } = new("template-name")
{
Description = SymbolStrings.Command_List_Argument_Name,
Expand All @@ -72,4 +80,12 @@ protected override Task<NewCommandStatus> ExecuteAsync(
protected override ListCommandArgs ParseContext(ParseResult parseResult) => new(this, parseResult);

}

#pragma warning disable SA1201 // Elements should appear in the correct order
public enum FormatOptions
{
Console,
Copy link
Member

Choose a reason for hiding this comment

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

I think the current layout would be more accurately described as 'table' instead of 'console', what do you think?

Json
}
#pragma warning restore SA1201 // Elements should appear in the correct order
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal ListCommandArgs(BaseListCommand command, ParseResult parseResult) : bas
Language = GetFilterValue(FilterOptionDefinition.LanguageFilter);
}
IgnoreConstraints = parseResult.GetValue(BaseListCommand.IgnoreConstraintsOption);
FormatOptions = ParseResult.GetValue(BaseListCommand.FormatOption);
}

public bool DisplayAllColumns { get; }
Expand All @@ -40,5 +41,7 @@ internal ListCommandArgs(BaseListCommand command, ParseResult parseResult) : bas
internal string? Language { get; }

internal bool IgnoreConstraints { get; }

internal FormatOptions FormatOptions { get; }
}
}

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.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Cli.Commands;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json;
Copy link
Member

Choose a reason for hiding this comment

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

Please use System.Text.Json for serialization by default


namespace Microsoft.TemplateEngine.Cli.TabularOutput
{
Expand All @@ -27,14 +28,23 @@ internal static void DisplayTemplateList(
IEnumerable<TemplateGroup> templateGroups,
TabularOutputSettings helpFormatterSettings,
IReporter reporter,
string? selectedLanguage = null)
string? selectedLanguage = null,
FormatOptions format = FormatOptions.Console)
{
IReadOnlyCollection<TemplateGroupTableRow> groupsForDisplay = GetTemplateGroupsForListDisplay(
templateGroups,
selectedLanguage,
engineEnvironmentSettings.GetDefaultLanguage(),
engineEnvironmentSettings.Environment);

if (format == FormatOptions.Json)
{
DisplayJsonTemplateList(groupsForDisplay, reporter);
return;
}

DisplayTemplateList(groupsForDisplay, helpFormatterSettings, reporter);

}

/// <summary>
Expand All @@ -54,16 +64,30 @@ internal static void DisplayTemplateList(
IEnumerable<ITemplateInfo> templates,
TabularOutputSettings helpFormatterSettings,
IReporter reporter,
string? selectedLanguage = null)
string? selectedLanguage = null,
FormatOptions format = FormatOptions.Console)
{
IReadOnlyCollection<TemplateGroupTableRow> groupsForDisplay = GetTemplateGroupsForListDisplay(
templates,
selectedLanguage,
engineEnvironmentSettings.GetDefaultLanguage(),
engineEnvironmentSettings.Environment);
if (format == FormatOptions.Json)
{
DisplayJsonTemplateList(groupsForDisplay, reporter);
return;
}

DisplayTemplateList(groupsForDisplay, helpFormatterSettings, reporter);
}

internal static void DisplayJsonTemplateList(IReadOnlyCollection<TemplateGroupTableRow> groupsForDisplay, IReporter reporter)
{
//var result = JsonSerializer.Serialize(groupsForDisplay);
var result = JsonConvert.SerializeObject(groupsForDisplay);
reporter.WriteLine(result);
}

/// <summary>
/// Displays the template languages.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ namespace Microsoft.TemplateEngine.Cli.TabularOutput
/// <summary>
/// Represents a table row for template group display.
/// </summary>
internal struct TemplateGroupTableRow
public struct TemplateGroupTableRow
{
internal string Author { get; set; }
public string Author { get; set; }

internal string Classifications { get; set; }
public string Classifications { get; set; }

internal string Languages { get; set; }
public string Languages { get; set; }

internal string Name { get; set; }
public string Name { get; set; }

internal string ShortNames { get; set; }
public string ShortNames { get; set; }

internal string Type { get; set; }
public string Type { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ internal async Task<NewCommandStatus> DisplayTemplateGroupListAsync(
resolutionResult.TemplateGroupsWithMatchingTemplateInfoAndParameters,
settings,
reporter: Reporter.Output,
selectedLanguage: args.Language);
selectedLanguage: args.Language,
format: args.FormatOptions);
return NewCommandStatus.Success;
}
else
Expand Down
Loading