AI-Studio/app/SourceGeneratedMappings/MappingRegistryGenerator.cs
nilskruthoff f6a128f2e4
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,deb,updater, appimage,deb) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,deb,updater, appimage,deb) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Added assistant plugins (#659)
2026-04-09 10:01:24 +02:00

130 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace SourceGeneratedMappings;
[Generator]
public sealed class MappingRegistryGenerator : IIncrementalGenerator
{
private const string GENERATED_NAMESPACE = "AIStudio.Tools.PluginSystem.Assistants.Icons";
private const string ROOT_TYPE_NAME = "MudBlazor.Icons";
private static readonly string[] ALLOWED_GROUP_PATHS = ["Material.Filled", "Material.Outlined"];
private static readonly DiagnosticDescriptor ROOT_TYPE_MISSING = new(
id: "MBI001",
title: "MudBlazor icon root type was not found",
messageFormat: "The generator could not find '{0}' in the current compilation references. No icon registry was generated.",
category: "SourceGeneration",
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private static readonly DiagnosticDescriptor NO_ICONS_FOUND = new(
id: "MBI002",
title: "No MudBlazor icons were discovered",
messageFormat: "The generator found '{0}', but no nested icon constants were discovered below it.",
category: "SourceGeneration",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterSourceOutput(context.CompilationProvider, Generate);
}
private static void Generate(SourceProductionContext context, Compilation compilation)
{
var rootType = compilation.GetTypeByMetadataName(ROOT_TYPE_NAME);
if (rootType is null)
{
context.ReportDiagnostic(Diagnostic.Create(ROOT_TYPE_MISSING, Location.None, ROOT_TYPE_NAME));
return;
}
var icons = new List<IconDefinition>();
CollectIcons(rootType, [], icons);
if (icons.Count == 0)
{
context.ReportDiagnostic(Diagnostic.Create(NO_ICONS_FOUND, Location.None, ROOT_TYPE_NAME));
return;
}
var source = RenderSource(icons);
context.AddSource("MudBlazorIconRegistry.g.cs", SourceText.From(source, Encoding.UTF8));
}
private static void CollectIcons(INamedTypeSymbol currentType, List<string> path, List<IconDefinition> icons)
{
foreach (var nestedType in currentType.GetTypeMembers().OrderBy(static t => t.Name, StringComparer.Ordinal))
{
path.Add(nestedType.Name);
CollectIcons(nestedType, path, icons);
path.RemoveAt(path.Count - 1);
}
foreach (var field in currentType.GetMembers().OfType<IFieldSymbol>().OrderBy(static f => f.Name, StringComparer.Ordinal))
{
if (!field.IsConst || field.Type.SpecialType != SpecialType.System_String || field.ConstantValue is not string svg)
continue;
if (path.Count == 0)
continue;
var groupPath = string.Join(".", path);
if (!ALLOWED_GROUP_PATHS.Contains(groupPath, StringComparer.Ordinal))
continue;
icons.Add(new IconDefinition(
QualifiedName: $"Icons.{groupPath}.{field.Name}",
Svg: svg));
}
}
private static string RenderSource(IReadOnlyList<IconDefinition> icons)
{
var builder = new StringBuilder();
builder.AppendLine("// <auto-generated />");
builder.AppendLine("#nullable enable");
builder.AppendLine("using System;");
builder.AppendLine("using System.Collections.Generic;");
builder.AppendLine();
builder.Append("namespace ").Append(GENERATED_NAMESPACE).AppendLine(";");
builder.AppendLine();
builder.AppendLine("public static class MudBlazorIconRegistry");
builder.AppendLine("{");
builder.AppendLine(" public static readonly IReadOnlyDictionary<string, string> SvgByIdentifier = new Dictionary<string, string>(StringComparer.Ordinal)");
builder.AppendLine(" {");
foreach (var icon in icons)
{
builder.Append(" [")
.Append(ToLiteral(icon.QualifiedName))
.Append("] = ")
.Append(ToLiteral(icon.Svg))
.AppendLine(",");
}
builder.AppendLine(" };");
builder.AppendLine();
builder.AppendLine(" public static bool TryGetSvg(string identifier, out string svg)");
builder.AppendLine(" {");
builder.AppendLine(" return SvgByIdentifier.TryGetValue(identifier, out svg!);");
builder.AppendLine(" }");
builder.AppendLine("}");
return builder.ToString();
}
private static string ToLiteral(string value)
{
return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(value, quote: true);
}
private sealed record IconDefinition(string QualifiedName, string Svg);
}