/
RunspacePipeline.cs
100 lines (86 loc) · 3.1 KB
/
RunspacePipeline.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using AvocadoShell.PowerShellService.Utilities;
using StandardLibrary.Processes;
using StandardLibrary.Utilities;
using StandardLibrary.Utilities.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Reflection;
namespace AvocadoShell.PowerShellService
{
sealed class RunspacePipeline
{
readonly ResetEventWithData<PipelineStateInfo> executionDone
= new ResetEventWithData<PipelineStateInfo>();
Pipeline pipeline;
public Runspace Runspace { get; }
public Autocomplete Autocomplete { get; }
public RunspacePipeline(Runspace runspace)
{
Runspace = runspace;
Autocomplete = new Autocomplete(runspace);
}
public string InitEnvironment()
{
var startupScripts = getSystemStartupScripts()
.Concat(getParameterScripts());
return ExecuteScripts(startupScripts);
}
IEnumerable<string> getSystemStartupScripts()
{
var asm = Assembly.GetExecutingAssembly();
return asm
.GetManifestResourceNames()
.Where(r => r.EndsWith(".ps1"))
.Select(r => EnvUtils.GetEmbeddedText(asm, r));
}
IEnumerable<string> getParameterScripts()
=> string.Join(" ", EnvUtils.GetArgs()).Yield();
/// <summary>
/// Request to stop the running pipeline.
/// </summary>
/// <returns>True if the pipeline is being stopped. False otherwise
/// (ex: the pipeline wasn't running).</returns>
public bool Stop()
{
if (pipeline?.PipelineStateInfo.State == PipelineState.Running)
{
pipeline.StopAsync();
return true;
}
return false;
}
void onPipelineStateChanged(object sender, PipelineStateEventArgs e)
{
switch (e.PipelineStateInfo.State)
{
case PipelineState.Completed:
case PipelineState.Stopped:
executionDone.Signal(e.PipelineStateInfo);
break;
}
}
public string ExecuteCommand(string command)
{
return executePipeline(command, true);
}
public string ExecuteScripts(IEnumerable<string> scripts)
{
var scriptStr = string.Join(Environment.NewLine, scripts);
return executePipeline(scriptStr, false);
}
string executePipeline(string command, bool addToHistory)
{
// Create the new pipeline.
pipeline = Runspace.CreatePipeline(command, addToHistory);
pipeline.StateChanged += onPipelineStateChanged;
// Have the our host format the output.
pipeline.Commands.Add("Out-Default");
// Execute the pipeline.
pipeline.InvokeAsync();
// Wait for the pipeline to finish and return any error output.
return executionDone.Block().Reason?.Message;
}
}
}