Browse Source

Refactor

Make signature of What.RunAsync `void` and remove async
modifier. Subsequently, move down the call stack and
make everything that needed to be async to support
this being async synchronous.

Enable Nullable and fix potential null references.
master
C-xC-c 6 months ago
parent
commit
f014d97674
Signed by: C-xC-c GPG Key ID: F52ED472284EF2F4
4 changed files with 20 additions and 21 deletions
  1. +3
    -5
      Contracts/What.cs
  2. +3
    -1
      DotWhat.csproj
  3. +3
    -4
      DuckTyping/CustomAwaiters.cs
  4. +11
    -11
      Program.cs

+ 3
- 5
Contracts/What.cs View File

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

+ 3
- 1
DotWhat.csproj View File

@ -1,12 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cocona.Lite" Version="1.5.0" />
<PackageReference Include="Roslynator.Analyzers" Version="3.0.0" PrivateAssets="all" />
</ItemGroup>
</Project>

+ 3
- 4
DuckTyping/CustomAwaiters.cs View File

@ -1,7 +1,6 @@
using DotWhat.Contracts;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace DotWhat.DuckTyping
{
@ -9,9 +8,9 @@ namespace DotWhat.DuckTyping
{
public override string Name => "CustomAwaiters";
public override async Task RunAsync()
public override async void Run()
{
int i = 123;
const int i = 123;
Console.WriteLine(await (await await 7 + await i));
}
@ -35,4 +34,4 @@ namespace DotWhat.DuckTyping
{
}
}
}
}

+ 11
- 11
Program.cs View File

@ -1,29 +1,29 @@
using DotWhat.Contracts;
using Cocona;
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
internal class Program
{
private static readonly IEnumerable<What> Whats =
private static readonly IEnumerable<What?> Whats =
Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => type.BaseType == typeof(What))
.Select(what => (What)Activator.CreateInstance(what));
.Select(what => (What?)Activator.CreateInstance(what));
private static async Task<int> Main(string[] args)
{
await CoconaLiteApp.RunAsync<Program>(args);
await CoconaLiteApp.RunAsync<Program>(args).ConfigureAwait(false);
return 0;
}
public async Task StartAsync([Argument] string name = default)
public void Start([Argument] string? name = default)
{
if (string.IsNullOrEmpty(name))
{
@ -32,7 +32,7 @@ namespace DotWhat
return;
}
var command = Whats.FirstOrDefault(what => what.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
var command = Whats.FirstOrDefault(what => what!.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (command is null)
{
@ -41,10 +41,10 @@ namespace DotWhat
return;
}
await command.RunAsync();
command.Run();
static void PrintWhats()
=> Whats.ToList().ForEach(what => Console.WriteLine(what.Name.ToLower()));
=> Whats.ToList().ForEach(what => Console.WriteLine(what!.Name.ToLower()));
}
}
}
}

Loading…
Cancel
Save