Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Ctrl+C handling #290

Merged
merged 45 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
2d7843d
Support Ctrl+C handling
tmds Nov 5, 2018
235bf17
Reimplement using middleware
tmds Nov 13, 2018
bf6528f
Add CancelOnUnload
tmds Nov 13, 2018
1076155
Undo changes to InvocationPipeline
tmds Nov 13, 2018
0da0b47
Undo changes to InvocationContext
tmds Nov 13, 2018
5180d09
Update MiddlewareOrder
tmds Nov 13, 2018
3866b9e
Add CancelOnCancelKeyTests
tmds Nov 13, 2018
b7930c7
Make InvocationContext aware if cancellation is enabled
tmds Nov 14, 2018
7374992
Merge CancelOn* methods into UseConsoleLifetime and improve interacti…
tmds Nov 15, 2018
d53b5ec
Merge remote-tracking branch 'upstream/master' into cancel_key
tmds Nov 15, 2018
1fcab8b
Unwrap InvokeAsync TargetInvocationException
tmds Nov 15, 2018
256fb39
Bind CancellationToken
tmds Nov 15, 2018
48a9af8
Undo changes to SystemConsole
tmds Nov 15, 2018
952e314
UseExceptionHandler: also print a message when the invocation gets ca…
tmds Nov 15, 2018
d903c2f
Undo special handling of OperationCanceledException in UseExceptionHa…
tmds Nov 15, 2018
e5e8c32
Add Linux test for UseConsoleLifetime
tmds Nov 16, 2018
bdacb72
MethodBindingCommandHandler: restyle TargetInvocationException check
tmds Nov 16, 2018
17da00a
UseExceptionHandlerTests: undo changes
tmds Nov 16, 2018
9600be2
Add some comments to clarify how the termination event handlers work
tmds Nov 16, 2018
95f53e3
Use some new names
tmds Nov 16, 2018
0ae0881
Fix ExitCode when UseConsoleLifetime with non cancellable invocation
tmds Nov 16, 2018
85d5c07
More renaming
tmds Nov 16, 2018
3a078fa
Add another test
tmds Nov 16, 2018
2d28edf
DragonFruit: change TargetFramework to netcoreapp2.1 to fix CI build
tmds Nov 19, 2018
7e23c0b
Update DotNetCliVersion to 2.1.500
tmds Nov 20, 2018
fc5fdb1
Merge branch 'master' into cancel_key
tmds Nov 20, 2018
0ae2773
Update global.json sdk version to 2.1.500
tmds Nov 20, 2018
f6bed06
InvocationExtensionsTest: help the compiler pick a Create overload
tmds Nov 20, 2018
03a89de
AddCancellationHandling: return instead of using an out parameter
tmds Nov 20, 2018
1ea9e2f
Rename IsCancelled to IsCancellationRequested
tmds Nov 20, 2018
0ec7e8b
Revert back to 2.1.400 sdk
tmds Nov 20, 2018
be385fa
Remove duplicate DotNetCliVersion
tmds Nov 20, 2018
fd362ac
Add description for the wiki
tmds Nov 21, 2018
963c30d
Reword wiki feature description
tmds Nov 28, 2018
87d57ec
Move child process timeouts to the test process
tmds Nov 28, 2018
c5fe41b
Simplify tests
tmds Nov 28, 2018
2757674
Fix race between producers and consumers of cancellation
tmds Nov 28, 2018
1f6208d
Merge remote-tracking branch 'upstream/master' into cancel_key
tmds Nov 29, 2018
e0115d0
Tests: help compiler pick a CommandHandler.Create overload
tmds Nov 29, 2018
840f267
InvocationContext: ensure order of CancellationHandlerAdded event and…
tmds Nov 29, 2018
610ca21
Use FluentAssertion style
tmds Nov 29, 2018
6aa5bd1
Fix typo
tmds Nov 29, 2018
269bb1e
Make RemoteExceptionException directly derive from Exception
tmds Nov 30, 2018
a03dcdb
UseDefaults: include CancelOnProcessTermination
tmds Dec 4, 2018
6116ef9
Remove wiki feature description
tmds Dec 5, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/System.CommandLine/Builder/CommandLineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ internal void AddMiddleware(

internal static class MiddlewareOrder
{
public const int ExceptionHandler = int.MinValue;
public const int UnloadHandler = int.MinValue;
public const int ExceptionHandler = UnloadHandler + 100;
public const int Configuration = ExceptionHandler + 100;
public const int Preprocessing = Configuration + 100;
public const int AfterPreprocessing = Preprocessing + 100;
Expand Down
2 changes: 2 additions & 0 deletions src/System.CommandLine/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public interface IConsole : IDisposable
bool IsVirtualTerminal { get; }

void TryEnableVirtualTerminal();

event ConsoleCancelEventHandler CancelKeyPress;
tmds marked this conversation as resolved.
Show resolved Hide resolved
}
}
5 changes: 5 additions & 0 deletions src/System.CommandLine/Invocation/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;

