-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell.cs
159 lines (124 loc) · 3.9 KB
/
Shell.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PowerSpace
{
internal class Shell
{
internal bool IsPipeline;
private Runspace _Runspace;
private Pipeline _Pipeline;
private PowerShell _PowerShell;
internal Shell(bool isPipeline, string prologue = null)
{
IsPipeline = isPipeline;
_Pipeline = null;
_PowerShell = null;
_Runspace = RunspaceFactory.CreateRunspace();
_Runspace.Open();
if (IsPipeline)
_Pipeline = _Runspace.CreatePipeline();
else
{
_PowerShell = PowerShell.Create();
_PowerShell.Runspace = _Runspace;
}
if (prologue != null)
Invoke(prologue);
}
~Shell()
{
Close();
}
internal string BuiltinCD(string command)
{
string[] args = command.Split(' ');
if (args.Length != 2)
throw new Exception("Usage: cd [directory]");
Directory.SetCurrentDirectory(args[1]);
return Invoke(command);
}
internal string BuiltinReset()
{
Reset();
return "[PowerSpace] Shell successfully reset\n";
}
internal static string Prompt(bool colorized=false)
{
string path = Directory.GetCurrentDirectory();
// string user = Environment.UserName;
// string host = Environment.MachineName;
if (colorized)
return "\x1b[90m[\x1b[91mPowerSpace\x1b[90m] " + path + "\x1b[0m> ";
return "[PowerSpace] " + path + "> ";
}
internal string Invoke(string command)
{
string output = "";
if (IsPipeline)
{
_Pipeline.Commands.AddScript(command);
try
{
foreach (PSObject pso in _Pipeline.Invoke())
output += pso.ToString() + "\n";
foreach (object e in _Pipeline.Error.ReadToEnd())
output += e.ToString() + "\n";
}
catch (Exception e)
{
output += e.ToString() + "\n";
}
_Pipeline.Commands.Clear();
_Pipeline.Dispose();
_Pipeline = _Runspace.CreatePipeline();
}
else
{
_PowerShell.AddScript(command);
foreach (PSObject pso in _PowerShell.Invoke())
output += pso.ToString() + "\n";
foreach (ErrorRecord e in _PowerShell.Streams.Error)
output += e.ToString() + "\n";
_PowerShell.Commands.Clear();
_PowerShell.Streams.ClearStreams();
}
return output;
}
internal void Close()
{
if (_Pipeline != null)
{
_Pipeline.Dispose();
_Pipeline = null;
}
if (_PowerShell != null)
{
_PowerShell.Dispose();
_PowerShell = null;
}
if (_Runspace != null)
{
_Runspace.Close();
_Runspace = null;
}
}
internal void Reset()
{
if (_Pipeline != null)
{
_Pipeline.Commands.Clear();
_Pipeline.Dispose();
_Pipeline = _Runspace.CreatePipeline();
return;
}
if (_PowerShell != null)
{
_PowerShell.Dispose();
_PowerShell = PowerShell.Create();
_PowerShell.Runspace = _Runspace;
}
}
}
}