-
Notifications
You must be signed in to change notification settings - Fork 3
Add pre and post processor support to cli hosting #76
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8012d54
Refactor `EnableHelp` functionality to support multiple `HelpCommandK…
darthsharp 0bebd4c
Introduce pre- and post-processors in CLI framework
darthsharp 2dfa381
Refactor and extend CLI processing framework
darthsharp caa5b2d
Add unit tests for `DefaultCliHost` to verify pre-and post-processor …
darthsharp efc2666
Annotate processors and builder extension methods with `[UsedImplicit…
darthsharp 45a0173
Add `[PublicAPI]` annotations and minor cleanup
darthsharp 3ce0b78
Add pre/post-processor exception handling and refactor processor exec…
darthsharp d7cbf47
Add unit tests to verify exception handling for pre- and post-process…
darthsharp 0c0d6de
Add methods for pre- and post-processor registration and header/foote…
darthsharp 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
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
8 changes: 8 additions & 0 deletions
8
source/Cli/CreativeCoders.Cli.Core/CliProcessorExecutionCondition.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,8 @@ | ||
| namespace CreativeCoders.Cli.Core; | ||
|
|
||
| public enum CliProcessorExecutionCondition | ||
| { | ||
| Always, | ||
| OnlyOnHelp, | ||
| OnlyOnCommand | ||
| } |
2 changes: 1 addition & 1 deletion
2
...i/CreativeCoders.Cli.Hosting/CliResult.cs → .../Cli/CreativeCoders.Cli.Core/CliResult.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
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 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace CreativeCoders.Cli.Core; | ||
|
|
||
| [PublicAPI] | ||
| public interface ICliPostProcessor | ||
| { | ||
| Task ExecuteAsync(CliResult cliResult); | ||
|
|
||
| CliProcessorExecutionCondition ExecutionCondition { get; } | ||
| } |
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 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace CreativeCoders.Cli.Core; | ||
|
|
||
| [PublicAPI] | ||
| public interface ICliPreProcessor | ||
| { | ||
| Task ExecuteAsync(string[] args); | ||
|
|
||
| CliProcessorExecutionCondition ExecutionCondition { get; } | ||
| } |
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
125 changes: 125 additions & 0 deletions
125
source/Cli/CreativeCoders.Cli.Hosting/CliHostBuilderExtensions.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,125 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using CreativeCoders.Cli.Core; | ||
| using CreativeCoders.Cli.Hosting.PreProcessors; | ||
|
|
||
| namespace CreativeCoders.Cli.Hosting; | ||
|
|
||
| [ExcludeFromCodeCoverage] | ||
| public static class CliHostBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Registers a pre-processor to print a header as plain text. The header is displayed when the CLI application | ||
| /// is executed, before the command processing, based on the specified execution condition. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The <see cref="ICliHostBuilder"/> instance to which the pre-processor is being added. | ||
| /// </param> | ||
| /// <param name="lines"> | ||
| /// An enumerable collection of strings representing the lines of text to be displayed in the header. | ||
| /// Each string represents an individual line. | ||
| /// </param> | ||
| /// <param name="executionCondition"> | ||
| /// A value from <see cref="CliProcessorExecutionCondition"/> that determines when the header should be displayed. | ||
| /// Defaults to <see cref="CliProcessorExecutionCondition.Always"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="ICliHostBuilder"/> instance for method chaining. | ||
| /// </returns> | ||
| public static ICliHostBuilder PrintHeaderText(this ICliHostBuilder builder, IEnumerable<string> lines, | ||
| CliProcessorExecutionCondition executionCondition = CliProcessorExecutionCondition.Always) | ||
| { | ||
| return builder.RegisterPreProcessor<PrintHeaderPreProcessor>(x => | ||
| { | ||
| x.Lines = lines; | ||
| x.PlainText = true; | ||
| x.ExecutionCondition = executionCondition; | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Registers a pre-processor to print a header using markup text. The header is displayed during the CLI application | ||
| /// execution, before the command processing, based on the specified execution condition. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The <see cref="ICliHostBuilder"/> instance to which the pre-processor is being added. | ||
| /// </param> | ||
| /// <param name="lines"> | ||
| /// An enumerable collection of strings representing the lines of markup text to be displayed in the header. | ||
| /// The markup can include formatting instructions that are interpreted during rendering. | ||
| /// </param> | ||
| /// <param name="executionCondition"> | ||
| /// A value from <see cref="CliProcessorExecutionCondition"/> that determines when the header should be displayed. | ||
| /// Defaults to <see cref="CliProcessorExecutionCondition.Always"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="ICliHostBuilder"/> instance for method chaining. | ||
| /// </returns> | ||
| public static ICliHostBuilder PrintHeaderMarkup(this ICliHostBuilder builder, IEnumerable<string> lines, | ||
| CliProcessorExecutionCondition executionCondition = CliProcessorExecutionCondition.Always) | ||
| { | ||
| return builder.RegisterPreProcessor<PrintHeaderPreProcessor>(x => | ||
| { | ||
| x.Lines = lines; | ||
| x.PlainText = false; | ||
| x.ExecutionCondition = executionCondition; | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Registers a post-processor to print a footer as plain text. The footer is displayed after the command processing | ||
| /// is complete, based on the specified execution condition. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The <see cref="ICliHostBuilder"/> instance to which the post-processor is being added. | ||
| /// </param> | ||
| /// <param name="lines"> | ||
| /// An enumerable collection of strings representing the lines of text to be displayed in the footer. | ||
| /// Each string represents an individual line. | ||
| /// </param> | ||
| /// <param name="executionCondition"> | ||
| /// A value from <see cref="CliProcessorExecutionCondition"/> that determines when the footer should be displayed. | ||
| /// Defaults to <see cref="CliProcessorExecutionCondition.Always"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="ICliHostBuilder"/> instance for method chaining. | ||
| /// </returns> | ||
| public static ICliHostBuilder PrintFooterText(this ICliHostBuilder builder, IEnumerable<string> lines, | ||
| CliProcessorExecutionCondition executionCondition = CliProcessorExecutionCondition.Always) | ||
| { | ||
| return builder.RegisterPostProcessor<PrintFooterPostProcessor>(x => | ||
| { | ||
| x.Lines = lines; | ||
| x.PlainText = true; | ||
| x.ExecutionCondition = executionCondition; | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Registers a post-processor to print a footer using markup formatting. The footer is displayed | ||
| /// after the command execution, based on the specified execution condition. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The <see cref="ICliHostBuilder"/> instance to which the post-processor is being added. | ||
| /// </param> | ||
| /// <param name="lines"> | ||
| /// An enumerable collection of strings representing the lines of footer text to be displayed. | ||
| /// Each string supports markup formatting for better visual representation. | ||
| /// </param> | ||
| /// <param name="executionCondition"> | ||
| /// A value from <see cref="CliProcessorExecutionCondition"/> that determines when the footer should be displayed. | ||
| /// Defaults to <see cref="CliProcessorExecutionCondition.Always"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="ICliHostBuilder"/> instance for method chaining. | ||
| /// </returns> | ||
| public static ICliHostBuilder PrintFooterMarkup(this ICliHostBuilder builder, IEnumerable<string> lines, | ||
| CliProcessorExecutionCondition executionCondition = CliProcessorExecutionCondition.Always) | ||
| { | ||
| return builder.RegisterPostProcessor<PrintFooterPostProcessor>(x => | ||
| { | ||
| x.Lines = lines; | ||
| x.PlainText = false; | ||
| x.ExecutionCondition = executionCondition; | ||
| }); | ||
| } | ||
| } |
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,6 @@ | ||
| namespace CreativeCoders.Cli.Hosting; | ||
|
|
||
| public class CliHostSettings | ||
| { | ||
| public bool UseValidation { get; init; } | ||
| } |
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
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.