public async Task QueueAction(
            KernelCommandInvocation action)
        {
            var command = new AnonymousKernelCommand(action);

            await HandlingKernel.SendAsync(command);
        }
Esempio n. 2
0
        public IReadOnlyList <IKernelCommand> SplitSubmission(SubmitCode submitCode)
        {
            var directiveParser = GetDirectiveParser();

            var lines = new Queue <string>(
                submitCode.Code.Split(new[] { "\r\n", "\n" },
                                      StringSplitOptions.None));

            var nonDirectiveLines = new List <string>();
            var commands          = new List <IKernelCommand>();
            var hoistedCommands   = new List <IKernelCommand>();
            var commandWasSplit   = false;

            while (lines.Count > 0)
            {
                var currentLine = lines.Dequeue();

                if (string.IsNullOrWhiteSpace(currentLine))
                {
                    nonDirectiveLines.Add(currentLine);
                    continue;
                }

                var parseResult = directiveParser.Parse(currentLine);
                var command     = parseResult.CommandResult.Command;

                if (parseResult.Errors.Count == 0)
                {
                    commandWasSplit = true;

                    if (AccumulatedSubmission() is { } cmd)
                    {
                        commands.Add(cmd);
                    }

                    var runDirective = new AnonymousKernelCommand(
                        (_, __) => parseResult.InvokeAsync());

                    if (command.Name == "#r")
                    {
                        hoistedCommands.Add(runDirective);
                    }
                    else
                    {
                        commands.Add(runDirective);
                    }
                }
                else
                {
                    if (command == parseResult.Parser.Configuration.RootCommand ||
                        command.Name == "#r")
                    {
                        nonDirectiveLines.Add(currentLine);
                    }
                    else
                    {
                        var message =
                            string.Join(Environment.NewLine,
                                        parseResult.Errors
                                        .Select(e => e.ToString()));

                        commands.Add(new DisplayError(message));
                    }
                }
            }

            if (commandWasSplit)
            {
                if (AccumulatedSubmission() is { } command)
                {
                    commands.Add(command);
                }
            }
            else
            {
                commands.Add(submitCode);
            }

            if (hoistedCommands.Count > 0)
            {
                var parseResult = directiveParser.Parse("#!nuget-restore");

                hoistedCommands.Add(
                    new AnonymousKernelCommand(
                        (_, __) => parseResult.InvokeAsync()));
            }

            return(hoistedCommands.Concat(commands).ToArray());

            IKernelCommand AccumulatedSubmission()
            {
                if (nonDirectiveLines.Any())
                {
                    var code = string.Join(Environment.NewLine, nonDirectiveLines);

                    nonDirectiveLines.Clear();

                    if (!string.IsNullOrWhiteSpace(code))
                    {
                        return(new SubmitCode(code));
                    }
                }

                return(null);
            }
        }