Skip to content

Commit 05432f5

Browse files
committed
fixes #3039 show all available short names for template group
1 parent 13e41ad commit 05432f5

File tree

10 files changed

+235
-120
lines changed

10 files changed

+235
-120
lines changed

src/Microsoft.TemplateEngine.Cli/HelpAndUsage/HelpForTemplateResolution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ private static void DisplayTemplateList(
444444
headerSeparator: '-',
445445
blankLineBetweenRows: false)
446446
.DefineColumn(t => t.Name, LocalizableStrings.ColumnNameTemplateName, shrinkIfNeeded: true, minWidth: 15, showAlways: true)
447-
.DefineColumn(t => t.ShortName, LocalizableStrings.ColumnNameShortName, showAlways: true)
447+
.DefineColumn(t => t.ShortNames, LocalizableStrings.ColumnNameShortName, showAlways: true)
448448
.DefineColumn(t => t.Languages, out object languageColumn, LocalizableStrings.ColumnNameLanguage, NewCommandInputCli.LanguageColumnFilter, defaultColumn: true)
449449
.DefineColumn(t => t.Type, LocalizableStrings.ColumnNameType, NewCommandInputCli.TypeColumnFilter, defaultColumn: false)
450450
.DefineColumn(t => t.Author, LocalizableStrings.ColumnNameAuthor, NewCommandInputCli.AuthorColumnFilter, defaultColumn: false, shrinkIfNeeded: true, minWidth: 10)

src/Microsoft.TemplateEngine.Cli/TableOutput/TemplateGroupDisplay.cs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable enable
5+
46
using System;
57
using System.Collections.Generic;
68
using System.Linq;
@@ -26,13 +28,25 @@ internal static class TemplateGroupDisplay
2628
/// <param name="language">language from the command input.</param>
2729
/// <param name="defaultLanguage">default language.</param>
2830
/// <returns></returns>
29-
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IEnumerable<ITemplateInfo> templateList, string language, string defaultLanguage)
31+
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IEnumerable<ITemplateInfo> templateList, string? language, string? defaultLanguage)
3032
{
3133
List<TemplateGroupTableRow> templateGroupsForDisplay = new List<TemplateGroupTableRow>();
32-
IEnumerable<IGrouping<string, ITemplateInfo>> groupedTemplateList = templateList.GroupBy(x => x.GroupIdentity, x => !string.IsNullOrEmpty(x.GroupIdentity), StringComparer.OrdinalIgnoreCase);
33-
foreach (IGrouping<string, ITemplateInfo> grouping in groupedTemplateList)
34+
IEnumerable<IGrouping<string?, ITemplateInfo>> groupedTemplateList = templateList.GroupBy(x => x.GroupIdentity, x => !string.IsNullOrEmpty(x.GroupIdentity), StringComparer.OrdinalIgnoreCase);
35+
foreach (IGrouping<string?, ITemplateInfo> templateGroup in groupedTemplateList)
3436
{
35-
templateGroupsForDisplay.Add(GetTemplateGroupRow(grouping, language, defaultLanguage));
37+
ITemplateInfo highestPrecedenceTemplate = templateGroup.OrderByDescending(x => x.Precedence).First();
38+
string shortNames = string.Join(",", templateGroup.SelectMany(t => t.ShortNameList).Distinct(StringComparer.OrdinalIgnoreCase));
39+
40+
TemplateGroupTableRow groupDisplayInfo = new TemplateGroupTableRow
41+
{
42+
Name = highestPrecedenceTemplate.Name,
43+
ShortNames = shortNames,
44+
Languages = string.Join(",", GetLanguagesToDisplay(templateGroup, language, defaultLanguage)),
45+
Classifications = highestPrecedenceTemplate.Classifications != null ? string.Join("/", highestPrecedenceTemplate.Classifications) : string.Empty,
46+
Author = highestPrecedenceTemplate.Author ?? string.Empty,
47+
Type = highestPrecedenceTemplate.GetTemplateType() ?? string.Empty
48+
};
49+
templateGroupsForDisplay.Add(groupDisplayInfo);
3650
}
3751

3852
return templateGroupsForDisplay;
@@ -48,28 +62,38 @@ internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDis
4862
/// - Author
4963
/// - Type.
5064
/// </summary>
51-
/// <param name="templateList">list of template groups to be displayed.</param>
65+
/// <param name="templateGroupList">list of template groups to be displayed.</param>
5266
/// <param name="language">language from the command input.</param>
5367
/// <param name="defaultLanguage">default language.</param>
5468
/// <returns></returns>
55-
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IReadOnlyCollection<TemplateGroup> templateGroupList, string language, string defaultLanguage)
69+
internal static IReadOnlyList<TemplateGroupTableRow> GetTemplateGroupsForListDisplay(IReadOnlyCollection<TemplateGroup> templateGroupList, string? language, string? defaultLanguage)
5670
{
5771
List<TemplateGroupTableRow> templateGroupsForDisplay = new List<TemplateGroupTableRow>();
5872
foreach (TemplateGroup templateGroup in templateGroupList)
5973
{
60-
templateGroupsForDisplay.Add(GetTemplateGroupRow(templateGroup.Templates.Select(mi => mi.Info), language, defaultLanguage));
74+
ITemplateInfo highestPrecedenceTemplate = templateGroup.Templates.OrderByDescending(x => x.Info.Precedence).First().Info;
75+
TemplateGroupTableRow groupDisplayInfo = new TemplateGroupTableRow
76+
{
77+
Name = highestPrecedenceTemplate.Name,
78+
ShortNames = string.Join(",", templateGroup.ShortNames),
79+
Languages = string.Join(",", GetLanguagesToDisplay(templateGroup.Templates.Select(t => t.Info), language, defaultLanguage)),
80+
Classifications = highestPrecedenceTemplate.Classifications != null ? string.Join("/", highestPrecedenceTemplate.Classifications) : string.Empty,
81+
Author = highestPrecedenceTemplate.Author ?? string.Empty,
82+
Type = highestPrecedenceTemplate.GetTemplateType() ?? string.Empty
83+
};
84+
templateGroupsForDisplay.Add(groupDisplayInfo);
6185
}
6286
return templateGroupsForDisplay;
6387
}
6488

