Skip to content

fixes dotnet/templating#3038 show all available short names for template group #3191

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

Merged
merged 1 commit into from
Jun 8, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private static void DisplayTemplateList(
headerSeparator: '-',
blankLineBetweenRows: false)
.DefineColumn(t => t.Name, out object nameColumn, LocalizableStrings.ColumnNameTemplateName, shrinkIfNeeded: true, minWidth: 15, showAlways: true)
.DefineColumn(t => t.ShortName, LocalizableStrings.ColumnNameShortName, showAlways: true)
.DefineColumn(t => t.ShortNames, LocalizableStrings.ColumnNameShortName, showAlways: true)
.DefineColumn(t => t.Languages, out object languageColumn, LocalizableStrings.ColumnNameLanguage, NewCommandInputCli.LanguageColumnFilter, defaultColumn: true)
.DefineColumn(t => t.Type, LocalizableStrings.ColumnNameType, NewCommandInputCli.TypeColumnFilter, defaultColumn: false)
.DefineColumn(t => t.Author, LocalizableStrings.ColumnNameAuthor, NewCommandInputCli.AuthorColumnFilter, defaultColumn: false, shrinkIfNeeded: true, minWidth: 10)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -26,13 +28,25 @@ internal static class TemplateGroupDisplay
/// <param name="language">language from the command input.</param>
/// <param name="defaultLanguage">default language.</param>
/// <returns></returns>
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IEnumerable<ITemplateInfo> templateList, string language, string defaultLanguage)
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IEnumerable<ITemplateInfo> templateList, string? language, string? defaultLanguage)
{
List<TemplateGroupTableRow> templateGroupsForDisplay = new List<TemplateGroupTableRow>();
IEnumerable<IGrouping<string, ITemplateInfo>> groupedTemplateList = templateList.GroupBy(x => x.GroupIdentity, x => !string.IsNullOrEmpty(x.GroupIdentity), StringComparer.OrdinalIgnoreCase);
foreach (IGrouping<string, ITemplateInfo> grouping in groupedTemplateList)
IEnumerable<IGrouping<string?, ITemplateInfo>> groupedTemplateList = templateList.GroupBy(x => x.GroupIdentity, x => !string.IsNullOrEmpty(x.GroupIdentity), StringComparer.OrdinalIgnoreCase);
foreach (IGrouping<string?, ITemplateInfo> templateGroup in groupedTemplateList)
{
templateGroupsForDisplay.Add(GetTemplateGroupRow(grouping, language, defaultLanguage));
ITemplateInfo highestPrecedenceTemplate = templateGroup.OrderByDescending(x => x.Precedence).First();
string shortNames = string.Join(",", templateGroup.SelectMany(t => t.ShortNameList).Distinct(StringComparer.OrdinalIgnoreCase));

TemplateGroupTableRow groupDisplayInfo = new TemplateGroupTableRow
{
Name = highestPrecedenceTemplate.Name,
ShortNames = shortNames,
Languages = string.Join(",", GetLanguagesToDisplay(templateGroup, language, defaultLanguage)),
Classifications = highestPrecedenceTemplate.Classifications != null ? string.Join("/", highestPrecedenceTemplate.Classifications) : string.Empty,
Author = highestPrecedenceTemplate.Author ?? string.Empty,
Type = highestPrecedenceTemplate.GetTemplateType() ?? string.Empty
};
templateGroupsForDisplay.Add(groupDisplayInfo);
}

return templateGroupsForDisplay;
Expand All @@ -52,24 +66,34 @@ internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDis
/// <param name="language">language from the command input.</param>
/// <param name="defaultLanguage">default language.</param>
/// <returns></returns>
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IReadOnlyCollection<TemplateGroup> templateGroupList, string language, string defaultLanguage)
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IReadOnlyCollection<TemplateGroup> templateGroupList, string? language, string? defaultLanguage)
{
List<TemplateGroupTableRow> templateGroupsForDisplay = new List<TemplateGroupTableRow>();
foreach (TemplateGroup templateGroup in templateGroupList)
{
templateGroupsForDisplay.Add(GetTemplateGroupRow(templateGroup.Templates.Select(mi => mi.Info), language, defaultLanguage));
ITemplateInfo highestPrecedenceTemplate = templateGroup.Templates.OrderByDescending(x => x.Info.Precedence).First().Info;
TemplateGroupTableRow groupDisplayInfo = new TemplateGroupTableRow
{
Name = highestPrecedenceTemplate.Name,
ShortNames = string.Join(",", templateGroup.ShortNames),
Languages = string.Join(",", GetLanguagesToDisplay(templateGroup.Templates.Select(t => t.Info), language, defaultLanguage)),
Classifications = highestPrecedenceTemplate.Classifications != null ? string.Join("/", highestPrecedenceTemplate.Classifications) : string.Empty,
Author = highestPrecedenceTemplate.Author ?? string.Empty,
Type = highestPrecedenceTemplate.GetTemplateType() ?? string.Empty
};
templateGroupsForDisplay.Add(groupDisplayInfo);
}
return templateGroupsForDisplay;
}

