Пример #1
0
        public static IReadOnlyCollection<ITemplateMatchInfo> PerformCoreTemplateQueryForHelp(IReadOnlyList<ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, INewCommandInput commandInput, string defaultLanguage)
        {
            IReadOnlyList<FilterableTemplateInfo> filterableTemplateInfo = SetupFilterableTemplateInfoFromTemplateInfo(templateInfo);
            IReadOnlyList<ITemplateMatchInfo> coreMatchedTemplates = TemplateListFilter.GetTemplateMatchInfo
            (
                filterableTemplateInfo,
                TemplateListFilter.PartialMatchFilter,
                WellKnownSearchFilters.NameFilter(commandInput.TemplateName),
                WellKnownSearchFilters.LanguageFilter(commandInput.Language),
                WellKnownSearchFilters.ContextFilter(commandInput.TypeFilter),
                WellKnownSearchFilters.BaselineFilter(commandInput.BaselineName)
            )
            .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            //for help if template name from CLI exactly matches the template name we should consider only that template
            IReadOnlyList<ITemplateMatchInfo> matchesWithExactDispositionsInNameFields = coreMatchedTemplates.Where(x => x.MatchDisposition.Any(y => NameFields.Contains(y.Location) && y.Kind == MatchKind.Exact)).ToList();
            if (matchesWithExactDispositionsInNameFields.Count > 0)
            {               
                coreMatchedTemplates = matchesWithExactDispositionsInNameFields;
            }

            //for help we also need to match on default language if language was not specified as parameter
            if (string.IsNullOrEmpty(commandInput.Language) && !string.IsNullOrEmpty(defaultLanguage))
            {
                // default language matching only makes sense if the user didn't specify a language.
                AddDefaultLanguageMatchingToTemplates(coreMatchedTemplates, defaultLanguage);
            }
            AddParameterMatchingToTemplates(coreMatchedTemplates, hostDataLoader, commandInput);
            return coreMatchedTemplates;
        }
        private static void DisplayPartialNameMatchAndContextProblems(string templateName, string context, TemplateListResolutionResult templateResolutionResult, out bool shouldShowTemplateList)
        {
            shouldShowTemplateList = false;

            if (templateResolutionResult.IsTemplateAmbiguous)
            {
                // Unable to determine the desired template from the input template name: {0}..
                Reporter.Error.WriteLine(string.Format(LocalizableStrings.AmbiguousInputTemplateName, templateName).Bold().Red());
            }
            else if (templateResolutionResult.IsNoTemplatesMatchedState)
            {
                // No templates matched the input template name: {0}
                Reporter.Error.WriteLine(string.Format(LocalizableStrings.NoTemplatesMatchName, templateName).Bold().Red());
                Reporter.Error.WriteLine();
                return;
            }

            bool anythingReported = false;

            foreach (IReadOnlyList <ITemplateMatchInfo> templateGroup in templateResolutionResult.ContextProblemMatchGroups)
            {
                // all templates in a group should have the same context & name
                if (templateGroup[0].Info.Tags != null && templateGroup[0].Info.Tags.TryGetValue("type", out ICacheTag typeTag))
                {
                    MatchInfo?matchInfo = WellKnownSearchFilters.ContextFilter(context)(templateGroup[0].Info);
                    if ((matchInfo?.Kind ?? MatchKind.Mismatch) == MatchKind.Mismatch)
                    {
                        // {0} matches the specified name, but has been excluded by the --type parameter. Remove or change the --type parameter to use that template
                        Reporter.Error.WriteLine(string.Format(LocalizableStrings.TemplateNotValidGivenTheSpecifiedFilter, templateGroup[0].Info.Name).Bold().Red());
                        anythingReported = true;
                    }
                }
                else
                {
                    // this really shouldn't ever happen. But better to have a generic error than quietly ignore the partial match.
                    //
                    //{0} cannot be created in the target location
                    Reporter.Error.WriteLine(string.Format(LocalizableStrings.GenericPlaceholderTemplateContextError, templateGroup[0].Info.Name).Bold().Red());
                    anythingReported = true;
                }
            }

            if (templateResolutionResult.RemainingPartialMatchGroups.Count > 0)
            {
                // The following templates partially match the input. Be more specific with the template name and/or language
                Reporter.Error.WriteLine(LocalizableStrings.TemplateMultiplePartialNameMatches.Bold().Red());
                anythingReported       = true;
                shouldShowTemplateList = true;
            }

            if (anythingReported)
            {
                Reporter.Error.WriteLine();
            }
        }
