Ejemplo n.º 1
0
        private void Execute(ClientGeneratorContext context)
        {
            try
            {
                CreateDirectoryIfNotExists(context.OutputDirectory);

                if (!TryGenerateClient(context, out CSharpGeneratorResult? result))
                {
                    // there were unexpected errors and we will stop generating this client.
                    return;
                }

                if (result.HasErrors())
                {
                    // if we have generator errors like invalid GraphQL syntax we will also stop.
                    context.ReportError(result.Errors);
                    return;
                }

                // If the generator has no errors we will write the documents.
                IDocumentWriter writer = context.Settings.UseSingleFile
                    ? new SingleFileDocumentWriter()
                    : new FileDocumentWriter();

                foreach (SourceDocument document in
                         result.Documents.Where(t => t.Kind == SourceDocumentKind.CSharp))
                {
                    writer.WriteDocument(context, document);
                }

                writer.Flush();

                // if we have persisted query support enabled we need to write the query files.
                string?persistedQueryDirectory = context.GetPersistedQueryDirectory();
                if (context.Settings.RequestStrategy == RequestStrategy.PersistedQuery &&
                    persistedQueryDirectory is not null)
                {
                    if (!Directory.Exists(persistedQueryDirectory))
                    {
                        Directory.CreateDirectory(persistedQueryDirectory);
                    }

                    foreach (SourceDocument document in
                             result.Documents.Where(t => t.Kind == SourceDocumentKind.GraphQL))
                    {
                        WriteGraphQLQuery(context, persistedQueryDirectory, document);
                    }
                }

                // remove files that are now obsolete
                Clean(context);
            }
            catch (Exception ex)
            {
                context.Log.Error(ex);
                context.ReportError(ex);
            }
        }
Ejemplo n.º 2
0
        private void Execute(ClientGeneratorContext context)
        {
            try
            {
                var hasErrors = !TryGenerateClient(context, out CSharpGeneratorResult? result);

                // Ensure that all needed packages are installed.
                if (!EnsurePreconditionsAreMet(context.Execution, context.Settings))
                {
                    return;
                }

                if (result?.HasErrors() ?? false)
                {
                    // if we have generator errors like invalid GraphQL syntax we will also stop.
                    context.ReportError(result.Errors);
                    hasErrors = true;
                }

                // If the generator has no errors we will write the documents.
                IDocumentWriter writer = context.Settings.UseSingleFile
                    ? new SingleFileDocumentWriter()
                    : new FileDocumentWriter();

                IReadOnlyList <SourceDocument> documents = hasErrors || result is null
                    ? context.GetLastSuccessfulGeneratedSourceDocuments()
                    : result.Documents;

                if (documents.Count == 0)
                {
                    return;
                }

                if (!hasErrors && result is not null && result.Documents.Count > 0)
                {
                    context.PreserveSourceDocuments(result.Documents);
                }

                foreach (SourceDocument document in documents.SelectCSharp())
                {
                    writer.WriteDocument(context, document);
                }

                writer.Flush();

                // if we have persisted query support enabled we need to write the query files.
                var persistedQueryDirectory = context.GetPersistedQueryDirectory();
                if (context.Settings.RequestStrategy == RequestStrategy.PersistedQuery &&
                    persistedQueryDirectory is not null)
                {
                    if (!Directory.Exists(persistedQueryDirectory))
                    {
                        Directory.CreateDirectory(persistedQueryDirectory);
                    }

                    foreach (SourceDocument document in documents.SelectGraphQL())
                    {
                        WriteGraphQLQuery(context, persistedQueryDirectory, document);
                    }
                }

                // remove files that are now obsolete
                Clean(context);
            }
            catch (Exception ex)
            {
                context.Log.Error(ex);
                context.ReportError(ex);
            }
        }