Skip to content

Commit 33ba4d0

Browse files
Generate enum from file instead of string
1 parent e3de5d6 commit 33ba4d0

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
namespace Zomp.SyncMethodGenerator;
2-
3-
/// <summary>
4-
/// All types that an IAsyncEnumerable can be converted into.
5-
/// </summary>
6-
[Flags]
7-
public enum CollectionTypes
1+
namespace Zomp.SyncMethodGenerator
82
{
93
/// <summary>
10-
/// Type for System.Collections.Generic.IEnumerable .
4+
/// All types that an IAsyncEnumerable can be converted into.
115
/// </summary>
12-
IEnumerable = 1,
6+
[Flags]
7+
public enum CollectionTypes
8+
{
9+
/// <summary>
10+
/// Type for System.Collections.Generic.IEnumerable .
11+
/// </summary>
12+
IEnumerable = 1,
1313

14-
/// <summary>
15-
/// Type for System.Collections.Generic.IList .
16-
/// </summary>
17-
IList = 2,
14+
/// <summary>
15+
/// Type for System.Collections.Generic.IList .
16+
/// </summary>
17+
IList = 2,
1818

19-
/// <summary>
20-
/// Type for System.ReadOnlySpan .
21-
/// </summary>
22-
ReadOnlySpan = 4,
19+
/// <summary>
20+
/// Type for System.ReadOnlySpan .
21+
/// </summary>
22+
ReadOnlySpan = 4,
2323

24-
/// <summary>
25-
/// Type for System.Span .
26-
/// </summary>
27-
Span = 8,
24+
/// <summary>
25+
/// Type for System.Span .
26+
/// </summary>
27+
Span = 8,
28+
}
2829
}

src/Zomp.SyncMethodGenerator/SyncMethodSourceGenerator.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2828
Debugger.Launch();
2929
}
3030
#endif
31+
32+
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
33+
$"CollectionTypes.g.cs", SourceText.From(GetResourceText("Zomp.SyncMethodGenerator.tests.CollectionTypes.cs"), Encoding.UTF8)));
34+
3135
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
3236
$"{CreateSyncVersionAttribute}.g.cs", SourceText.From(SourceGenerationHelper.CreateSyncVersionAttributeSource, Encoding.UTF8)));
3337

@@ -249,4 +253,14 @@ private static List<MethodToGenerate> GetTypesToGenerate(SourceProductionContext
249253

250254
return methodsToGenerate;
251255
}
256+
257+
private string GetResourceText(string name)
258+
{
259+
using var stream = GetType().Assembly.GetManifestResourceStream(name);
260+
using var streamReader = new StreamReader(stream);
261+
return $"""
262+
// <auto-generated/>
263+
{streamReader.ReadToEnd()}
264+
""";
265+
}
252266
}

src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
2525
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
2626
</ItemGroup>
27+
28+
<ItemGroup>
29+
<EmbeddedResource Include="..\..\src\Zomp.SyncMethodGenerator\CollectionTypes.cs" Link="CollectionTypes.cs">
30+
<LogicalName>Zomp.SyncMethodGenerator.tests.CollectionTypes.cs</LogicalName>
31+
</EmbeddedResource>
32+
</ItemGroup>
2733

2834
<!-- https://stackoverflow.com/a/59893520/6461844 -->
2935
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">

tests/Generator.Tests/TestHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ partial class Class
126126
var target = new RunResultWithIgnoreList
127127
{
128128
Result = driver.GetRunResult(),
129-
IgnoredFiles = { $"{SyncMethodSourceGenerator.CreateSyncVersionAttribute}.g.cs", $"{SyncMethodSourceGenerator.ReplaceWithAttribute}.g.cs" },
129+
IgnoredFiles = { $"{SyncMethodSourceGenerator.CreateSyncVersionAttribute}.g.cs", $"{SyncMethodSourceGenerator.ReplaceWithAttribute}.g.cs", $"CollectionTypes.g.cs" },
130130
};
131131

132132
var verifier = Verifier

0 commit comments

Comments
 (0)