Skip to content

Commit

Permalink
Track generation duplicates. Fix nullability warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
byme8 committed Mar 7, 2023
1 parent 97a84d2 commit e0f8efd
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/TestApp/ZeroQL.TestApp/Generated/GraphQL.g.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 291685ad31ee73dba6a2172b2f6dcc25
// f63f7a964078b30c19f6630749226655
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -15,19 +17,33 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var invocations = context.SyntaxProvider
.CreateSyntaxProvider(FindMethods, (c, ct) => (Invocation: (InvocationExpressionSyntax)c.Node, c.SemanticModel));

context.RegisterImplementationSourceOutput(invocations, (sourceContext, data) =>
Utils.ErrorWrapper(sourceContext, data.Invocation,() => GenerateSource(sourceContext, data)));
var collectedInvocations = invocations.Collect();

context.RegisterImplementationSourceOutput(collectedInvocations, GenerateSource);
}

private void GenerateSource(
SourceProductionContext context,
(InvocationExpressionSyntax Invocation, SemanticModel SemanticModel) input)
ImmutableArray<(InvocationExpressionSyntax Invocation, SemanticModel SemanticModel)> invocations)
{
var (invocation, semanticModel) = input;
if (context.CancellationToken.IsCancellationRequested)
var processed = new HashSet<string>();
foreach (var input in invocations)
{
return;
var (invocation, semanticModel) = input;
if (context.CancellationToken.IsCancellationRequested)
{
return;
}

Utils.ErrorWrapper(
context,
invocation,
() => GenerateFile(context, invocation, semanticModel, processed));
}
}

private static void GenerateFile(SourceProductionContext context, InvocationExpressionSyntax? invocation, SemanticModel? semanticModel, HashSet<string> processed)
{

var resolver = new GraphQLLambdaLikeContextResolver();
var (lambdaContext, error) = resolver.Resolve(invocation, semanticModel, context.CancellationToken).Unwrap();
Expand Down Expand Up @@ -65,6 +81,12 @@ private void GenerateSource(
return;
}

if (processed.Contains(lambdaContext.OperationHash))
{
return;
}

processed.Add(lambdaContext.OperationHash);
context.AddSource($"ZeroQLModuleInitializer.{lambdaContext.OperationHash}.g.cs", source);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
Expand All @@ -18,14 +20,27 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.CreateSyntaxProvider(FindGraphQLRequests,
(c, ct) => (Record: (RecordDeclarationSyntax)c.Node, c.SemanticModel));

context.RegisterImplementationSourceOutput(invocations, (sourceContext, data) =>
Utils.ErrorWrapper(sourceContext, data.Record, () => GenerateSource(sourceContext, data)));
var collectedInvocations = invocations.Collect();

context.RegisterImplementationSourceOutput(collectedInvocations, GenerateSource);
}

private void GenerateSource(SourceProductionContext context,
(RecordDeclarationSyntax Record, SemanticModel SemanticModel) input)
ImmutableArray<(RecordDeclarationSyntax Record, SemanticModel SemanticModel)> invocations)
{
var processed = new HashSet<string>();
foreach (var input in invocations)
{
var (record, semanticModel) = input;
Utils.ErrorWrapper(
context,
record,
() => GenerateFile(context, semanticModel, record, processed));
}
}

private void GenerateFile(SourceProductionContext context, SemanticModel semanticModel, RecordDeclarationSyntax record, HashSet<string> processed)
{
var (record, semanticModel) = input;
var recordSymbol = semanticModel.GetDeclaredSymbol(record);
var graphQLRequest = semanticModel.Compilation.GetTypeByMetadataName("ZeroQL.GraphQL`2");

Expand Down Expand Up @@ -67,6 +82,12 @@ private void GenerateSource(SourceProductionContext context,
return;
}

if (processed.Contains(requestLikeContext.OperationHash))
{
return;
}

processed.Add(requestLikeContext.OperationHash);
context.AddSource($"ZeroQLModuleInitializer.{requestLikeContext.OperationHash}.g.cs", source);
}

Expand Down
6 changes: 4 additions & 2 deletions src/ZeroQL.SourceGenerators/Resolver/GraphQLSourceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static string Resolve(
using ZeroQL.Json;
using ZeroQL.Internal;
#nullable enable
namespace {semanticModel.Compilation.Assembly.Name}
{{
{SourceGeneratorInfo.CodeGenerationAttribute}
Expand All @@ -44,9 +46,9 @@ public static void Init()
}};
}}
public static async Task<GraphQLResult<{context.QueryTypeName}>> Execute(IGraphQLClient qlClient, string queryKey, object variablesObject, CancellationToken cancellationToken)
public static async Task<GraphQLResult<{context.QueryTypeName}>> Execute(IGraphQLClient qlClient, string queryKey, object? variablesObject, CancellationToken cancellationToken)
{{
var variables = ({context.RequestExecutorInputSymbol.ToGlobalName()})variablesObject;
var variables = ({context.RequestExecutorInputSymbol.ToGlobalName()})variablesObject!;
var qlResponse = await qlClient.QueryPipeline.ExecuteAsync<{context.QueryTypeName}>(qlClient.HttpClient, queryKey, variablesObject, cancellationToken, queryRequest =>
{{
{GraphQLUploadResolver.GenerateRequestPreparations(graphQLInputTypeSafeName, typeInfo)}
Expand Down
12 changes: 12 additions & 0 deletions src/ZeroQL.Tests/CLI/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.CompilerServices;

namespace ZeroQL.Tests.CLI;

public static class ModuleInitializer
{
[ModuleInitializer]
public static void Initialize()
{
VerifyNodaTime.Enable();
}
}
2 changes: 2 additions & 0 deletions src/ZeroQL.Tests/Core/IntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Xunit;
using ZeroQL.Tests.Core.Fixtures;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace ZeroQL.Tests.Core;

[Collection("Integration")]
Expand Down
1 change: 0 additions & 1 deletion src/ZeroQL.Tests/Data/TestProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static class TestProject

static TestProject()
{
VerifyNodaTime.Enable();
var manager = new AnalyzerManager();
manager.GetProject(@"../../../../TestApp/ZeroQL.TestApp/ZeroQL.TestApp.csproj");
Workspace = manager.GetWorkspace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using ZeroQL.Stores;
using ZeroQL.Json;
using ZeroQL.Internal;

#nullable enable

namespace ZeroQL.TestApp
{
[System.CodeDom.Compiler.GeneratedCode("ZeroQL", "1.0.0.0")]
Expand All @@ -30,9 +32,9 @@ namespace ZeroQL.TestApp
};
}

public static async Task<GraphQLResult<global::GraphQL.TestServer.Query>> Execute(IGraphQLClient qlClient, string queryKey, object variablesObject, CancellationToken cancellationToken)
public static async Task<GraphQLResult<global::GraphQL.TestServer.Query>> Execute(IGraphQLClient qlClient, string queryKey, object? variablesObject, CancellationToken cancellationToken)
{
var variables = (global::ZeroQL.Unit)variablesObject;
var variables = (global::ZeroQL.Unit)variablesObject!;
var qlResponse = await qlClient.QueryPipeline.ExecuteAsync<global::GraphQL.TestServer.Query>(qlClient.HttpClient, queryKey, variablesObject, cancellationToken, queryRequest =>
{

Expand Down

0 comments on commit e0f8efd

Please sign in to comment.