65-
private static TemplateGroupTableRow GetTemplateGroupRow(IEnumerable<ITemplateInfo> templateGroup, string language, string defaultLanguage)
89+
private static IEnumerable<string> GetLanguagesToDisplay(IEnumerable<ITemplateInfo> templateGroup, string? language, string? defaultLanguage)
6690
{
67-
List<string> languageForDisplay = new List<string>();
91+
List<string> languagesForDisplay = new List<string>();
6892
HashSet<string> uniqueLanguages = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
6993
string defaultLanguageDisplay = string.Empty;
7094
foreach (ITemplateInfo template in templateGroup)
7195
{
72-
string lang = template.GetLanguage();
96+
string? lang = template.GetLanguage();
7397
if (string.IsNullOrWhiteSpace(lang))
7498
{
7599
continue;
@@ -85,29 +109,16 @@ private static TemplateGroupTableRow GetTemplateGroupRow(IEnumerable<ITemplateIn
85109
}
86110
else
87111
{
88-
languageForDisplay.Add(lang);
112+
languagesForDisplay.Add(lang);
89113
}
90114
}
91115

92-
languageForDisplay.Sort(StringComparer.OrdinalIgnoreCase);
116+
languagesForDisplay.Sort(StringComparer.OrdinalIgnoreCase);
93117
if (!string.IsNullOrEmpty(defaultLanguageDisplay))
94118
{
95-
languageForDisplay.Insert(0, defaultLanguageDisplay);
119+
languagesForDisplay.Insert(0, defaultLanguageDisplay);
96120
}
97-
98-
ITemplateInfo highestPrecedenceTemplate = templateGroup.OrderByDescending(x => x.Precedence).First();
99-
string shortName = highestPrecedenceTemplate.ShortNameList[0];
100-
101-
TemplateGroupTableRow groupDisplayInfo = new TemplateGroupTableRow
102-
{
103-
Name = highestPrecedenceTemplate.Name,
104-
ShortName = shortName,
105-
Languages = string.Join(",", languageForDisplay),
106-
Classifications = highestPrecedenceTemplate.Classifications != null ? string.Join("/", highestPrecedenceTemplate.Classifications) : null,
107-
Author = highestPrecedenceTemplate.Author,
108-
Type = highestPrecedenceTemplate.GetTemplateType() ?? string.Empty
109-
};
110-
return groupDisplayInfo;
121+
return languagesForDisplay;
111122
}
112123
}
113124
}

