74 lines
3.2 KiB
C#
74 lines
3.2 KiB
C#
// -----------------------------------------------------------------------------
|
|
// file="PanelStateIncrementalGenerator.Emitter"
|
|
// Project: StgSharp
|
|
// Copyright (c) Nitload.
|
|
// SPDX-License-Identifier: MIT
|
|
// -----------------------------------------------------------------------------
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.Text;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace StgSharp.UserInterface.Generator
|
|
{
|
|
internal static class PanelStateIncrementalGeneratorEmitter
|
|
{
|
|
|
|
public static void Emit(
|
|
SourceProductionContext context,
|
|
PanelStateIncrementalGeneratorOutput source
|
|
)
|
|
{
|
|
if (source.TypeAnalyses.IsDefaultOrEmpty) {
|
|
return;
|
|
}
|
|
|
|
StringBuilder builder = new();
|
|
_ = builder.AppendLine("// <auto-generated />");
|
|
_ = builder.AppendLine("#nullable enable");
|
|
_ = builder.AppendLine();
|
|
_ = builder.AppendLine("namespace StgSharp.UserInterface.Generator");
|
|
_ = builder.AppendLine("{");
|
|
_ = builder.AppendLine(" internal static class PanelStateIncrementalGeneratorManifest");
|
|
_ = builder.AppendLine(" {");
|
|
_ = builder.AppendLine(" /*");
|
|
_ = builder.AppendLine(" internal static string[] DiscoveredTypes { get; } = new string[]");
|
|
_ = builder.AppendLine(" {");
|
|
|
|
foreach (string typeName in source.TypeAnalyses
|
|
.Select(static analysis => analysis.Target.TypeDisplayName)
|
|
.Distinct()
|
|
.OrderBy(static name => name, StringComparer.Ordinal))
|
|
{
|
|
_ = builder.Append(" \"");
|
|
_ = builder.Append(typeName.Replace("\"", "\"\"", StringComparison.Ordinal));
|
|
_ = builder.AppendLine("\",");
|
|
}
|
|
|
|
_ = builder.AppendLine(" };");
|
|
_ = builder.AppendLine();
|
|
_ = builder.AppendLine(" internal static string[] AnalysisSummary { get; } = new string[]");
|
|
_ = builder.AppendLine(" {");
|
|
|
|
foreach (PanelStateIncrementalGeneratorTypeAnalysis analysis in source.TypeAnalyses
|
|
.OrderBy(static item => item.Target.TypeDisplayName, StringComparer.Ordinal))
|
|
{
|
|
string summary = $"{analysis.Target.TypeDisplayName} | symbols={analysis.StateSymbols.Members.Length} | bindings={analysis.StateChangeBindings.Members.Length} | handlers={analysis.StateChangeCommandHandlers.Members.Length}";
|
|
_ = builder.Append(" \"");
|
|
_ = builder.Append(summary.Replace("\"", "\"\"", StringComparison.Ordinal));
|
|
_ = builder.AppendLine("\",");
|
|
}
|
|
|
|
_ = builder.AppendLine(" };");
|
|
_ = builder.AppendLine(" }");
|
|
_ = builder.AppendLine("}");
|
|
|
|
context.AddSource(
|
|
"PanelStateIncrementalGeneratorManifest.g.cs",
|
|
SourceText.From(builder.ToString(), Encoding.UTF8));
|
|
}
|
|
|
|
}
|
|
}
|