Skip to content

Commit e7ee7e9

Browse files
committed
Feat: CodeOfChaos.GeneratorTools Usage
1 parent 78b8461 commit e7ee7e9

File tree

11 files changed

+63
-324
lines changed

11 files changed

+63
-324
lines changed

src/CodeOfChaos.CliArgsParser.Generators.Sample/CodeOfChaos.CliArgsParser.Generators.Sample.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<ItemGroup>
1111
<ProjectReference Include="..\CodeOfChaos.CliArgsParser.Generators\CodeOfChaos.CliArgsParser.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1212
<ProjectReference Include="..\CodeOfChaos.CliArgsParser\CodeOfChaos.CliArgsParser.csproj"/>
13+
14+
<!-- Ugly, but this is because of some weirdness with how analyzers are imported through a project reference -->
15+
<PackageReference Include="CodeOfChaos.GeneratorTools" Version="1.4.1" GeneratePathProperty="true" PrivateAssets="all" />
16+
<Analyzer Include="$(PkgCodeOfChaos_GeneratorTools)\lib\netstandard2.0\*.dll" />
1317
</ItemGroup>
1418

1519
</Project>

src/CodeOfChaos.CliArgsParser.Generators/CodeOfChaos.CliArgsParser.Generators.csproj

+11-8
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@
2626
</PropertyGroup>
2727

2828
<ItemGroup>
29-
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
30-
<None Include="..\..\README.md" Pack="true" PackagePath="" />
31-
<None Include="../../assets/icon.png" Pack="true" PackagePath="" />
29+
<None Include="../../LICENSE" Pack="true" PackagePath="" Visible="false"/>
30+
<None Include="../../README.md" Pack="true" PackagePath="" Visible="false"/>
31+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false"/>
3232
</ItemGroup>
3333

3434
<ItemGroup>
35-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
36-
<PrivateAssets>all</PrivateAssets>
37-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35+
<PackageReference Include="IsExternalInit" Version="1.0.3">
36+
<PrivateAssets>all</PrivateAssets>
37+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3838
</PackageReference>
39-
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.13.0" />
40-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
39+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
40+
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.13.0" PrivateAssets="all" />
41+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" PrivateAssets="all" />
42+
<PackageReference Include="CodeOfChaos.GeneratorTools" Version="1.4.1" Pack="true" GeneratePathProperty="true" PrivateAssets="all" />
4143
</ItemGroup>
4244
<ItemGroup>
4345
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
46+
<None Include="$(PkgCodeOfChaos_GeneratorTools)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
4447
</ItemGroup>
4548
</Project>

