Files
Aaru/Aaru.Generators/PluginRegisterGenerator.cs

169 lines
7.5 KiB
C#
Raw Normal View History

2025-08-14 17:38:51 +01:00
#nullable enable
using System.Collections.Generic;
2025-08-14 17:38:51 +01:00
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
2025-08-14 17:38:51 +01:00
using Microsoft.CodeAnalysis.Text;
namespace Aaru.Generators;
[Generator]
2025-08-14 17:38:51 +01:00
public sealed class PluginRegisterGenerator : IIncrementalGenerator
{
2025-08-14 17:38:51 +01:00
private static readonly Dictionary<string, string> PluginInterfaces = new()
{
["IArchive"] = "RegisterArchivePlugins",
["IChecksum"] = "RegisterChecksumPlugins",
["IFilesystem"] = "RegisterFilesystemPlugins",
["IFilter"] = "RegisterFilterPlugins",
["IFloppyImage"] = "RegisterFloppyImagePlugins",
["IMediaImage"] = "RegisterMediaImagePlugins",
["IPartition"] = "RegisterPartitionPlugins",
["IReadOnlyFilesystem"] = "RegisterReadOnlyFilesystemPlugins",
["IWritableFloppyImage"] = "RegisterWritableFloppyImagePlugins",
["IWritableImage"] = "RegisterWritableImagePlugins",
["IByteAddressableImage"] = "RegisterByteAddressablePlugins",
["IFluxImage"] = "RegisterFluxImagePlugins",
["IWritableFluxImage"] = "RegisterWritableFluxImagePlugins"
};
#region IIncrementalGenerator Members
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValueProvider<ImmutableArray<PluginInfo>> pluginClasses = context.SyntaxProvider
.CreateSyntaxProvider(static (node, _) => node is ClassDeclarationSyntax,
static (ctx, _) => GetPluginInfo(ctx))
.Where(static info => info is not null)
.Collect();
2023-10-03 23:24:05 +01:00
2025-08-14 17:38:51 +01:00
context.RegisterSourceOutput(pluginClasses, (ctx, pluginInfos) => GeneratePluginRegister(ctx, pluginInfos!));
}
2025-08-14 17:38:51 +01:00
#endregion
2025-08-14 17:38:51 +01:00
private static PluginInfo? GetPluginInfo(GeneratorSyntaxContext context)
{
2025-08-14 17:38:51 +01:00
if(context.Node is not ClassDeclarationSyntax classDecl) return null;
var info = new PluginInfo
{
ClassName = classDecl.Identifier.Text,
Namespace = GetNamespace(classDecl),
IsRegister = ImplementsInterface(classDecl, "IPluginRegister")
};
foreach(string? iface in PluginInterfaces.Keys)
{
if(ImplementsInterface(classDecl, iface)) info.Interfaces.Add(iface);
}
2025-08-14 17:38:51 +01:00
if(info is { IsRegister: false, Interfaces.Count: 0 }) return null;
2025-08-14 17:38:51 +01:00
return info;
}
2025-08-14 17:38:51 +01:00
private static bool ImplementsInterface(ClassDeclarationSyntax classDecl, string interfaceName)
{
return classDecl.BaseList?.Types.Any(t => (t.Type as IdentifierNameSyntax)?.Identifier.ValueText ==
interfaceName) ==
true;
}
2025-08-14 17:38:51 +01:00
private static string? GetNamespace(SyntaxNode node) =>
node.Ancestors().OfType<BaseNamespaceDeclarationSyntax>().FirstOrDefault()?.Name.ToString();
2025-08-14 17:38:51 +01:00
private static void GeneratePluginRegister(SourceProductionContext context, IReadOnlyList<PluginInfo> pluginInfos)
{
PluginInfo? registerClass = pluginInfos.FirstOrDefault(p => p.IsRegister);
2025-08-14 17:38:51 +01:00
if(registerClass is null) return;
2025-08-14 17:38:51 +01:00
var sb = new StringBuilder();
2023-10-04 08:26:35 +01:00
sb.AppendLine("""
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Register.g.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Registers all plugins in this assembly.
//
// --[ License ] --------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2023-10-04 08:26:35 +01:00
// ****************************************************************************/
""");
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using Microsoft.Extensions.DependencyInjection;");
2025-08-14 17:38:51 +01:00
sb.AppendLine("using Aaru.CommonTypes.Interfaces;");
sb.AppendLine();
2025-08-14 17:38:51 +01:00
sb.AppendLine($"namespace {registerClass.Namespace};");
sb.AppendLine();
2025-08-14 17:38:51 +01:00
sb.AppendLine($"public sealed partial class {registerClass.ClassName} : IPluginRegister");
sb.AppendLine("{");
2025-08-14 17:38:51 +01:00
foreach(KeyValuePair<string, string> kvp in PluginInterfaces)
{
2025-08-14 17:38:51 +01:00
string? interfaceName = kvp.Key;
string? methodName = kvp.Value;
2025-08-14 17:38:51 +01:00
var plugins = pluginInfos.Where(p => p.Interfaces.Contains(interfaceName))
.Select(p => p.ClassName)
.Distinct()
.ToList();
2025-08-14 17:38:51 +01:00
sb.AppendLine($" public void {methodName}(IServiceCollection services)");
sb.AppendLine(" {");
2025-08-14 17:38:51 +01:00
foreach(string? plugin in plugins)
sb.AppendLine($" services.AddTransient<{interfaceName}, {plugin}>();");
sb.AppendLine(" }");
}
sb.AppendLine("}");
2025-08-14 17:38:51 +01:00
context.AddSource("Register.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
}
2025-08-14 17:38:51 +01:00
#region Nested type: PluginInfo
2023-10-03 23:24:05 +01:00
2025-08-14 17:38:51 +01:00
private sealed class PluginInfo
{
2025-08-14 17:38:51 +01:00
public string? Namespace { get; set; }
public string ClassName { get; set; } = "";
public bool IsRegister { get; set; }
public List<string> Interfaces { get; } = [];
}
2023-10-03 23:24:05 +01:00
#endregion
}