Skip to content

Commit 2d8a509

Browse files
committed
Opt for disabling generator via MSBuild
1 parent 182f02d commit 2d8a509

File tree

3 files changed

+9
-84
lines changed

3 files changed

+9
-84
lines changed

src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public class RequestDelegateGenerator : IIncrementalGenerator
2525

2626
public void Initialize(IncrementalGeneratorInitializationContext context)
2727
{
28-
var isGeneratorEnabled = context.AnalyzerConfigOptionsProvider.Select((provider, _) =>
29-
provider.GlobalOptions.TryGetValue("build_property.EnableRequestDelegateGenerator", out var enableRequestDelegateGenerator)
30-
&& enableRequestDelegateGenerator == "true");
31-
3228
var mapActionOperations = context.SyntaxProvider.CreateSyntaxProvider(
3329
predicate: (node, _) => node is InvocationExpressionSyntax
3430
{
@@ -43,13 +39,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4339
} && _knownMethods.Contains(method),
4440
transform: (context, token) => context.SemanticModel.GetOperation(context.Node, token) as IInvocationOperation);
4541

46-
// Filter out any map actions if the generator is not enabled
47-
// via config
48-
var conditionalMapActionOperations = mapActionOperations.Combine(isGeneratorEnabled)
49-
.Where(pair => pair.Right)
50-
.Select((pair, _) => pair.Left);
51-
52-
var endpoints = conditionalMapActionOperations
42+
var endpoints = mapActionOperations
5343
.Select((operation, _) => StaticRouteHandlerModelParser.GetEndpointFromOperation(operation))
5444
.WithTrackingName("EndpointModel");
5545

@@ -135,12 +125,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
135125
context.AddSource("GeneratedRouteBuilderExtensions.Endpoints.g.cs", code);
136126
});
137127

138-
context.RegisterSourceOutput(isGeneratorEnabled, (context, isGeneratorEnabled) =>
128+
context.RegisterSourceOutput(endpoints.Collect(), (context, isGeneratorEnabled) =>
139129
{
140-
if (isGeneratorEnabled)
141-
{
142-
context.AddSource("GeneratedRouteBuilderExtensions.Helpers.g.cs", RequestDelegateGeneratorSources.GeneratedRouteBuilderExtensionsSource);
143-
}
130+
context.AddSource("GeneratedRouteBuilderExtensions.Helpers.g.cs", RequestDelegateGeneratorSources.GeneratedRouteBuilderExtensionsSource);
144131
});
145132
}
146133
}

