Skip to content

Commit

Permalink
A MetadataReference can have multiple modules of interest.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Jun 15, 2022
1 parent 43d1011 commit 4f0006f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -24,43 +23,40 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.SelectMany(static (states, _) => states.Distinct());

var generators = context.GetMetadataReferencesProvider()
.SelectMany(static (reference, _) => TryGetCodeGeneratorFactory(reference, out var factory)
? ImmutableArray.Create(factory)
: ImmutableArray<ICodeGeneratorFactory<EnumSourceGeneratorState>>.Empty)
.SelectMany(static (reference, _) => GetCodeGeneratorFactories(reference))
.Collect()
.WithComparer(new SetComparer<ICodeGeneratorFactory<EnumSourceGeneratorState>>());

context.RegisterSourceOutput(candidates.Combine(generators), GenerateCode);
}

private static bool TryGetCodeGeneratorFactory(
MetadataReference reference,
[MaybeNullWhen(false)] out ICodeGeneratorFactory<EnumSourceGeneratorState> factory)
private static ImmutableArray<ICodeGeneratorFactory<EnumSourceGeneratorState>> GetCodeGeneratorFactories(MetadataReference reference)
{
var factories = ImmutableArray<ICodeGeneratorFactory<EnumSourceGeneratorState>>.Empty;

foreach (var module in reference.GetModules())
{
switch (module.Name)
{
case THINKTECTURE_RUNTIME_EXTENSIONS:
factory = new SmartEnumCodeGeneratorFactory();
return true;
factories = factories.Add(new SmartEnumCodeGeneratorFactory());
break;

case THINKTECTURE_RUNTIME_EXTENSIONS_JSON:
factory = new JsonSmartEnumCodeGeneratorFactory();
return true;
factories = factories.Add(new JsonSmartEnumCodeGeneratorFactory());
break;

case THINKTECTURE_RUNTIME_EXTENSIONS_NEWTONSOFT_JSON:
factory = new NewtonsoftJsonSmartEnumCodeGeneratorFactory();
return true;
factories = factories.Add(new NewtonsoftJsonSmartEnumCodeGeneratorFactory());
break;

case THINKTECTURE_RUNTIME_EXTENSIONS_MESSAGEPACK:
factory = new MessagePackSmartEnumCodeGeneratorFactory();
return true;
factories = factories.Add(new MessagePackSmartEnumCodeGeneratorFactory());
break;
}
}

factory = null;
return false;
return factories;
}

private static bool IsCandidate(SyntaxNode syntaxNode, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -24,43 +23,40 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.SelectMany(static (states, _) => states.Distinct());

var generators = context.GetMetadataReferencesProvider()
.SelectMany(static (reference, _) => TryGetCodeGeneratorFactory(reference, out var factory)
? ImmutableArray.Create(factory)
: ImmutableArray<ICodeGeneratorFactory<ValueObjectSourceGeneratorState>>.Empty)
.SelectMany(static (reference, _) => GetCodeGeneratorFactories(reference))
.Collect()
.WithComparer(new SetComparer<ICodeGeneratorFactory<ValueObjectSourceGeneratorState>>());

context.RegisterSourceOutput(candidates.Combine(generators), GenerateCode);
}

private static bool TryGetCodeGeneratorFactory(
MetadataReference reference,
[MaybeNullWhen(false)] out ICodeGeneratorFactory<ValueObjectSourceGeneratorState> factory)
private static ImmutableArray<ICodeGeneratorFactory<ValueObjectSourceGeneratorState>> GetCodeGeneratorFactories(MetadataReference reference)
{
var factories = ImmutableArray<ICodeGeneratorFactory<ValueObjectSourceGeneratorState>>.Empty;

foreach (var module in reference.GetModules())
{
switch (module.Name)
{
case THINKTECTURE_RUNTIME_EXTENSIONS:
factory = new ValueObjectCodeGeneratorFactory();
return true;
factories = factories.Add(new ValueObjectCodeGeneratorFactory());
break;

case THINKTECTURE_RUNTIME_EXTENSIONS_JSON:
factory = new JsonValueObjectCodeGeneratorFactory();
return true;
factories = factories.Add(new JsonValueObjectCodeGeneratorFactory());
break;

case THINKTECTURE_RUNTIME_EXTENSIONS_NEWTONSOFT_JSON:
factory = new NewtonsoftJsonValueObjectCodeGeneratorFactory();
return true;
factories = factories.Add(new NewtonsoftJsonValueObjectCodeGeneratorFactory());
break;

case THINKTECTURE_RUNTIME_EXTENSIONS_MESSAGEPACK:
factory = new MessagePackValueObjectCodeGeneratorFactory();
return true;
factories = factories.Add(new MessagePackValueObjectCodeGeneratorFactory());
break;
}
}

factory = null;
return false;
return factories;
}

private static bool IsCandidate(SyntaxNode syntaxNode, CancellationToken cancellationToken)
Expand Down

0 comments on commit 4f0006f

Please sign in to comment.