src/CodeOfChaos.CliArgsParser.Generators/Content/CommandGenerator/ClassDto.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4-
using CodeOfChaos.CliArgsParser.Generators.Helpers;
4+
using CodeOfChaos.GeneratorTools;
55
using Microsoft.CodeAnalysis;
66
using Microsoft.CodeAnalysis.CSharp;
77
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -49,20 +49,16 @@ public string ToDeclarationName() {
4949

5050
public void ToCommandData(GeneratorStringBuilder builder) {
5151
builder.AppendLine("public CommandData CommandData { get; } = new CommandData(")
52-
.Indent()
53-
.AppendLine($"\"{CommandName}\",")
54-
.AppendLine($"\"{Description}\",")
55-
.AppendLine($"typeof({symbol.ToDisplayString()})")
56-
.UnIndent()
52+
.AppendLineIndented($"\"{CommandName}\",")
53+
.AppendLineIndented($"\"{Description}\",")
54+
.AppendLineIndented($"typeof({symbol.ToDisplayString()})")
5755
.AppendLine(");");
5856
}
5957

6058
public void ToCommandInitialization(GeneratorStringBuilder builder) {
6159
builder.AppendLine("public Task InitializeAsync(IUserInputRegistry registry) {")
62-
.Indent()
63-
.AppendLine($"var data = {GenericTypeDisplayName}.FromRegistry(registry);")
64-
.AppendLine("return ExecuteAsync(data);")
65-
.UnIndent()
60+
.AppendLineIndented($"var data = {GenericTypeDisplayName}.FromRegistry(registry);")
61+
.AppendLineIndented("return ExecuteAsync(data);")
6662
.AppendLine("}");
6763
}
6864

src/CodeOfChaos.CliArgsParser.Generators/Content/CommandGenerator/Generator.cs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4-
using CodeOfChaos.CliArgsParser.Generators.Helpers;
4+
using CodeOfChaos.GeneratorTools;
55
using Microsoft.CodeAnalysis;
66
using Microsoft.CodeAnalysis.CSharp.Syntax;
77
using System.Collections.Immutable;
@@ -55,16 +55,13 @@ private static void GenerateSources(SourceProductionContext context, (Compilatio
5555
.AppendLine($"namespace {dto.Namespace};")
5656
.AppendLine("#nullable enable")
5757
.AppendLine($"public partial class {dto.ToDeclarationName()} {{")
58-
.Indent();
59-
60-
dto.ToCommandData(builder);
61-
builder.AppendLine();
62-
63-
dto.ToCommandInitialization(builder);
64-
builder.AppendLine();
65-
66-
builder
67-
.UnIndentLine("}");
58+
.Indent(b => {
59+
dto.ToCommandData(b);
60+
b.AppendLine();
61+
dto.ToCommandInitialization(b);
62+
b.AppendLine();
63+
} )
64+
.AppendLine("}");
6865

6966
context.AddSource($"{dto.ClassName}.g.cs", builder.ToStringAndClear());
7067
}

src/CodeOfChaos.CliArgsParser.Generators/Content/ParametersGenerator/Generator.cs

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4-
using CodeOfChaos.CliArgsParser.Generators.Helpers;
4+
using CodeOfChaos.GeneratorTools;
55
using Microsoft.CodeAnalysis;
66
using Microsoft.CodeAnalysis.CSharp.Syntax;
77
using System.Collections.Immutable;
@@ -61,28 +61,21 @@ private static void GenerateSources(SourceProductionContext context, (Compilatio
6161
.AppendLine($"namespace {dto.Namespace};")
6262
.AppendLine("#nullable enable")
6363
.AppendLine($"public partial struct {dto.ToDeclarationName()} {{")
64-
.IndentLine($"public static {dto.ClassName} FromRegistry(IUserInputRegistry registry) {{")
65-
.IndentLine("return new() {")
66-
.Indent();
67-
68-
foreach (PropertyDto propertyDto in dto.PropertyDtos) {
69-
builder.AppendLine(propertyDto.ToPropertyInitialization());
70-
}
71-
72-
builder
73-
.UnIndentLine("};")
74-
.UnIndentLine("}");
75-
76-
builder.AppendLine()
77-
.AppendLine("public T NewFromRegistry<T>(IUserInputRegistry registry) where T : struct, IParameters {")
78-
.Indent()
79-
.AppendLine($"if(typeof(T) != typeof({dto.ClassName})) throw new ArgumentException(); ")
80-
.AppendLine("object boxed = FromRegistry(registry)!;")
81-
.AppendLine("return (T)boxed;")
82-
.UnIndentLine("}");
83-
84-
builder
85-
.UnIndentLine("}");
64+
.Indent(b => {
65+
b.AppendLine($"public static {dto.ClassName} FromRegistry(IUserInputRegistry registry) => new() {{");
66+
b.ForEachAppendLineIndented(dto.PropertyDtos, propertyDto => propertyDto.ToPropertyInitialization());
67+
b.AppendLine("};");
68+
})
69+
.Indent(b => {
70+
b.AppendLine("public T NewFromRegistry<T>(IUserInputRegistry registry) where T : struct, IParameters {");
71+
b.AppendBodyIndented($"""
72+
if(typeof(T) != typeof({dto.ClassName})) throw new ArgumentException();
73+
object boxed = FromRegistry(registry)!;
74+
return (T)boxed;
75+
""");
76+
b.AppendLine("}");
77+
})
78+
.AppendLine("}");
8679

8780
context.AddSource($"{dto.ClassName}.g.cs", builder.ToStringAndClear());
8881
}

src/CodeOfChaos.CliArgsParser.Generators/Helpers/GeneratorStringBuilder.cs

-94
This file was deleted.

0 commit comments

Comments
 (0)