src/Microsoft.TemplateEngine.Cli/TableOutput/TemplateGroupTableRow.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable enable
5+
46
namespace Microsoft.TemplateEngine.Cli.TableOutput
57
{
68
/// <summary>
@@ -16,7 +18,7 @@ internal struct TemplateGroupTableRow
1618

1719
internal string Name { get; set; }
1820

19-
internal string ShortName { get; set; }
21+
internal string ShortNames { get; set; }
2022

2123
internal string Type { get; set; }
2224
}

src/Microsoft.TemplateEngine.Cli/TemplateResolution/TemplateGroup.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,26 @@ internal IReadOnlyList<string> ShortNames
6565
{
6666
if (HasSingleTemplate)
6767
{
68-
return Templates.First().Info.ShortNameList;
68+
if (Templates.First().Info is TemplateInfoWithGroupShortNames groupAwareTemplate)
69+
{
70+
return groupAwareTemplate.GroupShortNameList;
71+
}
72+
else
73+
{
74+
return Templates.First().Info.ShortNameList;
75+
}
6976
}
70-
HashSet<string> shortNames = new HashSet<string>();
77+
HashSet<string> shortNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
7178
foreach (ITemplateMatchInfo template in Templates)
7279
{
73-
shortNames.UnionWith(template.Info.ShortNameList);
80+
if (template.Info is TemplateInfoWithGroupShortNames groupAwareTemplate)
81+
{
82+
shortNames.UnionWith(groupAwareTemplate.GroupShortNameList);
83+
}
84+
else
85+
{
86+
shortNames.UnionWith(template.Info.ShortNameList);
87+
}
7488
}
7589
return shortNames.ToList();
7690
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using Microsoft.TemplateEngine.Abstractions;
10+
11+
namespace Microsoft.TemplateEngine.Cli.TemplateResolution
12+
{
13+
/// <summary>
14+
/// 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.
15+
/// 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.
16+
/// </summary>
17+
internal class TemplateInfoWithGroupShortNames : ITemplateInfo
18+
{
19+
private ITemplateInfo _parent;
20+
21+
internal TemplateInfoWithGroupShortNames(ITemplateInfo source, IEnumerable<string> groupShortNameList)
22+
{
23+
_parent = source;
24+
GroupShortNameList = groupShortNameList.ToList();
25+
}
26+
27+
public string? Author => _parent.Author;
28+
29+
public string? Description => _parent.Description;
30+
31+
public IReadOnlyList<string> Classifications => _parent.Classifications;
32+
33+
public string? DefaultName => _parent.DefaultName;
34+
35+
public string Identity => _parent.Identity;
36+
37+
public Guid GeneratorId => _parent.GeneratorId;
38+
39+
public string? GroupIdentity => _parent.GroupIdentity;
40+
41+
public int Precedence => _parent.Precedence;
42+
43+
public string Name => _parent.Name;
44+
45+
[Obsolete]
46+
public string ShortName => _parent.ShortName;
47+
48+
public IReadOnlyList<string> ShortNameList => _parent.ShortNameList;
49+
50+
public IReadOnlyList<string> GroupShortNameList { get; } = new List<string>();
51+
52+
[Obsolete]
53+
public IReadOnlyDictionary<string, ICacheTag> Tags => _parent.Tags;
54+
55+
[Obsolete]
56+
public IReadOnlyDictionary<string, ICacheParameter> CacheParameters => _parent.CacheParameters;
57+
58+
public IReadOnlyList<ITemplateParameter> Parameters => _parent.Parameters;
59+
60+
public string MountPointUri => _parent.MountPointUri;
61+
62+
public string ConfigPlace => _parent.ConfigPlace;
63+
64+
public string? LocaleConfigPlace => _parent.LocaleConfigPlace;
65+
66+
public string? HostConfigPlace => _parent.HostConfigPlace;
67+
68+
public string? ThirdPartyNotices => _parent.ThirdPartyNotices;
69+
70+
public IReadOnlyDictionary<string, IBaselineInfo> BaselineInfo => _parent.BaselineInfo;
71+
72+
public IReadOnlyDictionary<string, string> TagsCollection => _parent.TagsCollection;
73+
74+
bool ITemplateInfo.HasScriptRunningPostActions { get; set; }
75+
76+
}
77+
}

src/Microsoft.TemplateEngine.Cli/TemplateResolution/TemplateResolver.cs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private static IReadOnlyList<ITemplateInfo> SetupTemplateInfoWithGroupShortNames
391391

392392
if (!shortNamesByGroup.TryGetValue(effectiveGroupIdentity, out HashSet<string>? shortNames))
393393
{
394-
shortNames = new HashSet<string>();
394+
shortNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
395395
shortNamesByGroup[effectiveGroupIdentity] = shortNames;
396396
}
397397
shortNames.UnionWith(template.ShortNameList);
@@ -471,69 +471,5 @@ private static bool IsTemplateHiddenByHostFile(ITemplateInfo templateInfo, IHost
471471
HostSpecificTemplateData hostData = hostDataLoader.ReadHostSpecificTemplateData(templateInfo);
472472
return hostData.IsHidden;
473473
}
474-
475-
/// <summary>
476-
/// 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.
477-
/// 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.
478-
/// </summary>
479-
private class TemplateInfoWithGroupShortNames : ITemplateInfo
480-
{
481-
private ITemplateInfo _parent;
482-
483-
internal TemplateInfoWithGroupShortNames(ITemplateInfo source, IEnumerable<string> groupShortNameList)
484-
{
485-
_parent = source;
486-
GroupShortNameList = groupShortNameList.ToList();
487-
}
488-
489-
public string? Author => _parent.Author;
490-
491-
public string? Description => _parent.Description;
492-
493-
public IReadOnlyList<string> Classifications => _parent.Classifications;
494-
495-
public string? DefaultName => _parent.DefaultName;
496-
497-
public string Identity => _parent.Identity;
498-
499-
public Guid GeneratorId => _parent.GeneratorId;
500-
501-
public string? GroupIdentity => _parent.GroupIdentity;
502-
503-
public int Precedence => _parent.Precedence;
504-
505-
public string Name => _parent.Name;
506-
507-
[Obsolete]
508-
public string ShortName => _parent.ShortName;
509-
510-
public IReadOnlyList<string> ShortNameList => _parent.ShortNameList;
511-
512-
public IReadOnlyList<string> GroupShortNameList { get; } = new List<string>();
513-
514-
[Obsolete]
515-
public IReadOnlyDictionary<string, ICacheTag> Tags => _parent.Tags;
516-
517-
[Obsolete]
518-
public IReadOnlyDictionary<string, ICacheParameter> CacheParameters => _parent.CacheParameters;
519-
520-
public IReadOnlyList<ITemplateParameter> Parameters => _parent.Parameters;
521-
522-
public string MountPointUri => _parent.MountPointUri;
523-
524-
public string ConfigPlace => _parent.ConfigPlace;
525-
526-
public string? LocaleConfigPlace => _parent.LocaleConfigPlace;
527-
528-
public string? HostConfigPlace => _parent.HostConfigPlace;
529-
530-
public string? ThirdPartyNotices => _parent.ThirdPartyNotices;
531-
532-
public IReadOnlyDictionary<string, IBaselineInfo> BaselineInfo => _parent.BaselineInfo;
533-
534-
public IReadOnlyDictionary<string, string> TagsCollection => _parent.TagsCollection;
535-
536-
bool ITemplateInfo.HasScriptRunningPostActions { get; set; }
537-
}
538474
}
539475
}

