Skip to content

Commit

Permalink
fixes dotnet/templating #3811 display curated list for dotnet new
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Nov 26, 2021
1 parent 34990a8 commit b57e49f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 76 deletions.
2 changes: 0 additions & 2 deletions src/Microsoft.TemplateEngine.Cli/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.CommandLine.Parsing;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Edge.Settings;
using Microsoft.TemplateEngine.Edge.Template;

namespace Microsoft.TemplateEngine.Cli.Commands
{
Expand Down Expand Up @@ -97,7 +96,6 @@ protected override async Task<NewCommandStatus> ExecuteAsync(ListCommandArgs arg
TemplateListCoordinator templateListCoordinator = new TemplateListCoordinator(
environmentSettings,
templatePackageManager,
new TemplateCreator(environmentSettings),
new HostSpecificDataLoader(environmentSettings),
TelemetryLogger);

Expand Down
33 changes: 22 additions & 11 deletions src/Microsoft.TemplateEngine.Cli/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ internal NewCommand(string commandName, ITemplateEngineHost host, ITelemetryLogg
Arity = new ArgumentArity(0, 999)
};

internal Option<bool> HelpOption { get; } = new Option<bool>(new string[] { "-h", "--help", "-?" });
internal Option<bool> HelpOption { get; } = new Option<bool>(new string[] { "-h", "--help", "-?" })
{
IsHidden = true,
};

#region Legacy Options
internal Option<bool> InteractiveOption { get; } = SharedOptionsFactory.CreateInteractiveOption().AsHidden();
Expand Down Expand Up @@ -168,22 +171,30 @@ protected override IEnumerable<string> GetSuggestions(NewCommandArgs args, IEngi

protected override async Task<NewCommandStatus> ExecuteAsync(NewCommandArgs args, IEngineEnvironmentSettings environmentSettings, InvocationContext context)
{
if (string.IsNullOrWhiteSpace(args.ShortName))
if (string.IsNullOrWhiteSpace(args.ShortName) && args.HelpRequested)
{
if (args.HelpRequested)
{
context.HelpBuilder.Write(
context.ParseResult.CommandResult.Command,
StandardStreamWriter.Create(context.Console.Out),
context.ParseResult);
context.HelpBuilder.Write(
context.ParseResult.CommandResult.Command,
StandardStreamWriter.Create(context.Console.Out),
context.ParseResult);

return NewCommandStatus.Success;
}
//show curated list
return NewCommandStatus.Success;
}

using TemplatePackageManager templatePackageManager = new TemplatePackageManager(environmentSettings);

if (string.IsNullOrWhiteSpace(args.ShortName))
{
TemplateListCoordinator templateListCoordinator = new TemplateListCoordinator(
environmentSettings,
templatePackageManager,
new HostSpecificDataLoader(environmentSettings),
TelemetryLogger);

//TODO: we need to await, otherwise templatePackageManager will be disposed.
return await templateListCoordinator.DisplayCommandDescriptionAsync(args, default).ConfigureAwait(false);
}

var templates = await templatePackageManager.GetTemplatesAsync(context.GetCancellationToken()).ConfigureAwait(false);
var template = templates.FirstOrDefault(template => template.ShortNameList.Contains(args.ShortName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,46 +166,6 @@ internal async Task<NewCommandStatus> DisplayTemplateHelpAsync(
}
}

/// <summary>
/// Handles display for dotnet new command without parameters.
/// </summary>
/// <param name="commandInput">user command input.</param>
/// <param name="cancellationToken">cancellation token.</param>
/// <returns></returns>
internal async Task<NewCommandStatus> DisplayCommandDescriptionAsync(
INewCommandInput commandInput,
CancellationToken cancellationToken)
{
IEnumerable<ITemplateInfo> curatedTemplates = await GetCuratedListAsync(commandInput, cancellationToken).ConfigureAwait(false);

Reporter.Output.WriteLine(string.Format(
LocalizableStrings.TemplateInformationCoordinator_DotnetNew_Description,
CommandExamples.New3CommandExample(commandInput.CommandName)));
Reporter.Output.WriteLine();

Reporter.Output.WriteLine(string.Format(
LocalizableStrings.TemplateInformationCoordinator_DotnetNew_TemplatesHeader,
CommandExamples.New3CommandExample(commandInput.CommandName)));
TemplateGroupDisplay.DisplayTemplateList(_engineEnvironmentSettings, curatedTemplates, _defaultTabularOutputSettings);

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_ExampleHeader);
Reporter.Output.WriteCommand(CommandExamples.InstantiateTemplateExample(commandInput.CommandName, "console"));
Reporter.Output.WriteLine();

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_DisplayOptionsHint);
Reporter.Output.WriteCommand(CommandExamples.HelpCommandExample(commandInput.CommandName, "console"));

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_ListTemplatesHint);
Reporter.Output.WriteCommand(CommandExamples.ListCommandExample(commandInput.CommandName));

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_SearchTemplatesHint);
Reporter.Output.WriteCommand(CommandExamples.SearchCommandExample(commandInput.CommandName, "web"));

Reporter.Output.WriteLine();

return NewCommandStatus.Success;
}

/// <summary>
/// Displays the help in case <paramref name="commandInput"/> contains invalid parameters for resolved <paramref name="templateGroupMatchInfo"/>.
/// </summary>
Expand Down Expand Up @@ -272,25 +232,6 @@ private static void DisplayHintForOtherLanguages(INewCommandInput commandInput,
}
}

