|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using Flow.Launcher.Localization.Shared; |
| 4 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.Text; |
| 7 | + |
| 8 | +namespace Flow.Launcher.Localization.SourceGenerators.Localize |
| 9 | +{ |
| 10 | + [Generator] |
| 11 | + public partial class PublicApiSourceGenerator : IIncrementalGenerator |
| 12 | + { |
| 13 | + #region Fields |
| 14 | + |
| 15 | + private static readonly Version PackageVersion = typeof(PublicApiSourceGenerator).Assembly.GetName().Version; |
| 16 | + |
| 17 | + #endregion |
| 18 | + |
| 19 | + #region Incremental Generator |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes the generator and registers source output based on build property FLLUseDependencyInjection. |
| 23 | + /// </summary> |
| 24 | + /// <param name="context">The initialization context.</param> |
| 25 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 26 | + { |
| 27 | + var compilation = context.CompilationProvider; |
| 28 | + |
| 29 | + var configOptions = context.AnalyzerConfigOptionsProvider; |
| 30 | + |
| 31 | + var compilationEnums = configOptions.Combine(compilation); |
| 32 | + |
| 33 | + context.RegisterSourceOutput(compilationEnums, Execute); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Executes the generation of public api property based on the provided data. |
| 38 | + /// </summary> |
| 39 | + /// <param name="spc">The source production context.</param> |
| 40 | + /// <param name="data">The provided data.</param> |
| 41 | + private void Execute(SourceProductionContext spc, |
| 42 | + (AnalyzerConfigOptionsProvider ConfigOptionsProvider, Compilation Compilation) data) |
| 43 | + { |
| 44 | + var compilation = data.Compilation; |
| 45 | + var configOptions = data.ConfigOptionsProvider; |
| 46 | + |
| 47 | + var assemblyNamespace = compilation.AssemblyName ?? Constants.DefaultNamespace; |
| 48 | + var useDI = configOptions.GetFLLUseDependencyInjection(); |
| 49 | + |
| 50 | + // If we do not use dependency injection, we do not need to generate the public api property |
| 51 | + if (!useDI) return; |
| 52 | + |
| 53 | + GenerateSource(spc, assemblyNamespace); |
| 54 | + } |
| 55 | + |
| 56 | + #endregion |
| 57 | + |
| 58 | + #region Generate Source |
| 59 | + |
| 60 | + private void GenerateSource( |
| 61 | + SourceProductionContext spc, |
| 62 | + string assemblyNamespace) |
| 63 | + { |
| 64 | + var tabString = Helper.Spacing(1); |
| 65 | + |
| 66 | + var sourceBuilder = new StringBuilder(); |
| 67 | + |
| 68 | + // Generate header |
| 69 | + GeneratedHeaderFromPath(sourceBuilder); |
| 70 | + sourceBuilder.AppendLine(); |
| 71 | + |
| 72 | + // Generate nullable enable |
| 73 | + sourceBuilder.AppendLine("#nullable enable"); |
| 74 | + sourceBuilder.AppendLine(); |
| 75 | + |
| 76 | + // Generate namespace |
| 77 | + sourceBuilder.AppendLine($"namespace {assemblyNamespace};"); |
| 78 | + sourceBuilder.AppendLine(); |
| 79 | + |
| 80 | + // Generate class |
| 81 | + sourceBuilder.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{nameof(PublicApiSourceGenerator)}\", \"{PackageVersion}\")]"); |
| 82 | + sourceBuilder.AppendLine($"internal static class {Constants.PublicApiClassName}"); |
| 83 | + sourceBuilder.AppendLine("{"); |
| 84 | + |
| 85 | + // Generate properties |
| 86 | + sourceBuilder.AppendLine($"{tabString}private static Flow.Launcher.Plugin.IPublicAPI? {Constants.PublicApiPrivatePropertyName} = null;"); |
| 87 | + sourceBuilder.AppendLine(); |
| 88 | + sourceBuilder.AppendLine($"{tabString}/// <summary>"); |
| 89 | + sourceBuilder.AppendLine($"{tabString}/// Get <see cref=\"Flow.Launcher.Plugin.IPublicAPI\"> instance"); |
| 90 | + sourceBuilder.AppendLine($"{tabString}/// </summary>"); |
| 91 | + sourceBuilder.AppendLine($"{tabString}internal static Flow.Launcher.Plugin.IPublicAPI {Constants.PublicApiInternalPropertyName} => {Constants.PublicApiPrivatePropertyName} ??= CommunityToolkit.Mvvm.DependencyInjection.Ioc.Default.GetRequiredService<Flow.Launcher.Plugin.IPublicAPI>();"); |
| 92 | + sourceBuilder.AppendLine(); |
| 93 | + |
| 94 | + sourceBuilder.AppendLine($"}}"); |
| 95 | + |
| 96 | + // Add source to context |
| 97 | + spc.AddSource($"{Constants.PublicApiClassName}.{assemblyNamespace}.g.cs", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); |
| 98 | + } |
| 99 | + |
| 100 | + private static void GeneratedHeaderFromPath(StringBuilder sb) |
| 101 | + { |
| 102 | + sb.AppendLine("/// <auto-generated/>"); |
| 103 | + } |
| 104 | + |
| 105 | + #endregion |
| 106 | + } |
| 107 | +} |
0 commit comments