-
Notifications
You must be signed in to change notification settings - Fork 185
/
FunctionMetadataProviderGenerator.cs
127 lines (104 loc) · 5.7 KB
/
FunctionMetadataProviderGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.Azure.Functions.Worker.Sdk.Generators
{
/// <summary>
/// Generates a class that implements IFunctionMetadataProvider and the method GetFunctionsMetadataAsync() which returns a list of IFunctionMetadata.
/// This source generator indexes a Function App and explicitly creates a list of DefaultFunctionMetadata (which implements IFunctionMetadata) from the functions defined
/// in the user's compilation. This allows the worker to index functions at build time, rather than waiting for the process to start.
/// </summary>
[Generator]
public partial class FunctionMetadataProviderGenerator : ISourceGenerator
{
private const string NetFxTargetFrameworkIdentifierValue = ".NETFramework";
private const string ExecutableFileExtension = ".exe";
private const string DynamicLinkLibraryFileExtension = ".dll";
public void Execute(GeneratorExecutionContext context)
{
if (!context.IsRunningInAzureFunctionProject())
{
return;
}
if (context.SyntaxReceiver is not FunctionMethodSyntaxReceiver receiver || receiver.CandidateMethods.Count == 0)
{
return;
}
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(Constants.BuildProperties.EnableSourceGen, out var sourceGenSwitch);
bool.TryParse(sourceGenSwitch, out bool enableSourceGen);
if (!enableSourceGen)
{
return;
}
// attempt to parse user compilation
var p = new Parser(context);
var entryAssemblyFunctionSymbols = GetEntryAssemblyFunctions(receiver.CandidateMethods, context);
var dependentAssemblyFunctionSymbols = GetDependentAssemblyFunctions(context);
var entryAssemblyParsingContext = new FunctionsMetadataParsingContext
{
ScriptFileExtension = GetScriptFileExtensionForEntryPointAssemblyFunctions(context)
};
var entryAssemblyFunctions = p.GetFunctionMetadataInfo(entryAssemblyFunctionSymbols.ToList(), entryAssemblyParsingContext);
var dependentAssemblyFunctions = p.GetFunctionMetadataInfo(dependentAssemblyFunctionSymbols.ToList());
IReadOnlyList<GeneratorFunctionMetadata> functionMetadataInfo = entryAssemblyFunctions.Concat(dependentAssemblyFunctions).ToList();
// 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);
context.AddSource(Constants.FileNames.GeneratedFunctionMetadata, SourceText.From(result, Encoding.UTF8));
}
}
/// <summary>
/// Register a factory that can create our custom syntax receiver
/// </summary>
/// <param name="context"></param>
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new FunctionMethodSyntaxReceiver());
}
// For dependent assemblies, it will be always "dll". We only need to find out for entry point assembly.
private static string GetScriptFileExtensionForEntryPointAssemblyFunctions(GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(Constants.BuildProperties.MSBuildTargetFrameworkIdentifier, out var value);
return string.Equals(value, NetFxTargetFrameworkIdentifierValue, StringComparison.OrdinalIgnoreCase) ? ExecutableFileExtension : DynamicLinkLibraryFileExtension;
}
private static bool ShouldIncludeAutoGeneratedAttributes(GeneratorExecutionContext context)
{
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
Constants.BuildProperties.AutoRegisterGeneratedMetadataProvider, out var value))
{
return false;
}
return string.Equals(value, bool.TrueString, System.StringComparison.OrdinalIgnoreCase);
}
private IEnumerable<IMethodSymbol> GetEntryAssemblyFunctions(List<MethodDeclarationSyntax> candidateMethods, GeneratorExecutionContext context)
{
foreach (MethodDeclarationSyntax method in candidateMethods)
{
var model = context.Compilation.GetSemanticModel(method.SyntaxTree);
if (FunctionsUtil.IsValidFunctionMethod(context, context.Compilation, model, method))
{
IMethodSymbol? methodSymbol = (IMethodSymbol)model.GetDeclaredSymbol(method)!;
yield return methodSymbol;
}
}
}
/// <summary>
/// Collect methods with Function attributes on them from dependent/referenced assemblies.
/// </summary>
private IEnumerable<IMethodSymbol> GetDependentAssemblyFunctions(GeneratorExecutionContext context)
{
var visitor = new ReferencedAssemblyMethodVisitor(context.Compilation);
visitor.Visit(context.Compilation.SourceModule);
return visitor.FunctionMethods;
}
}
}