private static string GetInputParametersString(ListCommandArgs args /*, IReadOnlyDictionary<string, string?>? templateParameters = null*/) { string separator = ", "; IEnumerable <string> appliedFilters = args.AppliedFilters .Select(filter => $"{args.GetFilterToken(filter)}='{args.GetFilterValue(filter)}'"); //IEnumerable<string> appliedTemplateParameters = templateParameters? // .Select(param => string.IsNullOrWhiteSpace(param.Value) ? param.Key : $"{param.Key}='{param.Value}'") ?? Array.Empty<string>(); StringBuilder inputParameters = new StringBuilder(); string? mainCriteria = args.ListNameCriteria; if (!string.IsNullOrWhiteSpace(mainCriteria)) { inputParameters.Append($"'{mainCriteria}'"); if (appliedFilters.Any() /* || appliedTemplateParameters.Any()*/) { inputParameters.Append(separator); } } if (appliedFilters /*.Concat(appliedTemplateParameters)*/.Any()) { inputParameters.Append(string.Join(separator, appliedFilters /*.Concat(appliedTemplateParameters)*/)); } return(inputParameters.ToString()); }
public string Execute(string searchTerms) { var args = new ListCommandArgs(this.Sources); args.SearchTerms = searchTerms; args.ShowAllVersions = this.ShowAllVersions; args.ShowPrerelase = this.ShowPrerelease; return CallNuGet(args.ToString()); }
public void List_CanParseColumnsAll(string command) { ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false)); NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks()); var parseResult = myCommand.Parse(command); ListCommandArgs args = new ListCommandArgs((BaseListCommand)parseResult.CommandResult.Command, parseResult); Assert.True(args.DisplayAllColumns); }
public void List_CanParseFilterOption(string command, string expectedFilter) { FilterOptionDefinition expectedDef = _stringToFilterDefMap[expectedFilter]; ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false)); NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks()); var parseResult = myCommand.Parse(command); ListCommandArgs args = new ListCommandArgs((BaseListCommand)parseResult.CommandResult.Command, parseResult); Assert.Single(args.AppliedFilters); Assert.Contains("filter-value", args.GetFilterValue(expectedDef)); Assert.Equal("source", args.ListNameCriteria); }
//[InlineData("new --list --columns author,type", new[] { "author", "type" })] public void List_CanParseColumns(string command, string[] expectedColumns) { ITemplateEngineHost host = TestHost.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(includeTestTemplates: false)); NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host, _ => new TelemetryLogger(null, false), new NewCommandCallbacks()); var parseResult = myCommand.Parse(command); ListCommandArgs args = new ListCommandArgs((BaseListCommand)parseResult.CommandResult.Command, parseResult); Assert.False(args.DisplayAllColumns); Assert.NotEmpty(args.ColumnsToDisplay); Assert.Equal(expectedColumns.Length, args.ColumnsToDisplay?.Count); foreach (var column in expectedColumns) { Assert.Contains(column, args.ColumnsToDisplay); } }
/// <summary> /// Handles template list display (dotnet new3 --list). /// </summary> /// <param name="args">user command input.</param> /// <param name="cancellationToken">cancellation token.</param> /// <returns></returns> internal async Task <NewCommandStatus> DisplayTemplateGroupListAsync( ListCommandArgs args, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ListTemplateResolver resolver = new ListTemplateResolver(_constraintManager, _templatePackageManager, _hostSpecificDataLoader); TemplateResolutionResult resolutionResult = await resolver.ResolveTemplatesAsync(args, _defaultLanguage, cancellationToken).ConfigureAwait(false); //IReadOnlyDictionary<string, string?>? appliedParameterMatches = resolutionResult.GetAllMatchedParametersList(); if (resolutionResult.TemplateGroupsWithMatchingTemplateInfoAndParameters.Any()) { Reporter.Output.WriteLine( string.Format( LocalizableStrings.TemplatesFoundMatchingInputParameters, GetInputParametersString(args /*, appliedParameterMatches*/))); Reporter.Output.WriteLine(); TabularOutputSettings settings = new TabularOutputSettings(_engineEnvironmentSettings.Environment, args); TemplateGroupDisplay.DisplayTemplateList( _engineEnvironmentSettings, resolutionResult.TemplateGroupsWithMatchingTemplateInfoAndParameters, settings, reporter: Reporter.Output, selectedLanguage: args.Language); return(NewCommandStatus.Success); } else { //if there is no criteria and filters it means that dotnet new list was run but there is no templates installed. if (args.ListNameCriteria == null && !args.AppliedFilters.Any()) { //No templates installed. Reporter.Output.WriteLine(LocalizableStrings.NoTemplatesFound); Reporter.Output.WriteLine(); // To search for the templates on NuGet.org, run: Reporter.Output.WriteLine(LocalizableStrings.SearchTemplatesCommand); Reporter.Output.WriteCommand( Example .For <NewCommand>(args.ParseResult) .WithSubcommand <SearchCommand>() .WithArgument(SearchCommand.NameArgument)); Reporter.Output.WriteLine(); return(NewCommandStatus.Success); } // at least one criteria was specified. // No templates found matching the following input parameter(s): {0}. Reporter.Error.WriteLine( string.Format( LocalizableStrings.NoTemplatesMatchingInputParameters, GetInputParametersString(args /*, appliedParameterMatches*/)) .Bold().Red()); if (resolutionResult.HasTemplateGroupMatches && resolutionResult.ListFilterMismatchGroupCount > 0) { // {0} template(s) partially matched, but failed on {1}. Reporter.Error.WriteLine( string.Format( LocalizableStrings.TemplatesNotValidGivenTheSpecifiedFilter, resolutionResult.ListFilterMismatchGroupCount, GetPartialMatchReason(resolutionResult, args /*, appliedParameterMatches*/)) .Bold().Red()); } if (resolutionResult.HasTemplateGroupMatches && resolutionResult.ContraintsMismatchGroupCount > 0) { // {0} template(s) are not displayed due to their constraints are not satisfied. // To display them add "--ignore-constraints" option. Reporter.Error.WriteLine( string.Format( LocalizableStrings.TemplateListCoordinator_Error_FailedConstraints, resolutionResult.ContraintsMismatchGroupCount, ListCommand.IgnoreConstraintsOption.Aliases.First()) .Bold().Red()); } Reporter.Error.WriteLine(); // To search for the templates on NuGet.org, run: Reporter.Error.WriteLine(LocalizableStrings.SearchTemplatesCommand); if (string.IsNullOrWhiteSpace(args.ListNameCriteria)) { Reporter.Error.WriteCommand( Example .For <NewCommand>(args.ParseResult) .WithSubcommand <SearchCommand>() .WithArgument(SearchCommand.NameArgument)); } else { Reporter.Error.WriteCommand( Example .For <NewCommand>(args.ParseResult) .WithSubcommand <SearchCommand>() .WithArgument(SearchCommand.NameArgument, args.ListNameCriteria)); } Reporter.Error.WriteLine(); return(NewCommandStatus.NotFound); } }
private static string GetPartialMatchReason(TemplateResolutionResult templateResolutionResult, ListCommandArgs args /*, IReadOnlyDictionary<string, string?>? templateParameters = null*/) { string separator = ", "; IEnumerable <string> appliedFilters = args.AppliedFilters .OfType <TemplateFilterOptionDefinition>() .Where(filter => filter.MismatchCriteria(templateResolutionResult)) .Select(filter => $"{args.GetFilterToken(filter)}='{args.GetFilterValue(filter)}'"); //IEnumerable<string> appliedTemplateParameters = templateParameters? // .Where(parameter => // templateResolutionResult.IsParameterMismatchReason(parameter.Key)) // .Select(param => string.IsNullOrWhiteSpace(param.Value) ? param.Key : $"{param.Key}='{param.Value}'") ?? Array.Empty<string>(); StringBuilder inputParameters = new StringBuilder(); if (appliedFilters /*.Concat(appliedTemplateParameters)*/.Any()) { inputParameters.Append(string.Join(separator, appliedFilters /*.Concat(appliedTemplateParameters)*/)); } return(inputParameters.ToString()); }