/// <summary>
        /// Executes a command by running it through the IronPython parser.
        /// </summary>
        /// <param name="output">Console output buffer to append any output messages.</param>
        /// <param name="command">Command to execute.</param>
        public void Execute(IConsoleOutput output, string command)
        {
            if (!_initialized)
            {
                var memStream    = new MemoryStream();
                var pythonWriter = new OutputBufferWriter(memStream, output);
                _scriptEngine.Runtime.IO.SetOutput(memStream, pythonWriter);
                _scriptEngine.Runtime.IO.SetErrorOutput(memStream, pythonWriter);
                _initialized = true;
            }

            if (EchoEnabled)
            {
                output.Append(command);
            }

            string resultStr;

            try
            {
                dynamic result = RunScript(command);
                resultStr = result == null ? null : result.ToString();
            }
            catch (Exception ex)
            {
                resultStr = ex.Message;
            }

            output.Append(resultStr);
        }
Exemplo n.º 2
0
        public async void Execute(IConsoleOutput output, string command)
        {
            try
            {
                var reply     = string.Empty;
                var arguments = command.Split(new char[] { ' ' });

                if (arguments[0].Equals("Loaded", StringComparison.OrdinalIgnoreCase))
                {
                    reply = getLoadedScripts();
                }
                else if (arguments[0].Equals("Running", StringComparison.OrdinalIgnoreCase))
                {
                    reply = getRunningScripts();
                }
                else if (arguments[0].Equals("Exec", StringComparison.OrdinalIgnoreCase))
                {
                    var scriptFile = arguments[1];
                    ExecuteScript(scriptFile);
                }
                else if (arguments[0].Equals("Cancel", StringComparison.OrdinalIgnoreCase))
                {
                    CancelScript();
                }
                else
                {
                    reply = await ExecuteExpression(command);
                }
                output.Append(reply);
            }
            catch (Exception ex)
            {
                output.Append(ex.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes a console command as C# script.
        /// </summary>
        /// <param name="output">Console output buffer to append any output messages.</param>
        /// <param name="command">Command to execute.</param>
        public void Execute(IConsoleOutput output, string command)
        {
            if (EchoEnabled)
            {
                output.Append(command);
            }


            if (!_warmupTask.IsCompleted)
            {
                _warmupTask.Wait();
            }

            Task.Run(async() =>
            {
                try
                {
                    _executionSignal.WaitOne(); // TODO: timeout

                    _scriptState = await _scriptState.ContinueWithAsync(command, ScriptOptions);
                    if (_scriptState.ReturnValue != null)
                    {
                        output.Append(_scriptState.ReturnValue.ToString());
                    }
                }
                catch (CompilationErrorException e)
                {
                    output.Append(string.Join(Environment.NewLine, e.Diagnostics));
                }
                finally
                {
                    _executionSignal.Set();
                }
            });
        }
 public override void Write(string value)
 {
     if (value != "\r\n")
     {
         _output.Append(value);
     }
 }
        /// <summary>
        /// Executes a command that is matched by the first input word.
        /// </summary>
        /// <param name="output">Console output to append any output messages.</param>
        /// <param name="input">Command to execute.</param>
        public void Execute(IConsoleOutput output, string input)
        {
            if (EchoEnabled)
            {
                output.Append(input);
            }

            string[] instructions = input.Split(InstructionSeparator, StringSplitOptions.RemoveEmptyEntries);

            foreach (var instruction in instructions)
            {
                string[] inputSplit = instruction.Trim().Split(CommandAndArgumentSeparator, StringSplitOptions.RemoveEmptyEntries);
                if (inputSplit.Length == 0)
                {
                    return;
                }

                string   command     = inputSplit[0];
                string[] commandArgs = inputSplit.Skip(1).ToArray();

                List <Func <string[], string> > commandList;
                if (_commandMap.TryGetValue(command, out commandList))
                {
                    foreach (Func <string[], string> cmd in commandList)
                    {
                        try
                        {
                            string result = cmd(commandArgs);
                            if (!string.IsNullOrWhiteSpace(result))
                            {
                                output.Append(result);
                            }
                        }
                        catch (Exception ex)
                        {
                            output.Append($"Command '{command}' failed. {ex.Message}");
                        }
                    }
                }
                else
                {
                    output.Append($"Command '{command}' not found.");
                }
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Does nothing.
 /// </summary>
 /// <param name="output">Console output buffer to append any output messages.</param>
 /// <param name="command">Command to execute.</param>
 public void Execute(IConsoleOutput output, string command)
 {
     output.Append(command);
 }