/// <summary>
/// Displays curated list of templates for dotnet new command.
/// </summary>
private async Task<IEnumerable<ITemplateInfo>> GetCuratedListAsync(INewCommandInput commandInput, CancellationToken cancellationToken)
{
string[] curatedGroupIdentityList = new[]
{
"Microsoft.Common.Library", //classlib
"Microsoft.Common.Console", //console
"Microsoft.Common.WPF", //wpf
"Microsoft.Common.WinForms", //winforms
"Microsoft.Web.Blazor.Server", //blazorserver
"Microsoft.Web.RazorPages" //webapp
};

IReadOnlyList<ITemplateInfo> templates = await _templatePackageManager.GetTemplatesAsync(cancellationToken).ConfigureAwait(false);
return templates.Where(t => curatedGroupIdentityList.Contains(t.GroupIdentity, StringComparer.OrdinalIgnoreCase));
}

/// <summary>
/// Displays the help when <paramref name="templateGroupMatchInfo"/> contains the templates with ambiguous precedence.
/// </summary>
Expand Down
66 changes: 62 additions & 4 deletions src/Microsoft.TemplateEngine.Cli/TemplateListCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@
using Microsoft.TemplateEngine.Cli.TabularOutput;
using Microsoft.TemplateEngine.Cli.TemplateResolution;
using Microsoft.TemplateEngine.Edge.Settings;
using Microsoft.TemplateEngine.Edge.Template;

