public async void Syntax_Tree_Has_Same_Language_As_Package()
        {
            var package = await Create.ConsoleWorkspaceCopy();

            var         workspace             = WorkspaceFactory.CreateWorkspaceFromDirectory(package.Directory, package.Name, includeInstrumentation: true);
            var         roslynWorkspaceServer = new RoslynWorkspaceServer(package);
            Func <Task> run = () => roslynWorkspaceServer.Run(new WorkspaceRequest(workspace));

            run.Should().NotThrow();
        }
Esempio n. 2
0
        public async Task When_workspace_was_not_registered_then_GetWorkspaceServer_will_return_a_working_server()
        {
            var unregisteredWorkspace = await Default.ConsoleWorkspace();

            var server = new RoslynWorkspaceServer(_registry);

            var workspaceRequest = WorkspaceRequestFactory.CreateRequestFromDirectory(unregisteredWorkspace.Directory, unregisteredWorkspace.Name);

            var result = await server.Run(workspaceRequest);

            result.Succeeded.Should().BeTrue();
        }
        public async Task Run_starts_the_kestrel_server_and_provides_a_WebServer_feature_that_can_receive_requests()
        {
            var registry = Default.PackageFinder;
            var server   = new RoslynWorkspaceServer(registry);
            var package  = await registry.Get <IHaveADirectory>("aspnet.webapi");

            var workspace = WorkspaceFactory.CreateWorkspaceFromDirectory(package.Directory, "aspnet.webapi");

            using (var runResult = await server.Run(new WorkspaceRequest(workspace, "Program.cs")))
            {
                var webServer = runResult.GetFeature <WebServer>();

                var response = await webServer.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/api/values")).CancelIfExceeds(new TimeBudget(35.Seconds()));

                var result = await response.EnsureSuccess()
                             .DeserializeAs <string[]>();

                result.Should().Equal("value1", "value2");
            }
        }
        public async Task NuGet_packages_can_be_added_during_initialization()
        {
            var workspaceId = Package.CreateDirectory(nameof(NuGet_packages_can_be_added_during_initialization)).Name;

            registry.Add(workspaceId,
                         options =>
            {
                options.CreateUsingDotnet("console");
                options.AddPackageReference("Twilio", "5.9.2");
            });

            var workspaceServer = new RoslynWorkspaceServer(registry);

            var workspace = Workspace.FromSource(
                @"
using System;
using Twilio.Clients;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Twilio_try.dot.net_sample
{
    class Program
    {
        static void Main()
        {
            var sendFromPhoneNumber = new PhoneNumber(""TWILIO_PHONE_NUMBER"");
            var sendToPhoneNumber = new PhoneNumber(""RECIPIENT_PHONE_NUMBER"");
        }
    }
}",
                workspaceType: workspaceId);

            var result = await workspaceServer.Run(new WorkspaceRequest(workspace));

            result.Succeeded.Should().BeTrue(because: "compilation can't succeed unless the NuGet package has been restored.");
        }
