Skip to content

Commit

Permalink
Generate enum from file instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
ValerieFernandes committed Dec 21, 2023
1 parent e3de5d6 commit 33ba4d0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
43 changes: 22 additions & 21 deletions src/Zomp.SyncMethodGenerator/CollectionTypes.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
namespace Zomp.SyncMethodGenerator;

/// <summary>
/// All types that an IAsyncEnumerable can be converted into.
/// </summary>
[Flags]
public enum CollectionTypes
namespace Zomp.SyncMethodGenerator
{
/// <summary>
/// Type for System.Collections.Generic.IEnumerable .
/// All types that an IAsyncEnumerable can be converted into.
/// </summary>
IEnumerable = 1,
[Flags]
public enum CollectionTypes
{
/// <summary>
/// Type for System.Collections.Generic.IEnumerable .
/// </summary>
IEnumerable = 1,

/// <summary>
/// Type for System.Collections.Generic.IList .
/// </summary>
IList = 2,
/// <summary>
/// Type for System.Collections.Generic.IList .
/// </summary>
IList = 2,

/// <summary>
/// Type for System.ReadOnlySpan .
/// </summary>
ReadOnlySpan = 4,
/// <summary>
/// Type for System.ReadOnlySpan .
/// </summary>
ReadOnlySpan = 4,

/// <summary>
/// Type for System.Span .
/// </summary>
Span = 8,
/// <summary>
/// Type for System.Span .
/// </summary>
Span = 8,
}
}
14 changes: 14 additions & 0 deletions src/Zomp.SyncMethodGenerator/SyncMethodSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
Debugger.Launch();
}
#endif

context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"CollectionTypes.g.cs", SourceText.From(GetResourceText("Zomp.SyncMethodGenerator.tests.CollectionTypes.cs"), Encoding.UTF8)));

context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"{CreateSyncVersionAttribute}.g.cs", SourceText.From(SourceGenerationHelper.CreateSyncVersionAttributeSource, Encoding.UTF8)));

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

return methodsToGenerate;
}

private string GetResourceText(string name)
{
using var stream = GetType().Assembly.GetManifestResourceStream(name);
using var streamReader = new StreamReader(stream);
return $"""
// <auto-generated/>
{streamReader.ReadToEnd()}
""";
}
}
6 changes: 6 additions & 0 deletions src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\..\src\Zomp.SyncMethodGenerator\CollectionTypes.cs" Link="CollectionTypes.cs">
<LogicalName>Zomp.SyncMethodGenerator.tests.CollectionTypes.cs</LogicalName>
</EmbeddedResource>
</ItemGroup>

<!-- https://stackoverflow.com/a/59893520/6461844 -->
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ partial class Class
var target = new RunResultWithIgnoreList
{
Result = driver.GetRunResult(),
IgnoredFiles = { $"{SyncMethodSourceGenerator.CreateSyncVersionAttribute}.g.cs", $"{SyncMethodSourceGenerator.ReplaceWithAttribute}.g.cs" },
IgnoredFiles = { $"{SyncMethodSourceGenerator.CreateSyncVersionAttribute}.g.cs", $"{SyncMethodSourceGenerator.ReplaceWithAttribute}.g.cs", $"CollectionTypes.g.cs" },
};

var verifier = Verifier
Expand Down

0 comments on commit 33ba4d0

Please sign in to comment.