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
6 changes: 3 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
working-directory: ./src

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Setup .NET
uses: actions/setup-dotnet@v4
uses: actions/setup-dotnet@v5
with:
dotnet-version: 8.0.x
dotnet-version: 10.0.x

- name: Restore dependencies
working-directory: ${{env.working-directory}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using Text;

[Generator]
public sealed class CommandLineApplicationOnExecuteMethodsGenerator : ISourceGenerator
public sealed class CommandLineApplicationOnExecuteMethodsGenerator : IIncrementalGenerator
{
private const int NumOverloads = 8;

private const string NamespaceName = "System.CommandLine.Extensions";
private const string TypeName = "CommandLineApplication";

Expand All @@ -20,30 +20,31 @@ public sealed class CommandLineApplicationOnExecuteMethodsGenerator : ISourceGen
"System.Threading.Tasks"
};


public void Initialize(GeneratorInitializationContext context)
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(static ctx =>
{
var sourceText = GenerateSource();
ctx.AddSource(nameof(CommandLineApplicationOnExecuteMethodsGenerator), sourceText);
});
}

public void Execute(GeneratorExecutionContext context)
private static SourceText GenerateSource()
{
var builder = new StringBuilder();

builder.AppendLine($"namespace {NamespaceName}");
builder.AppendBlock(ns =>
{
foreach (string importedNamespace in UsingList.OrderBy(s => s))
{
ns.AppendLine($"using {importedNamespace};");
}
foreach (var importedNamespace in UsingList.OrderBy(s => s)) ns.AppendLine($"using {importedNamespace};");

ns.AppendLine($"partial class {TypeName}");
ns.AppendBlock(t =>
{
for (var i = 1; i <= NumOverloads; i++)
{
int methodArgumentsCount = i;
string typeArgumentsString = string.Join(", ", Enumerable.Range(1, methodArgumentsCount).Select(typeParameterIndex => $"T{typeParameterIndex}"));
var methodArgumentsCount = i;
var typeArgumentsString = string.Join(", ", Enumerable.Range(1, methodArgumentsCount).Select(typeParameterIndex => $"T{typeParameterIndex}"));

t.AppendLine($"public void OnExecute<{typeArgumentsString}>(Func<{typeArgumentsString}, Task<int>> execute)");
t.AppendBlock(methodBody =>
Expand All @@ -53,33 +54,32 @@ public void Execute(GeneratorExecutionContext context)
var parameters = new List<string>();
for (var j = 0; j < methodArgumentsCount; j++)
{
int parameterIndex = j + 1;
var parameterIndex = j + 1;
var variableName = $"descriptor{parameterIndex}";
methodBody.AppendLine($"IValueDescriptor<T{parameterIndex}> {variableName} = this.FindValueDescriptorForOption<T{parameterIndex}>({j});");
parameters.Add(variableName);
}

string parametersString = string.Join(", ", parameters);
var parametersString = string.Join(", ", parameters);
methodBody.AppendLine($"this.command.SetHandler(execute, {parametersString});");
});
}
});

ns.AppendLine("partial class CommandOptionExtensions");
ns.AppendBlock(t =>
{
for (var i = 1; i <= NumOverloads; i++)
{
int methodArgumentsCount = i;
string typeArgumentsString = string.Join(", ", Enumerable.Range(1, methodArgumentsCount).Select(typeParameterIndex => $"T{typeParameterIndex}"));
var methodArgumentsCount = i;
var typeArgumentsString = string.Join(", ", Enumerable.Range(1, methodArgumentsCount).Select(typeParameterIndex => $"T{typeParameterIndex}"));

t.AppendLine($"public static void OnExecute<{typeArgumentsString}>(this CommandOption commandOption, Func<{typeArgumentsString}, Task<int>> execute) => commandOption.Command.OnExecute(execute);");
}
});
});

SourceText sourceText = SourceText.From(builder.ToString(), Encoding.UTF8);
context.AddSource(nameof(CommandLineApplicationOnExecuteMethodsGenerator), sourceText);
return SourceText.From(builder.ToString(), Encoding.UTF8);
}
}

Expand All @@ -94,36 +94,27 @@ public static void AppendBlock(this StringBuilder builder, Action<IndentedString
}
}

internal sealed class IndentedStringWriter
internal sealed class IndentedStringWriter(StringBuilder stringBuilder, int indentionLevel)
{
private readonly int indentionLevel;
private readonly string indentionString;
private readonly StringBuilder stringBuilder;

public IndentedStringWriter(StringBuilder stringBuilder, int indentionLevel)
{
this.stringBuilder = stringBuilder;
this.indentionLevel = indentionLevel;
this.indentionString = new string(' ', indentionLevel * 4);
}
private readonly string indentionString = new(' ', indentionLevel * 4);

public void AppendBlock(Action<IndentedStringWriter> blockWriter)
{
this.stringBuilder.Append(this.indentionString);
this.stringBuilder.AppendLine("{");
stringBuilder.Append(this.indentionString);
stringBuilder.AppendLine("{");

var writer = new IndentedStringWriter(this.stringBuilder, this.indentionLevel + 1);
var writer = new IndentedStringWriter(stringBuilder, indentionLevel + 1);
blockWriter(writer);

this.stringBuilder.Append(this.indentionString);
this.stringBuilder.AppendLine("}");
this.stringBuilder.AppendLine();
stringBuilder.Append(this.indentionString);
stringBuilder.AppendLine("}");
stringBuilder.AppendLine();
}

public void AppendLine(string text)
{
this.stringBuilder.Append(this.indentionString);
this.stringBuilder.AppendLine(text);
stringBuilder.Append(this.indentionString);
stringBuilder.AppendLine(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<AssemblyVersion>2.1.2.0</AssemblyVersion>
<FileVersion>2.1.2.0</FileVersion>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
2 changes: 1 addition & 1 deletion src/demo/demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down