forked from mayuki/Cocona
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw exceptions in Run when building the application.
- Loading branch information
Showing
27 changed files
with
298 additions
and
265 deletions.
There are no files selected for viewing
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,73 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Cocona.Application; | ||
using Cocona.Command.Dispatcher; | ||
using Cocona.CommandLine; | ||
using Cocona.Internal; | ||
using Cocona.Resources; | ||
|
||
namespace Cocona.Command; | ||
|
||
public class CoconaBootstrapper : ICoconaBootstrapper | ||
{ | ||
private readonly ICoconaCommandLineArgumentProvider _commandLineArgumentProvider; | ||
private readonly ICoconaCommandProvider _commandProvider; | ||
private readonly ICoconaCommandResolver _commandResolver; | ||
private readonly ICoconaCommandDispatcher _dispatcher; | ||
private readonly ICoconaConsoleProvider _console; | ||
|
||
private CommandCollection? _commandCollection; | ||
public CoconaBootstrapper( | ||
ICoconaCommandLineArgumentProvider commandLineArgumentProvider, | ||
ICoconaCommandProvider commandProvider, | ||
ICoconaCommandResolver commandResolver, | ||
ICoconaCommandDispatcher dispatcher, | ||
ICoconaConsoleProvider console | ||
) | ||
{ | ||
_commandLineArgumentProvider = commandLineArgumentProvider; | ||
_commandProvider = commandProvider; | ||
_commandResolver = commandResolver; | ||
_dispatcher = dispatcher; | ||
_console = console; | ||
} | ||
|
||
public void Initialize() | ||
{ | ||
_commandCollection = _commandProvider.GetCommandCollection(); | ||
} | ||
|
||
public async ValueTask<int> RunAsync(CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
var resolved = _commandResolver.ParseAndResolve(_commandCollection ?? throw new CoconaException("Call Initialize before RunAsync"), _commandLineArgumentProvider.GetArguments()); | ||
return await _dispatcher.DispatchAsync(resolved, cancellationToken); | ||
} | ||
catch (CommandNotFoundException cmdNotFoundEx) | ||
{ | ||
if (string.IsNullOrWhiteSpace(cmdNotFoundEx.Command)) | ||
{ | ||
_console.Error.WriteLine(string.Format(Strings.Dispatcher_Error_CommandNotFound, cmdNotFoundEx.Message)); | ||
} | ||
else | ||
{ | ||
_console.Error.WriteLine(string.Format(Strings.Dispatcher_Error_NotACommand, cmdNotFoundEx.Command)); | ||
} | ||
|
||
var similarCommands = cmdNotFoundEx.ImplementedCommands.All.Where(x => Levenshtein.GetDistance(cmdNotFoundEx.Command.ToLowerInvariant(), x.Name.ToLowerInvariant()) < 3).ToArray(); | ||
if (similarCommands.Any()) | ||
{ | ||
_console.Error.WriteLine(); | ||
_console.Error.WriteLine(Strings.Dispatcher_Error_SimilarCommands); | ||
foreach (var c in similarCommands) | ||
{ | ||
_console.Error.WriteLine($" {c.Name}"); | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
src/Cocona.Core/Command/Dispatcher/ICoconaCommandDispatcher.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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
using System.Threading; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cocona.Command.Dispatcher | ||
{ | ||
public interface ICoconaCommandDispatcher | ||
{ | ||
ValueTask<int> DispatchAsync(CancellationToken cancellationToken = default); | ||
ValueTask<int> DispatchAsync(CommandResolverResult commandResolverResult, CancellationToken cancellationToken = default); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cocona.Command | ||
{ | ||
public interface ICoconaBootstrapper | ||
{ | ||
void Initialize(); | ||
ValueTask<int> RunAsync(CancellationToken cancellationToken); | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.