Skip to content

Commit

Permalink
fix for bug #1373
Browse files Browse the repository at this point in the history
  • Loading branch information
seancpeters authored and mlorbetske committed Dec 21, 2017
1 parent fbf17b8 commit 530be98
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/Microsoft.TemplateEngine.Cli/TemplateListResolutionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Edge.Template;

namespace Microsoft.TemplateEngine.Cli
Expand Down Expand Up @@ -109,16 +110,46 @@ public bool TryGetAllInvokableTemplates(out IReadOnlyList<ITemplateMatchInfo> in

public bool TryGetSingularInvokableMatch(out ITemplateMatchInfo template)
{
IReadOnlyList<ITemplateMatchInfo> invokableMatches = _coreMatchedTemplates.Where(x => x.IsInvokableMatch()
&& (_hasUserInputLanguage
|| x.DispositionOfDefaults.Count == 0
|| x.DispositionOfDefaults.Any(y => y.Location == MatchLocation.DefaultLanguage && y.Kind == MatchKind.Exact))
)
.ToList();

if (invokableMatches.Count == 1)
{
template = invokableMatches[0];
IReadOnlyList<ITemplateMatchInfo> invokableMatches = _coreMatchedTemplates.Where(x => x.IsInvokableMatch()).ToList();
IReadOnlyList<ITemplateMatchInfo> languageFilteredInvokableMatches;

if (_hasUserInputLanguage)
{
languageFilteredInvokableMatches = invokableMatches;
}
else
{
// check for templates with the default language
languageFilteredInvokableMatches = invokableMatches.Where(x => x.DispositionOfDefaults.Any(y => y.Location == MatchLocation.DefaultLanguage && y.Kind == MatchKind.Exact)).ToList();

if (languageFilteredInvokableMatches.Count == 0)
{
// no templates with the default language.
// Check if there are multiple languages among the results. If so, can't invoke.
HashSet<string> languagesForMatches = new HashSet<string>();
foreach (ITemplateMatchInfo templateInfo in invokableMatches)
{
if (templateInfo.Info.Tags.TryGetValue("language", out ICacheTag languageTag))
{
languagesForMatches.Add(languageTag.DefaultValue);
}
}

if (languagesForMatches.Count > 1)
{
// there are multiple languages in the possible templates. No way to decide which one to use.
template = null;
return false;
}

// all the invokableMatches are the same language, or have no language. Continue checking them.
languageFilteredInvokableMatches = invokableMatches;
}
}

if (languageFilteredInvokableMatches.Count == 1)
{
template = languageFilteredInvokableMatches[0];
return true;
}

Expand All @@ -127,7 +158,7 @@ public bool TryGetSingularInvokableMatch(out ITemplateMatchInfo template)
// The one with single starts with is chosen as invokable because if the template with an ambiguous match
// was not installed, the one with the singluar invokable would be chosen.
HashSet<string> singleStartsWithParamNames = new HashSet<string>();
foreach (ITemplateMatchInfo checkTemplate in invokableMatches)
foreach (ITemplateMatchInfo checkTemplate in languageFilteredInvokableMatches)
{
IList<string> singleStartParamNames = checkTemplate.MatchDisposition.Where(x => x.Location == MatchLocation.OtherParameter && x.Kind == MatchKind.SingleStartsWith).Select(x => x.ChoiceIfLocationIsOtherChoice).ToList();
foreach (string paramName in singleStartParamNames)
Expand All @@ -140,7 +171,7 @@ public bool TryGetSingularInvokableMatch(out ITemplateMatchInfo template)
}
}

ITemplateMatchInfo highestInGroupIfSingleGroup = TemplateListResolver.FindHighestPrecedenceTemplateIfAllSameGroupIdentity(invokableMatches);
ITemplateMatchInfo highestInGroupIfSingleGroup = TemplateListResolver.FindHighestPrecedenceTemplateIfAllSameGroupIdentity(languageFilteredInvokableMatches);
if (highestInGroupIfSingleGroup != null)
{
template = highestInGroupIfSingleGroup;
Expand Down

0 comments on commit 530be98

Please sign in to comment.