-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CookieCrumble Module Source Generator (#7851)
- Loading branch information
1 parent
61f1f59
commit b184815
Showing
122 changed files
with
323 additions
and
999 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/CookieCrumble/src/CookieCrumble.Analyzers/CookieCrumble.Analyzers.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency --> | ||
<IncludeSymbols>false</IncludeSymbols> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageId>CookieCrumble.Analyzers</PackageId> | ||
<AssemblyName>CookieCrumble.Analyzers</AssemblyName> | ||
<RootNamespace>CookieCrumble.Analyzers</RootNamespace> | ||
<Description>This package provides source generators for CookieCrumble.</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" VersionOverride="4.1.0" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" VersionOverride="3.3.3" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.Bcl.HashCode" VersionOverride="1.1.0" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
src/CookieCrumble/src/CookieCrumble.Analyzers/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"Generators": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "../../test/CookieCrumble.Tests/CookieCrumble.Tests.csproj" | ||
} | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
src/CookieCrumble/src/CookieCrumble.Analyzers/SnapshotModuleGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using System.Collections.Immutable; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace CookieCrumble.Analyzers; | ||
|
||
[Generator] | ||
public class SnapshotModuleGenerator : IIncrementalGenerator | ||
{ | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
var rootNamespaceProvider = context.AnalyzerConfigOptionsProvider | ||
.Select((options, _) => | ||
{ | ||
options.GlobalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace); | ||
return string.IsNullOrWhiteSpace(rootNamespace) ? "CookieCrumble" : rootNamespace; | ||
}); | ||
|
||
var compilationProvider = context.CompilationProvider; | ||
|
||
var providerInterfaceProvider = compilationProvider | ||
.Select((comp, _) => comp.GetTypeByMetadataName("CookieCrumble.ISnapshotModule")); | ||
|
||
var providerTypes = compilationProvider | ||
.Combine(providerInterfaceProvider) | ||
.SelectMany((tuple, _) => | ||
{ | ||
var (comp, providerInterface) = tuple; | ||
if (providerInterface is null) | ||
{ | ||
return ImmutableArray<INamedTypeSymbol>.Empty; | ||
} | ||
|
||
var providers = GetProviderTypes(comp.GlobalNamespace, providerInterface); | ||
return providers.ToImmutableArray(); | ||
}) | ||
.Collect(); | ||
|
||
var combined = providerTypes.Combine(rootNamespaceProvider); | ||
|
||
context.RegisterSourceOutput(combined, (spc, tuple) => | ||
{ | ||
var (types, rootNamespace) = tuple; | ||
var source = GenerateModuleInitializerClass(types, rootNamespace); | ||
spc.AddSource("ModuleInitializer.g.cs", source); | ||
}); | ||
} | ||
|
||
private static IEnumerable<INamedTypeSymbol> GetProviderTypes( | ||
INamespaceSymbol namespaceSymbol, | ||
INamedTypeSymbol providerInterface) | ||
{ | ||
foreach (var member in namespaceSymbol.GetMembers()) | ||
{ | ||
if (member is INamespaceSymbol ns) | ||
{ | ||
foreach (var nestedType in GetProviderTypes(ns, providerInterface)) | ||
{ | ||
if (IsProvider(nestedType, providerInterface)) | ||
{ | ||
yield return nestedType; | ||
} | ||
} | ||
} | ||
else if (member is INamedTypeSymbol type) | ||
{ | ||
if (IsProvider(type, providerInterface)) | ||
{ | ||
yield return type; | ||
} | ||
|
||
foreach (var nested in type.GetTypeMembers()) | ||
{ | ||
foreach (var nestedType in GetAllNamedTypes(nested)) | ||
{ | ||
if (IsProvider(nestedType, providerInterface)) | ||
{ | ||
yield return nestedType; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
static bool IsProvider(INamedTypeSymbol type, INamedTypeSymbol providerInterface) | ||
=> type.TypeKind == TypeKind.Class | ||
&& !type.IsAbstract | ||
&& type.AllInterfaces.Contains(providerInterface, SymbolEqualityComparer.Default); | ||
} | ||
|
||
private static IEnumerable<INamedTypeSymbol> GetAllNamedTypes(INamedTypeSymbol typeSymbol) | ||
{ | ||
yield return typeSymbol; | ||
|
||
foreach (var nested in typeSymbol.GetTypeMembers()) | ||
{ | ||
foreach (var nestedType in GetAllNamedTypes(nested)) | ||
{ | ||
yield return nestedType; | ||
} | ||
} | ||
} | ||
|
||
private static string GenerateModuleInitializerClass( | ||
ImmutableArray<INamedTypeSymbol> providerTypes, | ||
string? rootNamespace) | ||
{ | ||
rootNamespace ??= "CookieCrumble"; | ||
|
||
var sb = new StringBuilder(); | ||
sb.AppendLine("// <auto-generated />"); | ||
sb.AppendLine("using System;"); | ||
sb.AppendLine("using System.Runtime.CompilerServices;"); | ||
sb.AppendLine(); | ||
sb.AppendLine($"namespace {rootNamespace}"); | ||
sb.AppendLine("{"); | ||
sb.AppendLine(" internal static partial class ModuleInitializer"); | ||
sb.AppendLine(" {"); | ||
sb.AppendLine(" [ModuleInitializer]"); | ||
sb.AppendLine(" public static void Initialize()"); | ||
sb.AppendLine(" {"); | ||
|
||
var count = 1; | ||
foreach (var t in providerTypes) | ||
{ | ||
var providerName = "provider" + count++; | ||
sb.AppendLine($" var {providerName} = new {t.ToDisplayString(format: SymbolDisplayFormat.FullyQualifiedFormat)}();"); | ||
sb.AppendLine($" {providerName}.Initialize();"); | ||
} | ||
|
||
sb.AppendLine(" OnInitialize(global::CookieCrumble.Snapshot.RegisterFormatter);"); | ||
sb.AppendLine(" }"); | ||
sb.AppendLine(); | ||
sb.AppendLine(" static partial void OnInitialize(global::System.Action<global::CookieCrumble.Formatters.ISnapshotValueFormatter> register);"); | ||
sb.AppendLine(" }"); | ||
sb.AppendLine("}"); | ||
return sb.ToString(); | ||
} | ||
} |
9 changes: 5 additions & 4 deletions
9
src/CookieCrumble/src/CookieCrumble.Fusion/CookieCrumbleFusion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
using CookieCrumble.Fusion.Formatters; | ||
using CookieCrumble.Formatters; | ||
using SnapshotValueFormatters = CookieCrumble.Fusion.Formatters.SnapshotValueFormatters; | ||
|
||
namespace CookieCrumble.Fusion; | ||
|
||
public static class CookieCrumbleFusion | ||
public class CookieCrumbleFusion : SnapshotModule | ||
{ | ||
public static void Initialize() | ||
protected override IEnumerable<ISnapshotValueFormatter> CreateFormatters() | ||
{ | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.QueryPlan); | ||
yield return SnapshotValueFormatters.QueryPlan; | ||
} | ||
} |
19 changes: 10 additions & 9 deletions
19
src/CookieCrumble/src/CookieCrumble.HotChocolate/CookieCrumbleHotChocolate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
using CookieCrumble.HotChocolate.Formatters; | ||
using CookieCrumble.Formatters; | ||
using SnapshotValueFormatters = CookieCrumble.HotChocolate.Formatters.SnapshotValueFormatters; | ||
|
||
namespace CookieCrumble.HotChocolate; | ||
|
||
public static class CookieCrumbleHotChocolate | ||
public sealed class CookieCrumbleHotChocolate : SnapshotModule | ||
{ | ||
public static void Initialize() | ||
protected override IEnumerable<ISnapshotValueFormatter> CreateFormatters() | ||
{ | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.ExecutionResult); | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.GraphQL); | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.GraphQLHttp); | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.OperationResult); | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.Schema); | ||
Snapshot.TryRegisterFormatter(SnapshotValueFormatters.SchemaError); | ||
yield return SnapshotValueFormatters.ExecutionResult; | ||
yield return SnapshotValueFormatters.GraphQL; | ||
yield return SnapshotValueFormatters.GraphQLHttp; | ||
yield return SnapshotValueFormatters.OperationResult; | ||
yield return SnapshotValueFormatters.Schema; | ||
yield return SnapshotValueFormatters.SchemaError; | ||
} | ||
} |
8 changes: 3 additions & 5 deletions
8
src/CookieCrumble/src/CookieCrumble.TUnit/CookieCrumbleTUnit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
namespace CookieCrumble.TUnit; | ||
|
||
public static class CookieCrumbleTUnit | ||
public sealed class CookieCrumbleTUnit : SnapshotModule | ||
{ | ||
public static void Initialize() | ||
{ | ||
Snapshot.RegisterTestFramework(new TUnitFramework()); | ||
} | ||
protected override ITestFramework TryCreateTestFramework() | ||
=> new TUnitFramework(); | ||
} |
8 changes: 3 additions & 5 deletions
8
src/CookieCrumble/src/CookieCrumble.Xunit/CookieCrumbleXunit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
namespace CookieCrumble.Xunit; | ||
|
||
public static class CookieCrumbleXunit | ||
public class CookieCrumbleXunit : SnapshotModule | ||
{ | ||
public static void Initialize() | ||
{ | ||
Snapshot.RegisterTestFramework(new XunitFramework()); | ||
} | ||
protected override ITestFramework TryCreateTestFramework() | ||
=> new XunitFramework(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
src/CookieCrumble/src/CookieCrumble.Xunit3/CookieCrumbleXunit3.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
namespace CookieCrumble.Xunit3; | ||
|
||
public static class CookieCrumbleXunit3 | ||
public sealed class CookieCrumbleXunit3 : SnapshotModule | ||
{ | ||
public static void Initialize() | ||
{ | ||
Snapshot.RegisterTestFramework(new Xunit3Framework()); | ||
} | ||
protected override ITestFramework TryCreateTestFramework() | ||
=> new Xunit3Framework(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace CookieCrumble; | ||
|
||
public sealed class DisposableSnapshot(string? postFix = null, string? extension = null) | ||
: Snapshot(postFix, extension) | ||
, IDisposable | ||
{ | ||
public void Dispose() => Match(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using CookieCrumble.Formatters; | ||
|
||
namespace CookieCrumble; | ||
|
||
public interface ISnapshotModule | ||
{ | ||
void Initialize(); | ||
} | ||
|
||
public abstract class SnapshotModule : ISnapshotModule | ||
{ | ||
public void Initialize() | ||
{ | ||
var framework = TryCreateTestFramework(); | ||
if (framework is not null) | ||
{ | ||
Snapshot.RegisterTestFramework(framework); | ||
} | ||
|
||
foreach (var formatter in CreateFormatters()) | ||
{ | ||
Snapshot.RegisterFormatter(formatter); | ||
} | ||
} | ||
|
||
protected virtual ITestFramework? TryCreateTestFramework() => null; | ||
|
||
protected virtual IEnumerable<ISnapshotValueFormatter> CreateFormatters() | ||
{ | ||
yield break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.