| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Helper for discovering factory generation attributes from Roslyn symbols. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class FactoryDiscoveryHelper |
| | | 9 | | { |
| | | 10 | | private const string GenerateFactoryAttributeName = "GenerateFactoryAttribute"; |
| | | 11 | | private const string GenerateFactoryAttributeFullName = "NexusLabs.Needlr.GenerateFactoryAttribute"; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Checks if a type has the [GenerateFactory] attribute. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 17 | | /// <returns>True if the type has [GenerateFactory]; otherwise, false.</returns> |
| | | 18 | | public static bool HasGenerateFactoryAttribute(INamedTypeSymbol typeSymbol) |
| | | 19 | | { |
| | 6828358 | 20 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 21 | | { |
| | 1993868 | 22 | | var attributeClass = attribute.AttributeClass; |
| | 1993868 | 23 | | if (attributeClass == null) |
| | | 24 | | continue; |
| | | 25 | | |
| | | 26 | | // Check for non-generic GenerateFactoryAttribute |
| | 1993868 | 27 | | var name = attributeClass.Name; |
| | 1993868 | 28 | | if (name == GenerateFactoryAttributeName) |
| | 30 | 29 | | return true; |
| | | 30 | | |
| | 1993838 | 31 | | var fullName = attributeClass.ToDisplayString(); |
| | 1993838 | 32 | | if (fullName == GenerateFactoryAttributeFullName) |
| | 0 | 33 | | return true; |
| | | 34 | | |
| | | 35 | | // Check for generic GenerateFactoryAttribute<T> |
| | 1993838 | 36 | | if (attributeClass.IsGenericType) |
| | | 37 | | { |
| | 40 | 38 | | var originalDef = attributeClass.OriginalDefinition; |
| | 40 | 39 | | if (originalDef.Name == GenerateFactoryAttributeName || |
| | 40 | 40 | | originalDef.ToDisplayString().StartsWith(GenerateFactoryAttributeFullName + "<")) |
| | 0 | 41 | | return true; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | 1420296 | 45 | | return false; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the factory generation mode from the [GenerateFactory] attribute. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 52 | | /// <returns>The Mode value (1=Func, 2=Interface, 3=All), or 3 (All) if not specified.</returns> |
| | | 53 | | public static int GetFactoryGenerationMode(INamedTypeSymbol typeSymbol) |
| | | 54 | | { |
| | 102 | 55 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 56 | | { |
| | 26 | 57 | | var attributeClass = attribute.AttributeClass; |
| | 26 | 58 | | if (attributeClass == null) |
| | | 59 | | continue; |
| | | 60 | | |
| | 26 | 61 | | var name = attributeClass.Name; |
| | 26 | 62 | | var fullName = attributeClass.ToDisplayString(); |
| | | 63 | | |
| | 26 | 64 | | bool isFactoryAttribute = name == GenerateFactoryAttributeName || |
| | 26 | 65 | | fullName == GenerateFactoryAttributeFullName || |
| | 26 | 66 | | (attributeClass.IsGenericType && |
| | 26 | 67 | | attributeClass.OriginalDefinition.Name == GenerateFactoryAttributeName); |
| | | 68 | | |
| | 26 | 69 | | if (isFactoryAttribute) |
| | | 70 | | { |
| | | 71 | | // Check named argument "Mode" |
| | 54 | 72 | | foreach (var namedArg in attribute.NamedArguments) |
| | | 73 | | { |
| | 2 | 74 | | if (namedArg.Key == "Mode" && namedArg.Value.Value is int modeValue) |
| | | 75 | | { |
| | 2 | 76 | | return modeValue; |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 24 | 82 | | return 3; // Default is All (Func | Interface) |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets the interface type from a generic [GenerateFactory<T>] attribute, if present. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 89 | | /// <returns>The fully qualified interface type name, or null if non-generic attribute is used.</returns> |
| | | 90 | | public static string? GetFactoryReturnInterfaceType(INamedTypeSymbol typeSymbol) |
| | | 91 | | { |
| | 102 | 92 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 93 | | { |
| | 26 | 94 | | var attributeClass = attribute.AttributeClass; |
| | 26 | 95 | | if (attributeClass == null) |
| | | 96 | | continue; |
| | | 97 | | |
| | | 98 | | // Check for generic GenerateFactoryAttribute<T> |
| | 26 | 99 | | if (attributeClass.IsGenericType) |
| | | 100 | | { |
| | 2 | 101 | | var originalDef = attributeClass.OriginalDefinition; |
| | 2 | 102 | | if (originalDef.Name == GenerateFactoryAttributeName) |
| | | 103 | | { |
| | | 104 | | // Extract the type argument |
| | 2 | 105 | | var typeArg = attributeClass.TypeArguments.FirstOrDefault(); |
| | 2 | 106 | | if (typeArg != null) |
| | | 107 | | { |
| | 2 | 108 | | return $"global::{typeArg.ToDisplayString()}"; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | 24 | 114 | | return null; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Partitions constructor parameters into injectable (DI-resolvable) and runtime (must be provided). |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="typeSymbol">The type symbol to analyze.</param> |
| | | 121 | | /// <returns>A list of factory constructor infos, one for each viable constructor.</returns> |
| | | 122 | | public static IReadOnlyList<FactoryConstructorInfo> GetFactoryConstructors(INamedTypeSymbol typeSymbol) |
| | | 123 | | { |
| | 27 | 124 | | var result = new List<FactoryConstructorInfo>(); |
| | | 125 | | |
| | 114 | 126 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 127 | | { |
| | 30 | 128 | | if (ctor.IsStatic) |
| | | 129 | | continue; |
| | | 130 | | |
| | 30 | 131 | | if (ctor.DeclaredAccessibility != Accessibility.Public) |
| | | 132 | | continue; |
| | | 133 | | |
| | | 134 | | // Extract XML documentation for parameters |
| | 30 | 135 | | var paramDocs = GetConstructorParameterDocumentation(ctor); |
| | | 136 | | |
| | 30 | 137 | | var injectableParams = new List<TypeDiscoveryHelper.ConstructorParameterInfo>(); |
| | 30 | 138 | | var runtimeParams = new List<TypeDiscoveryHelper.ConstructorParameterInfo>(); |
| | | 139 | | |
| | 180 | 140 | | foreach (var param in ctor.Parameters) |
| | | 141 | | { |
| | 60 | 142 | | var typeName = param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 60 | 143 | | paramDocs.TryGetValue(param.Name, out var docComment); |
| | | 144 | | |
| | 60 | 145 | | if (IsInjectableParameterType(param.Type)) |
| | | 146 | | { |
| | | 147 | | // Check for [FromKeyedServices] attribute |
| | 25 | 148 | | var serviceKey = GetFromKeyedServicesKey(param); |
| | 25 | 149 | | var paramInfo = new TypeDiscoveryHelper.ConstructorParameterInfo(typeName, serviceKey, param.Name, d |
| | 25 | 150 | | injectableParams.Add(paramInfo); |
| | | 151 | | } |
| | | 152 | | else |
| | | 153 | | { |
| | 35 | 154 | | var paramInfo = new TypeDiscoveryHelper.ConstructorParameterInfo(typeName, null, param.Name, docComm |
| | 35 | 155 | | runtimeParams.Add(paramInfo); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | // Only include constructors that have at least one runtime parameter |
| | | 160 | | // (otherwise normal registration works fine) |
| | 30 | 161 | | if (runtimeParams.Count > 0) |
| | | 162 | | { |
| | 29 | 163 | | result.Add(new FactoryConstructorInfo( |
| | 29 | 164 | | injectableParams.ToArray(), |
| | 29 | 165 | | runtimeParams.ToArray())); |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | 27 | 169 | | return result; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Extracts parameter documentation from a constructor's XML documentation comments. |
| | | 174 | | /// </summary> |
| | | 175 | | private static Dictionary<string, string> GetConstructorParameterDocumentation(IMethodSymbol constructor) |
| | | 176 | | { |
| | 30 | 177 | | var result = new Dictionary<string, string>(StringComparer.Ordinal); |
| | | 178 | | |
| | 30 | 179 | | var xmlDoc = constructor.GetDocumentationCommentXml(); |
| | 30 | 180 | | if (string.IsNullOrWhiteSpace(xmlDoc)) |
| | 25 | 181 | | return result; |
| | | 182 | | |
| | | 183 | | try |
| | | 184 | | { |
| | | 185 | | // Parse the XML documentation |
| | 5 | 186 | | var doc = System.Xml.Linq.XDocument.Parse(xmlDoc); |
| | 5 | 187 | | var paramElements = doc.Descendants("param"); |
| | | 188 | | |
| | 32 | 189 | | foreach (var param in paramElements) |
| | | 190 | | { |
| | 11 | 191 | | var nameAttr = param.Attribute("name"); |
| | 11 | 192 | | if (nameAttr is null || string.IsNullOrWhiteSpace(nameAttr.Value)) |
| | | 193 | | continue; |
| | | 194 | | |
| | | 195 | | // Get the inner text/content of the param element |
| | 11 | 196 | | var content = param.Value?.Trim(); |
| | 11 | 197 | | if (!string.IsNullOrWhiteSpace(content)) |
| | | 198 | | { |
| | 11 | 199 | | result[nameAttr.Value] = content!; |
| | | 200 | | } |
| | | 201 | | } |
| | 5 | 202 | | } |
| | 0 | 203 | | catch |
| | | 204 | | { |
| | | 205 | | // If XML parsing fails, return empty dictionary |
| | 0 | 206 | | } |
| | | 207 | | |
| | 5 | 208 | | return result; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | private static string? GetFromKeyedServicesKey(IParameterSymbol param) |
| | | 212 | | { |
| | | 213 | | const string FromKeyedServicesAttributeName = "Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribu |
| | | 214 | | |
| | 50 | 215 | | foreach (var attr in param.GetAttributes()) |
| | | 216 | | { |
| | 0 | 217 | | var attrClass = attr.AttributeClass; |
| | 0 | 218 | | if (attrClass is null) |
| | | 219 | | continue; |
| | | 220 | | |
| | 0 | 221 | | var attrFullName = attrClass.ToDisplayString(); |
| | 0 | 222 | | if (attrFullName == FromKeyedServicesAttributeName) |
| | | 223 | | { |
| | | 224 | | // Extract the key from the constructor argument |
| | 0 | 225 | | if (attr.ConstructorArguments.Length > 0) |
| | | 226 | | { |
| | 0 | 227 | | var keyArg = attr.ConstructorArguments[0]; |
| | 0 | 228 | | if (keyArg.Value is string keyValue) |
| | | 229 | | { |
| | 0 | 230 | | return keyValue; |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | 25 | 236 | | return null; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private static bool IsInjectableParameterType(ITypeSymbol typeSymbol) |
| | | 240 | | { |
| | | 241 | | // Interfaces and abstract classes are typically injectable |
| | 60 | 242 | | if (typeSymbol.TypeKind == TypeKind.Interface) |
| | 25 | 243 | | return true; |
| | | 244 | | |
| | 35 | 245 | | if (typeSymbol is INamedTypeSymbol namedType && namedType.IsAbstract) |
| | 0 | 246 | | return true; |
| | | 247 | | |
| | | 248 | | // Common framework types are injectable |
| | 35 | 249 | | var fullName = typeSymbol.ToDisplayString(); |
| | 35 | 250 | | if (fullName.StartsWith("Microsoft.Extensions.", StringComparison.Ordinal)) |
| | 0 | 251 | | return true; |
| | | 252 | | |
| | | 253 | | // Classes with [Singleton], [Scoped], or [Transient] are injectable |
| | 35 | 254 | | if (typeSymbol is INamedTypeSymbol classType) |
| | | 255 | | { |
| | 244 | 256 | | foreach (var attr in classType.GetAttributes()) |
| | | 257 | | { |
| | 87 | 258 | | var attrName = attr.AttributeClass?.Name; |
| | 87 | 259 | | if (attrName is "SingletonAttribute" or "ScopedAttribute" or "TransientAttribute") |
| | 0 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 35 | 264 | | return false; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Represents a constructor suitable for factory generation. |
| | | 269 | | /// </summary> |
| | | 270 | | public readonly struct FactoryConstructorInfo |
| | | 271 | | { |
| | | 272 | | public FactoryConstructorInfo( |
| | | 273 | | TypeDiscoveryHelper.ConstructorParameterInfo[] injectableParameters, |
| | | 274 | | TypeDiscoveryHelper.ConstructorParameterInfo[] runtimeParameters) |
| | | 275 | | { |
| | 29 | 276 | | InjectableParameters = injectableParameters; |
| | 29 | 277 | | RuntimeParameters = runtimeParameters; |
| | 29 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary>Parameters that can be resolved from the service provider.</summary> |
| | 84 | 281 | | public TypeDiscoveryHelper.ConstructorParameterInfo[] InjectableParameters { get; } |
| | | 282 | | |
| | | 283 | | /// <summary>Parameters that must be provided at factory call time.</summary> |
| | 196 | 284 | | public TypeDiscoveryHelper.ConstructorParameterInfo[] RuntimeParameters { get; } |
| | | 285 | | } |
| | | 286 | | } |