Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,26 @@ public static IncrementalValuesProvider<CommandOptionsGeneratingModel> SelectCom
var attribute = typeSymbol.GetAttributes()
.FirstOrDefault(a => a.AttributeClass!.IsAttributeOf<VerbAttribute>());
// 4. 拥有 [Option] 特性的属性。
var optionProperties = typeSymbol.GetMembers()
.OfType<IPropertySymbol>()
.Select(OptionPropertyGeneratingModel.TryParse)
var optionProperties = typeSymbol
.EnumerateBaseTypesRecursively() // 递归获取所有基类
.Reverse() // (注意我们先给父类属性赋值,再给子类属性赋值)
.SelectMany(x => x.GetMembers()) // 的所有成员,
.OfType<IPropertySymbol>() // 然后取出属性,
.Select(OptionPropertyGeneratingModel.TryParse) // 解析出 OptionPropertyGeneratingModel。
.OfType<OptionPropertyGeneratingModel>()
.GroupBy(x => x.PropertyName) // 按属性名去重。
.Select(x => x.Last()) // 随后,取子类的属性(去除父类的重名属性)。
.ToImmutableArray();
// 5. 拥有 [Value] 特性的属性。
var valueProperties = typeSymbol.GetMembers()
.OfType<IPropertySymbol>()
.Select(ValuePropertyGeneratingModel.TryParse)
var valueProperties = typeSymbol
.EnumerateBaseTypesRecursively() // 递归获取所有基类
.Reverse() // (注意我们先给父类属性赋值,再给子类属性赋值)
.SelectMany(x => x.GetMembers()) // 的所有成员,
.OfType<IPropertySymbol>() // 然后取出属性,
.Select(ValuePropertyGeneratingModel.TryParse) // 解析出 ValuePropertyGeneratingModel。
.OfType<ValuePropertyGeneratingModel>()
.GroupBy(x => x.PropertyName) // 按属性名去重。
.Select(x => x.Last()) // 随后,取子类的属性(去除父类的重名属性)。
.ToImmutableArray();

if (!isOptions && !isHandler && attribute is null && optionProperties.IsEmpty && valueProperties.IsEmpty)
Expand Down Expand Up @@ -304,3 +314,16 @@ internal record AssemblyCommandsGeneratingModel

public required INamedTypeSymbol AssemblyCommandHandlerType { get; init; }
}

file static class Extensions
{
public static IEnumerable<ITypeSymbol> EnumerateBaseTypesRecursively(this ITypeSymbol type)
{
var current = type;
while (current != null)
{
yield return current;
current = current.BaseType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using CommandLine;
using dotnetCampus.Cli;
using DotNetCampus.Cli.Performance.Fakes;
using DotNetCampus.Cli.Tests.Fakes;
using static DotNetCampus.Cli.Tests.Fakes.CommandLineArgs;
Expand Down Expand Up @@ -51,6 +52,20 @@ public void Parse_NoArgs_PowerShell()
commandLine.As<Options>();
}

[Benchmark(Description = "parse [] -v=3.x -p=parser")]
public void Parse_NoArgs_3x_Parser()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(NoArgs);
commandLine.As(new OptionsParser());
}

[Benchmark(Description = "parse [] -v=3.x -p=runtime")]
public void Parse_NoArgs_3x_Runtime()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(NoArgs);
commandLine.As<Options>();
}

[Benchmark(Description = "parse [PS1] --flexible")]
public void Parse_PowerShell_Flexible()
{
Expand All @@ -65,6 +80,20 @@ public void Parse_PowerShell_PowerShell()
commandLine.As<Options>();
}

[Benchmark(Description = "parse [PS1] -v=3.x -p=parser")]
public void Parse_PowerShell_3x_Parser()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(WindowsStyleArgs);
commandLine.As(new OptionsParser());
}

[Benchmark(Description = "parse [PS1] -v=3.x -p=runtime")]
public void Parse_PowerShell_3x_Runtime()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(WindowsStyleArgs);
commandLine.As<Options>();
}

[Benchmark(Description = "parse [CMD] --flexible")]
public void Parse_Cmd_Flexible()
{
Expand All @@ -79,6 +108,20 @@ public void Parse_Cmd_PowerShell()
commandLine.As<Options>();
}

[Benchmark(Description = "parse [CMD] -v=3.x -p=parser")]
public void Parse_Cmd_3x_Parser()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(CmdStyleArgs);
commandLine.As(new OptionsParser());
}

[Benchmark(Description = "parse [CMD] -v=3.x -p=runtime")]
public void Parse_Cmd_3x_Runtime()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(CmdStyleArgs);
commandLine.As<Options>();
}

[Benchmark(Description = "parse [GNU] --flexible")]
public void Parse_Gnu_Flexible()
{
Expand All @@ -93,6 +136,20 @@ public void Parse_Gnu_Gnu()
commandLine.As<Options>();
}

[Benchmark(Description = "parse [GNU] -v=3.x -p=parser")]
public void Parse_Gnu_3x_Parser()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(LinuxStyleArgs);
commandLine.As(new OptionsParser());
}

[Benchmark(Description = "parse [GNU] -v=3.x -p=runtime")]
public void Parse_Gnu_3x_Runtime()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(LinuxStyleArgs);
commandLine.As<Options>();
}

[Benchmark(Description = "handle [Edit,Print] --flexible")]
public void Handle_Verbs_Flexible()
{
Expand All @@ -102,13 +159,37 @@ public void Handle_Verbs_Flexible()
.Run();
}

[Benchmark(Description = "handle [Edit,Print] -v=3.x -p=runtime")]
public void Handle_Verbs_Runtime()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(EditVerbArgs);
commandLine
.AddHandler<EditOptions>(options => 0)
.AddHandler<PrintOptions>(options => 0)
.Run();
}

[Benchmark(Description = "parse [URL]")]
public void Parse_Url()
{
var commandLine = CommandLine.Parse(UrlArgs, new CommandLineParsingOptions { SchemeNames = ["walterlv"] });
commandLine.As<Options>();
}

[Benchmark(Description = "parse [URL] -v=3.x -p=parser")]
public void Parse_Url_3x_Parser()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(UrlArgs);
commandLine.As(new OptionsParser());
}

[Benchmark(Description = "parse [URL] -v=3.x -p=runtime")]
public void Parse_Url_3x_Runtime()
{
var commandLine = dotnetCampus.Cli.CommandLine.Parse(UrlArgs);
commandLine.As<Options>();
}

[Benchmark(Description = "NuGet: CommandLineParser")]
public void CommandLineParser()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
<ItemGroup>
<Compile Include="..\DotNetCampus.CommandLine.Tests\Fakes\**" LinkBase="Fakes" />
<Compile Remove="..\DotNetCampus.CommandLine.Tests\Fakes\**\Runtime*.cs" />
<Compile Remove="..\DotNetCampus.CommandLine.Tests\Fakes\**\*Parser.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DotNetCampus.CommandLine.Analyzer\DotNetCampus.CommandLine.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\DotNetCampus.CommandLine\DotNetCampus.CommandLine.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="dotnetCampus.CommandLine.Legacy">
<HintPath>dotnetCampus.CommandLine.Legacy.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using dotnetCampus.Cli;

namespace DotNetCampus.Cli.Tests.Fakes
{
Expand Down
28 changes: 28 additions & 0 deletions tests/DotNetCampus.CommandLine.Tests/Fakes/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,32 @@ public class Options
/// </summary>
[Option("StartupSession")]
public string? StartupSession { get; init; }

/// <summary>
/// 创建 <see cref="Options"/> 类的新实例。
/// </summary>
public Options()
{
}

/// <summary>
/// 创建 <see cref="Options"/> 类的新实例。
/// </summary>
public Options(
string? filePath,
bool isFromCloud,
string? startupMode,
bool isSilence,
bool isIwb,
string? placement,
string? startupSession)
{
FilePath = filePath;
IsFromCloud = isFromCloud;
StartupMode = startupMode;
IsSilence = isSilence;
IsIwb = isIwb;
Placement = placement;
StartupSession = startupSession;
}
}
3 changes: 2 additions & 1 deletion tests/DotNetCampus.CommandLine.Tests/Fakes/OptionsParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using dotnetCampus.Cli;

namespace DotNetCampus.Cli.Tests.Fakes
{
Expand Down Expand Up @@ -93,4 +94,4 @@ public Options Commit()
return new Options(_filePath, _isFromCloud, _startupMode, _isSilence, _isIwb, _placement, _startupSession);
}
}
}
}
Loading
Loading