From 914e392e4b6838ef430d01a9c2ebf7d387933e2f Mon Sep 17 00:00:00 2001 From: Daniel Marbach Date: Mon, 26 Feb 2024 22:01:59 +0000 Subject: [PATCH] Align generator switches and some cleanup (#2208) --- sdk/Sdk.Generators/Constants.cs | 4 +-- .../FunctionExecutorGenerator.cs | 12 ++++---- ...nctionMetadataProviderGenerator.Emitter.cs | 8 ++--- .../FunctionMetadataProviderGenerator.cs | 30 ++++++++++++------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/sdk/Sdk.Generators/Constants.cs b/sdk/Sdk.Generators/Constants.cs index f7eb7b536..9a4fc9482 100644 --- a/sdk/Sdk.Generators/Constants.cs +++ b/sdk/Sdk.Generators/Constants.cs @@ -20,8 +20,8 @@ internal static class BuildProperties internal const string FunctionsExecutionModel = "build_property.FunctionsExecutionModel"; internal const string MSBuildTargetFrameworkIdentifier = "build_property.TargetFrameworkIdentifier"; internal const string GeneratedCodeNamespace = "build_property.FunctionsGeneratedCodeNamespace"; - internal const string EnableSourceGen = "build_property.FunctionsEnableMetadataSourceGen"; - internal const string EnablePlaceholder = "build_property.FunctionsEnableExecutorSourceGen"; + internal const string EnableMetadataSourceGen = "build_property.FunctionsEnableMetadataSourceGen"; + internal const string EnableExecutorSourceGen = "build_property.FunctionsEnableExecutorSourceGen"; internal const string AutoRegisterGeneratedFunctionsExecutor = "build_property.FunctionsAutoRegisterGeneratedFunctionsExecutor"; internal const string AutoRegisterGeneratedMetadataProvider = "build_property.FunctionsAutoRegisterGeneratedMetadataProvider"; } diff --git a/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.cs b/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.cs index 4cac87c9f..8a98266ff 100644 --- a/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.cs +++ b/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.cs @@ -61,7 +61,7 @@ private IEnumerable GetSymbolsMethodSyntaxes(List GetDependentAssemblyFunctionsSymbols(G private static bool ShouldIncludeAutoGeneratedAttributes(GeneratorExecutionContext context) { if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue( - Constants.BuildProperties.AutoRegisterGeneratedFunctionsExecutor, out var value)) + Constants.BuildProperties.AutoRegisterGeneratedFunctionsExecutor, out var autoRegisterSwitch)) { return false; } - return string.Equals(value, bool.TrueString, System.StringComparison.OrdinalIgnoreCase); + bool.TryParse(autoRegisterSwitch, out bool enableRegistration); + return enableRegistration; } private static bool ShouldExecuteGeneration(GeneratorExecutionContext context) { if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue( - Constants.BuildProperties.EnablePlaceholder, out var value)) + Constants.BuildProperties.EnableExecutorSourceGen, out var sourceGenSwitch)) { return false; } - return string.Equals(value, bool.TrueString, System.StringComparison.OrdinalIgnoreCase); + bool.TryParse(sourceGenSwitch, out bool enableSourceGen); + return enableSourceGen; } } } diff --git a/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs b/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs index 7619ba452..befcfac76 100644 --- a/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs +++ b/sdk/Sdk.Generators/FunctionMetadataProviderGenerator/FunctionMetadataProviderGenerator.Emitter.cs @@ -19,7 +19,7 @@ internal sealed class Emitter DictionaryKeyPolicy = JsonNamingPolicy.CamelCase }; - public string Emit(GeneratorExecutionContext context, IReadOnlyList funcMetadata, bool includeAutoRegistrationCode) + public static string Emit(GeneratorExecutionContext context, IReadOnlyList funcMetadata, bool includeAutoRegistrationCode) { string functionMetadataInfo = AddFunctionMetadataInfo(funcMetadata, context.CancellationToken); @@ -102,7 +102,7 @@ public void Configure(IHostBuilder hostBuilder) } return ""; } - private string AddFunctionMetadataInfo(IReadOnlyList functionMetadata, CancellationToken cancellationToken) + private static string AddFunctionMetadataInfo(IReadOnlyList functionMetadata, CancellationToken cancellationToken) { var functionCount = 0; var builder = new StringBuilder(); @@ -142,7 +142,7 @@ private string AddFunctionMetadataInfo(IReadOnlyList return builder.ToString(); } - private string BuildBindingInfo(string bindingListVariableName, IList> bindings) + private static string BuildBindingInfo(string bindingListVariableName, IList> bindings) { var builder = new StringBuilder(); foreach (var binding in bindings) @@ -154,7 +154,7 @@ private string BuildBindingInfo(string bindingListVariableName, IList 0) { - Emitter e = new(); var shouldIncludeAutoGeneratedAttributes = ShouldIncludeAutoGeneratedAttributes(context); - string result = e.Emit(context, functionMetadataInfo, shouldIncludeAutoGeneratedAttributes); + string result = Emitter.Emit(context, functionMetadataInfo, shouldIncludeAutoGeneratedAttributes); context.AddSource(Constants.FileNames.GeneratedFunctionMetadata, SourceText.From(result, Encoding.UTF8)); } @@ -91,12 +86,25 @@ private static string GetScriptFileExtensionForEntryPointAssemblyFunctions(Gener private static bool ShouldIncludeAutoGeneratedAttributes(GeneratorExecutionContext context) { if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue( - Constants.BuildProperties.AutoRegisterGeneratedMetadataProvider, out var value)) + Constants.BuildProperties.AutoRegisterGeneratedMetadataProvider, out var autoRegisterSwitch)) + { + return false; + } + + bool.TryParse(autoRegisterSwitch, out bool enableRegistration); + return enableRegistration; + } + + private static bool ShouldExecuteGeneration(GeneratorExecutionContext context) + { + if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue( + Constants.BuildProperties.EnableMetadataSourceGen, out var sourceGenSwitch)) { return false; } - return string.Equals(value, bool.TrueString, System.StringComparison.OrdinalIgnoreCase); + bool.TryParse(sourceGenSwitch, out bool enableSourceGen); + return enableSourceGen; } private IEnumerable GetEntryAssemblyFunctions(List candidateMethods, GeneratorExecutionContext context)