Skip to content

Commit

Permalink
Align generator switches and some cleanup (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach authored Feb 26, 2024
1 parent 8a128f4 commit 914e392
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions sdk/Sdk.Generators/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
12 changes: 7 additions & 5 deletions sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private IEnumerable<IMethodSymbol> GetSymbolsMethodSyntaxes(List<MethodDeclarati

if (FunctionsUtil.IsValidFunctionMethod(context, context.Compilation, model, method))
{
IMethodSymbol? methodSymbol = (IMethodSymbol)model.GetDeclaredSymbol(method)!;
var methodSymbol = (IMethodSymbol)model.GetDeclaredSymbol(method)!;
yield return methodSymbol;
}
}
Expand All @@ -81,23 +81,25 @@ private static IEnumerable<IMethodSymbol> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class Emitter
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
};

public string Emit(GeneratorExecutionContext context, IReadOnlyList<GeneratorFunctionMetadata> funcMetadata, bool includeAutoRegistrationCode)
public static string Emit(GeneratorExecutionContext context, IReadOnlyList<GeneratorFunctionMetadata> funcMetadata, bool includeAutoRegistrationCode)
{
string functionMetadataInfo = AddFunctionMetadataInfo(funcMetadata, context.CancellationToken);

Expand Down Expand Up @@ -102,7 +102,7 @@ public void Configure(IHostBuilder hostBuilder)
}
return "";
}
private string AddFunctionMetadataInfo(IReadOnlyList<GeneratorFunctionMetadata> functionMetadata, CancellationToken cancellationToken)
private static string AddFunctionMetadataInfo(IReadOnlyList<GeneratorFunctionMetadata> functionMetadata, CancellationToken cancellationToken)
{
var functionCount = 0;
var builder = new StringBuilder();
Expand Down Expand Up @@ -142,7 +142,7 @@ private string AddFunctionMetadataInfo(IReadOnlyList<GeneratorFunctionMetadata>
return builder.ToString();
}

private string BuildBindingInfo(string bindingListVariableName, IList<IDictionary<string, object>> bindings)
private static string BuildBindingInfo(string bindingListVariableName, IList<IDictionary<string, object>> bindings)
{
var builder = new StringBuilder();
foreach (var binding in bindings)
Expand All @@ -154,7 +154,7 @@ private string BuildBindingInfo(string bindingListVariableName, IList<IDictionar
return builder.ToString();
}

private StringBuilder BuildRetryOptions(GeneratorRetryOptions? retry)
private static StringBuilder BuildRetryOptions(GeneratorRetryOptions? retry)
{
var builder = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ public void Execute(GeneratorExecutionContext context)
return;
}

if (context.SyntaxReceiver is not FunctionMethodSyntaxReceiver receiver || receiver.CandidateMethods.Count == 0)
if (!ShouldExecuteGeneration(context))
{
return;
}

context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(Constants.BuildProperties.EnableSourceGen, out var sourceGenSwitch);

bool.TryParse(sourceGenSwitch, out bool enableSourceGen);

if (!enableSourceGen)

if (context.SyntaxReceiver is not FunctionMethodSyntaxReceiver receiver || receiver.CandidateMethods.Count == 0)
{
return;
}
Expand All @@ -62,10 +58,9 @@ public void Execute(GeneratorExecutionContext context)
// Proceed to generate the file if function metadata info was successfully returned
if (functionMetadataInfo.Count > 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));
}
Expand All @@ -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<IMethodSymbol> GetEntryAssemblyFunctions(List<MethodDeclarationSyntax> candidateMethods, GeneratorExecutionContext context)
Expand Down

0 comments on commit 914e392

Please sign in to comment.