private void Clean(ClientGeneratorContext context) { context.Log.BeginClean(); try { foreach (string fileName in Directory.GetFiles(context.OutputDirectory, "*.cs")) { if (!context.FileNames.Contains(fileName)) { context.Log.RemoveFile(fileName); File.Delete(fileName); } } } catch (Exception ex) { context.Log.Error(ex); context.ReportError(ex); } finally { context.Log.EndClean(); } }
private bool TryGenerateClient( ClientGeneratorContext context, [NotNullWhen(true)] out CSharpGeneratorResult?result) { context.Log.BeginGenerateCode(); try { var name = context.Settings.Name; var @namespace = context.GetNamespace(); var documents = context.GetDocuments(); result = new CSharpGenerator().Generate(documents, name, @namespace); return(true); } catch (GraphQLException ex) { context.ReportError(ex.Errors); result = null; return(false); } catch (Exception ex) { context.Log.Error(ex); context.ReportError(ex); result = null; return(false); } finally { context.Log.EndGenerateCode(); } }
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. foreach (CSharpDocument document in result.CSharpDocuments) { WriteDocument(context, document); } // remove files that are now obsolete Clean(context); } catch (Exception ex) { context.Log.Error(ex); context.ReportError(ex); } }
private void Clean(ClientGeneratorContext context) { context.Log.BeginClean(); try { if (Directory.Exists(context.OutputDirectory)) { foreach (string fileName in GetGeneratedFiles(context.OutputDirectory)) { if (!context.Settings.EmitGeneratedCode || !context.FileNames.Contains(fileName)) { context.Log.RemoveFile(fileName); File.Delete(fileName); } } if (!GetGeneratedFiles(context.OutputDirectory).Any()) { Directory.Delete(context.OutputDirectory, true); } } } catch (Exception ex) { context.Log.Error(ex); context.ReportError(ex); } finally { context.Log.EndClean(); } }
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); } }
private void WriteGraphQLQuery( ClientGeneratorContext context, string persistedQueryDirectory, SourceDocument document) { string documentName = document.Hash + ".graphql"; string fileName = IOPath.Combine(persistedQueryDirectory, documentName); // we only write the file if it does not exist to not trigger // dotnet watch. if (!File.Exists(fileName)) { context.Log.WriteDocument(documentName); File.WriteAllText(fileName, document.SourceText, Encoding.UTF8); } }
private void WriteDocument( ClientGeneratorContext context, CSharpDocument document) { string documentName = $"{document.Name}.{context.Settings.Name}.StrawberryShake.cs"; context.Log.WriteDocument(documentName); var fileName = IOPath.Combine(context.OutputDirectory, documentName); var sourceText = SourceText.From(document.SourceText, Encoding.UTF8); context.FileNames.Add(fileName); context.Execution.AddSource(documentName, sourceText); WriteFile(fileName, document.SourceText); }
private void WriteGraphQLQuery( ClientGeneratorContext context, string persistedQueryDirectory, SourceDocument document) { string documentName = document.Hash + ".graphql"; string fileName = IOPath.Combine(persistedQueryDirectory, documentName); context.Log.WriteDocument(documentName); if (File.Exists(fileName)) { File.Delete(fileName); } File.WriteAllText(fileName, document.SourceText, Encoding.UTF8); }
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, result)) { 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 = new FileDocumentWriter( keepFileName: context.Settings.UseSingleFile); 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); } }
private bool TryGenerateClient( ClientGeneratorContext context, [NotNullWhen(true)] out CSharpGeneratorResult?result) { context.Log.BeginGenerateCode(); try { var settings = new CSharpGeneratorSettings { ClientName = context.Settings.Name, Namespace = context.GetNamespace(), RequestStrategy = context.Settings.RequestStrategy, StrictSchemaValidation = context.Settings.StrictSchemaValidation, NoStore = context.Settings.NoStore, InputRecords = context.Settings.Records.Inputs, RazorComponents = context.Settings.RazorComponents, EntityRecords = context.Settings.Records.Entities, SingleCodeFile = context.Settings.UseSingleFile, HashProvider = context.Settings.HashAlgorithm.ToLowerInvariant() switch { "sha1" => new Sha1DocumentHashProvider(HashFormat.Hex), "sha256" => new Sha256DocumentHashProvider(HashFormat.Hex), "md5" => new MD5DocumentHashProvider(HashFormat.Hex), _ => new Sha1DocumentHashProvider(HashFormat.Hex) } }; if (context.Settings.TransportProfiles? .Where(t => !string.IsNullOrEmpty(t.Name)) .ToList() is { Count : > 0 } profiles) { settings.TransportProfiles.Clear(); foreach (var profile in profiles) { settings.TransportProfiles.Add( new TransportProfile( profile.Name, profile.Default, profile.Query, profile.Mutation, profile.Subscription)); } } var persistedQueryDirectory = context.GetPersistedQueryDirectory(); context.Log.SetGeneratorSettings(settings); context.Log.SetPersistedQueryLocation(persistedQueryDirectory); if (settings.RequestStrategy == RequestStrategy.PersistedQuery && persistedQueryDirectory is null) { settings.RequestStrategy = RequestStrategy.Default; } result = CSharpGenerator.Generate(context.GetDocuments(), settings); return(true); } catch (GraphQLException ex) { context.ReportError(ex.Errors); result = null; return(false); } catch (Exception ex) { context.Log.Error(ex); context.ReportError(ex); result = null; return(false); } finally { context.Log.EndGenerateCode(); } }
public void Begin(GraphQLConfig config, ClientGeneratorContext context) { }