internal static bool VerifyMatchingTemplates( IEngineEnvironmentSettings environmentSettings, IEnumerable <TemplateCommand> matchingTemplates, Reporter reporter, [NotNullWhen(true)] out IEnumerable <TemplateCommand>?filteredTemplates) { filteredTemplates = matchingTemplates; //if more than one language, this is an error - handle it IEnumerable <string?> languages = matchingTemplates.Select(c => c.Template.GetLanguage()).Distinct(); if (languages.Count() > 1) { var defaultLanguage = environmentSettings.GetDefaultLanguage(); if (languages.Contains(defaultLanguage, StringComparer.OrdinalIgnoreCase)) { var templatesForDefaultLanguage = filteredTemplates.Where(c => string.Equals(c.Template.GetLanguage(), defaultLanguage, StringComparison.OrdinalIgnoreCase)); if (templatesForDefaultLanguage.Any()) { filteredTemplates = templatesForDefaultLanguage; } else { HandleAmbiguousLanguage( environmentSettings, matchingTemplates.Select(c => c.Template), reporter); filteredTemplates = null; return(false); } } else { HandleAmbiguousLanguage( environmentSettings, matchingTemplates.Select(c => c.Template), reporter); filteredTemplates = null; return(false); } } //if more than one type, this is an error - handle it IEnumerable <string?> types = filteredTemplates.Select(c => c.Template.GetTemplateType()).Distinct(); if (types.Count() > 1) { HandleAmbiguousType( environmentSettings, matchingTemplates.Select(c => c.Template), reporter); filteredTemplates = null; return(false); } return(true); }
internal TemplateListCoordinator( IEngineEnvironmentSettings engineEnvironmentSettings, TemplatePackageManager templatePackageManager, IHostSpecificDataLoader hostSpecificDataLoader, ITelemetryLogger telemetryLogger) { _engineEnvironmentSettings = engineEnvironmentSettings ?? throw new ArgumentNullException(nameof(engineEnvironmentSettings)); _templatePackageManager = templatePackageManager ?? throw new ArgumentNullException(nameof(templatePackageManager)); _hostSpecificDataLoader = hostSpecificDataLoader ?? throw new ArgumentNullException(nameof(hostSpecificDataLoader)); _telemetryLogger = telemetryLogger ?? throw new ArgumentNullException(nameof(telemetryLogger)); _defaultLanguage = engineEnvironmentSettings.GetDefaultLanguage(); _constraintManager = new TemplateConstraintManager(_engineEnvironmentSettings); }
protected override async Task <NewCommandStatus> ExecuteAsync( SearchCommandArgs args, IEngineEnvironmentSettings environmentSettings, ITelemetryLogger telemetryLogger, InvocationContext context) { using TemplatePackageManager templatePackageManager = new TemplatePackageManager(environmentSettings); //we need to await, otherwise templatePackageManager will be disposed. return(await CliTemplateSearchCoordinator.SearchForTemplateMatchesAsync( environmentSettings, templatePackageManager, args, environmentSettings.GetDefaultLanguage(), context.GetCancellationToken()).ConfigureAwait(false)); }
/// <summary> /// Displays the list of templates in a table, one row per template group. /// /// The columns displayed are as follows: /// Except where noted, the values are taken from the highest-precedence template in the group. The info could vary among the templates in the group, but shouldn't. /// (There is no check that the info doesn't vary.) /// - Template Name /// - Short Name: displays the all available short names for the group. /// - Language: All languages supported by any template in the group are displayed, with the default language in brackets, e.g.: [C#] /// - Tags /// The columns can be configured via the command args, see <see cref="ITabularOutputArgs"/>/>. /// </summary> internal static void DisplayTemplateList( IEngineEnvironmentSettings engineEnvironmentSettings, IEnumerable <ITemplateInfo> templates, TabularOutputSettings helpFormatterSettings, Reporter reporter, string?selectedLanguage = null) { IReadOnlyCollection <TemplateGroupTableRow> groupsForDisplay = GetTemplateGroupsForListDisplay( templates, selectedLanguage, engineEnvironmentSettings.GetDefaultLanguage(), engineEnvironmentSettings.Environment); DisplayTemplateList(groupsForDisplay, helpFormatterSettings, reporter); }
internal static HashSet <TemplateCommand> GetTemplateCommand( InstantiateCommandArgs args, IEngineEnvironmentSettings environmentSettings, TemplatePackageManager templatePackageManager, TemplateGroup templateGroup) { //groups templates in the group by precedence foreach (IGrouping <int, CliTemplateInfo> templateGrouping in templateGroup.Templates.GroupBy(g => g.Precedence).OrderByDescending(g => g.Key)) { HashSet <TemplateCommand> candidates = ReparseForTemplate( args, environmentSettings, templatePackageManager, templateGroup, templateGrouping, out bool languageOptionSpecified); //if no candidates continue with next precedence if (!candidates.Any()) { continue; } //if language option is not specified, we do not need to do reparsing for default language if (languageOptionSpecified || string.IsNullOrWhiteSpace(environmentSettings.GetDefaultLanguage())) { return(candidates); } // try to reparse for default language return(ReparseForDefaultLanguage( args, environmentSettings, templatePackageManager, templateGroup, candidates)); } return(new HashSet <TemplateCommand>()); }
/// <summary> /// Create command for instantiation of specific template. /// </summary> /// <exception cref="InvalidTemplateParametersException">when <paramref name="template"/> has invalid template parameters.</exception> public TemplateCommand( BaseCommand instantiateCommand, IEngineEnvironmentSettings environmentSettings, TemplatePackageManager templatePackageManager, TemplateGroup templateGroup, CliTemplateInfo template, bool buildDefaultLanguageValidation = false) : base( templateGroup.ShortNames[0], template.Name + Environment.NewLine + template.Description) { _instantiateCommand = instantiateCommand; _environmentSettings = environmentSettings; _templatePackageManager = templatePackageManager; _templateGroup = templateGroup; _template = template; foreach (var item in templateGroup.ShortNames.Skip(1)) { AddAlias(item); } this.AddOption(OutputOption); this.AddOption(NameOption); this.AddOption(DryRunOption); this.AddOption(ForceOption); this.AddOption(NoUpdateCheckOption); string?templateLanguage = template.GetLanguage(); string?defaultLanguage = environmentSettings.GetDefaultLanguage(); if (!string.IsNullOrWhiteSpace(templateLanguage)) { LanguageOption = SharedOptionsFactory.CreateLanguageOption(); LanguageOption.Description = SymbolStrings.TemplateCommand_Option_Language; LanguageOption.FromAmongCaseInsensitive(new[] { templateLanguage }); if (!string.IsNullOrWhiteSpace(defaultLanguage) && buildDefaultLanguageValidation) { LanguageOption.SetDefaultValue(defaultLanguage); LanguageOption.AddValidator(optionResult => { var value = optionResult.GetValueOrDefault <string>(); if (value != template.GetLanguage()) { optionResult.ErrorMessage = "Languages don't match"; } } ); } this.AddOption(LanguageOption); } string?templateType = template.GetTemplateType(); if (!string.IsNullOrWhiteSpace(templateType)) { TypeOption = SharedOptionsFactory.CreateTypeOption(); TypeOption.Description = SymbolStrings.TemplateCommand_Option_Type; TypeOption.FromAmongCaseInsensitive(new[] { templateType }); this.AddOption(TypeOption); } if (template.BaselineInfo.Any(b => !string.IsNullOrWhiteSpace(b.Key))) { BaselineOption = SharedOptionsFactory.CreateBaselineOption(); BaselineOption.Description = SymbolStrings.TemplateCommand_Option_Baseline; BaselineOption.FromAmongCaseInsensitive(template.BaselineInfo.Select(b => b.Key).Where(b => !string.IsNullOrWhiteSpace(b)).ToArray()); this.AddOption(BaselineOption); } if (HasRunScriptPostActionDefined(template)) { AllowScriptsOption = new Option <AllowRunScripts>("--allow-scripts") { Description = SymbolStrings.TemplateCommand_Option_AllowScripts, Arity = new ArgumentArity(1, 1) }; AllowScriptsOption.SetDefaultValue(AllowRunScripts.Prompt); this.AddOption(AllowScriptsOption); } AddTemplateOptionsToCommand(template); }