2014-05-08 21:06:53 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace HelpersLib
|
|
|
|
|
{
|
|
|
|
|
public abstract class ExternalCLIManager : IDisposable
|
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
private Process cli = new Process();
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-05-09 10:18:14 +12:00
|
|
|
|
public bool SwapStdErrAndStdOut { get; set; }
|
2014-05-08 21:06:53 +12:00
|
|
|
|
public StringBuilder Output = new StringBuilder();
|
|
|
|
|
public StringBuilder Errors = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
public delegate void ErrorDataReceivedHandler();
|
|
|
|
|
public event ErrorDataReceivedHandler ErrorDataReceived;
|
|
|
|
|
|
2014-05-09 10:18:14 +12:00
|
|
|
|
public virtual void Open(string cliPath, string args = null, bool swapStdErrAndStdOut = false)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
if (File.Exists(cliPath))
|
|
|
|
|
{
|
2014-05-09 10:18:14 +12:00
|
|
|
|
SwapStdErrAndStdOut = swapStdErrAndStdOut;
|
|
|
|
|
|
2014-05-09 03:17:42 +12:00
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo(cliPath);
|
|
|
|
|
psi.UseShellExecute = false;
|
|
|
|
|
psi.ErrorDialog = false;
|
|
|
|
|
psi.CreateNoWindow = true;
|
|
|
|
|
psi.RedirectStandardInput = true;
|
|
|
|
|
psi.RedirectStandardError = true;
|
|
|
|
|
psi.RedirectStandardOutput = true;
|
|
|
|
|
psi.WorkingDirectory = Path.GetDirectoryName(cliPath);
|
|
|
|
|
psi.Arguments = args;
|
2014-05-09 01:28:46 +12:00
|
|
|
|
|
2014-05-09 10:18:14 +12:00
|
|
|
|
if (swapStdErrAndStdOut)
|
|
|
|
|
{
|
|
|
|
|
cli.ErrorDataReceived += (sender, e) => { if (e.Data != null) { Output.AppendLine(e.Data); } };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cli.ErrorDataReceived += (sender, e) => { if (e.Data != null) { Errors.AppendLine(e.Data); } };
|
|
|
|
|
}
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-05-09 03:17:42 +12:00
|
|
|
|
cli.EnableRaisingEvents = true;
|
|
|
|
|
cli.StartInfo = psi;
|
|
|
|
|
cli.Start();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("CLI Path: " + cliPath);
|
|
|
|
|
Console.WriteLine("CLI Args: " + psi.Arguments);
|
|
|
|
|
|
|
|
|
|
cli.BeginErrorReadLine();
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-05-09 03:17:42 +12:00
|
|
|
|
cli.WaitForExit();
|
2014-05-09 10:18:14 +12:00
|
|
|
|
|
|
|
|
|
Console.WriteLine(Output.ToString());
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendCommand(string command)
|
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
if (cli != null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
cli.StandardInput.WriteLine(command);
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Close()
|
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
cli.CloseMainWindow();
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void OnErrorDataReceived()
|
|
|
|
|
{
|
2014-05-09 10:18:14 +12:00
|
|
|
|
if (!SwapStdErrAndStdOut && ErrorDataReceived != null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
|
|
|
|
ErrorDataReceived();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
if (cli != null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 03:17:42 +12:00
|
|
|
|
cli.Dispose();
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|