Skip to content

Commit 8f6d90e

Browse files
Copilotdex3r
andauthored
Refactor generator Abstractions symbol names to centralized nameof-based constants (#23)
* Initial plan * refactor: replace hard-coded abstraction names with nameof constants 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>
1 parent 23d139f commit 8f6d90e

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

MattSourceGenHelpers.Generators/Consts.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ namespace MattSourceGenHelpers.Generators;
77
public static class Consts
88
{
99
public const string AbstractionsNamespace = $"{nameof(MattSourceGenHelpers)}.{nameof(Abstractions)}";
10+
public const string AbstractionsAssemblyName = AbstractionsNamespace;
1011
public const string SwitchCaseAttributeFullName = $"{AbstractionsNamespace}.{nameof(SwitchCase)}";
1112
public const string SwitchDefaultAttributeFullName = $"{AbstractionsNamespace}.{nameof(SwitchDefault)}";
1213
public const string GeneratesMethodAttributeFullName = $"{AbstractionsNamespace}.{nameof(GeneratesMethod)}";
1314
public const string IMethodImplementationGeneratorFullName = $"{AbstractionsNamespace}.{nameof(IMethodImplementationGenerator)}";
14-
}
15+
public const string GenerateTypeFullName = $"{AbstractionsNamespace}.{nameof(Generate)}";
16+
public const string RecordingGeneratorsFactoryTypeFullName = $"{AbstractionsNamespace}.{nameof(RecordingGeneratorsFactory)}";
17+
public const string CurrentGeneratorPropertyName = nameof(Generate.CurrentGenerator);
18+
public const string LastRecordPropertyName = nameof(RecordingGeneratorsFactory.LastRecord);
19+
}

MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ internal sealed record SwitchBodyData(
1515

1616
internal static class GeneratesMethodExecutionRuntime
1717
{
18-
private const string AbstractionsAssemblyName = "MattSourceGenHelpers.Abstractions";
19-
private const string GenerateTypeName = "MattSourceGenHelpers.Abstractions.Generate";
20-
private const string RecordingGeneratorsFactoryTypeName = "MattSourceGenHelpers.Abstractions.RecordingGeneratorsFactory";
21-
private const string CurrentGeneratorPropertyName = "CurrentGenerator";
22-
private const string LastRecordPropertyName = "LastRecord";
23-
2418
internal static (string? value, string? error) ExecuteSimpleGeneratorMethod(
2519
IMethodSymbol generatorMethod,
2620
IMethodSymbol partialMethod,
@@ -71,27 +65,27 @@ internal static (SwitchBodyData? record, string? error) ExecuteFluentGeneratorMe
7165
PortableExecutableReference? abstractionsReference = compilation.References
7266
.OfType<PortableExecutableReference>()
7367
.FirstOrDefault(reference => reference.FilePath is not null && string.Equals(
74-
Path.GetFileNameWithoutExtension(reference.FilePath),
75-
AbstractionsAssemblyName,
76-
StringComparison.OrdinalIgnoreCase));
68+
Path.GetFileNameWithoutExtension(reference.FilePath),
69+
Consts.AbstractionsAssemblyName,
70+
StringComparison.OrdinalIgnoreCase));
7771

7872
if (abstractionsReference?.FilePath == null)
7973
{
80-
return (null, $"Could not find {AbstractionsAssemblyName} reference in compilation");
74+
return (null, $"Could not find {Consts.AbstractionsAssemblyName} reference in compilation");
8175
}
8276

8377
string abstractionsAssemblyPath = ResolveImplementationAssemblyPath(abstractionsReference.FilePath);
8478
Assembly abstractionsAssembly = loadContext.LoadFromAssemblyPath(abstractionsAssemblyPath);
8579

86-
Type? generatorStaticType = abstractionsAssembly.GetType(GenerateTypeName);
87-
Type? recordingFactoryType = abstractionsAssembly.GetType(RecordingGeneratorsFactoryTypeName);
80+
Type? generatorStaticType = abstractionsAssembly.GetType(Consts.GenerateTypeFullName);
81+
Type? recordingFactoryType = abstractionsAssembly.GetType(Consts.RecordingGeneratorsFactoryTypeFullName);
8882
if (generatorStaticType == null || recordingFactoryType == null)
8983
{
90-
return (null, $"Could not find {GenerateTypeName} or {RecordingGeneratorsFactoryTypeName} types in Abstractions assembly");
84+
return (null, $"Could not find {Consts.GenerateTypeFullName} or {Consts.RecordingGeneratorsFactoryTypeFullName} types in Abstractions assembly");
9185
}
9286

9387
object? recordingFactory = Activator.CreateInstance(recordingFactoryType);
94-
PropertyInfo? currentGeneratorProperty = generatorStaticType.GetProperty(CurrentGeneratorPropertyName, BindingFlags.Public | BindingFlags.Static);
88+
PropertyInfo? currentGeneratorProperty = generatorStaticType.GetProperty(Consts.CurrentGeneratorPropertyName, BindingFlags.Public | BindingFlags.Static);
9589
currentGeneratorProperty?.SetValue(null, recordingFactory);
9690

9791
string typeName = generatorMethod.ContainingType.ToDisplayString();
@@ -109,7 +103,7 @@ internal static (SwitchBodyData? record, string? error) ExecuteFluentGeneratorMe
109103

110104
generatorMethodInfo.Invoke(null, null);
111105

112-
PropertyInfo? lastRecordProperty = recordingFactoryType.GetProperty(LastRecordPropertyName);
106+
PropertyInfo? lastRecordProperty = recordingFactoryType.GetProperty(Consts.LastRecordPropertyName);
113107
object? lastRecord = lastRecordProperty?.GetValue(recordingFactory);
114108
if (lastRecord == null)
115109
{

0 commit comments

Comments
 (0)