commit ea755bbe32ebf9ca014d554a1d223f1a5d7634f9 Author: C-xC-c Date: Sat Jan 23 16:58:22 2021 +0000 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2cd48b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin +obj +.vs diff --git a/Contracts/What.cs b/Contracts/What.cs new file mode 100644 index 0000000..8c683a3 --- /dev/null +++ b/Contracts/What.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; + +namespace DotWhat.Contracts +{ + public abstract class What + { + public abstract string Name { get; } + + public abstract Task RunAsync(); + } +} \ No newline at end of file diff --git a/DotWhat.csproj b/DotWhat.csproj new file mode 100644 index 0000000..7acb83e --- /dev/null +++ b/DotWhat.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/DuckTyping/CustomAwaiters.cs b/DuckTyping/CustomAwaiters.cs new file mode 100644 index 0000000..05a5be7 --- /dev/null +++ b/DuckTyping/CustomAwaiters.cs @@ -0,0 +1,38 @@ +using DotWhat.Contracts; +using System; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace DotWhat.DuckTyping +{ + public sealed class CustomAwaiters : What + { + public override string Name => "CustomAwaiters"; + + public override async Task RunAsync() + { + int i = 123; + + Console.WriteLine(await (await await 7 + await i)); + } + } + + public static class IntExtensions + { + public static IntAwaiter GetAwaiter(this int i) => new IntAwaiter(i); + } + + public sealed class IntAwaiter : INotifyCompletion + { + private readonly int _value; + public bool IsCompleted => true; + + public IntAwaiter(int i) => _value = i; + + public int GetResult() => _value; + + public void OnCompleted(Action continuation) + { + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..82a32ce --- /dev/null +++ b/Program.cs @@ -0,0 +1,50 @@ +using DotWhat.Contracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Cocona; + +namespace DotWhat +{ + class Program + { + private static readonly IEnumerable Whats = + Assembly.GetExecutingAssembly() + .GetTypes() + .Where(type => type.BaseType == typeof(What)) + .Select(what => (What)Activator.CreateInstance(what)); + + private static async Task Main(string[] args) + { + await CoconaLiteApp.RunAsync(args); + + return 0; + } + + public async Task StartAsync([Argument] string name = default) + { + if (string.IsNullOrEmpty(name)) + { + Console.WriteLine("No what passed. Current available whats are:"); + PrintWhats(); + return; + } + + var command = Whats.FirstOrDefault(what => what.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + + if (command is null) + { + Console.WriteLine("Invalid what passed. Current available whats are:"); + PrintWhats(); + return; + } + + await command.RunAsync(); + + static void PrintWhats() + => Whats.ToList().ForEach(what => Console.WriteLine(what.Name.ToLower())); + } + } +} \ No newline at end of file