Commit 60e316e0 authored by Tim Bureck's avatar Tim Bureck
Browse files

Add async application and commands API

* Add IAsyncApplication and AsyncApplication with RunAsync methods
* Extract IApplication interface from Application
* Move ArgsInput.Bind method to IInput interface
* Simplify Application.Run implementation
parent eaa88258
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Async application and commands API

## [0.2.0-alpha2] - 2019-07-06
### Changed
+15 −21
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ using TBureck.Terminal.IO;
namespace TBureck.Terminal
{
    
    public class Application : ICommandContainer
    public class Application : ICommandContainer, IApplication
    {
        
        public IList<ICommand> Commands { get; }
@@ -42,37 +42,31 @@ namespace TBureck.Terminal

        public int Run(IList<string> args, IInput input = null, IOutput output = null)
        {
            if (output == null) {
                output = new ConsoleOutput();
            }
            
            if (args.Count == 0) {
                return 1;
            }

            int exitCode;
            output = output ?? CreateDefaultOutput();

            try {
                // Treat first argument as the command name:
                string commandName = args.FirstOrDefault();
                ICommand command = this.FindCommand(commandName);

                if (input == null) {
                    ArgsInput argsInput = ArgsInput
                        .Of(args.Skip(1).ToList());
                    
                    argsInput.Bind(command.InputDefinition);

                    input = argsInput;
                }
                input = input ?? CreateDefaultInput(args);
                input.Bind(command.InputDefinition);
                
                exitCode = command.Execute(input, output);
                return command.Execute(input, output);
            } catch (Exception e) {
                output.WriteLine(e.Message);
                exitCode = 1;
                return 1;
            }
        }

            return exitCode;
        public static ArgsInput CreateDefaultInput(IList<string> args)
        {
            return ArgsInput.Of(args.Skip(1).ToList());
        }

        public static IOutput CreateDefaultOutput()
        {
            return new ConsoleOutput();
        }
    }
}
 No newline at end of file
+41 −0
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TBureck.Terminal.Commands;
using TBureck.Terminal.IO;

namespace TBureck.Terminal
{
    
    public class AsyncApplication : Application, IAsyncApplication
    {
        
        public async Task<int> RunAsync()
        {
            return await this.RunAsync(new List<string>());
        }

        public async Task<int> RunAsync(IList<string> arguments, IInput input = null, IOutput output = null)
        {
            output = output ?? CreateDefaultOutput();
            
            try {
                string commandName = arguments.FirstOrDefault();
                ICommand command = this.FindCommand(commandName);

                input = input ?? CreateDefaultInput(arguments);
                input.Bind(command.InputDefinition);

                if (command is IAsyncCommand asyncCommand) {
                    return await asyncCommand.ExecuteAsync(input, output);
                }
                
                return command.Execute(input, output);
            } catch (Exception e) {
                output.WriteLine(e.Message);
                return 1;
            }
        }
    }
}
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
using System.Threading.Tasks;
using TBureck.Terminal.IO;

namespace TBureck.Terminal.Commands
{
    
    public interface IAsyncCommand : ICommand
    {

        Task<int> ExecuteAsync(IInput input, IOutput output);
    }
}
 No newline at end of file
+15 −0
Original line number Diff line number Diff line
using System.Collections.Generic;
using TBureck.Terminal.Commands;
using TBureck.Terminal.IO;

namespace TBureck.Terminal
{
    
    public interface IApplication
    {
        
        void Add(ICommand command);
        int Run();
        int Run(IList<string> args, IInput input = null, IOutput output = null);
    }
}
 No newline at end of file
Loading