-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShell.cs
68 lines (58 loc) · 2.39 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
using System;
using System.Runtime.InteropServices;
using static SimpleExec.Command;
namespace Build.Buildary
{
public static class Shell
{
public static bool NoEcho { get; set; } = true;
public static void RunShell(string shell)
{
if (!NoEcho)
{
Console.WriteLine($"{Log.Message(Log.MessageType.Info, "Running:")} {shell}");
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Run("cmd.exe", $"/S /C \"{shell}\"", Directory.CurrentDirectory(), true);
}
else
{
Console.WriteLine("runnin!");
var escapedArgs = shell.Replace("\"", "\\\"");
Run("/usr/bin/env", $"bash -c \"{escapedArgs}\"", Directory.CurrentDirectory(), false);
Console.WriteLine("done");
}
}
public static void RunCommand(string command, string args)
{
if (!NoEcho)
{
Console.WriteLine($"{Log.Message(Log.MessageType.Info, "Running:")} {command}{(string.IsNullOrEmpty(args) ? "" : $" {args}")}");
}
Run(command, args, Directory.CurrentDirectory(), true);
}
public static string ReadShell(string shell)
{
if (!NoEcho)
{
Console.WriteLine($"{Log.Message(Log.MessageType.Info, "Running:")} {shell}");
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return ReadAsync("cmd.exe", $"/S /C \"{shell}\"", Directory.CurrentDirectory()).GetAwaiter().GetResult()
.StandardOutput;
}
var escapedArgs = shell.Replace("\"", "\\\"");
return ReadAsync("/usr/bin/env", $"bash -c \"{escapedArgs}\"", Directory.CurrentDirectory()).GetAwaiter().GetResult().StandardOutput;
}
public static string ReadCommand(string command, string args)
{
if (!NoEcho)
{
Console.WriteLine($"{Log.Message(Log.MessageType.Info, "Running:")} {command}{(string.IsNullOrEmpty(args) ? "" : $" {args}")}");
}
return ReadAsync(command, args, Directory.CurrentDirectory()).GetAwaiter().GetResult().StandardOutput;
}
}
}