namespace Microsoft.TemplateEngine.Cli
{
internal class TemplateListCoordinator
{
private readonly IEngineEnvironmentSettings _engineEnvironmentSettings;
private readonly TemplatePackageManager _templatePackageManager;
private readonly TemplateCreator _templateCreator;
private readonly IHostSpecificDataLoader _hostSpecificDataLoader;
private readonly ITelemetryLogger _telemetryLogger;
private readonly string? _defaultLanguage;

internal TemplateListCoordinator(
IEngineEnvironmentSettings engineEnvironmentSettings,
TemplatePackageManager templatePackageManager,
TemplateCreator templateCreator,
IHostSpecificDataLoader hostSpecificDataLoader,
ITelemetryLogger telemetryLogger)

{
_engineEnvironmentSettings = engineEnvironmentSettings ?? throw new ArgumentNullException(nameof(engineEnvironmentSettings));
_templatePackageManager = templatePackageManager ?? throw new ArgumentNullException(nameof(templatePackageManager));
_templateCreator = templateCreator ?? throw new ArgumentNullException(nameof(templateCreator));
_hostSpecificDataLoader = hostSpecificDataLoader ?? throw new ArgumentNullException(nameof(hostSpecificDataLoader));
_telemetryLogger = telemetryLogger ?? throw new ArgumentNullException(nameof(telemetryLogger));
_defaultLanguage = engineEnvironmentSettings.GetDefaultLanguage();
Expand Down Expand Up @@ -108,6 +104,49 @@ internal async Task<NewCommandStatus> DisplayTemplateGroupListAsync(
}
}

/// <summary>
/// Handles display for dotnet new command without parameters.
/// </summary>
/// <param name="args">command arguments.</param>
/// <param name="cancellationToken">cancellation token.</param>
/// <returns></returns>
internal async Task<NewCommandStatus> DisplayCommandDescriptionAsync(
GlobalArgs args,
CancellationToken cancellationToken)
{
IEnumerable<ITemplateInfo> curatedTemplates = await GetCuratedListAsync(cancellationToken).ConfigureAwait(false);

Reporter.Output.WriteLine(string.Format(
LocalizableStrings.TemplateInformationCoordinator_DotnetNew_Description,
CommandExamples.New3CommandExample(args.CommandName)));
Reporter.Output.WriteLine();

Reporter.Output.WriteLine(string.Format(
LocalizableStrings.TemplateInformationCoordinator_DotnetNew_TemplatesHeader,
CommandExamples.New3CommandExample(args.CommandName)));
TemplateGroupDisplay.DisplayTemplateList(
_engineEnvironmentSettings,
curatedTemplates,
new TabularOutputSettings(_engineEnvironmentSettings.Environment));

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_ExampleHeader);
Reporter.Output.WriteCommand(CommandExamples.InstantiateTemplateExample(args.CommandName, "console"));
Reporter.Output.WriteLine();

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_DisplayOptionsHint);
Reporter.Output.WriteCommand(CommandExamples.HelpCommandExample(args.CommandName, "console"));

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_ListTemplatesHint);
Reporter.Output.WriteCommand(CommandExamples.ListCommandExample(args.CommandName));

Reporter.Output.WriteLine(LocalizableStrings.TemplateInformationCoordinator_DotnetNew_SearchTemplatesHint);
Reporter.Output.WriteCommand(CommandExamples.SearchCommandExample(args.CommandName, "web"));

Reporter.Output.WriteLine();

return NewCommandStatus.Success;
}

private static string GetInputParametersString(ListCommandArgs args/*, IReadOnlyDictionary<string, string?>? templateParameters = null*/)
{
string separator = ", ";
Expand Down Expand Up @@ -155,5 +194,24 @@ private static string GetPartialMatchReason(TemplateResolutionResult templateRes
}
return inputParameters.ToString();
}

/// <summary>
/// Displays curated list of templates for dotnet new command.
/// </summary>
private async Task<IEnumerable<ITemplateInfo>> GetCuratedListAsync(CancellationToken cancellationToken)
{
string[] curatedGroupIdentityList = new[]
{
"Microsoft.Common.Library", //classlib
"Microsoft.Common.Console", //console
"Microsoft.Common.WPF", //wpf
"Microsoft.Common.WinForms", //winforms
"Microsoft.Web.Blazor.Wasm", //blazorwasm
"Microsoft.Web.RazorPages" //webapp
};

IReadOnlyList<ITemplateInfo> templates = await _templatePackageManager.GetTemplatesAsync(cancellationToken).ConfigureAwait(false);
return templates.Where(t => curatedGroupIdentityList.Contains(t.GroupIdentity, StringComparer.OrdinalIgnoreCase));
}
}
}

0 comments on commit b57e49f

Please sign in to comment.