Skip to content

Commit 6a81fc6

Browse files
Add fusion publish command to CLI (#8606)
1 parent 6b5920b commit 6a81fc6

13 files changed

+720
-240
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.CommandLine/Commands/ComposeCommand.cs

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
using System.Threading.Channels;
66
using HotChocolate.Buffers;
77
using HotChocolate.Fusion.Logging;
8+
using HotChocolate.Fusion.Logging.Contracts;
89
using HotChocolate.Fusion.Options;
910
using HotChocolate.Fusion.Packaging;
11+
using HotChocolate.Fusion.Results;
12+
using HotChocolate.Types.Mutable;
1013
using static HotChocolate.Fusion.Properties.CommandLineResources;
1114

1215
namespace HotChocolate.Fusion.CommandLine;
@@ -361,7 +364,7 @@ await ComposeAsync(
361364
}
362365
}
363366

364-
private static bool IsSchemaFile(string? fileName)
367+
public static bool IsSchemaFile(string? fileName)
365368
{
366369
if (fileName is null)
367370
{
@@ -381,23 +384,61 @@ private static async Task<int> ComposeAsync(
381384
bool printSchema,
382385
CancellationToken cancellationToken)
383386
{
384-
environment ??= Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
385-
386-
Dictionary<string, (SourceSchemaText, JsonDocument)> sourceSchemas;
387+
using var archive = File.Exists(archiveFile)
388+
? FusionArchive.Open(archiveFile, mode: FusionArchiveMode.Update)
389+
: FusionArchive.Create(archiveFile);
387390

388391
try
389392
{
390-
sourceSchemas = await ReadSourceSchemasAsync(sourceSchemaFiles, cancellationToken);
393+
var compositionLog = new CompositionLog();
394+
395+
var result = await ComposeAsync(
396+
compositionLog,
397+
sourceSchemaFiles,
398+
archive,
399+
environment,
400+
enableGlobalObjectIdentification,
401+
cancellationToken);
402+
403+
WriteCompositionLog(
404+
compositionLog,
405+
writer: result.IsSuccess ? console.Out : console.Error,
406+
writeAsGraphQLComments: result.IsSuccess && printSchema);
407+
408+
if (result.IsFailure)
409+
{
410+
foreach (var error in result.Errors)
411+
{
412+
console.Error.WriteLine(error.Message);
413+
}
414+
415+
return 1;
416+
}
417+
418+
console.Out.WriteLine(printSchema
419+
? result.Value.ToString()
420+
: string.Format(ComposeCommand_CompositeSchemaFile_Written, archiveFile));
421+
422+
return 0;
391423
}
392424
catch (Exception e)
393425
{
394426
console.Error.WriteLine(e.Message);
395427
return 1;
396428
}
429+
}
397430

398-
using var archive = File.Exists(archiveFile)
399-
? FusionArchive.Open(archiveFile, mode: FusionArchiveMode.Update)
400-
: FusionArchive.Create(archiveFile);
431+
public static async Task<CompositionResult<MutableSchemaDefinition>> ComposeAsync(
432+
ICompositionLog compositionLog,
433+
List<string> sourceSchemaFiles,
434+
FusionArchive archive,
435+
string? environment,
436+
bool enableGlobalObjectIdentification,
437+
CancellationToken cancellationToken)
438+
{
439+
environment ??= Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
440+
441+
var sourceSchemas = await ReadSourceSchemasAsync(sourceSchemaFiles, cancellationToken);
401442
var schemaNames = new SortedSet<string>(StringComparer.Ordinal);
402443

403444
await UpdateArchiveMetadata(archive, schemaNames, cancellationToken);
@@ -442,27 +483,16 @@ await archive.TryGetSourceSchemaConfigurationAsync(
442483
EnableGlobalObjectIdentification = enableGlobalObjectIdentification
443484
};
444485

445-
var compositionLog = new CompositionLog();
446486
var schemaComposer = new SchemaComposer(
447487
sourceSchemas.Values.Select(t => t.Item1),
448488
schemaComposerOptions,
449489
compositionLog);
450490

451491
var result = schemaComposer.Compose();
452492

453-
WriteCompositionLog(
454-
compositionLog,
455-
writer: result.IsSuccess ? console.Out : console.Error,
456-
writeAsGraphQLComments: result.IsSuccess && printSchema);
457-
458493
if (result.IsFailure)
459494
{
460-
foreach (var error in result.Errors)
461-
{
462-
console.Error.WriteLine(error.Message);
463-
}
464-
465-
return 1;
495+
return result;
466496
}
467497

468498
bufferWriter.Reset();
@@ -479,11 +509,7 @@ await archive.SetGatewayConfigurationAsync(
479509

480510
await archive.CommitAsync(cancellationToken);
481511

482-
console.Out.WriteLine(printSchema
483-
? result.Value.ToString()
484-
: string.Format(ComposeCommand_CompositeSchemaFile_Written, archiveFile));
485-
486-
return 0;
512+
return result;
487513
}
488514

489515
private static async Task UpdateArchiveMetadata(
@@ -510,7 +536,7 @@ private static async Task UpdateArchiveMetadata(
510536
await archive.SetArchiveMetadataAsync(metadata, cancellationToken);
511537
}
512538

513-
private static void WriteCompositionLog(
539+
public static void WriteCompositionLog(
514540
CompositionLog compositionLog,
515541
IStandardStreamWriter writer,
516542
bool writeAsGraphQLComments)

src/HotChocolate/Fusion-vnext/src/Fusion.CommandLine/HotChocolate.Fusion.CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<InternalsVisibleTo Include="HotChocolate.Fusion.CommandLine.Tests" />
15+
<InternalsVisibleTo Include="ChilliCream.Nitro.CommandLine.Cloud" />
1516
</ItemGroup>
1617

1718
<ItemGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using ChilliCream.Nitro.CommandLine.Cloud.Option;
2+
3+
namespace ChilliCream.Nitro.CommandLine.Cloud.Commands.Fusion;
4+
5+
internal sealed class FusionCommand : Command
6+
{
7+
public FusionCommand() : base("fusion")
8+
{
9+
AddGlobalOption(Opt<CloudUrlOption>.Instance);
10+
AddGlobalOption(Opt<ApiKeyOption>.Instance);
11+
12+
AddCommand(new FusionPublishCommand());
13+
}
14+
}

0 commit comments

Comments
 (0)