private static TemplateGroupTableRow GetTemplateGroupRow(IEnumerable<ITemplateInfo> templateGroup, string language, string defaultLanguage)
private static IEnumerable<string> GetLanguagesToDisplay(IEnumerable<ITemplateInfo> templateGroup, string? language, string? defaultLanguage)
{
List<string> languageForDisplay = new List<string>();
List<string> languagesForDisplay = new List<string>();
HashSet<string> uniqueLanguages = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
string defaultLanguageDisplay = string.Empty;
foreach (ITemplateInfo template in templateGroup)
{
string lang = template.GetLanguage();
string? lang = template.GetLanguage();
if (string.IsNullOrWhiteSpace(lang))
{
continue;
Expand All @@ -85,29 +109,16 @@ private static TemplateGroupTableRow GetTemplateGroupRow(IEnumerable<ITemplateIn
}
else
{
languageForDisplay.Add(lang);
languagesForDisplay.Add(lang);
}
}

languageForDisplay.Sort(StringComparer.OrdinalIgnoreCase);
languagesForDisplay.Sort(StringComparer.OrdinalIgnoreCase);
if (!string.IsNullOrEmpty(defaultLanguageDisplay))
{
languageForDisplay.Insert(0, defaultLanguageDisplay);
languagesForDisplay.Insert(0, defaultLanguageDisplay);
}

ITemplateInfo highestPrecedenceTemplate = templateGroup.OrderByDescending(x => x.Precedence).First();
string shortName = highestPrecedenceTemplate.ShortNameList[0];

TemplateGroupTableRow groupDisplayInfo = new TemplateGroupTableRow
{
Name = highestPrecedenceTemplate.Name,
ShortName = shortName,
Languages = string.Join(",", languageForDisplay),
Classifications = highestPrecedenceTemplate.Classifications != null ? string.Join("/", highestPrecedenceTemplate.Classifications) : null,
Author = highestPrecedenceTemplate.Author,
Type = highestPrecedenceTemplate.GetTemplateType() ?? string.Empty
};
return groupDisplayInfo;
return languagesForDisplay;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

namespace Microsoft.TemplateEngine.Cli.TableOutput
{
/// <summary>
Expand All @@ -16,7 +18,7 @@ internal struct TemplateGroupTableRow

internal string Name { get; set; }

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

internal string Type { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ internal IReadOnlyList<string> ShortNames
{
get
{
if (HasSingleTemplate)
{
return Templates.First().Info.ShortNameList;
}
HashSet<string> shortNames = new HashSet<string>();
HashSet<string> shortNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (ITemplateMatchInfo template in Templates)
{
shortNames.UnionWith(template.Info.ShortNameList);
if (template.Info is TemplateInfoWithGroupShortNames groupAwareTemplate)
{
shortNames.UnionWith(groupAwareTemplate.GroupShortNameList);
}
else
{
shortNames.UnionWith(template.Info.ShortNameList);
}
}
return shortNames.ToList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TemplateEngine.Abstractions;

namespace Microsoft.TemplateEngine.Cli.TemplateResolution
{
/// <summary>
/// In addition to <see cref="ITemplateInfo"/> the class contains <see cref="GroupShortNameList"/> property which contains the short names of other templates in the template group.
/// The class is used for template filtering using specific TemplateResolver.CliNameFilter which takes into account the short names of template group when matching names.
/// </summary>
internal class TemplateInfoWithGroupShortNames : ITemplateInfo
{
private ITemplateInfo _parent;

internal TemplateInfoWithGroupShortNames(ITemplateInfo source, IEnumerable<string> groupShortNameList)
{
_parent = source;
GroupShortNameList = groupShortNameList.ToList();
}

public string? Author => _parent.Author;

public string? Description => _parent.Description;

public IReadOnlyList<string> Classifications => _parent.Classifications;

public string? DefaultName => _parent.DefaultName;

public string Identity => _parent.Identity;

public Guid GeneratorId => _parent.GeneratorId;

public string? GroupIdentity => _parent.GroupIdentity;

public int Precedence => _parent.Precedence;

public string Name => _parent.Name;

[Obsolete]
public string ShortName => _parent.ShortName;

public IReadOnlyList<string> ShortNameList => _parent.ShortNameList;

public IReadOnlyList<string> GroupShortNameList { get; } = new List<string>();

[Obsolete]
public IReadOnlyDictionary<string, ICacheTag> Tags => _parent.Tags;

[Obsolete]
public IReadOnlyDictionary<string, ICacheParameter> CacheParameters => _parent.CacheParameters;

public IReadOnlyList<ITemplateParameter> Parameters => _parent.Parameters;

public string MountPointUri => _parent.MountPointUri;

public string ConfigPlace => _parent.ConfigPlace;

public string? LocaleConfigPlace => _parent.LocaleConfigPlace;

public string? HostConfigPlace => _parent.HostConfigPlace;

public string? ThirdPartyNotices => _parent.ThirdPartyNotices;

public IReadOnlyDictionary<string, IBaselineInfo> BaselineInfo => _parent.BaselineInfo;

public IReadOnlyDictionary<string, string> TagsCollection => _parent.TagsCollection;

bool ITemplateInfo.HasScriptRunningPostActions { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ private static IReadOnlyList<ITemplateInfo> SetupTemplateInfoWithGroupShortNames

if (!shortNamesByGroup.TryGetValue(effectiveGroupIdentity, out HashSet<string>? shortNames))
{
shortNames = new HashSet<string>();
shortNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
shortNamesByGroup[effectiveGroupIdentity] = shortNames;
}
shortNames.UnionWith(template.ShortNameList);
Expand Down Expand Up @@ -471,69 +471,5 @@ private static bool IsTemplateHiddenByHostFile(ITemplateInfo templateInfo, IHost
HostSpecificTemplateData hostData = hostDataLoader.ReadHostSpecificTemplateData(templateInfo);
return hostData.IsHidden;
}

/// <summary>
/// In addition to <see cref="ITemplateInfo"/> the class contains <see cref="TemplateInfoWithGroupShortNames.GroupShortNameList"/> property which contains the short names of other templates in the template group.
/// The class is used for template filtering using specific <see cref="CliNameFilter(string)"/> filter which takes into account the short names of template group when matching names.
/// </summary>
private class TemplateInfoWithGroupShortNames : ITemplateInfo
{
private ITemplateInfo _parent;

internal TemplateInfoWithGroupShortNames(ITemplateInfo source, IEnumerable<string> groupShortNameList)
{
_parent = source;
GroupShortNameList = groupShortNameList.ToList();
}

public string? Author => _parent.Author;

public string? Description => _parent.Description;

public IReadOnlyList<string> Classifications => _parent.Classifications;

public string? DefaultName => _parent.DefaultName;

public string Identity => _parent.Identity;

public Guid GeneratorId => _parent.GeneratorId;

public string? GroupIdentity => _parent.GroupIdentity;

public int Precedence => _parent.Precedence;

public string Name => _parent.Name;

[Obsolete]
public string ShortName => _parent.ShortName;

public IReadOnlyList<string> ShortNameList => _parent.ShortNameList;

public IReadOnlyList<string> GroupShortNameList { get; } = new List<string>();

[Obsolete]
public IReadOnlyDictionary<string, ICacheTag> Tags => _parent.Tags;

[Obsolete]
public IReadOnlyDictionary<string, ICacheParameter> CacheParameters => _parent.CacheParameters;

public IReadOnlyList<ITemplateParameter> Parameters => _parent.Parameters;

public string MountPointUri => _parent.MountPointUri;

public string ConfigPlace => _parent.ConfigPlace;

public string? LocaleConfigPlace => _parent.LocaleConfigPlace;

public string? HostConfigPlace => _parent.HostConfigPlace;

public string? ThirdPartyNotices => _parent.ThirdPartyNotices;

public IReadOnlyDictionary<string, IBaselineInfo> BaselineInfo => _parent.BaselineInfo;

public IReadOnlyDictionary<string, string> TagsCollection => _parent.TagsCollection;

bool ITemplateInfo.HasScriptRunningPostActions { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static void DisplayResultsForPack(TemplateSourceSearchResult sourceResul
headerSeparator: '-',
blankLineBetweenRows: false)
.DefineColumn(r => r.TemplateGroupInfo.Name, out object nameColumn, LocalizableStrings.ColumnNameTemplateName, showAlways: true, shrinkIfNeeded: true, minWidth: 15)
.DefineColumn(r => r.TemplateGroupInfo.ShortName, LocalizableStrings.ColumnNameShortName, showAlways: true)
.DefineColumn(r => r.TemplateGroupInfo.ShortNames, LocalizableStrings.ColumnNameShortName, showAlways: true)
.DefineColumn(r => r.TemplateGroupInfo.Author, LocalizableStrings.ColumnNameAuthor, NewCommandInputCli.AuthorColumnFilter, defaultColumn: true, shrinkIfNeeded: true, minWidth: 10)
.DefineColumn(r => r.TemplateGroupInfo.Languages, LocalizableStrings.ColumnNameLanguage, NewCommandInputCli.LanguageColumnFilter, defaultColumn: true)
.DefineColumn(r => r.TemplateGroupInfo.Type, LocalizableStrings.ColumnNameType, NewCommandInputCli.TypeColumnFilter, defaultColumn: false)
Expand Down
Loading