Пример #3
0
        // Lists all the templtes, filtered only by the context (item, project, etc)
        private Task <IReadOnlyCollection <IFilteredTemplateInfo> > PerformAllTemplatesInContextQueryAsync()
        {
            string context = DetermineTemplateContext();

            IReadOnlyCollection <IFilteredTemplateInfo> templates = _templateCreator.List(
                false,
                WellKnownSearchFilters.ContextFilter(context),
                WellKnownSearchFilters.NameFilter(string.Empty)
                );

            return(Task.FromResult(templates));
        }
Пример #4
0
        // Lists all the templates, filtered only by the context (item, project, etc) - and the host file.
        public static IReadOnlyCollection <IFilteredTemplateInfo> PerformAllTemplatesInContextQuery(IReadOnlyList <ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, string context)
        {
            IReadOnlyCollection <IFilteredTemplateInfo> templates = TemplateListFilter.FilterTemplates
                                                                    (
                templateInfo,
                false,
                WellKnownSearchFilters.ContextFilter(context),
                WellKnownSearchFilters.NameFilter(string.Empty)
                                                                    )
                                                                    .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            return(templates);
        }
Пример #5
0
        // Lists all the templates, filtered only by the context (item, project, etc) - and the host file.
        public static IReadOnlyCollection <ITemplateMatchInfo> PerformAllTemplatesInContextQuery(IReadOnlyList <ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, string context)
        {
            // If there is a context match, it must be exact. Other dispositions are irrelevant.
            Func <ITemplateMatchInfo, bool>          contextFilter = x => x.MatchDisposition.All(d => d.Location != MatchLocation.Context || d.Kind == MatchKind.Exact);
            IReadOnlyCollection <ITemplateMatchInfo> templates     = TemplateListFilter.GetTemplateMatchInfo
                                                                     (
                templateInfo,
                contextFilter,
                WellKnownSearchFilters.ContextFilter(context),
                WellKnownSearchFilters.NameFilter(string.Empty)
                                                                     )
                                                                     .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            return(templates);
        }
Пример #6
0
        private static void DisplayPartialNameMatchLanguageAndContextProblems(string templateName, string templateLanguage, string context, TemplateListResolutionResult templateResolutionResult, out bool shouldShowTemplateList)
        {
            shouldShowTemplateList = false;

            if (templateResolutionResult.IsNoTemplatesMatchedState || templateResolutionResult.UsingPartialMatches)
            {
                ShowNoTemplatesFoundMessage(templateName, templateLanguage, context);
                Reporter.Error.WriteLine();
                return;
            }

            bool anythingReported           = false;
            int  partialTemplatesMatchCount = templateResolutionResult.ContextProblemMatchGroups.Count(templateGroup =>
            {
                // all templates in a group should have the same context & name
                if (templateGroup[0].Info.Tags != null && templateGroup[0].Info.Tags.TryGetValue("type", out ICacheTag typeTag))
                {
                    MatchInfo?matchInfo = WellKnownSearchFilters.ContextFilter(context)(templateGroup[0].Info);
                    return((matchInfo?.Kind ?? MatchKind.Mismatch) == MatchKind.Mismatch);
                }

                // this really shouldn't ever happen. But better to have a generic error than quietly ignore the partial match.
                // Cannot retrieve the type for {0}.
                Reporter.Error.WriteLine(string.Format(LocalizableStrings.GenericPlaceholderTemplateContextError, templateGroup[0].Info.Name).Bold().Red());
                anythingReported = true;
                return(false);
            });

            if (partialTemplatesMatchCount > 0)
            {
                ShowNoTemplatesFoundMessage(templateName, templateLanguage, context);
                // {0} template(s) partially matched, but failed on {1}.
                Reporter.Error.WriteLine(string.Format(LocalizableStrings.TemplatesNotValidGivenTheSpecifiedFilter, partialTemplatesMatchCount, string.Concat("type=", context)).Bold().Red());
                anythingReported = true;
            }

            if (templateResolutionResult.RemainingPartialMatchGroups.Count > 0)
            {
                shouldShowTemplateList = true;
            }

            if (anythingReported)
            {
                Reporter.Error.WriteLine();
            }
        }
