| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | |
| | | 8 | | using Microsoft.CodeAnalysis; |
| | | 9 | | |
| | | 10 | | using NexusLabs.Needlr.Generators.Models; |
| | | 11 | | |
| | | 12 | | namespace NexusLabs.Needlr.Generators; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Discovers types and assemblies from referenced projects that have |
| | | 16 | | /// the <c>[GenerateTypeRegistry]</c> attribute for diagnostics, graph export, |
| | | 17 | | /// and force-loading. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class AssemblyDiscoveryHelper |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Discovers all referenced assemblies that have the [GenerateTypeRegistry] attribute. |
| | | 23 | | /// These assemblies need to be force-loaded to ensure their module initializers run. |
| | | 24 | | /// </summary> |
| | | 25 | | internal static IReadOnlyList<string> DiscoverReferencedAssembliesWithTypeRegistry(Compilation compilation) |
| | | 26 | | { |
| | 458 | 27 | | var result = new List<string>(); |
| | | 28 | | |
| | 155626 | 29 | | foreach (var reference in compilation.References) |
| | | 30 | | { |
| | 77355 | 31 | | if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assemblySymbol) |
| | | 32 | | { |
| | | 33 | | // Skip the current assembly |
| | 77168 | 34 | | if (SymbolEqualityComparer.Default.Equals(assemblySymbol, compilation.Assembly)) |
| | | 35 | | continue; |
| | | 36 | | |
| | 77168 | 37 | | if (TypeDiscoveryHelper.HasGenerateTypeRegistryAttribute(assemblySymbol)) |
| | | 38 | | { |
| | 18 | 39 | | result.Add(assemblySymbol.Name); |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 458 | 44 | | return result; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Discovers types from referenced assemblies with [GenerateTypeRegistry] for diagnostics purposes. |
| | | 49 | | /// Unlike the main discovery, this includes internal types since we're just showing them in diagnostics. |
| | | 50 | | /// </summary> |
| | | 51 | | internal static Dictionary<string, List<DiagnosticTypeInfo>> DiscoverReferencedAssemblyTypesForDiagnostics(Compilati |
| | | 52 | | { |
| | 95 | 53 | | var result = new Dictionary<string, List<DiagnosticTypeInfo>>(); |
| | | 54 | | |
| | 32328 | 55 | | foreach (var reference in compilation.References) |
| | | 56 | | { |
| | 16069 | 57 | | if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assemblySymbol) |
| | | 58 | | { |
| | | 59 | | // Skip the current assembly |
| | 16069 | 60 | | if (SymbolEqualityComparer.Default.Equals(assemblySymbol, compilation.Assembly)) |
| | | 61 | | continue; |
| | | 62 | | |
| | 16069 | 63 | | if (!TypeDiscoveryHelper.HasGenerateTypeRegistryAttribute(assemblySymbol)) |
| | | 64 | | continue; |
| | | 65 | | |
| | 14 | 66 | | var assemblyTypes = new List<DiagnosticTypeInfo>(); |
| | | 67 | | |
| | | 68 | | // First pass: collect intercepted service names so we can identify their proxies |
| | 14 | 69 | | var interceptedServiceNames = new HashSet<string>(); |
| | 120 | 70 | | foreach (var typeSymbol in TypeDiscoveryHelper.GetAllTypes(assemblySymbol.GlobalNamespace)) |
| | | 71 | | { |
| | 46 | 72 | | if (InterceptorDiscoveryHelper.HasInterceptAttributes(typeSymbol)) |
| | | 73 | | { |
| | 2 | 74 | | interceptedServiceNames.Add(typeSymbol.Name); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 120 | 78 | | foreach (var typeSymbol in TypeDiscoveryHelper.GetAllTypes(assemblySymbol.GlobalNamespace)) |
| | | 79 | | { |
| | | 80 | | // Check if it's a registerable type (injectable, plugin, factory source, or interceptor) |
| | 46 | 81 | | var hasFactoryAttr = FactoryDiscoveryHelper.HasGenerateFactoryAttribute(typeSymbol); |
| | 46 | 82 | | var hasInterceptAttr = InterceptorDiscoveryHelper.HasInterceptAttributes(typeSymbol); |
| | 46 | 83 | | var isInterceptorProxy = typeSymbol.Name.EndsWith("_InterceptorProxy"); |
| | | 84 | | |
| | 46 | 85 | | if (!hasFactoryAttr && !hasInterceptAttr && !isInterceptorProxy && |
| | 46 | 86 | | !TypeDiscoveryHelper.WouldBeInjectableIgnoringAccessibility(typeSymbol) && |
| | 46 | 87 | | !TypeDiscoveryHelper.WouldBePluginIgnoringAccessibility(typeSymbol, compilation.Assembly)) |
| | | 88 | | continue; |
| | | 89 | | |
| | 18 | 90 | | var typeName = TypeDiscoveryHelper.GetFullyQualifiedName(typeSymbol); |
| | 18 | 91 | | var shortName = typeSymbol.Name; |
| | 18 | 92 | | var lifetime = TypeDiscoveryHelper.DetermineLifetime(typeSymbol) ?? GeneratorLifetime.Singleton; |
| | 18 | 93 | | var interfaces = TypeDiscoveryHelper.GetRegisterableInterfaces(typeSymbol, compilation.Assembly) |
| | 14 | 94 | | .Select(i => TypeDiscoveryHelper.GetFullyQualifiedName(i)) |
| | 18 | 95 | | .ToArray(); |
| | 18 | 96 | | var dependencies = TypeDiscoveryHelper.GetBestConstructorParameters(typeSymbol)? |
| | 18 | 97 | | .ToArray() ?? Array.Empty<string>(); |
| | 18 | 98 | | var isDecorator = TypeDiscoveryHelper.HasDecoratorForAttribute(typeSymbol) || |
| | 18 | 99 | | OpenDecoratorDiscoveryHelper.HasOpenDecoratorForAttribute(typeSymbol); |
| | 18 | 100 | | var isPlugin = TypeDiscoveryHelper.WouldBePluginIgnoringAccessibility(typeSymbol, compilation.Assemb |
| | 18 | 101 | | var keyedValues = TypeDiscoveryHelper.GetKeyedServiceKeys(typeSymbol); |
| | 18 | 102 | | var keyedValue = keyedValues.Length > 0 ? keyedValues[0] : null; |
| | | 103 | | |
| | | 104 | | // Check if this service has an interceptor proxy (its name + "_InterceptorProxy" exists) |
| | 18 | 105 | | var hasInterceptorProxy = interceptedServiceNames.Contains(shortName); |
| | | 106 | | |
| | 18 | 107 | | assemblyTypes.Add(new DiagnosticTypeInfo( |
| | 18 | 108 | | typeName, |
| | 18 | 109 | | shortName, |
| | 18 | 110 | | lifetime, |
| | 18 | 111 | | interfaces, |
| | 18 | 112 | | dependencies, |
| | 18 | 113 | | isDecorator, |
| | 18 | 114 | | isPlugin, |
| | 18 | 115 | | hasFactoryAttr, |
| | 18 | 116 | | keyedValue, |
| | 18 | 117 | | isInterceptor: hasInterceptAttr, |
| | 18 | 118 | | hasInterceptorProxy: hasInterceptorProxy)); |
| | | 119 | | } |
| | | 120 | | |
| | 14 | 121 | | if (assemblyTypes.Count > 0) |
| | | 122 | | { |
| | 14 | 123 | | result[assemblySymbol.Name] = assemblyTypes; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | 95 | 128 | | return result; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Discovers types from referenced assemblies with [GenerateTypeRegistry] for graph export. |
| | | 133 | | /// Unlike the main discovery, this includes internal types since they are registered by their own TypeRegistry. |
| | | 134 | | /// Returns DiscoveredType objects that can be included in the graph export. |
| | | 135 | | /// </summary> |
| | | 136 | | internal static Dictionary<string, IReadOnlyList<DiscoveredType>> DiscoverReferencedAssemblyTypesForGraph(Compilatio |
| | | 137 | | { |
| | 4 | 138 | | var result = new Dictionary<string, IReadOnlyList<DiscoveredType>>(); |
| | | 139 | | |
| | 1360 | 140 | | foreach (var reference in compilation.References) |
| | | 141 | | { |
| | 676 | 142 | | if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assemblySymbol) |
| | | 143 | | { |
| | | 144 | | // Skip the current assembly |
| | 676 | 145 | | if (SymbolEqualityComparer.Default.Equals(assemblySymbol, compilation.Assembly)) |
| | | 146 | | continue; |
| | | 147 | | |
| | 676 | 148 | | if (!TypeDiscoveryHelper.HasGenerateTypeRegistryAttribute(assemblySymbol)) |
| | | 149 | | continue; |
| | | 150 | | |
| | | 151 | | // Try to get interface locations from the assembly's ServiceCatalog |
| | 0 | 152 | | var interfaceLocationLookup = GetInterfaceLocationsFromServiceCatalog(assemblySymbol); |
| | | 153 | | |
| | 0 | 154 | | var assemblyTypes = new List<DiscoveredType>(); |
| | | 155 | | |
| | 0 | 156 | | foreach (var typeSymbol in TypeDiscoveryHelper.GetAllTypes(assemblySymbol.GlobalNamespace)) |
| | | 157 | | { |
| | | 158 | | // Check if it's a registerable type |
| | 0 | 159 | | var hasFactoryAttr = FactoryDiscoveryHelper.HasGenerateFactoryAttribute(typeSymbol); |
| | | 160 | | |
| | | 161 | | // Skip types that are only factories (handled separately) |
| | 0 | 162 | | if (hasFactoryAttr) |
| | | 163 | | continue; |
| | | 164 | | |
| | 0 | 165 | | if (!TypeDiscoveryHelper.WouldBeInjectableIgnoringAccessibility(typeSymbol) && |
| | 0 | 166 | | !TypeDiscoveryHelper.WouldBePluginIgnoringAccessibility(typeSymbol, compilation.Assembly)) |
| | | 167 | | continue; |
| | | 168 | | |
| | | 169 | | // Skip decorators - they modify other services, not registered directly as services |
| | 0 | 170 | | if (TypeDiscoveryHelper.HasDecoratorForAttribute(typeSymbol) || |
| | 0 | 171 | | OpenDecoratorDiscoveryHelper.HasOpenDecoratorForAttribute(typeSymbol)) |
| | | 172 | | continue; |
| | | 173 | | |
| | 0 | 174 | | var typeName = TypeDiscoveryHelper.GetFullyQualifiedName(typeSymbol); |
| | 0 | 175 | | var interfaceSymbols = TypeDiscoveryHelper.GetRegisterableInterfaces(typeSymbol, compilation.Assembl |
| | 0 | 176 | | var interfaces = interfaceSymbols |
| | 0 | 177 | | .Select(i => TypeDiscoveryHelper.GetFullyQualifiedName(i)) |
| | 0 | 178 | | .ToArray(); |
| | | 179 | | |
| | | 180 | | // Get interface locations from ServiceCatalog lookup, falling back to symbol locations |
| | 0 | 181 | | var interfaceInfos = interfaceSymbols.Select(i => |
| | 0 | 182 | | { |
| | 0 | 183 | | var ifaceFullName = TypeDiscoveryHelper.GetFullyQualifiedName(i); |
| | 0 | 184 | | |
| | 0 | 185 | | // First try the ServiceCatalog lookup |
| | 0 | 186 | | if (interfaceLocationLookup.TryGetValue(ifaceFullName, out var catalogInfo)) |
| | 0 | 187 | | { |
| | 0 | 188 | | return catalogInfo; |
| | 0 | 189 | | } |
| | 0 | 190 | | |
| | 0 | 191 | | // Fall back to symbol locations (works for source references) |
| | 0 | 192 | | var ifaceLocation = i.Locations.FirstOrDefault(); |
| | 0 | 193 | | var ifaceFilePath = ifaceLocation?.SourceTree?.FilePath; |
| | 0 | 194 | | var ifaceLine = ifaceLocation?.GetLineSpan().StartLinePosition.Line + 1 ?? 0; |
| | 0 | 195 | | return new InterfaceInfo(ifaceFullName, ifaceFilePath, ifaceLine); |
| | 0 | 196 | | }).ToArray(); |
| | | 197 | | |
| | 0 | 198 | | var lifetime = TypeDiscoveryHelper.DetermineLifetime(typeSymbol) ?? GeneratorLifetime.Singleton; |
| | 0 | 199 | | var constructorParams = TypeDiscoveryHelper.GetBestConstructorParametersWithKeys(typeSymbol)?.ToArra |
| | 0 | 200 | | ?? Array.Empty<TypeDiscoveryHelper.ConstructorParameterInfo>(); |
| | 0 | 201 | | var keyedValues = TypeDiscoveryHelper.GetKeyedServiceKeys(typeSymbol); |
| | 0 | 202 | | var sourceFilePath = typeSymbol.Locations.FirstOrDefault()?.SourceTree?.FilePath; |
| | 0 | 203 | | var sourceLine = typeSymbol.Locations.FirstOrDefault() is { } location |
| | 0 | 204 | | ? location.GetLineSpan().StartLinePosition.Line + 1 |
| | 0 | 205 | | : 0; |
| | | 206 | | |
| | 0 | 207 | | var discoveredType = new DiscoveredType( |
| | 0 | 208 | | typeName, |
| | 0 | 209 | | interfaces, |
| | 0 | 210 | | assemblySymbol.Name, |
| | 0 | 211 | | lifetime, |
| | 0 | 212 | | constructorParams, |
| | 0 | 213 | | keyedValues, |
| | 0 | 214 | | sourceFilePath, |
| | 0 | 215 | | sourceLine, |
| | 0 | 216 | | TypeDiscoveryHelper.IsDisposableType(typeSymbol), |
| | 0 | 217 | | interfaceInfos); |
| | | 218 | | |
| | 0 | 219 | | assemblyTypes.Add(discoveredType); |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | if (assemblyTypes.Count > 0) |
| | | 223 | | { |
| | 0 | 224 | | result[assemblySymbol.Name] = assemblyTypes; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | } |
| | | 228 | | |
| | 4 | 229 | | return result; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Extracts interface location information from a referenced assembly's ServiceCatalog. |
| | | 234 | | /// The ServiceCatalog is generated by Needlr and contains compile-time interface location data. |
| | | 235 | | /// </summary> |
| | | 236 | | internal static Dictionary<string, InterfaceInfo> GetInterfaceLocationsFromServiceCatalog(IAssemblySymbol assemblySy |
| | | 237 | | { |
| | 0 | 238 | | var result = new Dictionary<string, InterfaceInfo>(StringComparer.Ordinal); |
| | | 239 | | |
| | | 240 | | // Look for the ServiceCatalog class in the Generated namespace |
| | 0 | 241 | | var serviceCatalogTypeName = $"{assemblySymbol.Name}.Generated.ServiceCatalog"; |
| | 0 | 242 | | var serviceCatalogType = assemblySymbol.GetTypeByMetadataName(serviceCatalogTypeName); |
| | | 243 | | |
| | 0 | 244 | | if (serviceCatalogType == null) |
| | 0 | 245 | | return result; |
| | | 246 | | |
| | | 247 | | // Find the Services property |
| | 0 | 248 | | var servicesProperty = serviceCatalogType.GetMembers("Services") |
| | 0 | 249 | | .OfType<IPropertySymbol>() |
| | 0 | 250 | | .FirstOrDefault(); |
| | | 251 | | |
| | 0 | 252 | | if (servicesProperty == null) |
| | 0 | 253 | | return result; |
| | | 254 | | |
| | | 255 | | // The Services property has an initializer with ServiceCatalogEntry array |
| | | 256 | | // We need to parse the initializer to extract interface locations |
| | | 257 | | // This requires looking at the declaring syntax reference |
| | 0 | 258 | | var syntaxRef = servicesProperty.DeclaringSyntaxReferences.FirstOrDefault(); |
| | 0 | 259 | | if (syntaxRef == null) |
| | 0 | 260 | | return result; |
| | | 261 | | |
| | 0 | 262 | | var syntax = syntaxRef.GetSyntax(); |
| | 0 | 263 | | if (syntax == null) |
| | 0 | 264 | | return result; |
| | | 265 | | |
| | | 266 | | // Parse the array initializer to extract InterfaceEntry data |
| | | 267 | | // The format is: new InterfaceEntry("fullName", "filePath", line) |
| | 0 | 268 | | var text = syntax.ToFullString(); |
| | | 269 | | |
| | | 270 | | // Use regex to extract InterfaceEntry values |
| | 0 | 271 | | var interfaceEntryPattern = new System.Text.RegularExpressions.Regex( |
| | 0 | 272 | | @"new\s+global::NexusLabs\.Needlr\.Catalog\.InterfaceEntry\(\s*""([^""]+)""\s*,\s*(""([^""]+)""|null)\s*,\s* |
| | 0 | 273 | | System.Text.RegularExpressions.RegexOptions.Compiled); |
| | | 274 | | |
| | 0 | 275 | | foreach (System.Text.RegularExpressions.Match match in interfaceEntryPattern.Matches(text)) |
| | | 276 | | { |
| | 0 | 277 | | var fullName = match.Groups[1].Value; |
| | 0 | 278 | | var filePath = match.Groups[3].Success ? match.Groups[3].Value : null; |
| | 0 | 279 | | var line = int.Parse(match.Groups[4].Value); |
| | | 280 | | |
| | 0 | 281 | | if (!result.ContainsKey(fullName)) |
| | | 282 | | { |
| | 0 | 283 | | result[fullName] = new InterfaceInfo(fullName, filePath, line); |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return result; |
| | | 288 | | } |
| | | 289 | | } |