Exemplo n.º 1
0
        /// <summary>
        /// Editor coroutine wrapper for OptionHelper.RunScriptAsync.
        /// </summary>
        protected IEnumerator Execute(System.Diagnostics.ProcessStartInfo startInfo, string input = null, System.Action <string> onOutput = null, System.Action <string> onError = null, bool logError = true)
        {
            var outputBuilder = new StringBuilder();
            var errorBuilder  = new StringBuilder();
            int?exitcode      = null;
            var terminator    = OptionHelper.RunScriptAsnyc(
                startInfo, input,
                (output) => {
                outputBuilder.AppendLine(output);
                if (onOutput != null)
                {
                    onOutput(output);
                }
            },
                (error) => {
                errorBuilder.AppendLine(error);
                if (onError != null)
                {
                    onError(error);
                }
            },
                (code) => {
                exitcode = code;
            }
                );

            if (runningScripts == null)
            {
                runningScripts = new List <System.Action <bool> >();
            }
            runningScripts.Add(terminator);

            while (exitcode == null)
            {
                yield return(null);
            }

            runningScripts.Remove(terminator);

            // 137 happens for Kill() and 143 for CloseMainWindow(),
            // which means the script has been canceled
            if (logError && exitcode != 0 && exitcode != 137 && exitcode != 143)
            {
                Debug.LogError(string.Format(
                                   "{0}: Failed to execute {1}: {2}\nOutput: {3}",
                                   name, Path.GetFileName(startInfo.FileName),
                                   errorBuilder.ToString(), outputBuilder.ToString()
                                   ));
            }
            yield return(exitcode);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Async wrapper for OptionHelper.RunScriptAsync.
        /// </summary>
        protected async Task <int> Execute(ExecutionArgs args, TaskToken task)
        {
            var outputBuilder = new StringBuilder();
            var errorBuilder  = new StringBuilder();
            int?exitcode      = null;
            var terminator    = OptionHelper.RunScriptAsnyc(
                args.startInfo, args.input,
                (output) => {
                outputBuilder.AppendLine(output);
                args.onOutput?.Invoke(output);
            },
                (error) => {
                errorBuilder.AppendLine(error);
                args.onError?.Invoke(error);
            },
                (code) => {
                exitcode = code;
            }
                );

            while (exitcode == null)
            {
                if (task.cancellation.IsCancellationRequested)
                {
                    terminator(true);
                    task.ThrowIfCancellationRequested();
                }
                await Task.Yield();
            }

            // 137 happens for Kill() and 143 for CloseMainWindow(),
            // which means the script has been canceled
            if (!args.silentError && exitcode != 0 && exitcode != 137 && exitcode != 143)
            {
                throw new Exception(string.Format(
                                        "{0}: Failed to execute {1} (code {2}): {3}\nOutput: {4}",
                                        name, Path.GetFileName(args.startInfo.FileName), exitcode,
                                        errorBuilder.ToString(), outputBuilder.ToString()
                                        ));
            }

            return(exitcode.Value);
        }