/// <summary> /// Open a install / license window and execute a command. /// </summary> /// <param name="toolPath">Tool to run.</param> /// <param name="toolArguments">Arguments to pass to the tool.</param> /// <param name="retrievingLicenses">Whether the command is retrieving licenses.</param> /// <param name="licenseResponder">Responds to license questions.</param> /// <param name="packages">List of package versions to install / upgrade.</param> /// <param name="complete">Called when installation is complete.</param> private static void DisplayInstallLicenseDialog( string toolPath, string toolArguments, bool retrievingLicenses, LicenseResponder licenseResponder, IEnumerable <AndroidSdkPackageNameVersion> packages, Action <CommandLine.Result> complete) { var summary = retrievingLicenses ? "Attempting Android SDK package installation..." : DIALOG_TITLE + "..."; var window = CommandLineDialog.CreateCommandLineDialog(DIALOG_TITLE); window.summaryText = summary; window.modal = false; window.bodyText = String.Format("{0} {1}\n\n", toolPath, toolArguments); window.progressTitle = window.summaryText; window.autoScrollToBottom = true; CommandLine.IOHandler ioHandler = null; if (licenseResponder != null) { ioHandler = licenseResponder.AggregateLine; } PlayServicesResolver.Log(String.Format("{0} {1}", toolPath, toolArguments), level: LogLevel.Verbose); window.RunAsync( toolPath, toolArguments, (CommandLine.Result result) => { window.Close(); LogInstallLicenseResult(toolPath, toolArguments, retrievingLicenses, packages, result); complete(result); }, ioHandler: ioHandler, maxProgressLines: retrievingLicenses ? 250 : 500); window.Show(); }
/// <summary> /// Asynchronously execute a command line tool in this window, showing progress /// and finally calling the specified delegate on completion from the main / UI thread. /// </summary> /// <param name="toolPath">Tool to execute.</param> /// <param name="arguments">String to pass to the tools' command line.</param> /// <param name="completionDelegate">Called when the tool completes.</param> /// <param name="workingDirectory">Directory to execute the tool from.</param> /// <param name="ioHandler">Allows a caller to provide interactive input and also handle /// both output and error streams from a single delegate.</param> /// <param name="maxProgressLines">Specifies the number of lines output by the /// command line that results in a 100% value on a progress bar.</param> /// <returns>Reference to the new window.</returns> public void RunAsync( string toolPath, string arguments, CommandLine.CompletionHandler completionDelegate, string workingDirectory = null, Dictionary <string, string> envVars = null, CommandLine.IOHandler ioHandler = null, int maxProgressLines = 0) { CommandLineDialog.ProgressReporter reporter = new CommandLineDialog.ProgressReporter(logger); reporter.maxProgressLines = maxProgressLines; // Call the reporter from the UI thread from this window. UpdateEvent += reporter.Update; // Connect the user's delegate to the reporter's completion method. reporter.Complete += completionDelegate; // Connect the caller's IoHandler delegate to the reporter. reporter.DataHandler += ioHandler; // Disconnect the reporter when the command completes. CommandLine.CompletionHandler reporterUpdateDisable = (CommandLine.Result unusedResult) => { this.UpdateEvent -= reporter.Update; }; reporter.Complete += reporterUpdateDisable; logger.Log(String.Format( "Executing command: {0} {1}", toolPath, arguments), level: LogLevel.Verbose); CommandLine.RunAsync(toolPath, arguments, reporter.CommandLineToolCompletion, workingDirectory: workingDirectory, envVars: envVars, ioHandler: reporter.AggregateLine); }
// public event CommandLine.IOHandler DataHandler // { // add // { // CommandLine.IOHandler iOHandler = this.DataHandler; // CommandLine.IOHandler iOHandler2; // do // { // iOHandler2 = iOHandler; // iOHandler = Interlocked.CompareExchange<CommandLine.IOHandler>(ref this.DataHandler, (CommandLine.IOHandler)Delegate.Combine(iOHandler2, value), iOHandler); // } // while (iOHandler != iOHandler2); // } // remove // { // CommandLine.IOHandler iOHandler = this.DataHandler; // CommandLine.IOHandler iOHandler2; // do // { // iOHandler2 = iOHandler; // iOHandler = Interlocked.CompareExchange<CommandLine.IOHandler>(ref this.DataHandler, (CommandLine.IOHandler)Delegate.Remove(iOHandler2, value), iOHandler); // } // while (iOHandler != iOHandler2); // } // } public LineReader(CommandLine.IOHandler handler = null) { if (handler != null) { this.LineHandler += handler; } }
/// <summary> /// Construct a new reporter. /// </summary> public ProgressReporter(CommandLine.IOHandler handler = null) { textQueue = System.Collections.Queue.Synchronized(new System.Collections.Queue()); maxProgressLines = 0; linesReported = 0; LineHandler += CommandLineIOHandler; Complete = null; }
public void RunAsync(string toolPath, string arguments, CommandLine.CompletionHandler completionDelegate, string workingDirectory = null, Dictionary <string, string> envVars = null, CommandLine.IOHandler ioHandler = null, int maxProgressLines = 0) { CommandLineDialog.ProgressReporter reporter = new CommandLineDialog.ProgressReporter(this.logger); reporter.maxProgressLines = maxProgressLines; this.UpdateEvent += new CommandLineDialog.UpdateDelegate(reporter.Update); reporter.Complete += completionDelegate; reporter.DataHandler += ioHandler; CommandLine.CompletionHandler value = delegate(CommandLine.Result unusedResult) { this.UpdateEvent -= new CommandLineDialog.UpdateDelegate(reporter.Update); }; reporter.Complete += value; this.logger.Log(string.Format("Executing command: {0} {1}", toolPath, arguments), LogLevel.Verbose); CommandLine.RunAsync(toolPath, arguments, new CommandLine.CompletionHandler(reporter.CommandLineToolCompletion), workingDirectory, envVars, new CommandLine.IOHandler(reporter.AggregateLine)); }
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); }
public static CommandLine.Result Run(string toolPath, string arguments, string workingDirectory = null, Dictionary <string, string> envVars = null, CommandLine.IOHandler ioHandler = null) { return(CommandLine.RunViaShell(toolPath, arguments, workingDirectory, envVars, ioHandler, false, true)); }
public static void RunAsync(string toolPath, string arguments, CommandLine.CompletionHandler completionDelegate, string workingDirectory = null, Dictionary <string, string> envVars = null, CommandLine.IOHandler ioHandler = null) { Action action = delegate { CommandLine.Result result = CommandLine.Run(toolPath, arguments, workingDirectory, envVars, ioHandler); completionDelegate(result); }; if (ExecutionEnvironment.InBatchMode) { action(); } else { Thread thread = new Thread(new ThreadStart(action.Invoke)); thread.Start(); } }