Esempio n. 1
0
        public static async Task LoadFromAssembliesInDirectory(
            this KernelExtensionLoader loader,
            IDirectoryAccessor directory,
            IKernel kernel,
            KernelInvocationContext context,
            IReadOnlyList <FileInfo> additionalDependencies = null)
        {
            if (directory.RootDirectoryExists())
            {
                context.Publish(new DisplayedValueProduced($"Loading kernel extensions in directory {directory.GetFullyQualifiedRoot().FullName}", context.Command));

                var extensionDlls = directory.GetAllFiles()
                                    .Where(file => file.Extension == ".dll")
                                    .Select(directory.GetFullyQualifiedFilePath);


                foreach (var extensionDll in extensionDlls)
                {
                    await loader.LoadFromAssembly(
                        extensionDll,
                        kernel,
                        context,
                        additionalDependencies);
                }

                context.Publish(new DisplayedValueProduced($"Loaded kernel extensions in directory {directory.GetFullyQualifiedRoot().FullName}", context.Command));
            }
        }
Esempio n. 2
0
        private async Task HandleRequestCompletion(
            RequestCompletion requestCompletion,
            KernelInvocationContext context,
            ScriptState scriptState)
        {
            var completionRequestReceived = new CompletionRequestReceived(requestCompletion);

            context.Publish(completionRequestReceived);

            var completionList =
                await GetCompletionList(requestCompletion.Code, requestCompletion.CursorPosition, scriptState);

            context.Publish(new CompletionRequestCompleted(completionList, requestCompletion));
        }
Esempio n. 3
0
        private async Task HandleCancelCurrentCommand(
            CancelCurrentCommand cancelCurrentCommand,
            KernelInvocationContext context)
        {
            var reply = new CurrentCommandCancelled(cancelCurrentCommand);

            lock (_cancellationSourceLock)
            {
                _cancellationSource.Cancel();
                _cancellationSource = new CancellationTokenSource();
            }

            context.Publish(reply);
        }
Esempio n. 4
0
        private void PublishError(
            string error,
            KernelInvocationContext context,
            IKernelCommand command)
        {
            var formattedValues = new List <FormattedValue>
            {
                new FormattedValue(
                    PlainTextFormatter.MimeType, error)
            };

            context.Publish(
                new StandardErrorValueProduced(
                    error,
                    command,
                    formattedValues));
        }
Esempio n. 5
0
        private void PublishOutput(
            string output,
            KernelInvocationContext context,
            IKernelCommand command)
        {
            var formattedValues = new List <FormattedValue>
            {
                new FormattedValue(
                    PlainTextFormatter.MimeType, output)
            };

            context.Publish(
                new StandardOutputValueProduced(
                    output,
                    command,
                    formattedValues));
        }
Esempio n. 6
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"));
            }
        }
Esempio n. 7
0
        public async Task LoadExtensionsInDirectory(IDirectoryAccessor directory, KernelInvocationContext context)
        {
            var extensionsDirectory = directory.GetDirectoryAccessorForRelativePath(_assemblyExtensionsPath);

            await new KernelExtensionLoader().LoadFromAssembliesInDirectory(extensionsDirectory, context.HandlingKernel, (kernelEvent) => context.Publish(kernelEvent));
        }