-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fixes #3804 implements search subcommand * Args.Language implementation * removed FilterDefinition.Name
- Loading branch information
1 parent
b4358eb
commit de6a53c
Showing
39 changed files
with
1,499 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Microsoft.TemplateEngine.Cli/Commands/BaseFilterableArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.CommandLine.Parsing; | ||
|
||
namespace Microsoft.TemplateEngine.Cli.Commands | ||
{ | ||
internal class BaseFilterableArgs : GlobalArgs | ||
{ | ||
private IReadOnlyDictionary<FilterOptionDefinition, OptionResult> _filters; | ||
|
||
internal BaseFilterableArgs(BaseCommand command, ParseResult parseResult) : base(command, parseResult) | ||
{ | ||
if (command is not IFilterableCommand filterableCommand) | ||
{ | ||
throw new ArgumentException($"{nameof(command)} should be {nameof(IFilterableCommand)}", nameof(command)); | ||
} | ||
_filters = ParseFilters(filterableCommand, parseResult); | ||
} | ||
|
||
/// <summary> | ||
/// Gets list of <see cref="FilterOptionDefinition"/> parsed from command. | ||
/// </summary> | ||
internal IEnumerable<FilterOptionDefinition> AppliedFilters => _filters.Keys; | ||
|
||
/// <summary> | ||
/// Gets value for filter <paramref name="filter"/>. | ||
/// </summary> | ||
/// <param name="filter"></param> | ||
/// <returns>value of the filter.</returns> | ||
/// <exception cref="ArgumentException">when <paramref name="filter"/> is not among <see cref="AppliedFilters"/>.</exception> | ||
internal string GetFilterValue(FilterOptionDefinition filter) | ||
{ | ||
if (!_filters.ContainsKey(filter)) | ||
{ | ||
throw new ArgumentException($"{nameof(filter)} is not available in parse result.", nameof(filter)); | ||
} | ||
|
||
return _filters[filter].GetValueOrDefault<string>() ?? string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Gets token name used for filter <paramref name="filter"/>. | ||
/// </summary> | ||
/// <param name="filter"></param> | ||
/// <returns>Token or null when token cannot be evaluated.</returns> | ||
internal string? GetFilterToken(FilterOptionDefinition filter) | ||
{ | ||
return _filters[filter].Token?.Value; | ||
} | ||
|
||
private static IReadOnlyDictionary<FilterOptionDefinition, OptionResult> ParseFilters(IFilterableCommand filterableCommand, ParseResult parseResult) | ||
{ | ||
Dictionary<FilterOptionDefinition, OptionResult> filterValues = new(); | ||
foreach (var filter in filterableCommand.Filters) | ||
{ | ||
OptionResult? value = parseResult.FindResultFor(filter.Value); | ||
if (value != null) | ||
{ | ||
filterValues[filter.Key] = value; | ||
} | ||
} | ||
return filterValues; | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/Microsoft.TemplateEngine.Cli/Commands/FilterOptionDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.CommandLine; | ||
using Microsoft.TemplateEngine.Abstractions; | ||
using Microsoft.TemplateEngine.Abstractions.TemplateFiltering; | ||
using Microsoft.TemplateEngine.Cli.TemplateResolution; | ||
using Microsoft.TemplateEngine.Utils; | ||
using Microsoft.TemplateSearch.Common.Abstractions; | ||
|
||
namespace Microsoft.TemplateEngine.Cli.Commands | ||
{ | ||
/// <summary> | ||
/// Defines supported dotnet new command filter option | ||
/// Filter options can be used along with other dotnet new subcommands to filter the required items for the action, for example for list subcommand filters can limit the templates to be shown. | ||
/// </summary> | ||
internal class FilterOptionDefinition | ||
{ | ||
internal FilterOptionDefinition(Func<Option> optionFactory) | ||
{ | ||
OptionFactory = optionFactory ?? throw new ArgumentNullException(nameof(optionFactory)); | ||
} | ||
|
||
internal static FilterOptionDefinition AuthorFilter { get; } = | ||
new TemplateFilterOptionDefinition( | ||
optionFactory: () => SharedOptionsFactory.CreateAuthorOption(), | ||
matchFilter: authorArg => WellKnownSearchFilters.AuthorFilter(authorArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasAuthorMismatch); | ||
|
||
internal static FilterOptionDefinition BaselineFilter { get; } = | ||
new TemplateFilterOptionDefinition( | ||
optionFactory: () => SharedOptionsFactory.CreateBaselineOption(), | ||
matchFilter: baselineArg => WellKnownSearchFilters.BaselineFilter(baselineArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasBaselineMismatch); | ||
|
||
internal static FilterOptionDefinition LanguageFilter { get; } = | ||
new TemplateFilterOptionDefinition( | ||
optionFactory: () => SharedOptionsFactory.CreateLanguageOption(), | ||
matchFilter: languageArg => WellKnownSearchFilters.LanguageFilter(languageArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasLanguageMismatch); | ||
|
||
internal static FilterOptionDefinition TagFilter { get; } = | ||
new TemplateFilterOptionDefinition( | ||
optionFactory: () => SharedOptionsFactory.CreateTagOption(), | ||
matchFilter: tagArg => WellKnownSearchFilters.ClassificationFilter(tagArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasClassificationMismatch); | ||
|
||
internal static FilterOptionDefinition TypeFilter { get; } = | ||
new TemplateFilterOptionDefinition( | ||
optionFactory: () => SharedOptionsFactory.CreateTypeOption(), | ||
matchFilter: typeArg => WellKnownSearchFilters.TypeFilter(typeArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasTypeMismatch); | ||
|
||
internal static FilterOptionDefinition PackageFilter { get; } = | ||
new PackageFilterOptionDefinition( | ||
optionFactory: () => SharedOptionsFactory.CreatePackageOption(), | ||
matchFilter: PackageMatchFilter); | ||
|
||
/// <summary> | ||
/// A predicate that creates instance of option. | ||
/// </summary> | ||
internal Func<Option> OptionFactory { get; } | ||
|
||
private static Func<ITemplatePackageInfo, bool> PackageMatchFilter(string? packageArg) | ||
{ | ||
return (pack) => | ||
{ | ||
if (string.IsNullOrWhiteSpace(packageArg)) | ||
{ | ||
return true; | ||
} | ||
return pack.Name.Contains(packageArg, StringComparison.OrdinalIgnoreCase); | ||
}; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Defines supported dotnet new command filter option applicable to the template. | ||
/// </summary> | ||
internal class TemplateFilterOptionDefinition : FilterOptionDefinition | ||
{ | ||
internal TemplateFilterOptionDefinition( | ||
Func<Option> optionFactory, | ||
Func<string?, Func<ITemplateInfo, MatchInfo?>> matchFilter, | ||
Func<TemplateResolutionResult, bool> mismatchCriteria) : base(optionFactory) | ||
{ | ||
TemplateMatchFilter = matchFilter ?? throw new ArgumentNullException(nameof(matchFilter)); | ||
MismatchCriteria = mismatchCriteria ?? throw new ArgumentNullException(nameof(mismatchCriteria)); | ||
} | ||
|
||
/// <summary> | ||
/// A predicate that returns the template match filter for the filter option. | ||
/// Template match filter should return the MatchInfo for the given template based on filter value. | ||
/// </summary> | ||
/// <remarks> | ||
/// Common template match filters are defined in Microsoft.TemplateEngine.Utils.WellKnonwnSearchFilter class. | ||
/// </remarks> | ||
internal Func<string?, Func<ITemplateInfo, MatchInfo?>> TemplateMatchFilter { get; set; } | ||
|
||
/// <summary> | ||
/// A predicate that returns if the filter option caused a mismatch in <see cref="TemplateResolutionResult"/> in case of partial match. | ||
/// </summary> | ||
internal Func<TemplateResolutionResult, bool> MismatchCriteria { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Defines supported dotnet new command filter option applicable to the package. | ||
/// </summary> | ||
internal class PackageFilterOptionDefinition : FilterOptionDefinition | ||
{ | ||
internal PackageFilterOptionDefinition( | ||
Func<Option> optionFactory, | ||
Func<string?, Func<ITemplatePackageInfo, bool>> matchFilter) : base(optionFactory) | ||
{ | ||
PackageMatchFilter = matchFilter ?? throw new ArgumentNullException(nameof(matchFilter)); | ||
} | ||
|
||
/// <summary> | ||
/// A predicate that returns the package match filter for the filter option | ||
/// Package match filter should if package is a match based on filter value. | ||
/// </summary> | ||
internal Func<string?, Func<ITemplatePackageInfo, bool>> PackageMatchFilter { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Microsoft.TemplateEngine.Cli/Commands/IFilterableCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.CommandLine; | ||
|
||
namespace Microsoft.TemplateEngine.Cli.Commands | ||
{ | ||
internal interface IFilterableCommand | ||
{ | ||
IReadOnlyDictionary<FilterOptionDefinition, Option> Filters { get; } | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Microsoft.TemplateEngine.Cli/Commands/ITabularOutputCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.CommandLine; | ||
|
||
namespace Microsoft.TemplateEngine.Cli.Commands | ||
{ | ||
internal interface ITabularOutputCommand | ||
{ | ||
internal Option<bool> ColumnsAllOption { get; } | ||
|
||
internal Option<IReadOnlyList<string>> ColumnsOption { get; } | ||
} | ||
|
||
internal interface ITabularOutputArgs | ||
{ | ||
internal bool DisplayAllColumns { get; } | ||
|
||
internal IReadOnlyList<string>? ColumnsToDisplay { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.