Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assembly name suffix to generated source provider. #71

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/PolyType.SourceGenerator/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using PolyType.Roslyn;
using PolyType.SourceGenerator.Helpers;
using PolyType.SourceGenerator.Model;
using System.Text.RegularExpressions;

namespace PolyType.SourceGenerator;

Expand Down Expand Up @@ -127,8 +128,9 @@ private Parser(ISymbol generationScope, PolyTypeKnownSymbols knownSymbols, Cance
}

Parser parser = new(knownSymbols.Compilation.Assembly, knownSymbols, cancellationToken);
TypeDeclarationModel shapeProviderDeclaration = CreateShapeProviderDeclaration(knownSymbols.Compilation);
ImmutableEquatableArray<TypeDeclarationModel> generateShapeTypes = parser.IncludeTypesUsingGenerateShapeAttributes(generateShapeDeclarations);
return parser.ExportTypeShapeProviderModel(s_globalProviderDeclaration, generateShapeTypes);
return parser.ExportTypeShapeProviderModel(shapeProviderDeclaration, generateShapeTypes);
}

private TypeShapeProviderModel ExportTypeShapeProviderModel(TypeDeclarationModel providerDeclaration, ImmutableEquatableArray<TypeDeclarationModel> generateShapeTypes)
Expand Down Expand Up @@ -339,20 +341,29 @@ private static TypeId CreateTypeId(ITypeSymbol type)
return null;
}

private static readonly TypeDeclarationModel s_globalProviderDeclaration = new()
private static TypeDeclarationModel CreateShapeProviderDeclaration(Compilation compilation)
{
Id = new()
string typeName = !string.IsNullOrWhiteSpace(compilation.AssemblyName)
? "ShapeProvider_" + s_escapeAssemblyName.Replace(compilation.AssemblyName, "_")
: "ShapeProvider";

return new()
{
FullyQualifiedName = "global::PolyType.SourceGenerator.ShapeProvider",
IsValueType = false,
SpecialType = SpecialType.None,
},
Name = "ShapeProvider",
Namespace = "PolyType.SourceGenerator",
SourceFilenamePrefix = "PolyType.SourceGenerator.ShapeProvider",
TypeDeclarationHeader = "internal sealed partial class ShapeProvider",
IsWitnessTypeDeclaration = false,
ContainingTypes = [],
ShapeableOfTImplementations = [],
};
Id = new()
{
FullyQualifiedName = $"global::PolyType.SourceGenerator.{typeName}",
IsValueType = false,
SpecialType = SpecialType.None,
},
Name = typeName,
Namespace = "PolyType.SourceGenerator",
SourceFilenamePrefix = "PolyType.SourceGenerator.ShapeProvider",
TypeDeclarationHeader = $"internal sealed partial class {typeName}",
IsWitnessTypeDeclaration = false,
ContainingTypes = [],
ShapeableOfTImplementations = [],
};
}

private static readonly Regex s_escapeAssemblyName = new(@"[^\w]", RegexOptions.Compiled);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ private void FormatNullableTypeShapeFactory(SourceWriter writer, string methodNa
{
return new global::PolyType.SourceGenModel.SourceGenNullableTypeShape<{{nullableShapeModel.ElementType.FullyQualifiedName}}>
{
Provider = this,
ElementType = {{GetShapeModel(nullableShapeModel.ElementType).SourceIdentifier}},
Provider = this,
};
}
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ private void FormatObjectTypeShapeFactory(SourceWriter writer, string methodName
{
return new global::PolyType.SourceGenModel.SourceGenObjectTypeShape<{{objectShapeModel.Type.FullyQualifiedName}}>
{
Provider = this,
IsRecordType = {{FormatBool(objectShapeModel.IsRecordType)}},
IsTupleType = {{FormatBool(objectShapeModel.IsTupleType)}},
CreatePropertiesFunc = {{FormatNull(propertiesFactoryMethodName)}},
CreateConstructorFunc = {{FormatNull(constructorFactoryMethodName)}},
IsRecordType = {{FormatBool(objectShapeModel.IsRecordType)}},
IsTupleType = {{FormatBool(objectShapeModel.IsTupleType)}},
Provider = this,
};
}
""", trimNullAssignmentLines: true);
Expand Down
Loading