src/Microsoft.TemplateEngine.Cli/TemplateSearch/CliTemplateSearchCoordinator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static void DisplayResultsForPack(TemplateSourceSearchResult sourceResul
9898
headerSeparator: '-',
9999
blankLineBetweenRows: false)
100100
.DefineColumn(r => r.TemplateGroupInfo.Name, LocalizableStrings.ColumnNameTemplateName, showAlways: true, shrinkIfNeeded: true, minWidth: 15)
101-
.DefineColumn(r => r.TemplateGroupInfo.ShortName, LocalizableStrings.ColumnNameShortName, showAlways: true)
101+
.DefineColumn(r => r.TemplateGroupInfo.ShortNames, LocalizableStrings.ColumnNameShortName, showAlways: true)
102102
.DefineColumn(r => r.TemplateGroupInfo.Author, LocalizableStrings.ColumnNameAuthor, NewCommandInputCli.AuthorColumnFilter, defaultColumn: true, shrinkIfNeeded: true, minWidth: 10)
103103
.DefineColumn(r => r.TemplateGroupInfo.Languages, LocalizableStrings.ColumnNameLanguage, NewCommandInputCli.LanguageColumnFilter, defaultColumn: true)
104104
.DefineColumn(r => r.TemplateGroupInfo.Type, LocalizableStrings.ColumnNameType, NewCommandInputCli.TypeColumnFilter, defaultColumn: false)

0 commit comments

Comments
 (0)