Skip to content

Commit 08e6afe

Browse files
CopilotYunchuWang
andcommitted
Add DurableTaskGeneratorProjectType configuration option
- Add support for MSBuild property DurableTaskGeneratorProjectType - Support values: DurableFunctions, Worker, DurableTaskScheduler, Auto - Add comprehensive unit tests for all configuration scenarios - All 46 generator tests passing Co-authored-by: YunchuWang <[email protected]>
1 parent 8380c39 commit 08e6afe

File tree

3 files changed

+488
-8
lines changed

3 files changed

+488
-8
lines changed

src/Generators/DurableTaskSourceGenerator.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,24 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5656
transform: static (ctx, _) => GetDurableFunction(ctx))
5757
.Where(static func => func != null)!;
5858

59+
// Get the project type configuration from MSBuild properties
60+
IncrementalValueProvider<string?> projectTypeProvider = context.AnalyzerConfigOptionsProvider
61+
.Select(static (provider, _) =>
62+
{
63+
provider.GlobalOptions.TryGetValue("build_property.DurableTaskGeneratorProjectType", out string? projectType);
64+
return projectType;
65+
});
66+
5967
// Collect all results and check if Durable Functions is referenced
60-
IncrementalValueProvider<(Compilation, ImmutableArray<DurableTaskTypeInfo>, ImmutableArray<DurableFunction>)> compilationAndTasks =
68+
IncrementalValueProvider<(Compilation, ImmutableArray<DurableTaskTypeInfo>, ImmutableArray<DurableFunction>, string?)> compilationAndTasks =
6169
durableTaskAttributes.Collect()
6270
.Combine(durableFunctions.Collect())
6371
.Combine(context.CompilationProvider)
64-
.Select((x, _) => (x.Right, x.Left.Left, x.Left.Right));
72+
.Combine(projectTypeProvider)
73+
.Select((x, _) => (x.Left.Right, x.Left.Left.Left, x.Left.Left.Right, x.Right));
6574

6675
// Generate the source
67-
context.RegisterSourceOutput(compilationAndTasks, static (spc, source) => Execute(spc, source.Item1, source.Item2, source.Item3));
76+
context.RegisterSourceOutput(compilationAndTasks, static (spc, source) => Execute(spc, source.Item1, source.Item2, source.Item3, source.Item4));
6877
}
6978

7079
static DurableTaskTypeInfo? GetDurableTaskTypeInfo(GeneratorSyntaxContext context)
@@ -177,17 +186,16 @@ static void Execute(
177186
SourceProductionContext context,
178187
Compilation compilation,
179188
ImmutableArray<DurableTaskTypeInfo> allTasks,
180-
ImmutableArray<DurableFunction> allFunctions)
189+
ImmutableArray<DurableFunction> allFunctions,
190+
string? projectType)
181191
{
182192
if (allTasks.IsDefaultOrEmpty && allFunctions.IsDefaultOrEmpty)
183193
{
184194
return;
185195
}
186196

187-
// This generator also supports Durable Functions for .NET isolated, but we only generate Functions-specific
188-
// code if we find the Durable Functions extension listed in the set of referenced assembly names.
189-
bool isDurableFunctions = compilation.ReferencedAssemblyNames.Any(
190-
assembly => assembly.Name.Equals("Microsoft.Azure.Functions.Worker.Extensions.DurableTask", StringComparison.OrdinalIgnoreCase));
197+
// Determine if we should generate Durable Functions specific code
198+
bool isDurableFunctions = DetermineIsDurableFunctions(compilation, projectType);
191199

192200
// Separate tasks into orchestrators, activities, and entities
193201
List<DurableTaskTypeInfo> orchestrators = new();
@@ -311,6 +319,34 @@ public static class GeneratedDurableTaskExtensions
311319
context.AddSource("GeneratedDurableTaskExtensions.cs", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
312320
}
313321

322+
static bool DetermineIsDurableFunctions(Compilation compilation, string? projectType)
323+
{
324+
// Check if the user has explicitly configured the project type
325+
if (!string.IsNullOrWhiteSpace(projectType))
326+
{
327+
// Explicit configuration takes precedence
328+
if (projectType!.Equals("DurableFunctions", StringComparison.OrdinalIgnoreCase) ||
329+
projectType.Equals("Functions", StringComparison.OrdinalIgnoreCase) ||
330+
projectType.Equals("AzureFunctions", StringComparison.OrdinalIgnoreCase))
331+
{
332+
return true;
333+
}
334+
else if (projectType.Equals("DurableTaskScheduler", StringComparison.OrdinalIgnoreCase) ||
335+
projectType.Equals("Worker", StringComparison.OrdinalIgnoreCase) ||
336+
projectType.Equals("DurableTaskWorker", StringComparison.OrdinalIgnoreCase))
337+
{
338+
return false;
339+
}
340+
// If "Auto" or unrecognized value, fall through to auto-detection
341+
}
342+
343+
// Auto-detect based on referenced assemblies
344+
// This generator also supports Durable Functions for .NET isolated, but we only generate Functions-specific
345+
// code if we find the Durable Functions extension listed in the set of referenced assembly names.
346+
return compilation.ReferencedAssemblyNames.Any(
347+
assembly => assembly.Name.Equals("Microsoft.Azure.Functions.Worker.Extensions.DurableTask", StringComparison.OrdinalIgnoreCase));
348+
}
349+
314350
static void AddOrchestratorFunctionDeclaration(StringBuilder sourceBuilder, DurableTaskTypeInfo orchestrator)
315351
{
316352
sourceBuilder.AppendLine($@"

0 commit comments

Comments
 (0)