Repository navigation
[Nitro CLI] Add --remove-source-schema option to fusion compose #9926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Nitro/CommandLine/src/CommandLine/Arguments/FusionReplaceSourceSchemaNameArgument.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace ChilliCream.Nitro.CommandLine.Arguments; | ||
|
|
||
| internal sealed class FusionReplaceSourceSchemaNameArgument : Argument<string> | ||
| { | ||
| public const string ArgumentName = "OLD_SOURCE_SCHEMA_NAME"; | ||
|
|
||
| public FusionReplaceSourceSchemaNameArgument() : base(ArgumentName) | ||
| { | ||
| Description = "The name of the source schema to replace"; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/Nitro/CommandLine/src/CommandLine/Arguments/FusionSourceSchemaNameArgument.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace ChilliCream.Nitro.CommandLine.Arguments; | ||
|
|
||
| internal sealed class FusionSourceSchemaNameArgument : Argument<string> | ||
| { | ||
| public const string ArgumentName = "SOURCE_SCHEMA_NAME"; | ||
|
|
||
| public FusionSourceSchemaNameArgument() : base(ArgumentName) | ||
| { | ||
| Description = "The name of the source schema to remove"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Nitro/CommandLine/src/CommandLine/Commands/Fusion/FusionSourceSchemaCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #if !NET9_0_OR_GREATER | ||
| using System.Diagnostics.CodeAnalysis; | ||
| #endif | ||
|
|
||
| namespace ChilliCream.Nitro.CommandLine.Commands.Fusion; | ||
|
|
||
| #if !NET9_0_OR_GREATER | ||
| [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] | ||
| [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] | ||
| #endif | ||
| internal sealed class FusionSourceSchemaCommand : Command | ||
| { | ||
| public FusionSourceSchemaCommand() : base("source-schema") | ||
| { | ||
| Description = "Manage source schemas in a Fusion archive."; | ||
|
|
||
| Subcommands.Add(new FusionSourceSchemaRemoveCommand()); | ||
| Subcommands.Add(new FusionSourceSchemaReplaceCommand()); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
src/Nitro/CommandLine/src/CommandLine/Commands/Fusion/FusionSourceSchemaHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| using System.Text.Json; | ||
| using ChilliCream.Nitro.CommandLine.Helpers; | ||
| using ChilliCream.Nitro.CommandLine.Services; | ||
| using HotChocolate.Fusion; | ||
| using HotChocolate.Fusion.Packaging; | ||
|
|
||
| namespace ChilliCream.Nitro.CommandLine.Commands.Fusion; | ||
|
|
||
| internal static class FusionSourceSchemaHelpers | ||
| { | ||
| public static string ResolveExistingArchiveFile(IFileSystem fileSystem, string archiveFile) | ||
| { | ||
| if (!Path.IsPathRooted(archiveFile)) | ||
| { | ||
| archiveFile = Path.Combine(fileSystem.GetCurrentDirectory(), archiveFile); | ||
| } | ||
|
|
||
| if (!fileSystem.FileExists(archiveFile)) | ||
| { | ||
| throw new ExitException(Messages.ArchiveFileDoesNotExist(archiveFile)); | ||
| } | ||
|
|
||
| return archiveFile; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies a mutation to an in-memory copy of the archive, recomposes it, and only writes | ||
| /// the result back to disk when the recomposition succeeds. The on-disk archive is left | ||
| /// untouched when the mutation or the recomposition fails. | ||
| /// </summary> | ||
| public static async Task<int> ApplyAndRecomposeAsync( | ||
| string archiveFile, | ||
| Func<FusionArchive, Task> mutateAsync, | ||
| Dictionary<string, (SourceSchemaText, JsonDocument)> newSourceSchemas, | ||
| string environment, | ||
| INitroConsole console, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var bytes = await File.ReadAllBytesAsync(archiveFile, cancellationToken); | ||
|
|
||
| var buffer = new MemoryStream(); | ||
| buffer.Write(bytes, 0, bytes.Length); | ||
| buffer.Position = 0; | ||
|
|
||
| using var archive = FusionArchive.Open(buffer, FusionArchiveMode.Update, leaveOpen: true); | ||
|
|
||
| await mutateAsync(archive); | ||
|
|
||
| await using var composeActivity = console.StartActivity( | ||
| "Composing new configuration", | ||
| "Failed to compose new configuration."); | ||
|
|
||
| var (result, compositionLog) = await FusionPublishHelpers.ComposeAsync( | ||
| archive, | ||
| environment, | ||
| newSourceSchemas, | ||
| compositionSettings: null, | ||
| legacyArchive: null, | ||
| cancellationToken); | ||
|
|
||
| if (result.IsSuccess) | ||
| { | ||
| buffer.Position = 0; | ||
| await File.WriteAllBytesAsync(archiveFile, buffer.ToArray(), cancellationToken); | ||
|
|
||
| composeActivity.Success("Composed new configuration."); | ||
|
|
||
| return ExitCodes.Success; | ||
| } | ||
| else | ||
| { | ||
| await composeActivity.FailAllAsync(); | ||
|
|
||
| console.WriteLine(); | ||
| console.WriteLine("## Composition log"); | ||
| console.WriteLine(); | ||
|
|
||
| FusionComposeCommand.WriteCompositionLog( | ||
| compositionLog, | ||
| console.Out, | ||
| false); | ||
|
|
||
| foreach (var error in result.Errors) | ||
| { | ||
| console.Error.WriteErrorLine(error.Message); | ||
| } | ||
|
|
||
| throw new ExitException(); | ||
| } | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
src/Nitro/CommandLine/src/CommandLine/Commands/Fusion/FusionSourceSchemaRemoveCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #if !NET9_0_OR_GREATER | ||
| using System.Diagnostics.CodeAnalysis; | ||
| #endif | ||
| using ChilliCream.Nitro.CommandLine.Arguments; | ||
| using ChilliCream.Nitro.CommandLine.Helpers; | ||
| using ChilliCream.Nitro.CommandLine.Services; | ||
| using HotChocolate.Fusion.Packaging; | ||
|
|
||
| namespace ChilliCream.Nitro.CommandLine.Commands.Fusion; | ||
|
|
||
| #if !NET9_0_OR_GREATER | ||
| [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] | ||
| [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] | ||
| #endif | ||
| internal sealed class FusionSourceSchemaRemoveCommand : Command | ||
| { | ||
| public FusionSourceSchemaRemoveCommand() : base("remove") | ||
| { | ||
| Description = | ||
| "Remove a source schema from a Fusion archive and recompose the remaining source schemas."; | ||
|
|
||
| Arguments.Add(Opt<FusionSourceSchemaNameArgument>.Instance); | ||
|
|
||
| Options.Add(Opt<FusionArchiveFileOption>.Instance); | ||
| Options.Add(Opt<FusionEnvironmentOption>.Instance); | ||
|
|
||
| this.AddGlobalNitroOptions(); | ||
|
|
||
| this.AddExamples( | ||
| """ | ||
| fusion source-schema remove reviews \ | ||
| --archive ./gateway.far | ||
| """); | ||
|
|
||
| this.SetActionWithExceptionHandling(ExecuteAsync); | ||
| } | ||
|
|
||
| private static async Task<int> ExecuteAsync( | ||
| ICommandServices services, | ||
| ParseResult parseResult, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var console = services.GetRequiredService<INitroConsole>(); | ||
| var fileSystem = services.GetRequiredService<IFileSystem>(); | ||
| var environmentVariables = services.GetRequiredService<IEnvironmentVariableProvider>(); | ||
|
|
||
| var sourceSchemaName = parseResult.GetRequiredValue(Opt<FusionSourceSchemaNameArgument>.Instance); | ||
| var archiveFile = parseResult.GetRequiredValue(Opt<FusionArchiveFileOption>.Instance); | ||
| var environment = parseResult.GetValue(Opt<FusionEnvironmentOption>.Instance); | ||
|
|
||
| archiveFile = FusionSourceSchemaHelpers.ResolveExistingArchiveFile(fileSystem, archiveFile); | ||
|
|
||
| environment ??= environmentVariables.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"; | ||
|
|
||
| return await FusionSourceSchemaHelpers.ApplyAndRecomposeAsync( | ||
| archiveFile, | ||
| MutateAsync, | ||
| [], | ||
| environment, | ||
| console, | ||
| cancellationToken); | ||
|
|
||
| async Task MutateAsync(FusionArchive archive) | ||
| { | ||
| var names = (await archive.GetSourceSchemaNamesAsync(cancellationToken)).ToList(); | ||
|
|
||
| if (!names.Contains(sourceSchemaName)) | ||
| { | ||
| throw new ExitException( | ||
| Messages.SourceSchemaDoesNotExistInArchive(sourceSchemaName, archiveFile)); | ||
| } | ||
|
|
||
| if (names.Count == 1) | ||
| { | ||
| throw new ExitException(Messages.CannotRemoveLastSourceSchema(sourceSchemaName)); | ||
| } | ||
|
|
||
| await archive.RemoveSourceSchemaConfigurationAsync(sourceSchemaName, cancellationToken); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.