Inital commit

master
C-xC-c 4 years ago
commit ea755bbe32
Signed by: C-xC-c
GPG Key ID: F52ED472284EF2F4

3
.gitignore vendored

@ -0,0 +1,3 @@
bin
obj
.vs

@ -0,0 +1,11 @@
using System.Threading.Tasks;
namespace DotWhat.Contracts
{
public abstract class What
{
public abstract string Name { get; }
public abstract Task RunAsync();
}
}

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cocona.Lite" Version="1.5.0" />
</ItemGroup>
</Project>

@ -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)
{
}
}
}

@ -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<What> Whats =
Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => type.BaseType == typeof(What))
.Select(what => (What)Activator.CreateInstance(what));
private static async Task<int> Main(string[] args)
{
await CoconaLiteApp.RunAsync<Program>(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()));
}
}
}
Loading…
Cancel
Save