Esempio n. 1
0
        async Task <EvaluationStatus> CoreEvaluateCodeCellAsync(
            CodeCellState codeCellState,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken = ClientSession.CancellationToken.LinkWith(cancellationToken);

            if (!ClientSession.Agent.IsConnected || ClientSession.CompilationWorkspace == null)
            {
                codeCellState.View.IsEvaluating        = false;
                codeCellState.View.HasErrorDiagnostics = true;
                codeCellState.View.RenderDiagnostic(new Diagnostic(
                                                        DiagnosticSeverity.Error,
                                                        "Cannot evaluate: not connected to agent."));
                return(EvaluationStatus.Disconnected);
            }

            CodeAnalysis.Compilation   compilation = null;
            IReadOnlyList <Diagnostic> diagnostics = null;
            ExceptionNode exception = null;
            bool          agentTerminatedWhileEvaluating = false;

            try {
                compilation = await ClientSession.CompilationWorkspace.EmitCellCompilationAsync(
                    codeCellState.CodeCellId,
                    new EvaluationEnvironment (ClientSession.WorkingDirectory),
                    cancellationToken);

                diagnostics = await ClientSession.CompilationWorkspace.GetCellDiagnosticsAsync(
                    codeCellState.CodeCellId,
                    cancellationToken);

                var integrationAssemblies = compilation
                                            .References
                                            .Where(ra => ra.HasIntegration)
                                            .ToArray();
                if (integrationAssemblies.Length > 0)
                {
                    await ClientSession.Agent.Api.EvaluationContextManager.LoadAssembliesAsync(
                        TargetCompilationConfiguration.EvaluationContextId,
                        integrationAssemblies);
                }
            } catch (Exception e) {
                exception = ExceptionNode.Create(e);
            }

            var hasErrorDiagnostics = codeCellState.View.HasErrorDiagnostics = diagnostics
                                                                               .Any(d => d.Severity == DiagnosticSeverity.Error);

            foreach (var diagnostic in diagnostics)
            {
                codeCellState.View.RenderDiagnostic(diagnostic);
            }

            try {
                if (compilation != null)
                {
                    await ClientSession.Agent.Api.EvaluationContextManager.EvaluateAsync(
                        TargetCompilationConfiguration.EvaluationContextId,
                        compilation,
                        cancellationToken);
                }
            } catch (XipErrorMessageException e) {
                exception = e.XipErrorMessage.Exception;
            } catch (Exception e) {
                Log.Error(TAG, "marking agent as terminated", e);
                agentTerminatedWhileEvaluating         = true;
                codeCellState.View.HasErrorDiagnostics = true;
                codeCellState.View.RenderDiagnostic(new Diagnostic(
                                                        DiagnosticSeverity.Error,
                                                        Catalog.GetString(
                                                            "The application terminated during evaluation of this cell. " +
                                                            "Run this cell manually to try again.")));
            }

            codeCellState.View.IsEvaluating = false;

            EvaluationStatus evaluationStatus;

            if (exception != null)
            {
                codeCellState.View.RenderResult(
                    CultureInfo.CurrentCulture,
                    exception,
                    EvaluationResultHandling.Replace);
                evaluationStatus = EvaluationStatus.EvaluationException;
            }
            else if (hasErrorDiagnostics)
            {
                return(EvaluationStatus.ErrorDiagnostic);
            }
            else if (agentTerminatedWhileEvaluating)
            {
                evaluationStatus = EvaluationStatus.Disconnected;
            }
            else
            {
                evaluationStatus = EvaluationStatus.Success;
            }

            if (ClientSession.SessionKind != ClientSessionKind.Workbook)
            {
                codeCellState.Freeze();
            }

            codeCellState.NotifyEvaluated(agentTerminatedWhileEvaluating);
            return(evaluationStatus);
        }
        async Task <CodeCellEvaluationStatus> CoreEvaluateCodeCellAsync(
            CodeCellState codeCellState,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken = ClientSession.CancellationToken.LinkWith(cancellationToken);

            if (!ClientSession.Agent.IsConnected || ClientSession.CompilationWorkspace == null)
            {
                codeCellState.View.IsEvaluating        = false;
                codeCellState.View.HasErrorDiagnostics = true;
                codeCellState.View.RenderDiagnostic(new InteractiveDiagnostic(
                                                        DiagnosticSeverity.Error,
                                                        "Cannot evaluate: not connected to agent."));
                return(CodeCellEvaluationStatus.Disconnected);
            }

            CodeAnalysis.Compilation compilation = null;
            ExceptionNode            exception   = null;
            bool agentTerminatedWhileEvaluating  = false;

            try {
                compilation = await ClientSession.CompilationWorkspace.GetSubmissionCompilationAsync(
                    codeCellState.DocumentId,
                    new EvaluationEnvironment (ClientSession.WorkingDirectory),
                    cancellationToken);

                var integrationAssemblies = compilation
                                            .References
                                            .Where(ra => ra.HasIntegration)
                                            .ToArray();
                if (integrationAssemblies.Length > 0)
                {
                    await ClientSession.Agent.Api.LoadAssembliesAsync(
                        ClientSession.CompilationWorkspace.EvaluationContextId,
                        integrationAssemblies);
                }

                foreach (var dependency in ClientSession.CompilationWorkspace.WebDependencies)
                {
                    if (ClientSession.AddWebResource(dependency.Location, out var guid))
                    {
                        await LoadWorkbookDependencyAsync(guid + dependency.Location.Extension);
                    }
                }
            } catch (Exception e) {
                exception = ExceptionNode.Create(e);
            }

            var diagnostics = ClientSession.CompilationWorkspace.CurrentSubmissionDiagnostics.Filter();

            codeCellState.View.HasErrorDiagnostics = diagnostics.HasErrors;

            foreach (var diagnostic in diagnostics)
            {
                codeCellState.View.RenderDiagnostic((InteractiveDiagnostic)diagnostic);
            }

            try {
                if (compilation != null)
                {
                    codeCellState.LastEvaluationRequestId = compilation.MessageId;
                    codeCellState.IsResultAnExpression    = compilation.IsResultAnExpression;

                    await ClientSession.Agent.Api.EvaluateAsync(
                        compilation,
                        cancellationToken);
                }
            } catch (XipErrorMessageException e) {
                exception = e.XipErrorMessage.Exception;
            } catch (Exception e) {
                Log.Error(TAG, "marking agent as terminated", e);
                agentTerminatedWhileEvaluating         = true;
                codeCellState.View.HasErrorDiagnostics = true;
                codeCellState.View.RenderDiagnostic(new InteractiveDiagnostic(
                                                        DiagnosticSeverity.Error,
                                                        Catalog.GetString(
                                                            "The application terminated during evaluation of this cell. " +
                                                            "Run this cell manually to try again.")));
            }

            codeCellState.View.IsEvaluating = false;

            CodeCellEvaluationStatus evaluationStatus;

            if (exception != null)
            {
                codeCellState.View.RenderResult(
                    CultureInfo.CurrentCulture,
                    FilterException(exception),
                    EvaluationResultHandling.Replace);
                evaluationStatus = CodeCellEvaluationStatus.EvaluationException;
            }
            else if (diagnostics.HasErrors)
            {
                return(CodeCellEvaluationStatus.ErrorDiagnostic);
            }
            else if (agentTerminatedWhileEvaluating)
            {
                evaluationStatus = CodeCellEvaluationStatus.Disconnected;
            }
            else
            {
                evaluationStatus = CodeCellEvaluationStatus.Success;
            }

            if (ClientSession.SessionKind != ClientSessionKind.Workbook)
            {
                codeCellState.Freeze();
            }

            codeCellState.NotifyEvaluated(agentTerminatedWhileEvaluating);
            return(evaluationStatus);
        }