src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateGeneratorTestBase.cs

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Immutable;
5-
using System.Linq;
65
using System.Reflection;
76
using System.Runtime.Loader;
87
using System.Text;
9-
using Microsoft.AspNetCore;
108
using Microsoft.AspNetCore.Builder;
119
using Microsoft.CodeAnalysis;
1210
using Microsoft.CodeAnalysis.CSharp;
13-
using Microsoft.AspNetCore.Http.SourceGeneration;
14-
using Microsoft.AspNetCore.Http.SourceGeneration.StaticRouteHandlerModel;
1511
using Microsoft.AspNetCore.Routing;
16-
using Microsoft.CodeAnalysis.Diagnostics;
1712
using Microsoft.CodeAnalysis.Emit;
1813
using Microsoft.CodeAnalysis.Text;
1914
using Microsoft.Extensions.DependencyInjection;
@@ -30,13 +25,10 @@ internal static (ImmutableArray<GeneratorRunResult>, Compilation) RunGenerator(s
3025
var generator = new RequestDelegateGenerator().AsSourceGenerator();
3126

3227
// Enable the source generator in tests
33-
var optionsProvider = new TestAnalyzerConfigOptionsProvider();
34-
optionsProvider.TestGlobalOptions["build_property.EnableRequestDelegateGenerator"] = "true";
3528
GeneratorDriver driver = CSharpGeneratorDriver.Create(generators: new[]
3629
{
3730
generator
3831
},
39-
optionsProvider: optionsProvider,
4032
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
4133

4234
// Run the source generator
@@ -51,10 +43,10 @@ internal static (ImmutableArray<GeneratorRunResult>, Compilation) RunGenerator(s
5143

5244
internal static StaticRouteHandlerModel.Endpoint GetStaticEndpoint(ImmutableArray<GeneratorRunResult> results, string stepName)
5345
{
54-
var StaticEndpointStep = results[0].TrackedSteps[stepName].Single();
55-
var StaticEndpointOutput = StaticEndpointStep.Outputs.Single();
56-
var (StaticEndpoint, _) = StaticEndpointOutput;
57-
var endpoint = Assert.IsType<StaticRouteHandlerModel.Endpoint>(StaticEndpoint);
46+
var staticEndpointStep = results[0].TrackedSteps[stepName].Single();
47+
var staticEndpointOutput = staticEndpointStep.Outputs.Single();
48+
var (staticEndpoint, _) = staticEndpointOutput;
49+
var endpoint = Assert.IsType<StaticRouteHandlerModel.Endpoint>(staticEndpoint);
5850
return endpoint;
5951
}
6052

@@ -97,7 +89,7 @@ internal static Endpoint GetEndpointFromCompilation(Compilation compilation)
9789
pdb.Position = 0;
9890

9991
var assembly = AssemblyLoadContext.Default.LoadFromStream(output, pdb);
100-
var handler = assembly?.GetType("TestMapActions")
92+
var handler = assembly.GetType("TestMapActions")
10193
?.GetMethod("MapTestEndpoints", BindingFlags.Public | BindingFlags.Static)
10294
?.CreateDelegate<Func<IEndpointRouteBuilder, IEndpointRouteBuilder>>();
10395
var sourceKeyType = assembly.GetType("Microsoft.AspNetCore.Builder.SourceKey");
@@ -203,8 +195,6 @@ private class EmptyServiceProvider : IServiceScope, IServiceProvider, IServiceSc
203195
{
204196
public IServiceProvider ServiceProvider => this;
205197

206-
public RouteHandlerOptions RouteHandlerOptions { get; set; } = new RouteHandlerOptions();
207-
208198
public IServiceScope CreateScope()
209199
{
210200
return this;
@@ -226,62 +216,12 @@ public DefaultEndpointRouteBuilder(IApplicationBuilder applicationBuilder)
226216
DataSources = new List<EndpointDataSource>();
227217
}
228218

229-
public IApplicationBuilder ApplicationBuilder { get; }
219+
private IApplicationBuilder ApplicationBuilder { get; }
230220

231221
public IApplicationBuilder CreateApplicationBuilder() => ApplicationBuilder.New();
232222

233223
public ICollection<EndpointDataSource> DataSources { get; }
234224

235225
public IServiceProvider ServiceProvider => ApplicationBuilder.ApplicationServices;
236226
}
237-
238-
private class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
239-
{
240-
public override AnalyzerConfigOptions GlobalOptions => TestGlobalOptions;
241-
242-
public TestAnalyzerConfigOptions TestGlobalOptions { get; } = new TestAnalyzerConfigOptions();
243-
244-
public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => throw new NotImplementedException();
245-
246-
public Dictionary<string, TestAnalyzerConfigOptions> AdditionalTextOptions { get; } = new();
247-
248-
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
249-
{
250-
return AdditionalTextOptions.TryGetValue(textFile.Path, out var options) ? options : new TestAnalyzerConfigOptions();
251-
}
252-
253-
public TestAnalyzerConfigOptionsProvider Clone()
254-
{
255-
var provider = new TestAnalyzerConfigOptionsProvider();
256-
foreach (var option in this.TestGlobalOptions.Options)
257-
{
258-
provider.TestGlobalOptions[option.Key] = option.Value;
259-
}
260-
foreach (var option in this.AdditionalTextOptions)
261-
{
262-
var newOptions = new TestAnalyzerConfigOptions();
263-
foreach (var subOption in option.Value.Options)
264-
{
265-
newOptions[subOption.Key] = subOption.Value;
266-
}
267-
provider.AdditionalTextOptions[option.Key] = newOptions;
268-
269-
}
270-
return provider;
271-
}
272-
}
273-
274-
private class TestAnalyzerConfigOptions : AnalyzerConfigOptions
275-
{
276-
public Dictionary<string, string> Options { get; } = new();
277-
278-
public string this[string name]
279-
{
280-
get => Options[name];
281-
set => Options[name] = value;
282-
}
283-
284-
public override bool TryGetValue(string key, out string value)
285-
=> Options.TryGetValue(key, out value);
286-
}
287227
}

src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateGeneratorTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Microsoft.AspNetCore.Http.SourceGeneration;
5-
64
namespace Microsoft.AspNetCore.Http.SourceGeneration.Tests;
75

86
public class RequestDelegateGeneratorTests : RequestDelegateGeneratorTestBase

0 commit comments

Comments
 (0)