| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | using NexusLabs.Needlr.Generators.Models; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Generators.Export; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Serializes the normalized source-generated registration plan for compiler tooling. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class RegistrationManifestSerializer |
| | | 14 | | { |
| | | 15 | | internal const string SchemaVersion = "1.0"; |
| | | 16 | | |
| | | 17 | | internal static string Serialize(GeneratedRegistrationPlan plan) |
| | | 18 | | { |
| | 12 | 19 | | var builder = new StringBuilder(); |
| | 12 | 20 | | var discovery = plan.DiscoveryResult; |
| | 12 | 21 | | var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier( |
| | 12 | 22 | | plan.AssemblyName); |
| | | 23 | | |
| | 12 | 24 | | builder.Append('{'); |
| | 12 | 25 | | AppendStringProperty(builder, "schemaVersion", SchemaVersion); |
| | 12 | 26 | | builder.Append(','); |
| | 12 | 27 | | AppendStringProperty(builder, "assemblyName", plan.AssemblyName); |
| | 12 | 28 | | builder.Append(','); |
| | 12 | 29 | | AppendBooleanProperty(builder, "isAotProject", plan.IsAotProject); |
| | 12 | 30 | | builder.Append(','); |
| | 12 | 31 | | AppendServiceCatalogRegistration( |
| | 12 | 32 | | builder, |
| | 12 | 33 | | plan.RegistersServiceCatalog, |
| | 12 | 34 | | safeAssemblyName); |
| | 12 | 35 | | builder.Append(','); |
| | 12 | 36 | | AppendStringArrayProperty( |
| | 12 | 37 | | builder, |
| | 12 | 38 | | "referencedRegistryAssemblies", |
| | 12 | 39 | | plan.ReferencedRegistryAssemblies); |
| | 12 | 40 | | builder.Append(','); |
| | 12 | 41 | | AppendInjectableTypes(builder, discovery.InjectableTypes); |
| | 12 | 42 | | builder.Append(','); |
| | 12 | 43 | | AppendDecorators(builder, discovery.Decorators); |
| | 12 | 44 | | builder.Append(','); |
| | 12 | 45 | | AppendHostedServices(builder, discovery.HostedServices); |
| | 12 | 46 | | builder.Append(','); |
| | 12 | 47 | | AppendInterceptedServices( |
| | 12 | 48 | | builder, |
| | 12 | 49 | | discovery.InterceptedServices, |
| | 12 | 50 | | safeAssemblyName); |
| | 12 | 51 | | builder.Append(','); |
| | 12 | 52 | | AppendFactories(builder, plan.EmittedFactories, safeAssemblyName); |
| | 12 | 53 | | builder.Append(','); |
| | 12 | 54 | | AppendProviders(builder, plan.EmittedProviders, safeAssemblyName); |
| | 12 | 55 | | builder.Append(','); |
| | 12 | 56 | | AppendOptions( |
| | 12 | 57 | | builder, |
| | 12 | 58 | | discovery.Options, |
| | 12 | 59 | | safeAssemblyName, |
| | 12 | 60 | | plan.IsAotProject); |
| | 12 | 61 | | builder.Append(','); |
| | 12 | 62 | | AppendHttpClients(builder, discovery.HttpClients); |
| | 12 | 63 | | builder.Append(','); |
| | 12 | 64 | | AppendComposedRegistrations( |
| | 12 | 65 | | builder, |
| | 12 | 66 | | discovery.ComposedRegistrations); |
| | 12 | 67 | | builder.Append(','); |
| | 12 | 68 | | AppendPlugins(builder, discovery.PluginTypes); |
| | 12 | 69 | | builder.Append('}'); |
| | | 70 | | |
| | 12 | 71 | | return builder.ToString(); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | private static void AppendServiceCatalogRegistration( |
| | | 75 | | StringBuilder builder, |
| | | 76 | | bool registersServiceCatalog, |
| | | 77 | | string safeAssemblyName) |
| | | 78 | | { |
| | 12 | 79 | | AppendPropertyName(builder, "serviceCatalogRegistration"); |
| | 12 | 80 | | if (!registersServiceCatalog) |
| | | 81 | | { |
| | 1 | 82 | | builder.Append("null"); |
| | 1 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | 11 | 86 | | builder.Append('{'); |
| | 11 | 87 | | AppendStringProperty( |
| | 11 | 88 | | builder, |
| | 11 | 89 | | "serviceType", |
| | 11 | 90 | | "global::NexusLabs.Needlr.Catalog.IServiceCatalog"); |
| | 11 | 91 | | builder.Append(','); |
| | 11 | 92 | | AppendStringProperty( |
| | 11 | 93 | | builder, |
| | 11 | 94 | | "implementationType", |
| | 11 | 95 | | $"global::{safeAssemblyName}.Generated.ServiceCatalog"); |
| | 11 | 96 | | builder.Append(','); |
| | 11 | 97 | | AppendStringProperty(builder, "lifetime", "Singleton"); |
| | 11 | 98 | | builder.Append('}'); |
| | 11 | 99 | | } |
| | | 100 | | |
| | | 101 | | private static void AppendInjectableTypes( |
| | | 102 | | StringBuilder builder, |
| | | 103 | | IReadOnlyList<DiscoveredType> injectableTypes) |
| | | 104 | | { |
| | 12 | 105 | | AppendPropertyName(builder, "injectableTypes"); |
| | 12 | 106 | | builder.Append('['); |
| | | 107 | | |
| | 12 | 108 | | var orderedTypes = injectableTypes |
| | 1108 | 109 | | .OrderBy(type => type.AssemblyName, StringComparer.Ordinal) |
| | 1108 | 110 | | .ThenBy(type => type.TypeName, StringComparer.Ordinal) |
| | 12 | 111 | | .ToArray(); |
| | | 112 | | |
| | 2250 | 113 | | for (var index = 0; index < orderedTypes.Length; index++) |
| | | 114 | | { |
| | 1113 | 115 | | if (index > 0) |
| | | 116 | | { |
| | 1103 | 117 | | builder.Append(','); |
| | | 118 | | } |
| | | 119 | | |
| | 1113 | 120 | | var type = orderedTypes[index]; |
| | 1113 | 121 | | builder.Append('{'); |
| | 1113 | 122 | | AppendStringProperty( |
| | 1113 | 123 | | builder, |
| | 1113 | 124 | | "implementationType", |
| | 1113 | 125 | | type.TypeName); |
| | 1113 | 126 | | builder.Append(','); |
| | 1113 | 127 | | AppendStringProperty(builder, "assemblyName", type.AssemblyName); |
| | 1113 | 128 | | builder.Append(','); |
| | 1113 | 129 | | AppendStringProperty( |
| | 1113 | 130 | | builder, |
| | 1113 | 131 | | "lifetime", |
| | 1113 | 132 | | type.Lifetime.ToString()); |
| | 1113 | 133 | | builder.Append(','); |
| | 1113 | 134 | | AppendServiceTypesProperty( |
| | 1113 | 135 | | builder, |
| | 1113 | 136 | | type.TypeName, |
| | 1113 | 137 | | type.InterfaceNames); |
| | 1113 | 138 | | builder.Append(','); |
| | 1113 | 139 | | AppendSortedStringArrayProperty( |
| | 1113 | 140 | | builder, |
| | 1113 | 141 | | "serviceKeys", |
| | 1113 | 142 | | type.ServiceKeys); |
| | 1113 | 143 | | builder.Append(','); |
| | 1113 | 144 | | AppendConstructorParameters( |
| | 1113 | 145 | | builder, |
| | 1113 | 146 | | "constructorParameters", |
| | 1113 | 147 | | type.ConstructorParameters); |
| | 1113 | 148 | | builder.Append('}'); |
| | | 149 | | } |
| | | 150 | | |
| | 12 | 151 | | builder.Append(']'); |
| | 12 | 152 | | } |
| | | 153 | | |
| | | 154 | | private static void AppendDecorators( |
| | | 155 | | StringBuilder builder, |
| | | 156 | | IReadOnlyList<DiscoveredDecorator> decorators) |
| | | 157 | | { |
| | 12 | 158 | | AppendPropertyName(builder, "decorators"); |
| | 12 | 159 | | builder.Append('['); |
| | | 160 | | |
| | 12 | 161 | | var orderedDecorators = decorators |
| | 1 | 162 | | .GroupBy(decorator => decorator.ServiceTypeName) |
| | 1 | 163 | | .OrderBy(group => group.Key, StringComparer.Ordinal) |
| | 2 | 164 | | .SelectMany(group => group.OrderBy(decorator => decorator.Order)) |
| | 12 | 165 | | .ToArray(); |
| | | 166 | | |
| | 26 | 167 | | for (var index = 0; index < orderedDecorators.Length; index++) |
| | | 168 | | { |
| | 1 | 169 | | if (index > 0) |
| | | 170 | | { |
| | 0 | 171 | | builder.Append(','); |
| | | 172 | | } |
| | | 173 | | |
| | 1 | 174 | | var decorator = orderedDecorators[index]; |
| | 1 | 175 | | builder.Append('{'); |
| | 1 | 176 | | AppendStringProperty( |
| | 1 | 177 | | builder, |
| | 1 | 178 | | "serviceType", |
| | 1 | 179 | | decorator.ServiceTypeName); |
| | 1 | 180 | | builder.Append(','); |
| | 1 | 181 | | AppendStringProperty( |
| | 1 | 182 | | builder, |
| | 1 | 183 | | "decoratorType", |
| | 1 | 184 | | decorator.DecoratorTypeName); |
| | 1 | 185 | | builder.Append(','); |
| | 1 | 186 | | AppendNumberProperty(builder, "order", decorator.Order); |
| | 1 | 187 | | builder.Append(','); |
| | 1 | 188 | | AppendStringProperty( |
| | 1 | 189 | | builder, |
| | 1 | 190 | | "assemblyName", |
| | 1 | 191 | | decorator.AssemblyName); |
| | 1 | 192 | | builder.Append('}'); |
| | | 193 | | } |
| | | 194 | | |
| | 12 | 195 | | builder.Append(']'); |
| | 12 | 196 | | } |
| | | 197 | | |
| | | 198 | | private static void AppendHostedServices( |
| | | 199 | | StringBuilder builder, |
| | | 200 | | IReadOnlyList<DiscoveredHostedService> hostedServices) |
| | | 201 | | { |
| | 12 | 202 | | AppendPropertyName(builder, "hostedServices"); |
| | 12 | 203 | | builder.Append('['); |
| | | 204 | | |
| | 26 | 205 | | for (var index = 0; index < hostedServices.Count; index++) |
| | | 206 | | { |
| | 1 | 207 | | if (index > 0) |
| | | 208 | | { |
| | 0 | 209 | | builder.Append(','); |
| | | 210 | | } |
| | | 211 | | |
| | 1 | 212 | | var service = hostedServices[index]; |
| | 1 | 213 | | builder.Append('{'); |
| | 1 | 214 | | AppendStringProperty( |
| | 1 | 215 | | builder, |
| | 1 | 216 | | "implementationType", |
| | 1 | 217 | | service.TypeName); |
| | 1 | 218 | | builder.Append(','); |
| | 1 | 219 | | AppendStringProperty( |
| | 1 | 220 | | builder, |
| | 1 | 221 | | "assemblyName", |
| | 1 | 222 | | service.AssemblyName); |
| | 1 | 223 | | builder.Append(','); |
| | 1 | 224 | | AppendStringProperty( |
| | 1 | 225 | | builder, |
| | 1 | 226 | | "lifetime", |
| | 1 | 227 | | service.Lifetime.ToString()); |
| | 1 | 228 | | builder.Append(','); |
| | 1 | 229 | | AppendConstructorParameters( |
| | 1 | 230 | | builder, |
| | 1 | 231 | | "constructorParameters", |
| | 1 | 232 | | service.ConstructorParameters); |
| | 1 | 233 | | builder.Append('}'); |
| | | 234 | | } |
| | | 235 | | |
| | 12 | 236 | | builder.Append(']'); |
| | 12 | 237 | | } |
| | | 238 | | |
| | | 239 | | private static void AppendInterceptedServices( |
| | | 240 | | StringBuilder builder, |
| | | 241 | | IReadOnlyList<DiscoveredInterceptedService> interceptedServices, |
| | | 242 | | string safeAssemblyName) |
| | | 243 | | { |
| | 12 | 244 | | AppendPropertyName(builder, "interceptedServices"); |
| | 12 | 245 | | builder.Append('['); |
| | | 246 | | |
| | 26 | 247 | | for (var index = 0; index < interceptedServices.Count; index++) |
| | | 248 | | { |
| | 1 | 249 | | if (index > 0) |
| | | 250 | | { |
| | 0 | 251 | | builder.Append(','); |
| | | 252 | | } |
| | | 253 | | |
| | 1 | 254 | | var service = interceptedServices[index]; |
| | 1 | 255 | | builder.Append('{'); |
| | 1 | 256 | | AppendStringProperty( |
| | 1 | 257 | | builder, |
| | 1 | 258 | | "implementationType", |
| | 1 | 259 | | service.TypeName); |
| | 1 | 260 | | builder.Append(','); |
| | 1 | 261 | | AppendStringProperty( |
| | 1 | 262 | | builder, |
| | 1 | 263 | | "proxyType", |
| | 1 | 264 | | $"global::{safeAssemblyName}.Generated." + |
| | 1 | 265 | | GeneratorHelpers.GetProxyTypeName(service.TypeName)); |
| | 1 | 266 | | builder.Append(','); |
| | 1 | 267 | | AppendStringProperty( |
| | 1 | 268 | | builder, |
| | 1 | 269 | | "assemblyName", |
| | 1 | 270 | | service.AssemblyName); |
| | 1 | 271 | | builder.Append(','); |
| | 1 | 272 | | AppendStringProperty( |
| | 1 | 273 | | builder, |
| | 1 | 274 | | "lifetime", |
| | 1 | 275 | | service.Lifetime.ToString()); |
| | 1 | 276 | | builder.Append(','); |
| | 1 | 277 | | AppendSortedStringArrayProperty( |
| | 1 | 278 | | builder, |
| | 1 | 279 | | "serviceTypes", |
| | 1 | 280 | | service.InterfaceNames); |
| | 1 | 281 | | builder.Append(','); |
| | 1 | 282 | | AppendSortedStringArrayProperty( |
| | 1 | 283 | | builder, |
| | 1 | 284 | | "interceptorTypes", |
| | 1 | 285 | | service.AllInterceptorTypeNames); |
| | 1 | 286 | | builder.Append('}'); |
| | | 287 | | } |
| | | 288 | | |
| | 12 | 289 | | builder.Append(']'); |
| | 12 | 290 | | } |
| | | 291 | | |
| | | 292 | | private static void AppendFactories( |
| | | 293 | | StringBuilder builder, |
| | | 294 | | IReadOnlyList<DiscoveredFactory> factories, |
| | | 295 | | string safeAssemblyName) |
| | | 296 | | { |
| | 12 | 297 | | AppendPropertyName(builder, "factories"); |
| | 12 | 298 | | builder.Append('['); |
| | | 299 | | |
| | 26 | 300 | | for (var index = 0; index < factories.Count; index++) |
| | | 301 | | { |
| | 1 | 302 | | if (index > 0) |
| | | 303 | | { |
| | 0 | 304 | | builder.Append(','); |
| | | 305 | | } |
| | | 306 | | |
| | 1 | 307 | | var factory = factories[index]; |
| | 1 | 308 | | builder.Append('{'); |
| | 1 | 309 | | AppendStringProperty(builder, "targetType", factory.TypeName); |
| | 1 | 310 | | builder.Append(','); |
| | 1 | 311 | | AppendStringProperty( |
| | 1 | 312 | | builder, |
| | 1 | 313 | | "returnType", |
| | 1 | 314 | | factory.ReturnTypeName); |
| | 1 | 315 | | builder.Append(','); |
| | 1 | 316 | | AppendStringProperty( |
| | 1 | 317 | | builder, |
| | 1 | 318 | | "assemblyName", |
| | 1 | 319 | | factory.AssemblyName); |
| | 1 | 320 | | builder.Append(','); |
| | 1 | 321 | | AppendBooleanProperty( |
| | 1 | 322 | | builder, |
| | 1 | 323 | | "generatesFunc", |
| | 1 | 324 | | factory.GenerateFunc); |
| | 1 | 325 | | builder.Append(','); |
| | 1 | 326 | | AppendBooleanProperty( |
| | 1 | 327 | | builder, |
| | 1 | 328 | | "generatesInterface", |
| | 1 | 329 | | factory.GenerateInterface); |
| | 1 | 330 | | builder.Append(','); |
| | 1 | 331 | | AppendNullableStringProperty( |
| | 1 | 332 | | builder, |
| | 1 | 333 | | "generatedFactoryServiceType", |
| | 1 | 334 | | factory.GenerateInterface |
| | 1 | 335 | | ? $"global::{safeAssemblyName}.Generated.I" + |
| | 1 | 336 | | $"{factory.SimpleTypeName}Factory" |
| | 1 | 337 | | : null); |
| | 1 | 338 | | builder.Append(','); |
| | 1 | 339 | | AppendNullableStringProperty( |
| | 1 | 340 | | builder, |
| | 1 | 341 | | "generatedFactoryImplementationType", |
| | 1 | 342 | | factory.GenerateInterface |
| | 1 | 343 | | ? $"global::{safeAssemblyName}.Generated." + |
| | 1 | 344 | | $"{factory.SimpleTypeName}Factory" |
| | 1 | 345 | | : null); |
| | 1 | 346 | | builder.Append(','); |
| | 1 | 347 | | AppendFactoryConstructors(builder, factory.Constructors); |
| | 1 | 348 | | builder.Append('}'); |
| | | 349 | | } |
| | | 350 | | |
| | 12 | 351 | | builder.Append(']'); |
| | 12 | 352 | | } |
| | | 353 | | |
| | | 354 | | private static void AppendFactoryConstructors( |
| | | 355 | | StringBuilder builder, |
| | | 356 | | IReadOnlyList<FactoryDiscoveryHelper.FactoryConstructorInfo> constructors) |
| | | 357 | | { |
| | 1 | 358 | | AppendPropertyName(builder, "constructors"); |
| | 1 | 359 | | builder.Append('['); |
| | | 360 | | |
| | 4 | 361 | | for (var index = 0; index < constructors.Count; index++) |
| | | 362 | | { |
| | 1 | 363 | | if (index > 0) |
| | | 364 | | { |
| | 0 | 365 | | builder.Append(','); |
| | | 366 | | } |
| | | 367 | | |
| | 1 | 368 | | var constructor = constructors[index]; |
| | 1 | 369 | | builder.Append('{'); |
| | 1 | 370 | | AppendConstructorParameters( |
| | 1 | 371 | | builder, |
| | 1 | 372 | | "injectableParameters", |
| | 1 | 373 | | constructor.InjectableParameters); |
| | 1 | 374 | | builder.Append(','); |
| | 1 | 375 | | AppendConstructorParameters( |
| | 1 | 376 | | builder, |
| | 1 | 377 | | "runtimeParameters", |
| | 1 | 378 | | constructor.RuntimeParameters); |
| | 1 | 379 | | builder.Append('}'); |
| | | 380 | | } |
| | | 381 | | |
| | 1 | 382 | | builder.Append(']'); |
| | 1 | 383 | | } |
| | | 384 | | |
| | | 385 | | private static void AppendProviders( |
| | | 386 | | StringBuilder builder, |
| | | 387 | | IReadOnlyList<DiscoveredProvider> providers, |
| | | 388 | | string safeAssemblyName) |
| | | 389 | | { |
| | 12 | 390 | | AppendPropertyName(builder, "providers"); |
| | 12 | 391 | | builder.Append('['); |
| | | 392 | | |
| | 26 | 393 | | for (var index = 0; index < providers.Count; index++) |
| | | 394 | | { |
| | 1 | 395 | | if (index > 0) |
| | | 396 | | { |
| | 0 | 397 | | builder.Append(','); |
| | | 398 | | } |
| | | 399 | | |
| | 1 | 400 | | var provider = providers[index]; |
| | 1 | 401 | | var providerNamespace = |
| | 1 | 402 | | GeneratorHelpers.GetNamespaceFromTypeName(provider.TypeName); |
| | 1 | 403 | | var serviceType = provider.IsInterface |
| | 1 | 404 | | ? provider.TypeName |
| | 1 | 405 | | : $"global::{providerNamespace}.{provider.InterfaceTypeName}"; |
| | 1 | 406 | | var implementationType = provider.IsInterface |
| | 1 | 407 | | ? $"global::{safeAssemblyName}.Generated." + |
| | 1 | 408 | | provider.ImplementationTypeName |
| | 1 | 409 | | : provider.TypeName; |
| | | 410 | | |
| | 1 | 411 | | builder.Append('{'); |
| | 1 | 412 | | AppendStringProperty(builder, "sourceType", provider.TypeName); |
| | 1 | 413 | | builder.Append(','); |
| | 1 | 414 | | AppendStringProperty( |
| | 1 | 415 | | builder, |
| | 1 | 416 | | "assemblyName", |
| | 1 | 417 | | provider.AssemblyName); |
| | 1 | 418 | | builder.Append(','); |
| | 1 | 419 | | AppendStringProperty( |
| | 1 | 420 | | builder, |
| | 1 | 421 | | "mode", |
| | 1 | 422 | | provider.IsInterface ? "interface" : "shorthand"); |
| | 1 | 423 | | builder.Append(','); |
| | 1 | 424 | | AppendStringProperty(builder, "serviceType", serviceType); |
| | 1 | 425 | | builder.Append(','); |
| | 1 | 426 | | AppendStringProperty( |
| | 1 | 427 | | builder, |
| | 1 | 428 | | "implementationType", |
| | 1 | 429 | | implementationType); |
| | 1 | 430 | | builder.Append(','); |
| | 1 | 431 | | AppendProviderProperties(builder, provider.Properties); |
| | 1 | 432 | | builder.Append('}'); |
| | | 433 | | } |
| | | 434 | | |
| | 12 | 435 | | builder.Append(']'); |
| | 12 | 436 | | } |
| | | 437 | | |
| | | 438 | | private static void AppendProviderProperties( |
| | | 439 | | StringBuilder builder, |
| | | 440 | | IReadOnlyList<ProviderPropertyInfo> properties) |
| | | 441 | | { |
| | 1 | 442 | | AppendPropertyName(builder, "properties"); |
| | 1 | 443 | | builder.Append('['); |
| | | 444 | | |
| | 4 | 445 | | for (var index = 0; index < properties.Count; index++) |
| | | 446 | | { |
| | 1 | 447 | | if (index > 0) |
| | | 448 | | { |
| | 0 | 449 | | builder.Append(','); |
| | | 450 | | } |
| | | 451 | | |
| | 1 | 452 | | var property = properties[index]; |
| | 1 | 453 | | builder.Append('{'); |
| | 1 | 454 | | AppendStringProperty(builder, "name", property.PropertyName); |
| | 1 | 455 | | builder.Append(','); |
| | 1 | 456 | | AppendStringProperty( |
| | 1 | 457 | | builder, |
| | 1 | 458 | | "serviceType", |
| | 1 | 459 | | property.ServiceTypeName); |
| | 1 | 460 | | builder.Append(','); |
| | 1 | 461 | | AppendStringProperty( |
| | 1 | 462 | | builder, |
| | 1 | 463 | | "resolutionKind", |
| | 1 | 464 | | property.Kind.ToString()); |
| | 1 | 465 | | builder.Append('}'); |
| | | 466 | | } |
| | | 467 | | |
| | 1 | 468 | | builder.Append(']'); |
| | 1 | 469 | | } |
| | | 470 | | |
| | | 471 | | private static void AppendOptions( |
| | | 472 | | StringBuilder builder, |
| | | 473 | | IReadOnlyList<DiscoveredOptions> options, |
| | | 474 | | string safeAssemblyName, |
| | | 475 | | bool isAotProject) |
| | | 476 | | { |
| | 12 | 477 | | AppendPropertyName(builder, "options"); |
| | 12 | 478 | | builder.Append('['); |
| | | 479 | | |
| | 28 | 480 | | for (var index = 0; index < options.Count; index++) |
| | | 481 | | { |
| | 2 | 482 | | if (index > 0) |
| | | 483 | | { |
| | 0 | 484 | | builder.Append(','); |
| | | 485 | | } |
| | | 486 | | |
| | 2 | 487 | | var option = options[index]; |
| | 2 | 488 | | var shortTypeName = |
| | 2 | 489 | | GeneratorHelpers.GetShortTypeName(option.TypeName); |
| | 2 | 490 | | var registersGeneratedValidator = |
| | 2 | 491 | | option.ValidateOnStart && option.HasValidatorMethod; |
| | 2 | 492 | | var registersDataAnnotationsValidator = |
| | 2 | 493 | | option.HasDataAnnotations && |
| | 2 | 494 | | (isAotProject |
| | 2 | 495 | | ? !option.RequiresFactoryPattern || |
| | 2 | 496 | | option.ValidateOnStart |
| | 2 | 497 | | : option.ValidateOnStart); |
| | | 498 | | |
| | 2 | 499 | | builder.Append('{'); |
| | 2 | 500 | | AppendStringProperty(builder, "optionsType", option.TypeName); |
| | 2 | 501 | | builder.Append(','); |
| | 2 | 502 | | AppendStringProperty( |
| | 2 | 503 | | builder, |
| | 2 | 504 | | "assemblyName", |
| | 2 | 505 | | option.AssemblyName); |
| | 2 | 506 | | builder.Append(','); |
| | 2 | 507 | | AppendStringProperty( |
| | 2 | 508 | | builder, |
| | 2 | 509 | | "sectionName", |
| | 2 | 510 | | option.SectionName); |
| | 2 | 511 | | builder.Append(','); |
| | 2 | 512 | | AppendNullableStringProperty(builder, "name", option.Name); |
| | 2 | 513 | | builder.Append(','); |
| | 2 | 514 | | AppendStringProperty( |
| | 2 | 515 | | builder, |
| | 2 | 516 | | "bindingMode", |
| | 2 | 517 | | GetOptionsBindingMode(option, isAotProject)); |
| | 2 | 518 | | builder.Append(','); |
| | 2 | 519 | | AppendBooleanProperty( |
| | 2 | 520 | | builder, |
| | 2 | 521 | | "validateOnStart", |
| | 2 | 522 | | option.ValidateOnStart); |
| | 2 | 523 | | builder.Append(','); |
| | 2 | 524 | | AppendNullableStringProperty( |
| | 2 | 525 | | builder, |
| | 2 | 526 | | "generatedValidatorType", |
| | 2 | 527 | | registersGeneratedValidator |
| | 2 | 528 | | ? $"global::{safeAssemblyName}.Generated." + |
| | 2 | 529 | | $"{shortTypeName}Validator" |
| | 2 | 530 | | : null); |
| | 2 | 531 | | builder.Append(','); |
| | 2 | 532 | | AppendNullableStringProperty( |
| | 2 | 533 | | builder, |
| | 2 | 534 | | "generatedDataAnnotationsValidatorType", |
| | 2 | 535 | | registersDataAnnotationsValidator |
| | 2 | 536 | | ? $"global::{safeAssemblyName}.Generated." + |
| | 2 | 537 | | $"{shortTypeName}DataAnnotationsValidator" |
| | 2 | 538 | | : null); |
| | 2 | 539 | | builder.Append(','); |
| | 2 | 540 | | AppendNullableStringProperty( |
| | 2 | 541 | | builder, |
| | 2 | 542 | | "externalValidatorType", |
| | 2 | 543 | | registersGeneratedValidator && |
| | 2 | 544 | | option.HasExternalValidator && |
| | 2 | 545 | | option.ValidatorMethod is { IsStatic: false } |
| | 2 | 546 | | ? option.ValidatorTypeName |
| | 2 | 547 | | : null); |
| | 2 | 548 | | builder.Append('}'); |
| | | 549 | | } |
| | | 550 | | |
| | 12 | 551 | | builder.Append(']'); |
| | 12 | 552 | | } |
| | | 553 | | |
| | | 554 | | private static string GetOptionsBindingMode( |
| | | 555 | | DiscoveredOptions options, |
| | | 556 | | bool isAotProject) |
| | | 557 | | { |
| | 2 | 558 | | if (!isAotProject) |
| | | 559 | | { |
| | 1 | 560 | | return "reflection"; |
| | | 561 | | } |
| | | 562 | | |
| | 1 | 563 | | if (options.IsPositionalRecord) |
| | | 564 | | { |
| | 0 | 565 | | return "aot-positional-record"; |
| | | 566 | | } |
| | | 567 | | |
| | 1 | 568 | | return options.HasInitOnlyProperties |
| | 1 | 569 | | ? "aot-init-only" |
| | 1 | 570 | | : "aot-configure"; |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | private static void AppendHttpClients( |
| | | 574 | | StringBuilder builder, |
| | | 575 | | IReadOnlyList<DiscoveredHttpClient> httpClients) |
| | | 576 | | { |
| | 12 | 577 | | AppendPropertyName(builder, "httpClients"); |
| | 12 | 578 | | builder.Append('['); |
| | | 579 | | |
| | 26 | 580 | | for (var index = 0; index < httpClients.Count; index++) |
| | | 581 | | { |
| | 1 | 582 | | if (index > 0) |
| | | 583 | | { |
| | 0 | 584 | | builder.Append(','); |
| | | 585 | | } |
| | | 586 | | |
| | 1 | 587 | | var client = httpClients[index]; |
| | 1 | 588 | | builder.Append('{'); |
| | 1 | 589 | | AppendStringProperty( |
| | 1 | 590 | | builder, |
| | 1 | 591 | | "optionsType", |
| | 1 | 592 | | client.TypeName); |
| | 1 | 593 | | builder.Append(','); |
| | 1 | 594 | | AppendStringProperty( |
| | 1 | 595 | | builder, |
| | 1 | 596 | | "assemblyName", |
| | 1 | 597 | | client.AssemblyName); |
| | 1 | 598 | | builder.Append(','); |
| | 1 | 599 | | AppendStringProperty( |
| | 1 | 600 | | builder, |
| | 1 | 601 | | "clientName", |
| | 1 | 602 | | client.ClientName); |
| | 1 | 603 | | builder.Append(','); |
| | 1 | 604 | | AppendStringProperty( |
| | 1 | 605 | | builder, |
| | 1 | 606 | | "sectionName", |
| | 1 | 607 | | client.SectionName); |
| | 1 | 608 | | builder.Append(','); |
| | 1 | 609 | | AppendHttpClientCapabilities(builder, client.Capabilities); |
| | 1 | 610 | | builder.Append('}'); |
| | | 611 | | } |
| | | 612 | | |
| | 12 | 613 | | builder.Append(']'); |
| | 12 | 614 | | } |
| | | 615 | | |
| | | 616 | | private static void AppendHttpClientCapabilities( |
| | | 617 | | StringBuilder builder, |
| | | 618 | | HttpClientCapabilities capabilities) |
| | | 619 | | { |
| | 1 | 620 | | AppendPropertyName(builder, "capabilities"); |
| | 1 | 621 | | builder.Append('['); |
| | | 622 | | |
| | 1 | 623 | | var separator = false; |
| | 1 | 624 | | AppendCapability( |
| | 1 | 625 | | builder, |
| | 1 | 626 | | capabilities, |
| | 1 | 627 | | HttpClientCapabilities.Timeout, |
| | 1 | 628 | | "Timeout", |
| | 1 | 629 | | ref separator); |
| | 1 | 630 | | AppendCapability( |
| | 1 | 631 | | builder, |
| | 1 | 632 | | capabilities, |
| | 1 | 633 | | HttpClientCapabilities.UserAgent, |
| | 1 | 634 | | "UserAgent", |
| | 1 | 635 | | ref separator); |
| | 1 | 636 | | AppendCapability( |
| | 1 | 637 | | builder, |
| | 1 | 638 | | capabilities, |
| | 1 | 639 | | HttpClientCapabilities.BaseAddress, |
| | 1 | 640 | | "BaseAddress", |
| | 1 | 641 | | ref separator); |
| | 1 | 642 | | AppendCapability( |
| | 1 | 643 | | builder, |
| | 1 | 644 | | capabilities, |
| | 1 | 645 | | HttpClientCapabilities.Headers, |
| | 1 | 646 | | "Headers", |
| | 1 | 647 | | ref separator); |
| | | 648 | | |
| | 1 | 649 | | builder.Append(']'); |
| | 1 | 650 | | } |
| | | 651 | | |
| | | 652 | | private static void AppendCapability( |
| | | 653 | | StringBuilder builder, |
| | | 654 | | HttpClientCapabilities capabilities, |
| | | 655 | | HttpClientCapabilities capability, |
| | | 656 | | string name, |
| | | 657 | | ref bool separator) |
| | | 658 | | { |
| | 4 | 659 | | if ((capabilities & capability) == 0) |
| | | 660 | | { |
| | 3 | 661 | | return; |
| | | 662 | | } |
| | | 663 | | |
| | 1 | 664 | | if (separator) |
| | | 665 | | { |
| | 0 | 666 | | builder.Append(','); |
| | | 667 | | } |
| | | 668 | | |
| | 1 | 669 | | AppendString(builder, name); |
| | 1 | 670 | | separator = true; |
| | 1 | 671 | | } |
| | | 672 | | |
| | | 673 | | private static void AppendComposedRegistrations( |
| | | 674 | | StringBuilder builder, |
| | | 675 | | IReadOnlyList<DiscoveredComposedRegistration> registrations) |
| | | 676 | | { |
| | 12 | 677 | | AppendPropertyName(builder, "composedRegistrations"); |
| | 12 | 678 | | builder.Append('['); |
| | | 679 | | |
| | 26 | 680 | | for (var index = 0; index < registrations.Count; index++) |
| | | 681 | | { |
| | 1 | 682 | | if (index > 0) |
| | | 683 | | { |
| | 0 | 684 | | builder.Append(','); |
| | | 685 | | } |
| | | 686 | | |
| | 1 | 687 | | var registration = registrations[index]; |
| | 1 | 688 | | builder.Append('{'); |
| | 1 | 689 | | AppendStringProperty( |
| | 1 | 690 | | builder, |
| | 1 | 691 | | "serviceType", |
| | 1 | 692 | | registration.FacadeTypeName); |
| | 1 | 693 | | builder.Append(','); |
| | 1 | 694 | | AppendStringProperty( |
| | 1 | 695 | | builder, |
| | 1 | 696 | | "implementationType", |
| | 1 | 697 | | registration.ClosedCompositionTypeName); |
| | 1 | 698 | | builder.Append(','); |
| | 1 | 699 | | AppendStringProperty( |
| | 1 | 700 | | builder, |
| | 1 | 701 | | "assemblyName", |
| | 1 | 702 | | registration.AssemblyName); |
| | 1 | 703 | | builder.Append(','); |
| | 1 | 704 | | AppendStringProperty( |
| | 1 | 705 | | builder, |
| | 1 | 706 | | "lifetime", |
| | 1 | 707 | | registration.Lifetime.ToString()); |
| | 1 | 708 | | builder.Append(','); |
| | 1 | 709 | | AppendStringArrayProperty( |
| | 1 | 710 | | builder, |
| | 1 | 711 | | "constructorArguments", |
| | 1 | 712 | | registration.ConstructorArguments); |
| | 1 | 713 | | builder.Append('}'); |
| | | 714 | | } |
| | | 715 | | |
| | 12 | 716 | | builder.Append(']'); |
| | 12 | 717 | | } |
| | | 718 | | |
| | | 719 | | private static void AppendPlugins( |
| | | 720 | | StringBuilder builder, |
| | | 721 | | IReadOnlyList<DiscoveredPlugin> plugins) |
| | | 722 | | { |
| | 12 | 723 | | AppendPropertyName(builder, "plugins"); |
| | 12 | 724 | | builder.Append('['); |
| | | 725 | | |
| | 12 | 726 | | var orderedPlugins = plugins |
| | 8 | 727 | | .GroupBy(plugin => plugin.AssemblyName) |
| | 5 | 728 | | .OrderBy(group => group.Key, StringComparer.Ordinal) |
| | 5 | 729 | | .SelectMany(group => group |
| | 8 | 730 | | .OrderBy(plugin => plugin.Order) |
| | 13 | 731 | | .ThenBy(plugin => plugin.TypeName, StringComparer.Ordinal)) |
| | 12 | 732 | | .ToArray(); |
| | | 733 | | |
| | 40 | 734 | | for (var index = 0; index < orderedPlugins.Length; index++) |
| | | 735 | | { |
| | 8 | 736 | | if (index > 0) |
| | | 737 | | { |
| | 3 | 738 | | builder.Append(','); |
| | | 739 | | } |
| | | 740 | | |
| | 8 | 741 | | var plugin = orderedPlugins[index]; |
| | 8 | 742 | | builder.Append('{'); |
| | 8 | 743 | | AppendStringProperty(builder, "pluginType", plugin.TypeName); |
| | 8 | 744 | | builder.Append(','); |
| | 8 | 745 | | AppendStringProperty( |
| | 8 | 746 | | builder, |
| | 8 | 747 | | "assemblyName", |
| | 8 | 748 | | plugin.AssemblyName); |
| | 8 | 749 | | builder.Append(','); |
| | 8 | 750 | | AppendNumberProperty(builder, "order", plugin.Order); |
| | 8 | 751 | | builder.Append(','); |
| | 8 | 752 | | AppendSortedStringArrayProperty( |
| | 8 | 753 | | builder, |
| | 8 | 754 | | "pluginInterfaces", |
| | 8 | 755 | | plugin.InterfaceNames); |
| | 8 | 756 | | builder.Append(','); |
| | 8 | 757 | | AppendSortedStringArrayProperty( |
| | 8 | 758 | | builder, |
| | 8 | 759 | | "attributeTypes", |
| | 8 | 760 | | plugin.AttributeNames); |
| | 8 | 761 | | builder.Append('}'); |
| | | 762 | | } |
| | | 763 | | |
| | 12 | 764 | | builder.Append(']'); |
| | 12 | 765 | | } |
| | | 766 | | |
| | | 767 | | private static void AppendServiceTypesProperty( |
| | | 768 | | StringBuilder builder, |
| | | 769 | | string implementationType, |
| | | 770 | | IReadOnlyList<string> interfaceTypes) |
| | | 771 | | { |
| | 1113 | 772 | | AppendPropertyName(builder, "serviceTypes"); |
| | 1113 | 773 | | builder.Append('['); |
| | 1113 | 774 | | AppendString(builder, implementationType); |
| | | 775 | | |
| | 4438 | 776 | | foreach (var interfaceType in interfaceTypes.OrderBy( |
| | 1106 | 777 | | value => value, |
| | 1113 | 778 | | StringComparer.Ordinal)) |
| | | 779 | | { |
| | 1106 | 780 | | builder.Append(','); |
| | 1106 | 781 | | AppendString(builder, interfaceType); |
| | | 782 | | } |
| | | 783 | | |
| | 1113 | 784 | | builder.Append(']'); |
| | 1113 | 785 | | } |
| | | 786 | | |
| | | 787 | | private static void AppendConstructorParameters( |
| | | 788 | | StringBuilder builder, |
| | | 789 | | string propertyName, |
| | | 790 | | IReadOnlyList<TypeDiscoveryHelper.ConstructorParameterInfo> parameters) |
| | | 791 | | { |
| | 1116 | 792 | | AppendPropertyName(builder, propertyName); |
| | 1116 | 793 | | builder.Append('['); |
| | | 794 | | |
| | 2240 | 795 | | for (var index = 0; index < parameters.Count; index++) |
| | | 796 | | { |
| | 4 | 797 | | if (index > 0) |
| | | 798 | | { |
| | 0 | 799 | | builder.Append(','); |
| | | 800 | | } |
| | | 801 | | |
| | 4 | 802 | | var parameter = parameters[index]; |
| | 4 | 803 | | builder.Append('{'); |
| | 4 | 804 | | AppendNullableStringProperty( |
| | 4 | 805 | | builder, |
| | 4 | 806 | | "name", |
| | 4 | 807 | | parameter.ParameterName); |
| | 4 | 808 | | builder.Append(','); |
| | 4 | 809 | | AppendStringProperty(builder, "type", parameter.TypeName); |
| | 4 | 810 | | builder.Append(','); |
| | 4 | 811 | | AppendNullableStringProperty( |
| | 4 | 812 | | builder, |
| | 4 | 813 | | "serviceKey", |
| | 4 | 814 | | parameter.ServiceKey); |
| | 4 | 815 | | builder.Append('}'); |
| | | 816 | | } |
| | | 817 | | |
| | 1116 | 818 | | builder.Append(']'); |
| | 1116 | 819 | | } |
| | | 820 | | |
| | | 821 | | private static void AppendStringArrayProperty( |
| | | 822 | | StringBuilder builder, |
| | | 823 | | string propertyName, |
| | | 824 | | IReadOnlyList<string> values) |
| | | 825 | | { |
| | 1144 | 826 | | AppendPropertyName(builder, propertyName); |
| | 1144 | 827 | | builder.Append('['); |
| | | 828 | | |
| | 2328 | 829 | | for (var index = 0; index < values.Count; index++) |
| | | 830 | | { |
| | 20 | 831 | | if (index > 0) |
| | | 832 | | { |
| | 4 | 833 | | builder.Append(','); |
| | | 834 | | } |
| | | 835 | | |
| | 20 | 836 | | AppendString(builder, values[index]); |
| | | 837 | | } |
| | | 838 | | |
| | 1144 | 839 | | builder.Append(']'); |
| | 1144 | 840 | | } |
| | | 841 | | |
| | | 842 | | private static void AppendSortedStringArrayProperty( |
| | | 843 | | StringBuilder builder, |
| | | 844 | | string propertyName, |
| | | 845 | | IEnumerable<string> values) |
| | | 846 | | { |
| | 1131 | 847 | | AppendStringArrayProperty( |
| | 1131 | 848 | | builder, |
| | 1131 | 849 | | propertyName, |
| | 1131 | 850 | | values.OrderBy( |
| | 7 | 851 | | value => value, |
| | 1131 | 852 | | StringComparer.Ordinal) |
| | 1131 | 853 | | .ToArray()); |
| | 1131 | 854 | | } |
| | | 855 | | |
| | | 856 | | private static void AppendStringProperty( |
| | | 857 | | StringBuilder builder, |
| | | 858 | | string propertyName, |
| | | 859 | | string value) |
| | | 860 | | { |
| | 3453 | 861 | | AppendPropertyName(builder, propertyName); |
| | 3453 | 862 | | AppendString(builder, value); |
| | 3453 | 863 | | } |
| | | 864 | | |
| | | 865 | | private static void AppendNullableStringProperty( |
| | | 866 | | StringBuilder builder, |
| | | 867 | | string propertyName, |
| | | 868 | | string? value) |
| | | 869 | | { |
| | 18 | 870 | | AppendPropertyName(builder, propertyName); |
| | 18 | 871 | | if (value is null) |
| | | 872 | | { |
| | 12 | 873 | | builder.Append("null"); |
| | 12 | 874 | | return; |
| | | 875 | | } |
| | | 876 | | |
| | 6 | 877 | | AppendString(builder, value); |
| | 6 | 878 | | } |
| | | 879 | | |
| | | 880 | | private static void AppendBooleanProperty( |
| | | 881 | | StringBuilder builder, |
| | | 882 | | string propertyName, |
| | | 883 | | bool value) |
| | | 884 | | { |
| | 16 | 885 | | AppendPropertyName(builder, propertyName); |
| | 16 | 886 | | builder.Append(value ? "true" : "false"); |
| | 16 | 887 | | } |
| | | 888 | | |
| | | 889 | | private static void AppendNumberProperty( |
| | | 890 | | StringBuilder builder, |
| | | 891 | | string propertyName, |
| | | 892 | | int value) |
| | | 893 | | { |
| | 9 | 894 | | AppendPropertyName(builder, propertyName); |
| | 9 | 895 | | builder.Append(GeneratorHelpers.Literal(value)); |
| | 9 | 896 | | } |
| | | 897 | | |
| | | 898 | | private static void AppendPropertyName( |
| | | 899 | | StringBuilder builder, |
| | | 900 | | string propertyName) |
| | | 901 | | { |
| | 7004 | 902 | | AppendString(builder, propertyName); |
| | 7004 | 903 | | builder.Append(':'); |
| | 7004 | 904 | | } |
| | | 905 | | |
| | | 906 | | private static void AppendString(StringBuilder builder, string value) |
| | | 907 | | { |
| | | 908 | | const string Hex = "0123456789abcdef"; |
| | | 909 | | |
| | 12703 | 910 | | builder.Append('"'); |
| | 459176 | 911 | | foreach (var character in value) |
| | | 912 | | { |
| | | 913 | | switch (character) |
| | | 914 | | { |
| | | 915 | | case '"': |
| | 1 | 916 | | builder.Append("\\\""); |
| | 1 | 917 | | break; |
| | | 918 | | case '\\': |
| | 0 | 919 | | builder.Append("\\\\"); |
| | 0 | 920 | | break; |
| | | 921 | | case '\b': |
| | 0 | 922 | | builder.Append("\\b"); |
| | 0 | 923 | | break; |
| | | 924 | | case '\f': |
| | 0 | 925 | | builder.Append("\\f"); |
| | 0 | 926 | | break; |
| | | 927 | | case '\n': |
| | 1 | 928 | | builder.Append("\\n"); |
| | 1 | 929 | | break; |
| | | 930 | | case '\r': |
| | 0 | 931 | | builder.Append("\\r"); |
| | 0 | 932 | | break; |
| | | 933 | | case '\t': |
| | 0 | 934 | | builder.Append("\\t"); |
| | 0 | 935 | | break; |
| | | 936 | | default: |
| | 216883 | 937 | | if (character < ' ' || |
| | 216883 | 938 | | char.IsSurrogate(character) || |
| | 216883 | 939 | | character == '\u0085' || |
| | 216883 | 940 | | character == '\u2028' || |
| | 216883 | 941 | | character == '\u2029') |
| | | 942 | | { |
| | 1 | 943 | | AppendUnicodeEscape(builder, character, Hex); |
| | | 944 | | } |
| | | 945 | | else |
| | | 946 | | { |
| | 216882 | 947 | | builder.Append(character); |
| | | 948 | | } |
| | | 949 | | break; |
| | | 950 | | } |
| | | 951 | | } |
| | 12703 | 952 | | builder.Append('"'); |
| | 12703 | 953 | | } |
| | | 954 | | |
| | | 955 | | private static void AppendUnicodeEscape( |
| | | 956 | | StringBuilder builder, |
| | | 957 | | char character, |
| | | 958 | | string hex) |
| | | 959 | | { |
| | 1 | 960 | | builder.Append("\\u"); |
| | 1 | 961 | | builder.Append(hex[(character >> 12) & 0x0f]); |
| | 1 | 962 | | builder.Append(hex[(character >> 8) & 0x0f]); |
| | 1 | 963 | | builder.Append(hex[(character >> 4) & 0x0f]); |
| | 1 | 964 | | builder.Append(hex[character & 0x0f]); |
| | 1 | 965 | | } |
| | | 966 | | } |