using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Aaru.Generators; [Generator] public class PluginRegisterGenerator : ISourceGenerator { #region ISourceGenerator Members /// public void Initialize(GeneratorInitializationContext context) => // Nothing to do context.RegisterForSyntaxNotifications(() => new PluginFinder()); /// public void Execute(GeneratorExecutionContext context) { /* #if DEBUG if(!Debugger.IsAttached) { Debugger.Launch(); } #endif */ ClassDeclarationSyntax pluginRegister = ((PluginFinder)context.SyntaxReceiver)?.Register; if(pluginRegister == null) return; var @namespace = (pluginRegister.Ancestors().FirstOrDefault(x => x is FileScopedNamespaceDeclarationSyntax) as FileScopedNamespaceDeclarationSyntax)?.Name.ToString(); @namespace ??= (pluginRegister.Ancestors().FirstOrDefault(x => x is NamespaceDeclarationSyntax) as NamespaceDeclarationSyntax)?.ToString(); string className = pluginRegister.Identifier.Text; List archives = ((PluginFinder)context.SyntaxReceiver)?.Archives; List checksums = ((PluginFinder)context.SyntaxReceiver)?.Checksums; List fileSystems = ((PluginFinder)context.SyntaxReceiver)?.FileSystems; List filters = ((PluginFinder)context.SyntaxReceiver)?.Filters; List floppyImagePlugins = ((PluginFinder)context.SyntaxReceiver)?.FloppyImagePlugins; List partitionPlugins = ((PluginFinder)context.SyntaxReceiver)?.PartitionPlugins; List mediaImagePlugins = ((PluginFinder)context.SyntaxReceiver)?.MediaImagePlugins; List readOnlyFileSystems = ((PluginFinder)context.SyntaxReceiver)?.ReadOnlyFileSystems; List writableFloppyImagePlugins = ((PluginFinder)context.SyntaxReceiver)?.WritableFloppyImagePlugins; List writableImagePlugins = ((PluginFinder)context.SyntaxReceiver)?.WritableImagePlugins; List byteAddressableImagePlugins = ((PluginFinder)context.SyntaxReceiver)?.ByteAddressableImagePlugins; StringBuilder sb = new(); sb.AppendLine(""" // /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Register.g.cs // Author(s) : Natalia Portillo // // --[ 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. // // ---------------------------------------------------------------------------- // Copyright © 2011-2023 Natalia Portillo // ****************************************************************************/ """); sb.AppendLine(); sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using Aaru.CommonTypes.Interfaces;"); sb.AppendLine("using Microsoft.Extensions.DependencyInjection;"); sb.AppendLine(); sb.AppendLine($"namespace {@namespace};"); sb.AppendLine(); sb.AppendLine($"public sealed partial class {className} : IPluginRegister"); sb.AppendLine("{"); if(archives?.Count > 0) { sb.AppendLine(" public void RegisterArchivePlugins(IServiceCollection services)"); sb.AppendLine(" {"); foreach(string plugin in archives) sb.AppendLine($" services.AddTransient();"); sb.AppendLine(" }"); } else sb.AppendLine(" public void RegisterArchivePlugins(IServiceCollection services) {}"); sb.AppendLine(); if(checksums?.Count > 0) { sb.AppendLine(" public void RegisterChecksumPlugins(IServiceCollection services)"); sb.AppendLine(" {"); foreach(string plugin in checksums) sb.AppendLine($" services.AddTransient();"); sb.AppendLine(" }"); } else sb.AppendLine(" public void RegisterChecksumPlugins(IServiceCollection services) {}"); sb.AppendLine(); if(fileSystems?.Count > 0) { sb.AppendLine(" public List GetAllFilesystemPlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in fileSystems) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllFilesystemPlugins() => null;"); sb.AppendLine(); if(filters?.Count > 0) { sb.AppendLine(" public void RegisterFilterPlugins(IServiceCollection services)"); sb.AppendLine(" {"); foreach(string plugin in filters) sb.AppendLine($" services.AddTransient();"); sb.AppendLine(" }"); } else sb.AppendLine(" public void RegisterFilterPlugins(IServiceCollection services) {}"); sb.AppendLine(); if(floppyImagePlugins?.Count > 0) { sb.AppendLine(" public List GetAllFloppyImagePlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in floppyImagePlugins) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllFloppyImagePlugins() => null;"); sb.AppendLine(); if(mediaImagePlugins?.Count > 0) { sb.AppendLine(" public List GetAllMediaImagePlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in mediaImagePlugins) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllMediaImagePlugins() => null;"); sb.AppendLine(); if(partitionPlugins?.Count > 0) { sb.AppendLine(" public void RegisterPartitionPlugins(IServiceCollection services)"); sb.AppendLine(" {"); foreach(string plugin in partitionPlugins) sb.AppendLine($" services.AddTransient();"); sb.AppendLine(" }"); } else sb.AppendLine(" public void RegisterPartitionPlugins(IServiceCollection services) {}"); sb.AppendLine(); if(readOnlyFileSystems?.Count > 0) { sb.AppendLine(" public List GetAllReadOnlyFilesystemPlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in readOnlyFileSystems) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllReadOnlyFilesystemPlugins() => null;"); sb.AppendLine(); if(writableFloppyImagePlugins?.Count > 0) { sb.AppendLine(" public List GetAllWritableFloppyImagePlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in writableFloppyImagePlugins) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllWritableFloppyImagePlugins() => null;"); sb.AppendLine(); if(writableImagePlugins?.Count > 0) { sb.AppendLine(" public List GetAllWritableImagePlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in writableImagePlugins) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllWritableImagePlugins() => null;"); sb.AppendLine(); if(byteAddressableImagePlugins?.Count > 0) { sb.AppendLine(" public List GetAllByteAddressablePlugins() => new()"); sb.AppendLine(" {"); foreach(string plugin in byteAddressableImagePlugins) sb.AppendLine($" typeof({plugin}),"); sb.AppendLine(" };"); } else sb.AppendLine(" public List GetAllByteAddressablePlugins() => null;"); sb.AppendLine("}"); context.AddSource("Register.g.cs", sb.ToString()); } #endregion #region Nested type: PluginFinder sealed class PluginFinder : ISyntaxReceiver { public List Archives { get; } = new(); public List Checksums { get; } = new(); public List FileSystems { get; } = new(); public List Filters { get; } = new(); public List FloppyImagePlugins { get; } = new(); public List MediaImagePlugins { get; } = new(); public List PartitionPlugins { get; } = new(); public List ReadOnlyFileSystems { get; } = new(); public List WritableFloppyImagePlugins { get; } = new(); public List WritableImagePlugins { get; } = new(); public List ByteAddressableImagePlugins { get; } = new(); public ClassDeclarationSyntax Register { get; private set; } #region ISyntaxReceiver Members public void OnVisitSyntaxNode(SyntaxNode syntaxNode) { if(syntaxNode is not ClassDeclarationSyntax plugin) return; if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IPluginRegister") == true) Register = plugin; if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IArchive") == true) { if(!Archives.Contains(plugin.Identifier.Text)) Archives.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IChecksum") == true) { if(!Checksums.Contains(plugin.Identifier.Text)) Checksums.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IFilesystem") == true) { if(!FileSystems.Contains(plugin.Identifier.Text)) FileSystems.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IFilter") == true) { if(!Filters.Contains(plugin.Identifier.Text)) Filters.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IFloppyImage") == true) { if(!FloppyImagePlugins.Contains(plugin.Identifier.Text)) FloppyImagePlugins.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText is "IMediaImage" or "IOpticalMediaImage" or "IFloppyImage" or "ITapeImage") == true) { if(!MediaImagePlugins.Contains(plugin.Identifier.Text)) MediaImagePlugins.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IPartition") == true) { if(!PartitionPlugins.Contains(plugin.Identifier.Text)) PartitionPlugins.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IReadOnlyFilesystem") == true) { if(!ReadOnlyFileSystems.Contains(plugin.Identifier.Text)) ReadOnlyFileSystems.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IWritableFloppyImage") == true) { if(!WritableFloppyImagePlugins.Contains(plugin.Identifier.Text)) WritableFloppyImagePlugins.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText is "IWritableImage" or "IWritableOpticalImage" or "IWritableTapeImage" or "IByteAddressableImage") == true) { if(!WritableImagePlugins.Contains(plugin.Identifier.Text)) WritableImagePlugins.Add(plugin.Identifier.Text); } if(plugin.BaseList?.Types.Any(t => ((t as SimpleBaseTypeSyntax)?.Type as IdentifierNameSyntax)?.Identifier. ValueText == "IByteAddressableImage") == true) { if(!ByteAddressableImagePlugins.Contains(plugin.Identifier.Text)) ByteAddressableImagePlugins.Add(plugin.Identifier.Text); } MediaImagePlugins.AddRange(WritableImagePlugins); FileSystems.AddRange(ReadOnlyFileSystems); } #endregion } #endregion }