Esempio n. 1
0
 public Task <PoshConsolePipelineResults> Invoke(PoshConsolePipeline pipeline)
 {
     // Even though CommandQueue is threaded
     // We NEED to ensure that our sets of commands stay together
     CommandQueue.Post(pipeline);
     return(pipeline.Task);
 }
Esempio n. 2
0
        private static void PipelineFinished(PoshConsolePipeline commands, PipelineStateEventArgs e, Pipeline pipeline)
        {
            // collect output
            var errors  = pipeline.Error.ReadToEnd();
            var results = pipeline.Output.ReadToEnd();
            var failure = e.PipelineStateInfo.Reason;

            if (commands.Output == ConsoleOutput.CommandOnly || commands.Output == ConsoleOutput.Default)
            {
                PoshConsole.CurrentConsole.WriteErrorRecords(errors);
            }

            // Fail the task, if applicable
            if (failure != null)
            {
                errors.Insert(0, failure);

                Debug.WriteLine(failure.GetType(), "PipelineFailure");
                Debug.WriteLine(failure.Message, "PipelineFailure");
                if (commands.Output != ConsoleOutput.None)
                {
                    PoshConsole.CurrentConsole.WriteErrorRecord(((RuntimeException)failure).ErrorRecord);
                }
                // commands.TaskSource.SetException(failure);
            }

            commands.TaskSource.SetResult(
                new PoshConsolePipelineResults(pipeline.InstanceId, commands.Commands, results, errors, pipeline.PipelineStateInfo.State));
        }
Esempio n. 3
0
        private Pipeline GetPowerShellPipeline(PoshConsolePipeline boundCommand)
        {
            Pipeline pipeline;

            if (boundCommand.IsScript)
            {
                var command = boundCommand.Commands.First();
                pipeline = _runSpace.CreatePipeline(command.CommandText, boundCommand.Output == ConsoleOutput.Default || boundCommand.Output == ConsoleOutput.CommandOnly);
            }
            else
            {
                pipeline = _runSpace.CreatePipeline();

                foreach (var command in boundCommand.Commands)
                {
                    pipeline.Commands.Add(command);
                }
            }

            if (boundCommand.Output == ConsoleOutput.Default || boundCommand.Output == ConsoleOutput.OutputOnly)
            {
                pipeline.Commands.Add(DefaultOutputCommand);
            }

            return(pipeline);
        }
Esempio n. 4
0
        public Task <PoshConsolePipelineResults> Invoke(IList <Command> commands, IEnumerable input = null, ConsoleOutput output = ConsoleOutput.Default)
        {
            var pipeline = new PoshConsolePipeline(commands, input, output);

            // Even though CommandQueue is threaded
            // We NEED to ensure that our sets of commands stay together
            CommandQueue.Post(pipeline);
            return(pipeline.Task);
        }
Esempio n. 5
0
        private void InvokePipeline(PoshConsolePipeline pcp)
        {
            _pipeline = GetPowerShellPipeline(pcp);
            _pipeline.StateChanged +=
                (sender, e) =>
            {
                Trace.WriteLine("Pipeline is " + e.PipelineStateInfo.State);

                if (e.PipelineStateInfo.IsDone())
                {
                    Trace.WriteLine("Pipeline is Done");

                    var completed = Interlocked.Exchange(ref _pipeline, null);
                    if (completed != null)
                    {
                        // Collect output for event before disposing of pipeline
                        PipelineFinished(pcp, e, completed);

                        if (pcp.Output != ConsoleOutput.None)
                        {
                            PoshConsole.CurrentConsole.OnCommandFinished(pcp.Commands, e.PipelineStateInfo.State);
                        }

                        completed.Dispose();
                        //_SyncEvents.PipelineFinishedEvent.Set();
                    }
                }
            };

            try
            {
                if (pcp.Input == null)
                {
                    _pipeline.Invoke();
                }
                else
                {
                    _pipeline.Invoke(pcp.Input);
                }
            }
            catch (Exception ipe)
            {
                // TODO: Handle IncompleteParseException with some elegance!
                //    klumsy suggested we could prevent these by using the tokenizer
                // Tokenizing in OnEnterPressed (before sending it to the CommandRunner)
                //    would allow us to let {Enter} be handled nicely ...
                // Tokenizing in KeyDown would let us do live syntax highlighting,
                //    is it fast enough to work?
                Debug.WriteLine(ipe.Message);
            }
            //catch (ParseException pe)
            //{
            //   // TODO: Handle ParseException with some elegance!
            //}
        }
Esempio n. 6
0
 public void Enqueue(PoshConsolePipeline pipeline)
 {
     // Even though CommandQueue is threaded
     // We NEED to ensure that our sets of commands stay together
     CommandQueue.Post(pipeline);
 }