|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.Text; |
| 6 | +using Scriban; |
| 7 | + |
| 8 | +namespace Npgsql.SourceGenerators |
| 9 | +{ |
| 10 | + [Generator] |
| 11 | + public class NpgsqlConnectionStringBuilderSourceGenerator : ISourceGenerator |
| 12 | + { |
| 13 | + static readonly DiagnosticDescriptor InternalError = new( |
| 14 | + id: "PGXXXX", |
| 15 | + title: "Internal issue when source-generating NpgsqlConnectionStringBuilder", |
| 16 | + messageFormat: "{0}", |
| 17 | + category: "Internal", |
| 18 | + DiagnosticSeverity.Error, |
| 19 | + isEnabledByDefault: true); |
| 20 | + |
| 21 | + public void Initialize(GeneratorInitializationContext context) {} |
| 22 | + |
| 23 | + public void Execute(GeneratorExecutionContext context) |
| 24 | + { |
| 25 | + if (context.Compilation.Assembly.GetTypeByMetadataName("Npgsql.NpgsqlConnectionStringBuilder") is not { } type) |
| 26 | + return; |
| 27 | + |
| 28 | + if (context.Compilation.Assembly.GetTypeByMetadataName("Npgsql.NpgsqlConnectionStringPropertyAttribute") is not |
| 29 | + { } connectionStringPropertyAttribute) |
| 30 | + { |
| 31 | + context.ReportDiagnostic(Diagnostic.Create( |
| 32 | + InternalError, |
| 33 | + location: null, |
| 34 | + "Could not find Npgsql.NpgsqlConnectionStringPropertyAttribute")); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + var obsoleteAttribute = context.Compilation.GetTypeByMetadataName("System.ObsoleteAttribute"); |
| 39 | + var displayNameAttribute = context.Compilation.GetTypeByMetadataName("System.ComponentModel.DisplayNameAttribute"); |
| 40 | + var defaultValueAttribute = context.Compilation.GetTypeByMetadataName("System.ComponentModel.DefaultValueAttribute"); |
| 41 | + |
| 42 | + if (obsoleteAttribute is null || displayNameAttribute is null || defaultValueAttribute is null) |
| 43 | + { |
| 44 | + context.ReportDiagnostic(Diagnostic.Create( |
| 45 | + InternalError, |
| 46 | + location: null, |
| 47 | + "Could not find ObsoleteAttribute, DisplayNameAttribute or DefaultValueAttribute")); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + var properties = new List<PropertyDetails>(); |
| 52 | + var propertiesByKeyword = new Dictionary<string, PropertyDetails>(); |
| 53 | + foreach (var member in type.GetMembers()) |
| 54 | + { |
| 55 | + if (member is not IPropertySymbol property || |
| 56 | + property.GetAttributes().FirstOrDefault(a => connectionStringPropertyAttribute.Equals(a.AttributeClass, SymbolEqualityComparer.Default)) is not { } propertyAttribute || |
| 57 | + property.GetAttributes() |
| 58 | + .FirstOrDefault(a => displayNameAttribute.Equals(a.AttributeClass, SymbolEqualityComparer.Default)) |
| 59 | + ?.ConstructorArguments[0].Value is not string displayName) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + var explicitDefaultValue = property.GetAttributes() |
| 65 | + .FirstOrDefault(a => defaultValueAttribute.Equals(a.AttributeClass, SymbolEqualityComparer.Default)) |
| 66 | + ?.ConstructorArguments[0].Value; |
| 67 | + |
| 68 | + if (explicitDefaultValue is string s) |
| 69 | + explicitDefaultValue = '"' + s.Replace("\"", "\"\"") + '"'; |
| 70 | + |
| 71 | + var propertyDetails = new PropertyDetails |
| 72 | + { |
| 73 | + Name = property.Name, |
| 74 | + CanonicalName = displayName, |
| 75 | + TypeName = property.Type.Name, |
| 76 | + IsEnum = property.Type.TypeKind == TypeKind.Enum, |
| 77 | + IsObsolete = property.GetAttributes().Any(a => obsoleteAttribute.Equals(a.AttributeClass, SymbolEqualityComparer.Default)), |
| 78 | + DefaultValue = explicitDefaultValue |
| 79 | + }; |
| 80 | + |
| 81 | + properties.Add(propertyDetails); |
| 82 | + |
| 83 | + propertiesByKeyword[displayName.ToUpperInvariant()] = propertyDetails; |
| 84 | + if (property.Name != displayName) |
| 85 | + propertiesByKeyword[property.Name.ToUpperInvariant()] = propertyDetails; |
| 86 | + if (propertyAttribute.ConstructorArguments.Length == 1) |
| 87 | + foreach (var synonymArg in propertyAttribute.ConstructorArguments[0].Values) |
| 88 | + if (synonymArg.Value is string synonym) |
| 89 | + propertiesByKeyword[synonym.ToUpperInvariant()] = propertyDetails; |
| 90 | + } |
| 91 | + |
| 92 | + var template = Template.Parse(EmbeddedResource.GetContent("NpgsqlConnectionStringBuilder.snbtxt"), "NpgsqlConnectionStringBuilder.snbtxt"); |
| 93 | + |
| 94 | + var output = template.Render(new |
| 95 | + { |
| 96 | + Properties = properties, |
| 97 | + PropertiesByKeyword = propertiesByKeyword |
| 98 | + }); |
| 99 | + |
| 100 | + context.AddSource(type.Name + ".Generated.cs", SourceText.From(output, Encoding.UTF8)); |
| 101 | + } |
| 102 | + |
| 103 | + class PropertyDetails |
| 104 | + { |
| 105 | + public string Name { get; set; } = null!; |
| 106 | + public string CanonicalName { get; set; } = null!; |
| 107 | + public string TypeName { get; set; } = null!; |
| 108 | + public bool IsEnum { get; set; } |
| 109 | + public bool IsObsolete { get; set; } |
| 110 | + public object? DefaultValue { get; set; } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments