151 lines
4.8 KiB
C#
151 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AyaNova.Util
|
|
{
|
|
public static class RunProgram
|
|
{
|
|
|
|
|
|
public static string Run(string cmd, List<string> arguments, ILogger log = null)
|
|
{
|
|
try
|
|
{
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
return RunWindows(cmd, arguments);
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
{
|
|
return RunOSX(cmd, arguments);
|
|
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
return RunLinuxBash(cmd, arguments);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (log != null)
|
|
{
|
|
log.LogError(ex, $"RunProgram error running command:{cmd} {arguments}");
|
|
}
|
|
throw ex;
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string RunWindows(string cmd, List<string> arguments)
|
|
{
|
|
/*
|
|
process.StartInfo.UseShellExecute = false;
|
|
process.StartInfo.RedirectStandardOutput = true;
|
|
process.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
|
|
process.Start();
|
|
process.BeginOutputReadLine();
|
|
|
|
*/
|
|
// string result = string.Empty;
|
|
// using (var process = new Process())
|
|
// {
|
|
// process.StartInfo = new ProcessStartInfo
|
|
// {
|
|
// FileName = cmd,
|
|
// Arguments = arguments,
|
|
// RedirectStandardOutput = true,
|
|
// UseShellExecute = false,
|
|
// CreateNoWindow = true,
|
|
// };
|
|
// process.OutputDataReceived += (sender, args) => result += args.Data;
|
|
// process.Start();
|
|
// // string result = process.StandardOutput.ReadToEnd();
|
|
// // process.WaitForExit();
|
|
// return result;
|
|
// }
|
|
|
|
|
|
|
|
//this executes but doesnt return error
|
|
using (var process = new Process())
|
|
{
|
|
process.StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = cmd,
|
|
// Arguments = arguments,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
};
|
|
|
|
foreach (string s in arguments)
|
|
process.StartInfo.ArgumentList.Add(s);
|
|
process.Start();
|
|
|
|
string result = $"{process.StandardOutput.ReadToEnd()}{process.StandardError.ReadToEnd()} ";
|
|
process.WaitForExit();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private static string RunLinuxBash(string cmd, List<string> arguments)
|
|
{
|
|
var escapedArgs = $"{cmd} {arguments}".Replace("\"", "\\\"");
|
|
|
|
using (var process = new Process())
|
|
{
|
|
process.StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "/bin/bash",
|
|
// Arguments = $"-c \"{escapedArgs}\"",
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
};
|
|
foreach (string s in arguments)
|
|
process.StartInfo.ArgumentList.Add(s);
|
|
|
|
process.Start();
|
|
string result = process.StandardOutput.ReadToEnd();
|
|
process.WaitForExit();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
private static string RunOSX(string cmd, List<string> arguments)
|
|
{
|
|
using (var process = new Process())
|
|
{
|
|
process.StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = cmd,
|
|
|
|
// Arguments = arguments,
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
};
|
|
foreach (string s in arguments)
|
|
process.StartInfo.ArgumentList.Add(s);
|
|
|
|
process.Start();
|
|
string result = process.StandardOutput.ReadToEnd();
|
|
process.WaitForExit();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
}
|
|
} |