-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathInvokeReadLineForEditorServicesCommand.cs
53 lines (45 loc) · 1.81 KB
/
InvokeReadLineForEditorServicesCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Threading;
namespace Microsoft.PowerShell.EditorServices.Commands
{
/// <summary>
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services.
/// </summary>
public sealed class InvokeReadLineForEditorServicesCommand : PSCmdlet
{
private delegate string ReadLineInvoker(
Runspace runspace,
EngineIntrinsics engineIntrinsics,
CancellationToken cancellationToken);
private static Lazy<ReadLineInvoker> s_readLine = new Lazy<ReadLineInvoker>(() =>
{
Type type = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2");
MethodInfo method = type?.GetMethod(
"ReadLine",
new[] { typeof(Runspace), typeof(EngineIntrinsics), typeof(CancellationToken) });
// TODO: Handle method being null here. This shouldn't ever happen.
return (ReadLineInvoker)method.CreateDelegate(typeof(ReadLineInvoker));
});
/// <summary>
/// The ID to give to the host's profile.
/// </summary>
[Parameter(Mandatory = true)]
[ValidateNotNullOrEmpty]
public CancellationToken CancellationToken { get; set; }
protected override void EndProcessing()
{
// This returns a string.
object result = s_readLine.Value(
Runspace.DefaultRunspace,
SessionState.PSVariable.Get("ExecutionContext").Value as EngineIntrinsics,
CancellationToken
);
WriteObject(result);
}
}
}