Пример #7
0
        // Query for template matches, filtered by everything available: name, language, context, parameters, and the host file.
        // this method is not used for list and help
        public static IReadOnlyCollection<ITemplateMatchInfo> PerformCoreTemplateQuery(IReadOnlyList<ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, INewCommandInput commandInput, string defaultLanguage)
        {
            IReadOnlyList<FilterableTemplateInfo> filterableTemplateInfo = SetupFilterableTemplateInfoFromTemplateInfo(templateInfo);

            IReadOnlyCollection<ITemplateMatchInfo> templates = TemplateListFilter.GetTemplateMatchInfo
            (
                filterableTemplateInfo,
                TemplateListFilter.PartialMatchFilter,
                WellKnownSearchFilters.NameFilter(commandInput.TemplateName),
                WellKnownSearchFilters.ClassificationsFilter(commandInput.TemplateName),
                WellKnownSearchFilters.LanguageFilter(commandInput.Language),
                WellKnownSearchFilters.ContextFilter(commandInput.TypeFilter),
                WellKnownSearchFilters.BaselineFilter(commandInput.BaselineName)
            )
            .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            IReadOnlyList<ITemplateMatchInfo> coreMatchedTemplates = templates.Where(x => x.IsMatch).ToList();

            if (coreMatchedTemplates.Count == 0)
            {
                // No exact matches, take the partial matches and be done.
                coreMatchedTemplates = templates.Where(x => x.IsPartialMatch).ToList();
            }
            else
            {
                IReadOnlyList<ITemplateMatchInfo> matchesWithExactDispositionsInNameFields = coreMatchedTemplates.Where(x => x.MatchDisposition.Any(y => NameFields.Contains(y.Location) && y.Kind == MatchKind.Exact)).ToList();

                if (matchesWithExactDispositionsInNameFields.Count > 0)
                {
                    // Start with the exact name matches, if there are any.
                    coreMatchedTemplates = matchesWithExactDispositionsInNameFields;
                }
            }

            if (string.IsNullOrEmpty(commandInput.Language) && !string.IsNullOrEmpty(defaultLanguage))
            {
                // default language matching only makes sense if the user didn't specify a language.
                AddDefaultLanguageMatchingToTemplates(coreMatchedTemplates, defaultLanguage);
            }

            AddParameterMatchingToTemplates(coreMatchedTemplates, hostDataLoader, commandInput);

            return coreMatchedTemplates;
        }
Пример #8
0
        // Query for template matches, filtered by everything available: name, language, context, parameters, and the host file.
        public static TemplateListResolutionResult PerformCoreTemplateQuery(IReadOnlyList <ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, INewCommandInput commandInput, string defaultLanguage)
        {
            IReadOnlyCollection <IFilteredTemplateInfo> templates = TemplateListFilter.FilterTemplates
                                                                    (
                templateInfo,
                false,
                WellKnownSearchFilters.NameFilter(commandInput.TemplateName),
                WellKnownSearchFilters.ClassificationsFilter(commandInput.TemplateName),
                WellKnownSearchFilters.LanguageFilter(commandInput.Language),
                WellKnownSearchFilters.ContextFilter(commandInput.TypeFilter?.ToLowerInvariant()),
                WellKnownSearchFilters.BaselineFilter(commandInput.BaselineName)
                                                                    )
                                                                    .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            IReadOnlyList <IFilteredTemplateInfo> coreMatchedTemplates = templates.Where(x => x.IsMatch).ToList();
            bool anyExactCoreMatches;

            if (coreMatchedTemplates.Count == 0)
            {
                coreMatchedTemplates = templates.Where(x => x.IsPartialMatch).ToList();
                anyExactCoreMatches  = false;
            }
            else
            {
                anyExactCoreMatches = true;
                IReadOnlyList <IFilteredTemplateInfo> matchesWithExactDispositionsInNameFields = coreMatchedTemplates.Where(x => x.MatchDisposition.Any(y => NameFields.Contains(y.Location) && y.Kind == MatchKind.Exact)).ToList();

                if (matchesWithExactDispositionsInNameFields.Count > 0)
                {
                    coreMatchedTemplates = matchesWithExactDispositionsInNameFields;
                }
            }

            TemplateListResolutionResult matchResults = new TemplateListResolutionResult()
            {
                CoreMatchedTemplates = coreMatchedTemplates
            };

            QueryForUnambiguousTemplateGroup(templateInfo, hostDataLoader, commandInput, matchResults, defaultLanguage, anyExactCoreMatches);

            return(matchResults);
        }
Пример #9
0
        public static IReadOnlyCollection <ITemplateMatchInfo> PerformCoreTemplateQueryForList(IReadOnlyList <ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, INewCommandInput commandInput, string defaultLanguage)
        {
            IReadOnlyList <FilterableTemplateInfo> filterableTemplateInfo = SetupFilterableTemplateInfoFromTemplateInfo(templateInfo);

            // for list we also try to get match on template name in classification (tags). These matches only will be used if short name and name has a mismatch.
            // filter below only sets the exact or partial match if name matches the tag. If name doesn't match the tag, no match disposition is added to collection.
            IReadOnlyList <ITemplateMatchInfo> coreMatchedTemplates = TemplateListFilter.GetTemplateMatchInfo
                                                                      (
                filterableTemplateInfo,
                TemplateListFilter.PartialMatchFilter,
                WellKnownSearchFilters.NameFilter(commandInput.TemplateName),
                WellKnownSearchFilters.ClassificationsFilter(commandInput.TemplateName),
                WellKnownSearchFilters.LanguageFilter(commandInput.Language),
                WellKnownSearchFilters.ContextFilter(commandInput.TypeFilter),
                WellKnownSearchFilters.BaselineFilter(commandInput.BaselineName)
                                                                      )
                                                                      .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            AddParameterMatchingToTemplates(coreMatchedTemplates, hostDataLoader, commandInput);
            return(coreMatchedTemplates);
        }
