public static CommandLine.AsyncStreamReader[] CreateFromStreams(Stream[] streams, int bufferSize) { CommandLine.AsyncStreamReader[] array = new CommandLine.AsyncStreamReader[streams.Length]; for (int i = 0; i < streams.Length; i++) { array[i] = new CommandLine.AsyncStreamReader(i, streams[i], bufferSize); } return(array); }
// public event CommandLine.AsyncStreamReader.Handler DataReceived // { // add // { // CommandLine.AsyncStreamReader.Handler handler = this.DataReceived; // CommandLine.AsyncStreamReader.Handler handler2; // do // { // handler2 = handler; // handler = Interlocked.CompareExchange<CommandLine.AsyncStreamReader.Handler>(ref this.DataReceived, (CommandLine.AsyncStreamReader.Handler)Delegate.Combine(handler2, value), handler); // } // while (handler != handler2); // } // remove // { // CommandLine.AsyncStreamReader.Handler handler = this.DataReceived; // CommandLine.AsyncStreamReader.Handler handler2; // do // { // handler2 = handler; // handler = Interlocked.CompareExchange<CommandLine.AsyncStreamReader.Handler>(ref this.DataReceived, (CommandLine.AsyncStreamReader.Handler)Delegate.Remove(handler2, value), handler); // } // while (handler != handler2); // } // } public AsyncStreamReaderMultiplexer(CommandLine.AsyncStreamReader[] readers, CommandLine.AsyncStreamReader.Handler handler, CommandLine.AsyncStreamReaderMultiplexer.CompletionHandler complete = null) { this.queuedItem = new AutoResetEvent(false); this.queue = Queue.Synchronized(new Queue()); this.activeStreams = new HashSet <int>(); for (int i = 0; i < readers.Length; i++) { CommandLine.AsyncStreamReader asyncStreamReader = readers[i]; asyncStreamReader.DataReceived += new CommandLine.AsyncStreamReader.Handler(this.HandleRead); this.activeStreams.Add(asyncStreamReader.Handle); } this.DataReceived += handler; if (complete != null) { this.Complete += complete; } new Thread(new ThreadStart(this.PollQueue)).Start(); }
public static CommandLine.Result RunViaShell(string toolPath, string arguments, string workingDirectory = null, Dictionary <string, string> envVars = null, CommandLine.IOHandler ioHandler = null, bool useShellExecution = false, bool stdoutRedirectionInShellMode = true) { Encoding inputEncoding = Console.InputEncoding; Encoding outputEncoding = Console.OutputEncoding; Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; // if (Application.platform == RuntimePlatform.WindowsEditor && toolPath.Contains("'")) // { // useShellExecution = true; // stdoutRedirectionInShellMode = true; // } string text = null; string text2 = null; if (useShellExecution && stdoutRedirectionInShellMode) { text = Path.GetTempFileName(); text2 = Path.GetTempFileName(); string text3 = toolPath; string text4; string text5; string text6; // if (Application.platform == RuntimePlatform.WindowsEditor) // { // text4 = "cmd.exe"; // text5 = "/c \""; // text6 = "\""; // } // else { text4 = "bash"; text5 = "-l -c '"; text6 = "'"; text3 = toolPath.Replace("'", "'\\''"); } arguments = string.Format("{0}\"{1}\" {2} 1> {3} 2> {4}{5}", new object[] { text5, text3, arguments, text, text2, text6 }); toolPath = text4; } Process process = new Process(); process.StartInfo.UseShellExecute = useShellExecution; process.StartInfo.Arguments = arguments; if (useShellExecution) { process.StartInfo.CreateNoWindow = false; process.StartInfo.RedirectStandardOutput = false; process.StartInfo.RedirectStandardError = false; } else { process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; if (envVars != null) { foreach (KeyValuePair <string, string> current in envVars) { process.StartInfo.EnvironmentVariables[current.Key] = current.Value; } } } process.StartInfo.RedirectStandardInput = (!useShellExecution && ioHandler != null); process.StartInfo.FileName = toolPath; process.StartInfo.WorkingDirectory = (workingDirectory ?? Environment.CurrentDirectory); process.Start(); if (ioHandler != null) { ioHandler(process, process.StandardInput, CommandLine.StreamData.Empty); } List <string>[] stdouterr = new List <string>[] { new List <string>(), new List <string>() }; if (useShellExecution) { process.WaitForExit(); if (stdoutRedirectionInShellMode) { stdouterr[0].Add(File.ReadAllText(text)); stdouterr[1].Add(File.ReadAllText(text2)); File.Delete(text); File.Delete(text2); } } else { AutoResetEvent complete = new AutoResetEvent(false); CommandLine.AsyncStreamReader[] array = CommandLine.AsyncStreamReader.CreateFromStreams(new Stream[] { process.StandardOutput.BaseStream, process.StandardError.BaseStream }, 1); new CommandLine.AsyncStreamReaderMultiplexer(array, delegate(CommandLine.StreamData data) { stdouterr[data.handle].Add(data.text); if (ioHandler != null) { ioHandler(process, process.StandardInput, data); } }, delegate { complete.Set(); }); CommandLine.AsyncStreamReader[] array2 = array; for (int i = 0; i < array2.Length; i++) { CommandLine.AsyncStreamReader asyncStreamReader = array2[i]; asyncStreamReader.Start(); } process.WaitForExit(); complete.WaitOne(); } CommandLine.Result result = new CommandLine.Result(); result.stdout = string.Join(string.Empty, stdouterr[0].ToArray()); result.stderr = string.Join(string.Empty, stdouterr[1].ToArray()); result.exitCode = process.ExitCode; result.message = CommandLine.FormatResultMessage(toolPath, arguments, result.stdout, result.stderr, result.exitCode); Console.InputEncoding = inputEncoding; Console.OutputEncoding = outputEncoding; return(result); }