Esempio n. 5
0
        public async Task <ICommandDeliveryResult> Handle(
            ICommandDelivery <JupyterRequestContext> delivery)
        {
            switch (delivery.Command.Request.Header.MessageType)
            {
            case MessageTypeValues.ExecuteRequest:
                var transient = new Dictionary <string, object> {
                    { "display_id", Guid.NewGuid().ToString() }
                };

                var jObject        = (JObject)delivery.Command.Request.Content;
                var executeRequest = jObject.ToObject <ExecuteRequest>();

                var code = executeRequest.Code;

                var workspace = new Workspace(
                    files: new[]
                {
                    new File("Program.cs", Scaffold())
                },
                    buffers: new[]
                {
                    new Buffer(new BufferId("Program.cs", "main"), code),
                },
                    workspaceType: "console");

                var workspaceRequest = new WorkspaceRequest(workspace);

                var server = new RoslynWorkspaceServer(new PackageRegistry());

                var result = await server.Run(workspaceRequest);

                var messageBuilder = delivery.Command.Builder;
                var ioPubChannel   = delivery.Command.IoPubChannel;
                var serverChannel  = delivery.Command.ServerChannel;

                if (!executeRequest.Silent)
                {
                    _executionCount++;

                    var executeInput = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteInput,
                        new ExecuteInput
                    {
                        Code           = code,
                        ExecutionCount = _executionCount
                    },
                        delivery.Command.Request.Header);

                    ioPubChannel.Send(executeInput);
                }

                // execute result
                var output = string.Join("\n", result.Output);


                // executeResult data
                var executeResultData = new ExecuteResult()
                {
                    Data = new JObject
                    {
                        { "text/html", output },
                        { "text/plain", output }
                    },
                    Transient      = transient,
                    ExecutionCount = _executionCount
                };



                var resultSucceeded = result.Succeeded &&
                                      result.Exception == null;

                if (resultSucceeded)
                {
                    // reply ok
                    var executeReplyPayload = new ExecuteReplyOk
                    {
                        ExecutionCount = _executionCount
                    };

                    // send to server
                    var executeReply = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteReply,
                        executeReplyPayload,
                        delivery.Command.Request.Header);

                    executeReply.Identifiers = delivery.Command.Request.Identifiers;

                    serverChannel.Send(executeReply);
                }
                else
                {
                    var errorContent = new Error
                    {
                        EName = string.IsNullOrWhiteSpace(result.Exception)
                                        ? "Compile Error"
                                        : "Unhandled Exception",
                        EValue    = output,
                        Traceback = new List <string>()
                    };

                    //  reply Error
                    var executeReplyPayload = new ExecuteReplyError(errorContent)
                    {
                        ExecutionCount = _executionCount
                    };

                    // send to server
                    var executeReply = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteReply,
                        executeReplyPayload,
                        delivery.Command.Request.Header);

                    executeReply.Identifiers = delivery.Command.Request.Identifiers;

                    serverChannel.Send(executeReply);

                    if (!executeRequest.Silent)
                    {
                        // send on io
                        var error = messageBuilder.CreateMessage(
                            MessageTypeValues.Error,
                            errorContent,
                            delivery.Command.Request.Header);
                        ioPubChannel.Send(error);

                        // send on stderr
                        var stdErr = new StdErrStream
                        {
                            Text = errorContent.EValue
                        };
                        var stream = messageBuilder.CreateMessage(
                            MessageTypeValues.Stream,
                            stdErr,
                            delivery.Command.Request.Header);
                        ioPubChannel.Send(stream);
                    }
                }

                if (!executeRequest.Silent && resultSucceeded)
                {
                    // send on io
                    var executeResultMessage = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteResult,
                        executeResultData,
                        delivery.Command.Request.Header);
                    ioPubChannel.Send(executeResultMessage);
                }

                break;
            }

            return(delivery.Complete());
        }
Esempio n. 6
0
        public async Task <IActionResult> Run(
            [FromBody] WorkspaceRequest request,
            [FromHeader(Name = "Timeout")] string timeoutInMilliseconds = "45000")
        {
            if (_options.IsLanguageService)
            {
                return(NotFound());
            }

            using (var operation = Log.OnEnterAndConfirmOnExit())
            {
                var workspaceType = request.Workspace.WorkspaceType;

                operation.Info("Processing workspaceType {workspaceType}", workspaceType);

                if (!int.TryParse(timeoutInMilliseconds, out var timeoutMs))
                {
                    return(BadRequest());
                }

                RunResult result;
                var       runTimeout = TimeSpan.FromMilliseconds(timeoutMs);

                var budget = new TimeBudget(runTimeout);

                if (string.Equals(workspaceType, "script", StringComparison.OrdinalIgnoreCase))
                {
                    var server = new ScriptingWorkspaceServer();

                    result = await server.Run(
                        request,
                        budget);
                }
                else
                {
                    using (result = await _workspaceServer.Run(request, budget))
                    {
                        _disposables.Add(result);

                        if (result.Succeeded &&
                            request.HttpRequest != null)
                        {
                            var webServer = result.GetFeature <WebServer>();

                            if (webServer != null)
                            {
                                var response = await webServer.SendAsync(
                                    request.HttpRequest.ToHttpRequestMessage())
                                               .CancelIfExceeds(budget);

                                result = new RunResult(
                                    true,
                                    await response.ToDisplayString());
                            }
                        }
                    }
                }

                budget.RecordEntry();
                operation.Succeed();

                return(Ok(result));
            }
        }