forked from dotnet/templating
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes dotnet#3804 implements search subcommand
- Loading branch information
1 parent
1449a0a
commit 45395f7
Showing
32 changed files
with
1,253 additions
and
347 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
142 changes: 142 additions & 0 deletions
142
src/Microsoft.TemplateEngine.Cli/Commands/FilterOption.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,142 @@ | ||
// 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 command options to filter the required items for the action defined by other option, for example for --list option filters can limit the templates to be shown. | ||
/// </summary> | ||
internal class FilterOptionDefinition | ||
{ | ||
internal FilterOptionDefinition(string name, Func<Option> optionFactory) | ||
{ | ||
Name = string.IsNullOrWhiteSpace(name) ? throw new ArgumentException($"{nameof(name)} should not be null or whitespace", nameof(name)) : name; | ||
OptionFactory = optionFactory ?? throw new ArgumentNullException(nameof(optionFactory)); | ||
} | ||
|
||
internal static FilterOptionDefinition AuthorFilter { get; } = | ||
new TemplateFilterOption( | ||
"author", | ||
optionFactory: () => SharedOptionsFactory.GetAuthorOption(), | ||
matchFilter: authorArg => WellKnownSearchFilters.AuthorFilter(authorArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasAuthorMismatch); | ||
|
||
internal static FilterOptionDefinition BaselineFilter { get; } = | ||
new TemplateFilterOption( | ||
"baseline", | ||
optionFactory: () => SharedOptionsFactory.GetBaselineOption(), | ||
matchFilter: baselineArg => WellKnownSearchFilters.BaselineFilter(baselineArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasBaselineMismatch); | ||
|
||
internal static FilterOptionDefinition LanguageFilter { get; } = | ||
new TemplateFilterOption( | ||
"language", | ||
optionFactory: () => SharedOptionsFactory.GetLanguageOption(), | ||
matchFilter: languageArg => WellKnownSearchFilters.LanguageFilter(languageArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasLanguageMismatch); | ||
|
||
internal static FilterOptionDefinition TagFilter { get; } = | ||
new TemplateFilterOption( | ||
"tag", | ||
optionFactory: () => SharedOptionsFactory.GetTagOption(), | ||
matchFilter: tagArg => WellKnownSearchFilters.ClassificationFilter(tagArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasClassificationMismatch); | ||
|
||
internal static FilterOptionDefinition TypeFilter { get; } = | ||
new TemplateFilterOption( | ||
"type", | ||
optionFactory: () => SharedOptionsFactory.GetTypeOption(), | ||
matchFilter: typeArg => WellKnownSearchFilters.TypeFilter(typeArg), | ||
mismatchCriteria: resolutionResult => resolutionResult.HasTypeMismatch); | ||
|
||
internal static FilterOptionDefinition PackageFilter { get; } = | ||
new PackageFilterOption( | ||
"package", | ||
optionFactory: () => SharedOptionsFactory.GetPackageOption(), | ||
matchFilter: PackageMatchFilter); | ||
|
||
//TODO: check if it's needed | ||
|
||
/// <summary> | ||
/// Name of the option, should match option long name. | ||
/// </summary> | ||
internal string Name { get; } | ||
|
||
/// <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.IndexOf(packageArg, StringComparison.OrdinalIgnoreCase) > -1; | ||
}; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Defines supported dotnet new command filter option applicable to the template. | ||
/// </summary> | ||
internal class TemplateFilterOption : FilterOptionDefinition | ||
{ | ||
internal TemplateFilterOption( | ||
string name, | ||
Func<Option> optionFactory, | ||
Func<string, Func<ITemplateInfo, MatchInfo?>> matchFilter, | ||
Func<TemplateResolutionResult, bool> mismatchCriteria) : base(name, 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 PackageFilterOption : FilterOptionDefinition | ||
{ | ||
internal PackageFilterOption( | ||
string name, | ||
Func<Option> optionFactory, | ||
Func<string, Func<ITemplatePackageInfo, bool>> matchFilter) : base(name, 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
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
// 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; } | ||
} | ||
|
||
internal interface IFilterableArgs | ||
{ | ||
IReadOnlyDictionary<FilterOptionDefinition, string> 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.