Пример #10
0
        /// <summary>
        /// Performs the filtering of installed templates for template instantiated.
        /// Filters applied: template name filter; language, type, classification and baseline filters. Only templates that match the filters are returned, no partial matches allowed.
        /// In case any templates in match above are matching name or short name exactly, only they are returned.
        /// The matches for default language and template specific parameters are added to the result.
        /// </summary>
        /// <param name="templateInfo">the list of templates to be filtered</param>
        /// <param name="hostDataLoader">data of the host</param>
        /// <param name="commandInput">new command data used in CLI</param>
        /// <param name="defaultLanguage"></param>
        /// <returns>the collection of the templates with their match dispositions (<seealso cref="ITemplateMatchInfo"/>). The templates that do not match are not added to the collection</returns>
        public static IReadOnlyCollection <ITemplateMatchInfo> PerformCoreTemplateQuery(IReadOnlyList <ITemplateInfo> templateInfo, IHostSpecificDataLoader hostDataLoader, INewCommandInput commandInput, string defaultLanguage)
        {
            IReadOnlyList <FilterableTemplateInfo> filterableTemplateInfo = SetupFilterableTemplateInfoFromTemplateInfo(templateInfo);

            IReadOnlyCollection <ITemplateMatchInfo> templates = TemplateListFilter.GetTemplateMatchInfo
                                                                 (
                filterableTemplateInfo,
                TemplateListFilter.ExactMatchFilter,
                WellKnownSearchFilters.NameFilter(commandInput.TemplateName),
                //WellKnownSearchFilters.ClassificationsFilter(commandInput.TemplateName),
                WellKnownSearchFilters.LanguageFilter(commandInput.Language),
                WellKnownSearchFilters.ContextFilter(commandInput.TypeFilter),
                WellKnownSearchFilters.BaselineFilter(commandInput.BaselineName)
                                                                 )
                                                                 .Where(x => !IsTemplateHiddenByHostFile(x.Info, hostDataLoader)).ToList();

            //select only the templates which do not have mismatches
            //if any template has exact match for name - use those; otherwise partial name matches are also considered when resolving templates
            IReadOnlyList <ITemplateMatchInfo> matchesWithExactDispositionsInNameFields = templates.Where(x => x.MatchDisposition.Any(y => NameFields.Contains(y.Location) && y.Kind == MatchKind.Exact)).ToList();

            if (matchesWithExactDispositionsInNameFields.Count > 0)
            {
                templates = matchesWithExactDispositionsInNameFields;
            }

            if (string.IsNullOrEmpty(commandInput.Language) && !string.IsNullOrEmpty(defaultLanguage))
            {
                // add default language matches to the list
                // default language matching only makes sense if the user didn't specify a language.
                AddDefaultLanguageMatchingToTemplates(templates, defaultLanguage);
            }

            //add specific template parameters matches to the list
            AddParameterMatchingToTemplates(templates, hostDataLoader, commandInput);

            return(templates);
        }
Пример #11
0
        private Task PerformCoreTemplateQueryAsync()
        {
            string context = DetermineTemplateContext();

            //Perform the core query to search for templates
            IReadOnlyCollection <IFilteredTemplateInfo> templates = _templateCreator.List
                                                                    (
                false,
                WellKnownSearchFilters.AliasFilter(TemplateName),
                WellKnownSearchFilters.NameFilter(TemplateName),
                WellKnownSearchFilters.ClassificationsFilter(TemplateName),
                WellKnownSearchFilters.LanguageFilter(Language),
                WellKnownSearchFilters.ContextFilter(context)
                                                                    );

            IReadOnlyList <IFilteredTemplateInfo> matchedTemplates = templates.Where(x => x.IsMatch).ToList();

            if (matchedTemplates.Count == 0)
            {
                matchedTemplates    = templates.Where(x => x.IsPartialMatch).ToList();
                _forceAmbiguousFlow = true;
            }
            else
            {
                IReadOnlyList <IFilteredTemplateInfo> matchesWithExactDispositionsInNameFields = matchedTemplates.Where(x => x.MatchDisposition.Any(y => NameFields.Contains(y.Location) && y.Kind == MatchKind.Exact)).ToList();

                if (matchesWithExactDispositionsInNameFields.Count > 0)
                {
                    matchedTemplates = matchesWithExactDispositionsInNameFields;
                }
            }

            _matchedTemplates = matchedTemplates;

            return(Task.FromResult(true));
        }