diff --git a/src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/Command/ExportCommand.cs b/src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/Command/ExportCommand.cs index 697c904a810..a12ea975d0e 100644 --- a/src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/Command/ExportCommand.cs +++ b/src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/Command/ExportCommand.cs @@ -1,5 +1,6 @@ using System.CommandLine; using System.IO; +using System.Text; using System.Threading; using System.Threading.Tasks; using HotChocolate.Execution; @@ -51,7 +52,7 @@ private static async Task ExecuteAsync( if (output is { }) { - await File.WriteAllTextAsync(output.FullName, sdl, cancellationToken); + await File.WriteAllTextAsync(output.FullName, sdl, Encoding.UTF8, cancellationToken); } else { diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/ComposeCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/ComposeCommand.cs index 33e4187f5a7..b112e605a34 100644 --- a/src/HotChocolate/Fusion/src/CommandLine/Commands/ComposeCommand.cs +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/ComposeCommand.cs @@ -1,5 +1,6 @@ using System.CommandLine; using System.Diagnostics.CodeAnalysis; +using System.IO.Packaging; using System.Text.Json; using System.Text.Json.Serialization; using HotChocolate.Fusion.CommandLine.Helpers; diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs new file mode 100644 index 00000000000..b14c52f8b34 --- /dev/null +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs @@ -0,0 +1,19 @@ +using System.CommandLine; + +namespace HotChocolate.Fusion.CommandLine.Commands; + +/// +/// The export command +/// +internal sealed class ExportCommand : Command +{ + /// + /// Initializes a new instance of . + /// + public ExportCommand() : base("export") + { + Description = "Export commands."; + + AddCommand(new ExportGraphCommand()); + } +} diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs new file mode 100644 index 00000000000..0fee6e25cb6 --- /dev/null +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs @@ -0,0 +1,65 @@ +using System.CommandLine; +using System.Text; +using HotChocolate.Fusion.CommandLine.Helpers; +using HotChocolate.Utilities; +using static System.IO.Path; +using static HotChocolate.Fusion.CommandLine.Extensions; + +namespace HotChocolate.Fusion.CommandLine.Commands; + +internal sealed class ExportGraphCommand : Command +{ + public ExportGraphCommand() : base("graph") + { + var fusionPackageFile = new Option("--package-file"); + fusionPackageFile.AddAlias("--package"); + fusionPackageFile.AddAlias("-p"); + + var graphFile = new Option("--file"); + graphFile.AddAlias("-f"); + + AddOption(fusionPackageFile); + AddOption(graphFile); + + this.SetHandler( + ExecuteAsync, + Bind.FromServiceProvider(), + fusionPackageFile, + graphFile, + Bind.FromServiceProvider()); + } + + private static async Task ExecuteAsync( + IConsole console, + FileInfo? packageFile, + FileInfo? graphFile, + CancellationToken cancellationToken) + { + packageFile ??= new FileInfo(Combine(Environment.CurrentDirectory, "gateway" + FusionPackage)); + + if (!packageFile.Exists) + { + if (Directory.Exists(packageFile.FullName)) + { + packageFile = new FileInfo(Combine(packageFile.FullName, "gateway" + FusionPackage)); + } + else if (!packageFile.Extension.EqualsOrdinal(FusionPackage) && + !packageFile.Extension.EqualsOrdinal(ZipPackage)) + { + packageFile = new FileInfo(packageFile.FullName + FusionPackage); + } + + if (!packageFile.Exists) + { + console.WriteLine($"The package file `{packageFile.FullName}` does not exist."); + return; + } + } + + graphFile ??= new FileInfo(Combine(packageFile.DirectoryName!, "fusion.graphql")); + + await using var package = FusionGraphPackage.Open(packageFile.FullName); + var graph = await package.GetFusionGraphAsync(cancellationToken); + await File.WriteAllTextAsync(graphFile.FullName, graph.ToString(true), Encoding.UTF8, cancellationToken); + } +} \ No newline at end of file diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/RootCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/RootCommand.cs index 4860cd55294..c4f3e3b9c89 100644 --- a/src/HotChocolate/Fusion/src/CommandLine/Commands/RootCommand.cs +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/RootCommand.cs @@ -1,4 +1,5 @@ using System.CommandLine; +using System.Diagnostics.CodeAnalysis; using static System.IO.Path; namespace HotChocolate.Fusion.CommandLine.Commands; @@ -11,11 +12,13 @@ internal sealed class RootCommand : Command /// /// Initializes a new instance of . /// + [RequiresUnreferencedCode("Calls HotChocolate.Fusion.CommandLine.Commands.ComposeCommand.ComposeCommand()")] public RootCommand() : base("fusion") { Description = "A command line tool for a Hot Chocolate Fusion."; AddCommand(new ComposeCommand()); AddCommand(new SubgraphCommand()); + AddCommand(new ExportCommand()); } }