Skip to content

Commit 4a17a91

Browse files
Copilotdex3r
andauthored
Move MethodBuilder and RecordingGeneratorsFactory to Generators (#45)
* Initial plan * Move MethodBuilder and RecordingGeneratorsFactory from Abstractions to Generators Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> Co-authored-by: Mateusz Krzaczek <dex3r@users.noreply.github.com>
1 parent cec2835 commit 4a17a91

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

MattSourceGenHelpers.Generators/Consts.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ public static class Consts
99
public const string SolutionNamespace = $"{nameof(MattSourceGenHelpers)}";
1010
public const string AbstractionsNamespace = $"{nameof(MattSourceGenHelpers)}.{nameof(Abstractions)}";
1111
public const string AbstractionsAssemblyName = AbstractionsNamespace;
12+
public const string GeneratorsNamespace = "MattSourceGenHelpers.Generators";
13+
public const string GeneratorsAssemblyName = GeneratorsNamespace;
1214
public const string SwitchCaseAttributeFullName = $"{AbstractionsNamespace}.{nameof(SwitchCase)}";
1315
public const string SwitchDefaultAttributeFullName = $"{AbstractionsNamespace}.{nameof(SwitchDefault)}";
1416
public const string GeneratesMethodAttributeFullName = $"{AbstractionsNamespace}.{nameof(GeneratesMethod)}";
1517
public const string IMethodImplementationGeneratorFullName = $"{AbstractionsNamespace}.{nameof(IMethodImplementationGenerator)}";
1618
public const string GenerateTypeFullName = $"{AbstractionsNamespace}.{nameof(Generate)}";
17-
public const string RecordingGeneratorsFactoryTypeFullName = $"{AbstractionsNamespace}.{nameof(RecordingGeneratorsFactory)}";
19+
public const string RecordingGeneratorsFactoryTypeFullName = $"{GeneratorsNamespace}.{nameof(RecordingGeneratorsFactory)}";
1820
public const string CurrentGeneratorPropertyName = nameof(Generate.CurrentGenerator);
1921
public const string LastRecordPropertyName = nameof(RecordingGeneratorsFactory.LastRecord);
2022
}

MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ reference.Display is not null
135135
}
136136

137137
Type? generatorStaticType = abstractionsAssembly.GetType(Consts.GenerateTypeFullName);
138-
Type? recordingFactoryType = abstractionsAssembly.GetType(Consts.RecordingGeneratorsFactoryTypeFullName);
138+
Type? recordingFactoryType = assembly.GetType(Consts.RecordingGeneratorsFactoryTypeFullName);
139139
if (generatorStaticType == null || recordingFactoryType == null)
140140
{
141-
return (null, $"Could not find {Consts.GenerateTypeFullName} or {Consts.RecordingGeneratorsFactoryTypeFullName} types in Abstractions assembly");
141+
return (null, $"Could not find {Consts.GenerateTypeFullName} or {Consts.RecordingGeneratorsFactoryTypeFullName} types in compiled assembly");
142142
}
143143

144144
object? recordingFactory = Activator.CreateInstance(recordingFactoryType);
@@ -339,12 +339,26 @@ private static CSharpCompilation BuildExecutionCompilation(
339339
Compilation compilation)
340340
{
341341
string dummySource = BuildDummyImplementation(allPartialMethods);
342+
string methodBuilderSource = ReadEmbeddedResource($"{Consts.GeneratorsAssemblyName}.MethodBuilder.cs");
343+
string recordingFactorySource = ReadEmbeddedResource($"{Consts.GeneratorsAssemblyName}.RecordingGeneratorsFactory.cs");
342344
CSharpParseOptions parseOptions = compilation.SyntaxTrees.FirstOrDefault()?.Options as CSharpParseOptions
343345
?? CSharpParseOptions.Default;
344346

345347
return (CSharpCompilation)compilation
346348
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
347-
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(dummySource, parseOptions));
349+
.AddSyntaxTrees(
350+
CSharpSyntaxTree.ParseText(dummySource, parseOptions),
351+
CSharpSyntaxTree.ParseText(methodBuilderSource, parseOptions),
352+
CSharpSyntaxTree.ParseText(recordingFactorySource, parseOptions));
353+
}
354+
355+
private static string ReadEmbeddedResource(string resourceName)
356+
{
357+
using Stream? stream = typeof(GeneratesMethodExecutionRuntime).Assembly.GetManifestResourceStream(resourceName);
358+
if (stream == null)
359+
throw new InvalidOperationException($"Embedded resource '{resourceName}' not found in {Consts.GeneratorsAssemblyName} assembly");
360+
using StreamReader reader = new StreamReader(stream);
361+
return reader.ReadToEnd();
348362
}
349363

350364
private static string BuildDummyImplementation(IEnumerable<IMethodSymbol> partialMethods)

MattSourceGenHelpers.Generators/MattSourceGenHelpers.Generators.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717
<TargetFramework>net8.0</TargetFramework>
1818
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<None Include="..\README.md" Pack="true" PackagePath="\" />
22+
</ItemGroup>
1923

2024
<ItemGroup>
2125
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
@@ -26,6 +30,7 @@
2630
</ItemGroup>
2731

2832
<ItemGroup>
29-
<None Include="..\README.md" Pack="true" PackagePath="\" />
33+
<EmbeddedResource Include="MethodBuilder.cs" LogicalName="$(RootNamespace).MethodBuilder.cs" />
34+
<EmbeddedResource Include="RecordingGeneratorsFactory.cs" LogicalName="$(RootNamespace).RecordingGeneratorsFactory.cs" />
3035
</ItemGroup>
3136
</Project>

MattSourceGenHelpers.Abstractions/MethodBuilder.cs renamed to MattSourceGenHelpers.Generators/MethodBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace MattSourceGenHelpers.Abstractions;
1+
using MattSourceGenHelpers.Abstractions;
2+
3+
namespace MattSourceGenHelpers.Generators;
24

35
public class MethodBuilder(IGeneratorsFactory generatorsFactory) : IMethodBuilder
46
{
@@ -10,4 +12,4 @@ public class MethodBuilder(IGeneratorsFactory generatorsFactory) : IMethodBuilde
1012
public class MethodBuilder<TArg1>(IGeneratorsFactory generatorsFactory) : IMethodBuilder<TArg1>
1113
{
1214
public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => generatorsFactory.CreateImplementation<TArg1, TReturnType>();
13-
}
15+
}

MattSourceGenHelpers.Abstractions/RecordingGeneratorsFactory.cs renamed to MattSourceGenHelpers.Generators/RecordingGeneratorsFactory.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using JetBrains.Annotations;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using MattSourceGenHelpers.Abstractions;
25

3-
namespace MattSourceGenHelpers.Abstractions;
6+
namespace MattSourceGenHelpers.Generators;
47

58
public class SwitchBodyRecord
69
{
7-
[UsedImplicitly]
810
public List<object> CaseKeys { get; } = new();
9-
[UsedImplicitly]
1011
public List<object?> CaseValues { get; } = new();
1112
public bool HasDefaultCase { get; set; }
1213
}

0 commit comments

Comments
 (0)