-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathBash.cs
241 lines (209 loc) · 12.3 KB
/
Bash.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Anno.Common.Util;
namespace Anno.Common
{
/// <summary>Handles boilerplate for Bash commands and stores output information.</summary>
public class Bash
{
private static bool _linux { get; }
private static bool _mac { get; }
private static bool _windows { get; }
private static string _bashPath { get; }
/// <summary>Determines whether bash is running in a native OS (Linux/MacOS).</summary>
/// <returns>True if in *nix, else false.</returns>
public static bool Native { get; }
/// <summary>Determines if using Windows and if Linux subsystem is installed.</summary>
/// <returns>True if in Windows and bash detected.</returns>
public static bool Subsystem => _windows && File.Exists(@"C:\Windows\System32\bash.exe");
/// <summary>Stores output of the previous command if redirected.</summary>
public string Output { get; private set; }
/// <summary>
/// Gets an array of the command output split by newline characters if redirected. </summary>
public string[] Lines => Output?.Split(Environment.NewLine.ToCharArray());
/// <summary>Stores the exit code of the previous command.</summary>
public int ExitCode { get; private set; }
/// <summary>Stores the error message of the previous command if redirected.</summary>
public string ErrorMsg { get; private set; }
static Bash()
{
_linux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
_mac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
_windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
Native = _linux || _mac ? true : false;
_bashPath = Native ? "/usr/bin/sh" : "bash.exe";
}
/// <summary>
/// ��ִ���ļ�����·��
/// </summary>
public string CurrentExePath {
get
{
var path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
return path.Substring(0, path.LastIndexOf("/"));
}
}
/// <summary>Execute a new Bash command.</summary>
/// <param name="input">The command to execute.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Command(string input, bool redirect = true)
{
if (!Native && !Subsystem)
throw new PlatformNotSupportedException();
using (var bash = new Process { StartInfo = BashInfo(input, redirect) })
{
bash.Start();
if (redirect)
{
Output = bash.StandardOutput.ReadToEnd()
.TrimEnd(Environment.NewLine.ToCharArray());
ErrorMsg = bash.StandardError.ReadToEnd()
.TrimEnd(Environment.NewLine.ToCharArray());
}
else
{
Output = null;
ErrorMsg = null;
}
bash.WaitForExit();
ExitCode = bash.ExitCode;
bash.Close();
}
if (redirect)
return new BashResult(Output, ErrorMsg, ExitCode);
else
return new BashResult(null, null, ExitCode);
}
private ProcessStartInfo BashInfo(string input, bool redirectOutput)
{
return new ProcessStartInfo
{
FileName = _bashPath,
Arguments = $"-c \"{input}\"",
RedirectStandardInput = false,
RedirectStandardOutput = redirectOutput,
RedirectStandardError = redirectOutput,
UseShellExecute = false,
CreateNoWindow = true,
ErrorDialog = false
};
}
/// <summary>Echo the given string to standard output.</summary>
/// <param name="input">The string to print.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Echo(string input, bool redirect = false) =>
Command($"echo {input}", redirect: redirect);
/// <summary>Echo the given string to standard output.</summary>
/// <param name="input">The string to print.</param>
/// <param name="flags">Optional `echo` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Echo(string input, string flags, bool redirect = false) =>
Command($"echo {flags} {input}", redirect: redirect);
/// <summary>Echo the given string to standard output.</summary>
/// <param name="input">The string to print.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Echo(object input, bool redirect = false) =>
Command($"echo {input}", redirect: redirect);
/// <summary>Echo the given string to standard output.</summary>
/// <param name="input">The string to print.</param>
/// <param name="flags">Optional `echo` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Echo(object input, string flags, bool redirect = false) =>
Command($"echo {flags} {input}", redirect: redirect);
/// <summary>Search for `pattern` in each file in `location`.</summary>
/// <param name="pattern">The pattern to match.</param>
/// <param name="location">The files or directory to search.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Grep(string pattern, string location, bool redirect = true) =>
Command($"grep {pattern} {location}", redirect: redirect);
/// <summary>Search for `pattern` in each file in `location`.</summary>
/// <param name="pattern">The pattern to match.</param>
/// <param name="location">The files or directory to search.</param>
/// <param name="flags">Optional `grep` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
public BashResult Grep(string pattern, string location, string flags, bool redirect = true) =>
Command($"grep {pattern} {flags} {location}", redirect: redirect);
/// <summary>List information about files in the current directory.</summary>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Ls(bool redirect = true) =>
Command("ls", redirect: redirect);
/// <summary>List information about files in the current directory.</summary>
/// <param name="flags">Optional `ls` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Ls(string flags, bool redirect = true) =>
Command($"ls {flags}", redirect: redirect);
/// <summary>List information about the given files.</summary>
/// <param name="flags">Optional `ls` arguments.</param>
/// <param name="files">Files or directory to search.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Ls(string flags, string files, bool redirect = true) =>
Command($"ls {flags} {files}", redirect: redirect);
/// <summary>Move `source` to `directory`.</summary>
/// <param name="source">The file to be moved.</param>
/// <param name="directory">The destination directory.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Mv(string source, string directory, bool redirect = true) =>
Command($"mv {source} {directory}", redirect: redirect);
/// <summary>Move `source` to `directory`.</summary>
/// <param name="source">The file to be moved.</param>
/// <param name="directory">The destination directory.</param>
/// <param name="flags">Optional `mv` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Mv(string source, string directory, string flags, bool redirect = true) =>
Command($"mv {flags} {source} {directory}", redirect: redirect);
/// <summary>Copy `source` to `directory`.</summary>
/// <param name="source">The file to be copied.</param>
/// <param name="directory">The destination directory.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Cp(string source, string directory, bool redirect = true) =>
Command($"cp {source} {directory}", redirect: redirect);
/// <summary>Copy `source` to `directory`.</summary>
/// <param name="source">The file to be copied.</param>
/// <param name="directory">The destination directory.</param>
/// <param name="flags">Optional `cp` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Cp(string source, string directory, string flags, bool redirect = true) =>
Command($"cp {flags} {source} {directory}", redirect: redirect);
/// <summary>Remove or unlink the given file.</summary>
/// <param name="file">The file(s) to be removed.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Rm(string file, bool redirect = true) =>
Command($"rm {file}", redirect: redirect);
/// <summary>Remove or unlink the given file.</summary>
/// <param name="file">The file(s) to be removed.</param>
/// <param name="flags">Optional `rm` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Rm(string file, string flags, bool redirect = true) =>
Command($"rm {flags} {file}", redirect: redirect);
/// <summary>Concatenate `file` to standard input.</summary>
/// <param name="file">The source file.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Cat(string file, bool redirect = true) =>
Command($"cat {file}", redirect: redirect);
/// <summary>Concatenate `file` to standard input.</summary>
/// <param name="file">The source file.</param>
/// <param name="flags">Optional `cat` arguments.</param>
/// <param name="redirect">Print output to terminal if false.</param>
/// <returns>A `BashResult` containing the command's output information.</returns>
public BashResult Cat(string file, string flags, bool redirect = true) =>
Command($"cat {flags} {file}", redirect: redirect);
}
}