| | | 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 | | { |
| | 568 | 27 | | var result = new List<string>(); |
| | | 28 | | |
| | 192880 | 29 | | foreach (var reference in compilation.References) |
| | | 30 | | { |
| | 95872 | 31 | | if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assemblySymbol) |
| | | 32 | | { |
| | | 33 | | // Skip the current assembly |
| | 95640 | 34 | | if (SymbolEqualityComparer.Default.Equals(assemblySymbol, compilation.Assembly)) |
| | | 35 | | continue; |
| | | 36 | | |
| | 95640 | 37 | | if (TypeDiscoveryHelper.HasGenerateTypeRegistryAttribute(assemblySymbol)) |
| | | 38 | | { |
| | 18 | 39 | | result.Add(assemblySymbol.Name); |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 568 | 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 | | { |
| | 7 | 138 | | var result = new Dictionary<string, IReadOnlyList<DiscoveredType>>(); |
| | | 139 | | |
| | 2384 | 140 | | foreach (var reference in compilation.References) |
| | | 141 | | { |
| | 1185 | 142 | | if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assemblySymbol) |
| | | 143 | | { |
| | | 144 | | // Skip the current assembly |
| | 1185 | 145 | | if (SymbolEqualityComparer.Default.Equals(assemblySymbol, compilation.Assembly)) |
| | | 146 | | continue; |
| | | 147 | | |
| | 1185 | 148 | | if (!TypeDiscoveryHelper.HasGenerateTypeRegistryAttribute(assemblySymbol)) |
| | | 149 | | continue; |
| | | 150 | | |
| | 2 | 151 | | var assemblyTypes = new List<DiscoveredType>(); |
| | | 152 | | |
| | 18 | 153 | | foreach (var typeSymbol in TypeDiscoveryHelper.GetAllTypes(assemblySymbol.GlobalNamespace)) |
| | | 154 | | { |
| | | 155 | | // Check if it's a registerable type |
| | 7 | 156 | | var hasFactoryAttr = FactoryDiscoveryHelper.HasGenerateFactoryAttribute(typeSymbol); |
| | | 157 | | |
| | | 158 | | // Skip types that are only factories (handled separately) |
| | 7 | 159 | | if (hasFactoryAttr) |
| | | 160 | | continue; |
| | | 161 | | |
| | 7 | 162 | | if (!TypeDiscoveryHelper.WouldBeInjectableIgnoringAccessibility(typeSymbol) && |
| | 7 | 163 | | !TypeDiscoveryHelper.WouldBePluginIgnoringAccessibility(typeSymbol, compilation.Assembly)) |
| | | 164 | | continue; |
| | | 165 | | |
| | | 166 | | // Skip decorators - they modify other services, not registered directly as services |
| | 2 | 167 | | if (TypeDiscoveryHelper.HasDecoratorForAttribute(typeSymbol) || |
| | 2 | 168 | | OpenDecoratorDiscoveryHelper.HasOpenDecoratorForAttribute(typeSymbol)) |
| | | 169 | | continue; |
| | | 170 | | |
| | 2 | 171 | | var typeName = TypeDiscoveryHelper.GetFullyQualifiedName(typeSymbol); |
| | 2 | 172 | | var interfaceSymbols = TypeDiscoveryHelper.GetRegisterableInterfaces(typeSymbol, compilation.Assembl |
| | 2 | 173 | | var interfaces = interfaceSymbols |
| | 2 | 174 | | .Select(i => TypeDiscoveryHelper.GetFullyQualifiedName(i)) |
| | 2 | 175 | | .ToArray(); |
| | | 176 | | |
| | | 177 | | // Source/CompilationReference symbols retain locations. PE metadata |
| | | 178 | | // symbols deliberately produce null locations for IDE graph merging. |
| | 2 | 179 | | var interfaceInfos = interfaceSymbols.Select(i => |
| | 2 | 180 | | { |
| | 2 | 181 | | var ifaceFullName = TypeDiscoveryHelper.GetFullyQualifiedName(i); |
| | 2 | 182 | | var ifaceLocation = i.Locations.FirstOrDefault( |
| | 2 | 183 | | location => |
| | 4 | 184 | | location.IsInSource && |
| | 4 | 185 | | location.SourceTree is not null); |
| | 2 | 186 | | var ifaceFilePath = ifaceLocation?.SourceTree?.FilePath; |
| | 2 | 187 | | var ifaceLine = ifaceLocation is null |
| | 2 | 188 | | ? 0 |
| | 2 | 189 | | : ifaceLocation.GetLineSpan() |
| | 2 | 190 | | .StartLinePosition.Line + 1; |
| | 2 | 191 | | return new InterfaceInfo(ifaceFullName, ifaceFilePath, ifaceLine); |
| | 2 | 192 | | }).ToArray(); |
| | | 193 | | |
| | 2 | 194 | | var lifetime = TypeDiscoveryHelper.DetermineLifetime(typeSymbol) ?? GeneratorLifetime.Singleton; |
| | 2 | 195 | | var constructorParams = TypeDiscoveryHelper.GetBestConstructorParametersWithKeys(typeSymbol)?.ToArra |
| | 2 | 196 | | ?? Array.Empty<TypeDiscoveryHelper.ConstructorParameterInfo>(); |
| | 2 | 197 | | var keyedValues = TypeDiscoveryHelper.GetKeyedServiceKeys(typeSymbol); |
| | 2 | 198 | | var sourceLocation = typeSymbol.Locations.FirstOrDefault( |
| | 2 | 199 | | location => |
| | 4 | 200 | | location.IsInSource && |
| | 4 | 201 | | location.SourceTree is not null); |
| | 2 | 202 | | var sourceFilePath = |
| | 2 | 203 | | sourceLocation?.SourceTree?.FilePath; |
| | 2 | 204 | | var sourceLine = sourceLocation is null |
| | 2 | 205 | | ? 0 |
| | 2 | 206 | | : sourceLocation.GetLineSpan() |
| | 2 | 207 | | .StartLinePosition.Line + 1; |
| | | 208 | | |
| | 2 | 209 | | var discoveredType = new DiscoveredType( |
| | 2 | 210 | | typeName, |
| | 2 | 211 | | interfaces, |
| | 2 | 212 | | assemblySymbol.Name, |
| | 2 | 213 | | lifetime, |
| | 2 | 214 | | constructorParams, |
| | 2 | 215 | | keyedValues, |
| | 2 | 216 | | sourceFilePath, |
| | 2 | 217 | | sourceLine, |
| | 2 | 218 | | TypeDiscoveryHelper.IsDisposableType(typeSymbol), |
| | 2 | 219 | | interfaceInfos); |
| | | 220 | | |
| | 2 | 221 | | assemblyTypes.Add(discoveredType); |
| | | 222 | | } |
| | | 223 | | |
| | 2 | 224 | | if (assemblyTypes.Count > 0) |
| | | 225 | | { |
| | 2 | 226 | | result[assemblySymbol.Name] = assemblyTypes; |
| | | 227 | | } |
| | | 228 | | } |
| | | 229 | | } |
| | | 230 | | |
| | 7 | 231 | | return result; |
| | | 232 | | } |
| | | 233 | | } |