Skip to content

Commit

Permalink
WiP
Browse files Browse the repository at this point in the history
  • Loading branch information
scottt732 committed Oct 30, 2023
1 parent 288a8f1 commit 2aa8633
Show file tree
Hide file tree
Showing 49 changed files with 1,219 additions and 1,189 deletions.
300 changes: 153 additions & 147 deletions Source/Sholo.CommandLine/AppBuilder/BaseCommandLineAppBuilder.cs

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions Source/Sholo.CommandLine/Command/ICommand.cs

This file was deleted.

135 changes: 67 additions & 68 deletions Source/Sholo.CommandLine/CommandLineApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,93 @@
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;

namespace Sholo.CommandLine
namespace Sholo.CommandLine;

internal class CommandLineApp : ICommandLineApp
{
internal class CommandLineApp : ICommandLineApp
// https://github.com/natemcmaster/CommandLineUtils/issues/111#issuecomment-401276354
private static readonly int ExitCodeOperationCanceled;
private static readonly int ExitCodeUnhandledException;

static CommandLineApp()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// values from http://www.febooti.com/products/automation-workshop/online-help/events/run-dos-cmd-command/exit-codes/
ExitCodeOperationCanceled = unchecked((int)0xC000013A);
ExitCodeUnhandledException = unchecked((int)0xE0434F4D);
}
else
{
// Match Process.ExitCode (cfr bash) which uses 128 + signo.
ExitCodeOperationCanceled = 130; // SIGINT
ExitCodeUnhandledException = 134; // SIGABRT
}
}

private CommandLineApplication Application { get; }

public CommandLineApp(CommandLineApplication application)
{
// https://github.com/natemcmaster/CommandLineUtils/issues/111#issuecomment-401276354
private static readonly int ExitCodeOperationCanceled;
private static readonly int ExitCodeUnhandledException;
Application = application;
}

public async Task<int> ExecuteAsync(string[] args, CancellationToken cancellationToken)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

static CommandLineApp()
void CancelHandler(object o, ConsoleCancelEventArgs e)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
// ReSharper disable AccessToDisposedClosure

if (!cts.IsCancellationRequested)
{
// values from http://www.febooti.com/products/automation-workshop/online-help/events/run-dos-cmd-command/exit-codes/
ExitCodeOperationCanceled = unchecked((int)0xC000013A);
ExitCodeUnhandledException = unchecked((int)0xE0434F4D);
cts.Cancel();
e.Cancel = true;
}
else
{
// Match Process.ExitCode (cfr bash) which uses 128 + signo.
ExitCodeOperationCanceled = 130; // SIGINT
ExitCodeUnhandledException = 134; // SIGABRT
e.Cancel = false;
}
}

private CommandLineApplication Application { get; }

public CommandLineApp(CommandLineApplication application)
{
Application = application;
// ReSharper restore AccessToDisposedClosure
}

public async Task<int> ExecuteAsync(string[] args, CancellationToken cancellationToken)
void UnloadingHandler(AssemblyLoadContext ctx)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
// ReSharper disable AccessToDisposedClosure

void CancelHandler(object o, ConsoleCancelEventArgs e)
if (!cts.IsCancellationRequested)
{
// ReSharper disable AccessToDisposedClosure

if (!cts.IsCancellationRequested)
{
cts.Cancel();
e.Cancel = true;
}
else
{
e.Cancel = false;
}

// ReSharper restore AccessToDisposedClosure
cts.Cancel();
}

void UnloadingHandler(AssemblyLoadContext ctx)
{
// ReSharper disable AccessToDisposedClosure

if (!cts.IsCancellationRequested)
{
cts.Cancel();
}

// ReSharper restore AccessToDisposedClosure
}
// ReSharper restore AccessToDisposedClosure
}

try
{
Console.CancelKeyPress += CancelHandler;
AssemblyLoadContext.Default.Unloading += UnloadingHandler;
try
{
Console.CancelKeyPress += CancelHandler;
AssemblyLoadContext.Default.Unloading += UnloadingHandler;

var result = await Application.ExecuteAsync(args, cts.Token);
var result = await Application.ExecuteAsync(args, cts.Token);

return result;
}
catch (OperationCanceledException)
{
return ExitCodeOperationCanceled;
}
catch (Exception e)
{
await Console.Error.WriteLineAsync($"Unhandled exception: {e}");
return ExitCodeUnhandledException;
}
finally
{
AssemblyLoadContext.Default.Unloading -= UnloadingHandler;
Console.CancelKeyPress -= CancelHandler;
cts.Dispose();
}
return result;
}
catch (OperationCanceledException)
{
return ExitCodeOperationCanceled;
}
catch (Exception e)
{
await Console.Error.WriteLineAsync($"Unhandled exception: {e}");
return ExitCodeUnhandledException;
}
finally
{
AssemblyLoadContext.Default.Unloading -= UnloadingHandler;
Console.CancelKeyPress -= CancelHandler;
cts.Dispose();
}
}
}
9 changes: 4 additions & 5 deletions Source/Sholo.CommandLine/CommandLineAppBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Sholo.CommandLine.AppBuilder;

namespace Sholo.CommandLine
namespace Sholo.CommandLine;

// Inherit from the base class.
public sealed class CommandLineAppBuilder : BaseCommandLineAppBuilder<CommandLineAppBuilder>
{
// Inherit from the base class.
public sealed class CommandLineAppBuilder : BaseCommandLineAppBuilder<CommandLineAppBuilder>
{
}
}
13 changes: 13 additions & 0 deletions Source/Sholo.CommandLine/CommandLineAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Sholo.Utils.Logging;

namespace Sholo.CommandLine;

public static class CommandLineAppBuilderExtensions
{
public static ICommandLineAppBuilder<TSelf> UseSerilogConsole<TSelf>(this ICommandLineAppBuilder<TSelf> builder)
where TSelf : ICommandLineAppBuilder<TSelf>
{
builder.ConfigureLogging((ctx, lb) => lb.AddSerilogConsole(ctx.HostConfiguration));
return builder;
}
}
41 changes: 0 additions & 41 deletions Source/Sholo.CommandLine/CommandPlugin/BaseCommandPlugin.cs

This file was deleted.

46 changes: 0 additions & 46 deletions Source/Sholo.CommandLine/CommandPlugin/BaseLambdaCommandPlugin.cs

This file was deleted.

Loading

0 comments on commit 2aa8633

Please sign in to comment.