-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jakenuts/feature-singular-addcommand
Feature singular addcommand
- Loading branch information
Showing
11 changed files
with
216 additions
and
43 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/OtherCommand.cs
This file contains 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,45 @@ | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
namespace Community.Extensions.Spectre.Cli.Hosting.Sample.Commands; | ||
|
||
/// <summary> | ||
/// Another command, just to show that multiple commands can be added | ||
/// </summary> | ||
public class OtherCommand : AsyncCommand<OtherCommand.Options> | ||
{ | ||
private readonly IAnsiConsole _console; | ||
|
||
/// <summary> | ||
/// Creates a OtherCommand with access to the console and logging | ||
/// </summary> | ||
/// <param name="console"></param> | ||
/// <param name="log"></param> | ||
public OtherCommand(IAnsiConsole console, ILogger<HelloCommand> log) | ||
{ | ||
_console = console; | ||
} | ||
|
||
/// <summary>Executes the command.</summary> | ||
/// <param name="context">The command context.</param> | ||
/// <param name="options">The command options.</param> | ||
/// <returns>An integer indicating whether or not the command executed successfully.</returns> | ||
public override async Task<int> ExecuteAsync(CommandContext context, Options options) | ||
{ | ||
_console.MarkupLineInterpolated($"[springgreen2_1] Other {options.Stuff}![/]"); | ||
|
||
return 0; | ||
} | ||
|
||
[Description("OtherOptions")] | ||
public class Options : CommandSettings | ||
{ | ||
[Description("Other Stuff")] | ||
[CommandArgument(0, "<stuff>")] | ||
public string? Stuff { get; set; } | ||
} | ||
} |
29 changes: 15 additions & 14 deletions
29
src/Community.Extensions.Spectre.Cli.Hosting.Sample/Program.cs
This file contains 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
19 changes: 17 additions & 2 deletions
19
src/Community.Extensions.Spectre.Cli.Hosting.Sample/Properties/launchSettings.json
This file contains 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 |
---|---|---|
@@ -1,8 +1,23 @@ | ||
{ | ||
"profiles": { | ||
"Community.Extensions.Spectre.Cli.Hosting.Sample": { | ||
"Sample": { | ||
"commandName": "Project", | ||
"commandLineArgs": "You -p Sadie" | ||
//"commandLineArgs": "You -p Sadie" | ||
//"commandLineArgs": "other stuff" | ||
"commandLineArgs": "--help" | ||
}, | ||
"Interactive": { | ||
"commandName": "Executable", | ||
"executablePath": "pwsh.exe", | ||
"commandLineArgs": "-NoExit -c \"Set-Alias -Name hello -Value \"$(TargetDir)$(AssemblyName).exe\"", | ||
"workingDirectory": "$(ProjectDir)" | ||
} | ||
/*, For reference only | ||
"WT": { | ||
"commandName": "Executable", | ||
"executablePath": "wt.exe", | ||
"commandLineArgs": "pwsh.exe -NoExit -c \"Set-Alias -Name hello -Value \"$(TargetDir)$(AssemblyName).exe\"", | ||
"workingDirectory": "$(ProjectDir)" | ||
}*/ | ||
} | ||
} |
This file contains 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
24 changes: 24 additions & 0 deletions
24
src/Community.Extensions.Spectre.Cli.Hosting/Internal/CommandRegistration.TCommand.cs
This file contains 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,24 @@ | ||
using Spectre.Console.Cli; | ||
|
||
namespace Community.Extensions.Spectre.Cli.Hosting.Internal; | ||
|
||
/// <summary> | ||
/// A typed registration class for commands with their types and name | ||
/// </summary> | ||
/// <param name="Name"></param> | ||
/// <typeparam name="TCommand"></typeparam> | ||
public record CommandRegistration<TCommand>(string Name, Action<ICommandConfigurator>? CommandConfigurator = null) | ||
: CommandRegistration(typeof(TCommand), Name) where TCommand : class, ICommand | ||
{ | ||
/// <summary> | ||
/// </summary> | ||
/// <param name="configuration"></param> | ||
public override void Configure(IConfigurator configuration) | ||
{ | ||
// Add the command to Spectre's configuration | ||
var cmdConfig = configuration.AddCommand<TCommand>(Name); | ||
|
||
// Optionally configure the command | ||
CommandConfigurator?.Invoke(cmdConfig); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Community.Extensions.Spectre.Cli.Hosting/Internal/CommandRegistration.cs
This file contains 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,16 @@ | ||
using Spectre.Console.Cli; | ||
|
||
namespace Community.Extensions.Spectre.Cli.Hosting.Internal; | ||
|
||
/// <summary> | ||
/// A base registration class for commands with their types and name | ||
/// </summary> | ||
/// <param name="CommandType"></param> | ||
/// <param name="Name"></param> | ||
public abstract record CommandRegistration(Type CommandType, string Name) | ||
{ | ||
/// <summary> | ||
/// </summary> | ||
/// <param name="configuration"></param> | ||
public abstract void Configure(IConfigurator configuration); | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Community.Extensions.Spectre.Cli.Hosting/Internal/CommandRegistrationExtensions.cs
This file contains 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,63 @@ | ||
using System.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Hosting.Internal; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
namespace Community.Extensions.Spectre.Cli.Hosting.Internal; | ||
|
||
/// <summary> | ||
/// Extends <see cref="IHostBuilder" /> with SpectreConsole commands. | ||
/// </summary> | ||
public static class CommandRegistrationExtensions | ||
{ | ||
|
||
/// <summary> | ||
/// Returns registered commands | ||
/// </summary> | ||
/// <param name="serviceProvider"></param> | ||
/// <returns></returns> | ||
public static IEnumerable<CommandRegistration> GetRegisteredCommands(this IServiceProvider serviceProvider) => | ||
serviceProvider.GetServices<CommandRegistration>(); | ||
|
||
/// <summary> | ||
/// Registers a command with it's primary type, name and optional configuration action | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="name"></param> | ||
/// <param name="commandConfigurator"></param> | ||
/// <typeparam name="TCommand"></typeparam> | ||
/// <returns></returns> | ||
public static IServiceCollection RegisterCommand<TCommand>(this IServiceCollection services, string name, | ||
Action<ICommandConfigurator>? commandConfigurator = null) | ||
where TCommand : class, ICommand | ||
{ | ||
return services.AddTransient<CommandRegistration, CommandRegistration<TCommand>>(c => | ||
new CommandRegistration<TCommand>(name, commandConfigurator)); | ||
} | ||
|
||
/// <summary> | ||
/// Adds registered commands to the provided app and allows further customization of the app and commands | ||
/// </summary> | ||
/// <param name="app"></param> | ||
/// <param name="provider"></param> | ||
/// <param name="configureCommandApp"></param> | ||
/// <returns></returns> | ||
internal static ICommandApp ConfigureAppAndRegisteredCommands(this ICommandApp app, IServiceProvider provider, Action<IConfigurator>? configureCommandApp = null) | ||
{ | ||
app.Configure(config => | ||
{ | ||
// Add/Configure registered commands | ||
foreach (var cmd in provider.GetRegisteredCommands()) | ||
{ | ||
cmd.Configure(config); | ||
} | ||
// Optionally allow caller to configure the command app | ||
configureCommandApp?.Invoke(config); | ||
}); | ||
|
||
return app; | ||
} | ||
} |
This file contains 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 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 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 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