private async Task HandleSubmitCode( SubmitCode submitCode, KernelInvocationContext context) { var codeSubmissionReceived = new CodeSubmissionReceived( submitCode.Code, submitCode); context.OnNext(codeSubmissionReceived); var(shouldExecute, code) = IsBufferACompleteSubmission(submitCode.Code); if (shouldExecute) { 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) { var message = string.Join("\n", (_scriptState?.Script?.GetDiagnostics() ?? Enumerable.Empty <Diagnostic>()).Select(d => d.GetMessage())); context.OnNext(new CodeSubmissionEvaluationFailed(exception, message, submitCode)); context.OnError(exception); } else { if (HasReturnValue) { var returnValueType = _scriptState.ReturnValue?.GetType(); var mimeType = MimeTypeFor(returnValueType); var formatted = _scriptState.ReturnValue.ToDisplayString(mimeType); var formattedValues = new List <FormattedValue> { new FormattedValue(mimeType, formatted) }; context.OnNext( new ValueProduced( _scriptState.ReturnValue, submitCode, true, formattedValues)); } context.OnNext(new CodeSubmissionEvaluated(submitCode)); context.OnCompleted(); } } else { context.OnNext(new IncompleteCodeSubmissionReceived(submitCode)); context.OnNext(new CodeSubmissionEvaluated(submitCode)); context.OnCompleted(); } }
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(); } }
private async Task HandleSubmitCode( SubmitCode codeSubmission, KernelInvocationContext context) { var codeSubmissionReceived = new CodeSubmissionReceived( codeSubmission.Code, codeSubmission); context.OnNext(codeSubmissionReceived); var(shouldExecute, code) = IsBufferACompleteSubmission(codeSubmission.Code); if (shouldExecute) { context.OnNext(new CompleteCodeSubmissionReceived(codeSubmission)); Exception exception = null; 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) { var message = string.Join("\n", (_scriptState?.Script?.GetDiagnostics() ?? Enumerable.Empty <Diagnostic>()).Select(d => d.GetMessage())); context.OnNext(new CodeSubmissionEvaluationFailed(exception, message, codeSubmission)); context.OnError(exception); } else { if (HasReturnValue) { context.OnNext(new ValueProduced(_scriptState.ReturnValue, codeSubmission)); } context.OnNext(new CodeSubmissionEvaluated(codeSubmission)); context.OnCompleted(); } } else { context.OnNext(new IncompleteCodeSubmissionReceived(codeSubmission)); context.OnCompleted(); } }