| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | using NexusLabs.Needlr.Generators.Models; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.Generators.CodeGen; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Generates the <c>RegisterComposedTypes(IServiceCollection)</c> method that registers each open generic |
| | | 14 | | /// composition type closed over the type argument(s) of every discovered implementation of its designated |
| | | 15 | | /// open generic interface, exposed as the designated facade service type. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class ComposedRegistrationsCodeGenerator |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Emits the <c>RegisterComposedTypes(IServiceCollection)</c> method body into the TypeRegistry class. |
| | | 21 | | /// </summary> |
| | | 22 | | internal static void GenerateRegisterComposedTypesMethod( |
| | | 23 | | StringBuilder builder, |
| | | 24 | | IReadOnlyList<DiscoveredComposedRegistration> registrations, |
| | | 25 | | BreadcrumbWriter breadcrumbs, |
| | | 26 | | string? projectDirectory) |
| | | 27 | | { |
| | 42 | 28 | | builder.AppendLine(" /// <summary>"); |
| | 42 | 29 | | builder.AppendLine(" /// Registers each composition type closed over the type argument(s) of every discovered |
| | 42 | 30 | | builder.AppendLine(" /// implementation of its designated open generic interface, exposed as the designated f |
| | 42 | 31 | | builder.AppendLine(" /// </summary>"); |
| | 42 | 32 | | builder.AppendLine(" /// <param name=\"services\">The service collection to register to.</param>"); |
| | 42 | 33 | | builder.AppendLine(" private static void RegisterComposedTypes(IServiceCollection services)"); |
| | 42 | 34 | | builder.AppendLine(" {"); |
| | | 35 | | |
| | 192 | 36 | | foreach (var registration in registrations) |
| | | 37 | | { |
| | 54 | 38 | | var addMethod = GetAddMethod(registration.Lifetime); |
| | 54 | 39 | | var closedShortName = registration.ClosedCompositionTypeName.Split('.').Last(); |
| | 54 | 40 | | var facadeShortName = registration.FacadeTypeName.Split('.').Last(); |
| | 54 | 41 | | var sourcePath = registration.SourceFilePath != null |
| | 54 | 42 | | ? BreadcrumbWriter.GetRelativeSourcePath(registration.SourceFilePath, projectDirectory) |
| | 54 | 43 | | : $"[{registration.AssemblyName}]"; |
| | | 44 | | |
| | 54 | 45 | | breadcrumbs.WriteInlineComment(builder, " ", $"Composed: {closedShortName} as {facadeShortName} ← {so |
| | | 46 | | |
| | 54 | 47 | | if (registration.ConstructorArguments.Count == 0) |
| | | 48 | | { |
| | 0 | 49 | | builder.AppendLine($" services.{addMethod}<{registration.FacadeTypeName}>(sp => new {registration |
| | 0 | 50 | | continue; |
| | | 51 | | } |
| | | 52 | | |
| | 54 | 53 | | builder.AppendLine($" services.{addMethod}<{registration.FacadeTypeName}>(sp => new {registration.Clo |
| | | 54 | | |
| | 230 | 55 | | for (var i = 0; i < registration.ConstructorArguments.Count; i++) |
| | | 56 | | { |
| | 61 | 57 | | var isLast = i == registration.ConstructorArguments.Count - 1; |
| | 61 | 58 | | builder.AppendLine($" {registration.ConstructorArguments[i]}{(isLast ? "));" : ",")}"); |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 42 | 62 | | builder.AppendLine(" }"); |
| | 42 | 63 | | } |
| | | 64 | | |
| | 54 | 65 | | private static string GetAddMethod(GeneratorLifetime lifetime) => lifetime switch |
| | 54 | 66 | | { |
| | 2 | 67 | | GeneratorLifetime.Scoped => "AddScoped", |
| | 2 | 68 | | GeneratorLifetime.Transient => "AddTransient", |
| | 50 | 69 | | _ => "AddSingleton", |
| | 54 | 70 | | }; |
| | | 71 | | } |