< Summary

Information
Class: NexusLabs.Needlr.Generators.CodeGen.EmptyTypeRegistryCodeGenerator
Assembly: NexusLabs.Needlr.Generators
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators/CodeGen/EmptyTypeRegistryCodeGenerator.cs
Line coverage
100%
Covered lines: 48
Uncovered lines: 0
Coverable lines: 48
Total lines: 106
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GenerateTypeRegistrySource(...)100%11100%
GenerateBootstrapSource(...)100%11100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators/CodeGen/EmptyTypeRegistryCodeGenerator.cs

#LineLine coverage
 1// Copyright (c) NexusLabs. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Text;
 5
 6using NexusLabs.Needlr.Generators.Models;
 7
 8namespace NexusLabs.Needlr.Generators.CodeGen;
 9
 10/// <summary>
 11/// Emits a minimal <c>TypeRegistry</c> and module-initializer bootstrap for an assembly that
 12/// declares <c>[GenerateTypeRegistry]</c> but has nothing to register.
 13/// </summary>
 14/// <remarks>
 15/// The emitted code depends only on the attributes package (<c>InjectableTypeInfo</c>,
 16/// <c>PluginTypeInfo</c>, and <c>NeedlrSourceGenBootstrap</c>) — never on the injection packages.
 17/// A project must reference the attributes package to use <c>[GenerateTypeRegistry]</c> at all, so
 18/// this registry always compiles, even for a project that references no Needlr injection packages
 19/// (a documentation, contracts, or abstractions library). Emitting it keeps a type-less participant
 20/// force-loadable by consumers (<c>typeof(&lt;Assembly&gt;.Generated.TypeRegistry)</c>) without the
 21/// <c>CS0234</c> failure that omitting it would cause.
 22/// </remarks>
 23internal static class EmptyTypeRegistryCodeGenerator
 24{
 25    /// <summary>
 26    /// Emits the empty <c>TypeRegistry</c> exposing empty injectable and plugin providers.
 27    /// </summary>
 28    /// <param name="registrationPlan">The normalized registration plan for the assembly.</param>
 29    /// <param name="breadcrumbs">The breadcrumb writer supplied by the orchestration method.</param>
 30    /// <returns>The generated C# source for the empty <c>TypeRegistry</c>.</returns>
 31    internal static string GenerateTypeRegistrySource(
 32        GeneratedRegistrationPlan registrationPlan,
 33        BreadcrumbWriter breadcrumbs)
 34    {
 1135        var builder = new StringBuilder();
 1136        var assemblyName = registrationPlan.AssemblyName;
 1137        var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier(assemblyName);
 38
 1139        breadcrumbs.WriteFileHeader(builder, assemblyName, "Needlr Type Registry (empty)");
 1140        builder.AppendLine("#nullable enable");
 1141        builder.AppendLine();
 1142        builder.AppendLine($"namespace {safeAssemblyName}.Generated;");
 1143        builder.AppendLine();
 1144        builder.AppendLine("/// <summary>");
 1145        builder.AppendLine("/// Compile-time generated registry for an assembly that declares [GenerateTypeRegistry]");
 1146        builder.AppendLine("/// but contains no registerable types. Exposes empty providers so that consumers which");
 1147        builder.AppendLine("/// force-load this type compile, without depending on the Needlr injection packages.");
 1148        builder.AppendLine("/// </summary>");
 1149        builder.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.Generators\", \"1
 1150        builder.AppendLine("public static class TypeRegistry");
 1151        builder.AppendLine("{");
 1152        builder.AppendLine("    private static readonly global::NexusLabs.Needlr.Generators.InjectableTypeInfo[] _types 
 1153        builder.AppendLine("    private static readonly global::NexusLabs.Needlr.Generators.PluginTypeInfo[] _plugins = 
 1154        builder.AppendLine();
 1155        builder.AppendLine("    /// <summary>Gets all injectable types discovered at compile time (none).</summary>");
 1156        builder.AppendLine("    /// <returns>An empty read-only list.</returns>");
 1157        builder.AppendLine("    public static global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.
 1158        builder.AppendLine();
 1159        builder.AppendLine("    /// <summary>Gets all plugin types discovered at compile time (none).</summary>");
 1160        builder.AppendLine("    /// <returns>An empty read-only list.</returns>");
 1161        builder.AppendLine("    public static global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.
 1162        builder.AppendLine("}");
 63
 1164        return builder.ToString();
 65    }
 66
 67    /// <summary>
 68    /// Emits the module-initializer bootstrap that registers the empty providers with
 69    /// <c>NeedlrSourceGenBootstrap</c>.
 70    /// </summary>
 71    /// <param name="registrationPlan">The normalized registration plan for the assembly.</param>
 72    /// <param name="breadcrumbs">The breadcrumb writer supplied by the orchestration method.</param>
 73    /// <returns>The generated C# source for the module-initializer bootstrap.</returns>
 74    /// <remarks>
 75    /// Uses the registry-identity <c>Register</c> overload (no decorator applier, no options registrar),
 76    /// which takes no <c>IServiceCollection</c>/<c>IConfiguration</c> parameters and therefore keeps
 77    /// the emitted code free of any Microsoft.Extensions.DependencyInjection dependency.
 78    /// </remarks>
 79    internal static string GenerateBootstrapSource(
 80        GeneratedRegistrationPlan registrationPlan,
 81        BreadcrumbWriter breadcrumbs)
 82    {
 1183        var builder = new StringBuilder();
 1184        var assemblyName = registrationPlan.AssemblyName;
 1185        var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier(assemblyName);
 86
 1187        breadcrumbs.WriteFileHeader(builder, assemblyName, "Needlr Source-Gen Bootstrap (empty)");
 1188        builder.AppendLine("#nullable enable");
 1189        builder.AppendLine();
 1190        builder.AppendLine($"namespace {safeAssemblyName}.Generated;");
 1191        builder.AppendLine();
 1192        builder.AppendLine("internal static class NeedlrSourceGenModuleInitializer");
 1193        builder.AppendLine("{");
 1194        builder.AppendLine("    [global::System.Runtime.CompilerServices.ModuleInitializer]");
 1195        builder.AppendLine("    internal static void Initialize()");
 1196        builder.AppendLine("    {");
 1197        builder.AppendLine("        global::NexusLabs.Needlr.Generators.NeedlrSourceGenBootstrap.Register(");
 1198        builder.AppendLine($"            typeof(global::{safeAssemblyName}.Generated.TypeRegistry),");
 1199        builder.AppendLine($"            global::{safeAssemblyName}.Generated.TypeRegistry.GetInjectableTypes,");
 11100        builder.AppendLine($"            global::{safeAssemblyName}.Generated.TypeRegistry.GetPluginTypes);");
 11101        builder.AppendLine("    }");
 11102        builder.AppendLine("}");
 103
 11104        return builder.ToString();
 105    }
 106}