namespace System.CommandLine.Invocation
{
Expand Down Expand Up @@ -33,6 +34,10 @@ public static object[] GetMethodArguments(
{
arguments.Add(context.Console);
}
else if (parameterInfo.ParameterType == typeof(CancellationToken))
{
arguments.Add(context.Canceled);
}
else
{
var argument = context.ParseResult
Expand Down
7 changes: 7 additions & 0 deletions src/System.CommandLine/Invocation/InvocationContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading;

namespace System.CommandLine.Invocation
{
public sealed class InvocationContext : IDisposable
{
private readonly IDisposable _onDispose;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();

public InvocationContext(
ParseResult parseResult,
Expand Down Expand Up @@ -36,6 +39,10 @@ public InvocationContext(

public IInvocationResult InvocationResult { get; set; }

public CancellationToken Canceled => _cts.Token;

public void Cancel() => _cts.Cancel();

public void Dispose()
{
_onDispose?.Dispose();
Expand Down
51 changes: 51 additions & 0 deletions src/System.CommandLine/Invocation/InvocationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,65 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Environment;

namespace System.CommandLine.Invocation
{
public static class InvocationExtensions
{
public static CommandLineBuilder CancelOnCancelKey(this CommandLineBuilder builder)
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
builder.AddMiddleware(async (context, next) =>
{
ConsoleCancelEventHandler handler = (_, args) =>
{
args.Cancel = true;
context.Cancel();
};
tmds marked this conversation as resolved.
Show resolved Hide resolved
try
{
context.Console.CancelKeyPress += handler;
await next(context);
}
finally
{
context.Console.CancelKeyPress -= handler;
}
}, CommandLineBuilder.MiddlewareOrder.Configuration);

return builder;
}

public static CommandLineBuilder CancelOnUnload(this CommandLineBuilder builder)
{
builder.AddMiddleware(async (context, next) =>
{
var mre = new ManualResetEventSlim(initialState: false);
Action<AssemblyLoadContext> handler = _ =>
{
context.Cancel();
mre.Wait();
Environment.ExitCode = context.ResultCode;
tmds marked this conversation as resolved.
Show resolved Hide resolved
};
try
{
AssemblyLoadContext.Default.Unloading += handler;
await next(context);
}
finally
{
AssemblyLoadContext.Default.Unloading -= handler;
mre.Set();
}
}, CommandLineBuilder.MiddlewareOrder.UnloadHandler);

return builder;
}

public static CommandLineBuilder UseMiddleware(
this CommandLineBuilder builder,
InvocationMiddleware middleware)
Expand Down
6 changes: 6 additions & 0 deletions src/System.CommandLine/Invocation/SystemConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public event ConsoleCancelEventHandler CancelKeyPress
{
add => Console.CancelKeyPress += value;
remove => Console.CancelKeyPress -= value;
}

~SystemConsole()
{
ResetConsole();
Expand Down
1 change: 1 addition & 0 deletions src/System.CommandLine/System.CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>

</Project>