Skip to content

Commit

Permalink
Extract RefitInternalNamespace as an incremental value
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Aug 7, 2021
1 parent 3ed338c commit 3fd4b18
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;

namespace Refit.Generator
Expand Down Expand Up @@ -49,12 +48,14 @@ public void Execute(GeneratorExecutionContext context)
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
return;

context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RefitInternalNamespace", out var refitInternalNamespace);

GenerateInterfaceStubs(
context,
static (context, diagnostic) => context.ReportDiagnostic(diagnostic),
static (context, hintName, sourceText) => context.AddSource(hintName, sourceText),
(CSharpCompilation)context.Compilation,
context.AnalyzerConfigOptions,
refitInternalNamespace,
receiver.CandidateMethods.ToImmutableArray(),
receiver.CandidateInterfaces.ToImmutableArray());
}
Expand All @@ -66,12 +67,10 @@ public void GenerateInterfaceStubs<TContext>(
Action<TContext, Diagnostic> reportDiagnostic,
Action<TContext, string, SourceText> addSource,
CSharpCompilation compilation,
AnalyzerConfigOptionsProvider analyzerConfigOptions,
string? refitInternalNamespace,
ImmutableArray<MethodDeclarationSyntax> candidateMethods,
ImmutableArray<InterfaceDeclarationSyntax> candidateInterfaces)
{
analyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RefitInternalNamespace", out var refitInternalNamespace);

refitInternalNamespace = $"{refitInternalNamespace ?? string.Empty}RefitInternalGenerated";

// we're going to create a new compilation that contains the attribute.
Expand Down Expand Up @@ -556,12 +555,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
(syntax, cancellationToken) => syntax is InterfaceDeclarationSyntax { BaseList: not null },
(context, cancellationToken) => (InterfaceDeclarationSyntax)context.Node);

var refitInternalNamespace = context.AnalyzerConfigOptionsProvider.Select(
(analyzerConfigOptionsProvider, cancellationToken) => analyzerConfigOptionsProvider.GlobalOptions.TryGetValue("build_property.RefitInternalNamespace", out var refitInternalNamespace) ? refitInternalNamespace : null);

var inputs = candidateMethodsProvider.Collect()
.Combine(candidateInterfacesProvider.Collect())
.Select((combined, cancellationToken) => (candidateMethods: combined.Left, candidateInterfaces: combined.Right))
.Combine(context.AnalyzerConfigOptionsProvider)
.Combine(refitInternalNamespace)
.Combine(context.CompilationProvider)
.Select((combined, cancellationToken) => (combined.Left.Left.candidateMethods, combined.Left.Left.candidateInterfaces, analyzerConfigOptions: combined.Left.Right, compilation: combined.Right));
.Select((combined, cancellationToken) => (combined.Left.Left.candidateMethods, combined.Left.Left.candidateInterfaces, refitInternalNamespace: combined.Left.Right, compilation: combined.Right));

context.RegisterSourceOutput(
inputs,
Expand All @@ -572,7 +574,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
static (context, diagnostic) => context.ReportDiagnostic(diagnostic),
static (context, hintName, sourceText) => context.AddSource(hintName, sourceText),
(CSharpCompilation)collectedValues.compilation,
collectedValues.analyzerConfigOptions,
collectedValues.refitInternalNamespace,
collectedValues.candidateMethods,
collectedValues.candidateInterfaces);
});
Expand Down

0 comments on commit 3fd4b18

Please sign in to comment.