Skip to content

Commit

Permalink
fix(RunCommand): setup ExeCommandHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed Apr 18, 2024
1 parent cfeba48 commit 4a1d3b9
Showing 1 changed file with 7 additions and 38 deletions.
45 changes: 7 additions & 38 deletions src/PollinationSDK/Helper/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,48 +895,17 @@ public static void RunScriptFile(string scriptFile, bool silentMode)
}


public static bool RunCommand(string programPath, string argument, bool silentMode, out string results)
{
results = string.Empty;

var stdout = new List<string>();
var stdErr = new List<string>();
using (var p = new System.Diagnostics.Process())
{
p.StartInfo.FileName = programPath;
p.StartInfo.Arguments = argument;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = silentMode;
p.Start();

p.ErrorDataReceived += (s, m) => { if (m.Data != null) stdErr.Add(m.Data); };
//p.OutputDataReceived += (s, m) => { if (m.Data != null) stdout.Add(m.Data); };
p.BeginErrorReadLine();
//p.BeginOutputReadLine();

p.WaitForExit();

if (!p.HasExited)
{
p.Kill();
}
public delegate bool ExeCommandFunc(string program, string arg, bool silentMode, out string results);

}
public static ExeCommandFunc ExeCommandHandler = null;

stdout.AddRange(stdErr);
var msg = string.Join(Environment.NewLine, stdout);
var cmd = $"Command:\n{programPath} {argument}";
public static bool RunCommand(string programPath, string argument, bool silentMode, out string results)
{
if (ExeCommandHandler == null)
throw new ArgumentNullException("Set Helper.ExeCommandHandler first!");

if (stdErr.Count > 0)
{
msg = $"{cmd}\n{msg}";
throw new ArgumentException($"{cmd}\n{string.Join(Environment.NewLine, stdErr)}");
}
return ExeCommandHandler(programPath, argument, silentMode, out results);

results = msg;
return stdErr.Count == 0;
}


Expand Down

0 comments on commit 4a1d3b9

Please sign in to comment.