Ejemplo n.º 1
0
        private async Task HandleSubmitCode(
            SubmitCode submitCode,
            KernelInvocationContext context)
        {
            CancellationTokenSource cancellationSource;

            lock (_cancellationSourceLock)
            {
                cancellationSource = _cancellationSource;
            }
            var codeSubmissionReceived = new CodeSubmissionReceived(
                submitCode.Code,
                submitCode);

            context.Publish(codeSubmissionReceived);

            var code       = submitCode.Code;
            var isComplete = await IsCompleteSubmissionAsync(submitCode.Code);

            if (isComplete)
            {
                context.Publish(new CompleteCodeSubmissionReceived(submitCode));
            }
            else
            {
                context.Publish(new IncompleteCodeSubmissionReceived(submitCode));
            }

            if (submitCode.SubmissionType == SubmissionType.Diagnose)
            {
                return;
            }

            Exception exception = null;

            using var console = await ConsoleOutput.Capture();

            using (console.SubscribeToStandardOutput(std => PublishOutput(std, context, submitCode)))
                using (console.SubscribeToStandardError(std => PublishError(std, context, submitCode)))
                {
                    if (!cancellationSource.IsCancellationRequested)
                    {
                        try
                        {
                            if (ScriptState == null)
                            {
                                ScriptState = await CSharpScript.RunAsync(
                                    code,
                                    ScriptOptions,
                                    cancellationToken : cancellationSource.Token)
                                              .UntilCancelled(cancellationSource.Token);
                            }
                            else
                            {
                                ScriptState = await ScriptState.ContinueWithAsync(
                                    code,
                                    ScriptOptions,
                                    e =>
                                {
                                    exception = e;
                                    return(true);
                                },
                                    cancellationToken : cancellationSource.Token)
                                              .UntilCancelled(cancellationSource.Token);
                            }
                        }
                        catch (CompilationErrorException cpe)
                        {
                            exception = new CodeSubmissionCompilationErrorException(cpe);
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                    }
                }

            if (!cancellationSource.IsCancellationRequested)
            {
                if (exception != null)
                {
                    string message = null;

                    if (exception is CodeSubmissionCompilationErrorException compilationError)
                    {
                        message =
                            string.Join(Environment.NewLine,
                                        (compilationError.InnerException as CompilationErrorException)?.Diagnostics.Select(d => d.ToString()) ?? Enumerable.Empty <string>());
                    }

                    context.Publish(new CommandFailed(exception, submitCode, message));
                }
                else
                {
                    if (ScriptState != null && HasReturnValue)
                    {
                        var formattedValues = FormattedValue.FromObject(ScriptState.ReturnValue);
                        context.Publish(
                            new ReturnValueProduced(
                                ScriptState.ReturnValue,
                                submitCode,
                                formattedValues));
                    }

                    context.Complete();
                }
            }
            else
            {
                context.Publish(new CommandFailed(null, submitCode, "Command cancelled"));
            }
        }
Ejemplo n.º 2
0
        private async Task HandleSubmitCode(
            SubmitCode submitCode,
            KernelInvocationContext context)
        {
            var codeSubmissionReceived = new CodeSubmissionReceived(
                submitCode.Code,
                submitCode);

            context.OnNext(codeSubmissionReceived);

            var code = submitCode.Code;

            context.OnNext(new CompleteCodeSubmissionReceived(submitCode));
            Exception exception = null;

            using var console = await ConsoleOutput.Capture();

            using var _ = console.SubscribeToStandardOutput(std => PublishOutput(std, context, submitCode));

            try
            {
                if (_scriptState == null)
                {
                    _scriptState = await CSharpScript.RunAsync(
                        code,
                        ScriptOptions);
                }
                else
                {
                    _scriptState = await _scriptState.ContinueWithAsync(
                        code,
                        ScriptOptions,
                        e =>
                    {
                        exception = e;
                        return(true);
                    });
                }
            }
            catch (Exception e)
            {
                exception = e;
            }

            if (exception != null)
            {
                string message = null;

                if (exception is CompilationErrorException compilationError)
                {
                    message =
                        string.Join(Environment.NewLine,
                                    compilationError.Diagnostics.Select(d => d.ToString()));
                }

                context.OnNext(new CommandFailed(exception, submitCode, message));
                context.OnError(exception);
            }
            else
            {
                if (HasReturnValue)
                {
                    var formattedValues = FormattedValue.FromObject(_scriptState.ReturnValue);
                    context.OnNext(
                        new ValueProduced(
                            _scriptState.ReturnValue,
                            submitCode,
                            true,
                            formattedValues));
                }

                context.OnNext(new CodeSubmissionEvaluated(submitCode));

                context.OnCompleted();
            }
        }