| | | 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 static <c>_plugins</c> array of <see cref="DiscoveredPlugin"/> |
| | | 14 | | /// entries for the compile-time type registry. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class PluginsCodeGenerator |
| | | 17 | | { |
| | | 18 | | internal static void GeneratePluginTypesArray(StringBuilder builder, IReadOnlyList<DiscoveredPlugin> plugins, Breadc |
| | | 19 | | { |
| | 455 | 20 | | builder.AppendLine(" private static readonly PluginTypeInfo[] _plugins ="); |
| | 455 | 21 | | builder.AppendLine(" ["); |
| | | 22 | | |
| | | 23 | | // Sort plugins by Order first, then by TypeName for determinism |
| | 455 | 24 | | var sortedPlugins = plugins |
| | 1382 | 25 | | .OrderBy(p => p.Order) |
| | 1382 | 26 | | .ThenBy(p => p.TypeName, StringComparer.Ordinal) |
| | 455 | 27 | | .ToList(); |
| | | 28 | | |
| | | 29 | | // Group for breadcrumb display, but maintain the sorted order |
| | 2423 | 30 | | var pluginsByAssembly = sortedPlugins.GroupBy(p => p.AssemblyName).OrderBy(g => g.Key); |
| | | 31 | | |
| | 2034 | 32 | | foreach (var group in pluginsByAssembly) |
| | | 33 | | { |
| | 562 | 34 | | breadcrumbs.WriteInlineComment(builder, " ", $"From {group.Key}"); |
| | | 35 | | |
| | | 36 | | // Maintain order within assembly group |
| | 6748 | 37 | | foreach (var plugin in group.OrderBy(p => p.Order).ThenBy(p => p.TypeName, StringComparer.Ordinal)) |
| | | 38 | | { |
| | | 39 | | // Write verbose breadcrumb for this plugin |
| | 1406 | 40 | | if (breadcrumbs.Level == BreadcrumbLevel.Verbose) |
| | | 41 | | { |
| | 91 | 42 | | var sourcePath = plugin.SourceFilePath != null |
| | 91 | 43 | | ? BreadcrumbWriter.GetRelativeSourcePath(plugin.SourceFilePath, projectDirectory) |
| | 91 | 44 | | : $"[{plugin.AssemblyName}]"; |
| | 91 | 45 | | var interfaces = plugin.InterfaceNames.Length > 0 |
| | 91 | 46 | | ? string.Join(", ", plugin.InterfaceNames.Select(i => i.Split('.').Last())) |
| | 91 | 47 | | : "none"; |
| | 91 | 48 | | var orderInfo = plugin.Order != 0 ? $"Order: {plugin.Order}" : "Order: 0 (default)"; |
| | | 49 | | |
| | 91 | 50 | | breadcrumbs.WriteVerboseBox(builder, " ", |
| | 91 | 51 | | $"Plugin: {plugin.TypeName.Split('.').Last()}", |
| | 91 | 52 | | $"Source: {sourcePath}", |
| | 91 | 53 | | $"Implements: {interfaces}", |
| | 91 | 54 | | orderInfo); |
| | | 55 | | } |
| | 1315 | 56 | | else if (breadcrumbs.Level == BreadcrumbLevel.Minimal && plugin.Order != 0) |
| | | 57 | | { |
| | | 58 | | // Show order in minimal mode only if non-default |
| | 0 | 59 | | breadcrumbs.WriteInlineComment(builder, " ", $"{plugin.TypeName.Split('.').Last()} (Order: {p |
| | | 60 | | } |
| | | 61 | | |
| | 1406 | 62 | | builder.Append($" new(typeof({plugin.TypeName}), "); |
| | | 63 | | |
| | | 64 | | // Interfaces |
| | 1406 | 65 | | if (plugin.InterfaceNames.Length == 0) |
| | | 66 | | { |
| | 0 | 67 | | builder.Append("Array.Empty<Type>(), "); |
| | | 68 | | } |
| | | 69 | | else |
| | | 70 | | { |
| | 1406 | 71 | | builder.Append("["); |
| | 2817 | 72 | | builder.Append(string.Join(", ", plugin.InterfaceNames.Select(i => $"typeof({i})"))); |
| | 1406 | 73 | | builder.Append("], "); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // Factory lambda - no Activator.CreateInstance needed |
| | 1406 | 77 | | builder.Append($"() => new {plugin.TypeName}(), "); |
| | | 78 | | |
| | | 79 | | // Attributes |
| | 1406 | 80 | | if (plugin.AttributeNames.Length == 0) |
| | | 81 | | { |
| | 1359 | 82 | | builder.Append("Array.Empty<Type>(), "); |
| | | 83 | | } |
| | | 84 | | else |
| | | 85 | | { |
| | 47 | 86 | | builder.Append("["); |
| | 108 | 87 | | builder.Append(string.Join(", ", plugin.AttributeNames.Select(a => $"typeof({a})"))); |
| | 47 | 88 | | builder.Append("], "); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | // Order |
| | 1406 | 92 | | builder.AppendLine($"{plugin.Order}),"); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 455 | 96 | | builder.AppendLine(" ];"); |
| | 455 | 97 | | } |
| | | 98 | | } |