PublicApiGenerator.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233
  1. using Microsoft.CodeAnalysis;
  2. namespace QuestPDF.InteropGenerators;
  3. /// <summary>
  4. /// Source generator that creates interop bindings for QuestPDF public API.
  5. /// Combines analysis, C# generation, and Python generation into a complete pipeline.
  6. /// </summary>
  7. [Generator]
  8. public sealed class PublicApiGenerator : IIncrementalGenerator
  9. {
  10. public void Initialize(IncrementalGeneratorInitializationContext context)
  11. {
  12. context.RegisterSourceOutput(context.CompilationProvider, static (spc, compilation) =>
  13. {
  14. // Step 1: Analyze the public API and collect all interop methods
  15. // This includes both extension methods AND public methods from Fluent API classes
  16. // (descriptors, configurations, handlers, etc.)
  17. var allMethods = PublicApiAnalyzer.CollectAllInteropMethods(compilation.Assembly.GlobalNamespace);
  18. // Step 2: Generate C# UnmanagedCallersOnly interop code
  19. var csharpInteropCode = CSharpInteropGenerator.GenerateInteropCode(allMethods);
  20. spc.AddSource("GeneratedInterop.g.cs", csharpInteropCode);
  21. // Step 3: Generate Python ctypes bindings
  22. // Python bindings strictly follow all C# interop functionalities
  23. var pythonBindingsCode = PythonBindingsGenerator.GeneratePythonBindings(allMethods);
  24. // Note: Python file is added as .txt so it appears in generated files
  25. // Extract and rename to .py for actual use
  26. spc.AddSource("GeneratedInterop.g.py.txt", pythonBindingsCode);
  27. });
  28. }
  29. }