这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged

Magic #392

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion DotNetTry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactiv
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactive", "Microsoft.DotNet.Interactive\Microsoft.DotNet.Interactive.csproj", "{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XPlot.DotNet.Interactive.KernelExtensions", "XPlot.DotNet.Interactive.KernelExtensions\XPlot.DotNet.Interactive.KernelExtensions.csproj", "{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XPlot.DotNet.Interactive.KernelExtensions", "XPlot.DotNet.Interactive.KernelExtensions\XPlot.DotNet.Interactive.KernelExtensions.csproj", "{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactive.Tests", "Microsoft.DotNet.Interactive.Tests\Microsoft.DotNet.Interactive.Tests.csproj", "{11752CED-0F22-433D-B0B4-BA642C1BA613}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Microsoft.DotNet.Interactive.FSharp", "Microsoft.DotNet.Interactive.FSharp\Microsoft.DotNet.Interactive.FSharp.fsproj", "{12821999-9F44-486B-8EE3-38F0EFDFDA32}"
EndProject
Expand Down Expand Up @@ -193,6 +195,10 @@ Global
{12821999-9F44-486B-8EE3-38F0EFDFDA32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12821999-9F44-486B-8EE3-38F0EFDFDA32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12821999-9F44-486B-8EE3-38F0EFDFDA32}.Release|Any CPU.Build.0 = Release|Any CPU
{11752CED-0F22-433D-B0B4-BA642C1BA613}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11752CED-0F22-433D-B0B4-BA642C1BA613}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11752CED-0F22-433D-B0B4-BA642C1BA613}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11752CED-0F22-433D-B0B4-BA642C1BA613}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -228,6 +234,7 @@ Global
{2BB7CCD7-73D1-4B16-82EC-A5D0183F8CF5} = {6EE8F484-DFA2-4F0F-939F-400CE78DFAC2}
{90A9DF5F-CBEE-4B6B-8B58-BA94B0BDCF3C} = {6EE8F484-DFA2-4F0F-939F-400CE78DFAC2}
{12821999-9F44-486B-8EE3-38F0EFDFDA32} = {6EE8F484-DFA2-4F0F-939F-400CE78DFAC2}
{11752CED-0F22-433D-B0B4-BA642C1BA613} = {8192FEAD-BCE6-4E62-97E5-2E9EA884BD71}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D6CD99BA-B16B-4570-8910-225CBDFFA3AD}
Expand Down
11 changes: 5 additions & 6 deletions MLS.Agent/CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
using Clockwise;
using Microsoft.AspNetCore.Hosting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.Jupyter;
Expand Down Expand Up @@ -401,7 +398,7 @@ Command Jupyter()
.Trace()
.Handle(delivery));
})
.AddSingleton((Func<IServiceProvider, IKernel>)(c => CreateKernel()))
.AddSingleton(c => CreateKernel())
.AddSingleton(c => new JupyterRequestContextHandler(c.GetRequiredService<IKernel>())
.Trace())
.AddSingleton<IHostedService, Shell>()
Expand Down Expand Up @@ -500,7 +497,7 @@ Command Verify()
}
}

private static CompositeKernel CreateKernel()
private static IKernel CreateKernel()
{
return new CompositeKernel
{
Expand All @@ -511,7 +508,9 @@ private static CompositeKernel CreateKernel()
.UseXplot(),
new FSharpKernel()
.UseDefaultRendering()
}.UseExtendDirective();
}
.UseDefaultMagicCommands()
.UseExtendDirective();
}
}
}
6 changes: 3 additions & 3 deletions Microsoft.DotNet.Interactive.FSharp/FSharpKernel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ open Microsoft.DotNet.Interactive.Commands
open Microsoft.DotNet.Interactive.Events

type FSharpKernel() =
inherit KernelBase()
inherit KernelBase(Name = "fsharp")
let script = new FSharpScript()
do base.AddDisposable(script)
let handleSubmitCode (codeSubmission: SubmitCode) (context: KernelInvocationContext) =
Expand All @@ -24,7 +24,7 @@ type FSharpKernel() =
| ex -> Error(ex), [||]
if errors.Length > 0 then
let aggregateErrorMessage = System.String.Join("\n", errors)
context.OnNext(CodeSubmissionEvaluationFailed(aggregateErrorMessage, codeSubmission))
context.OnNext(CommandFailed(aggregateErrorMessage, codeSubmission))
match result with
| Ok(Some(value)) ->
let value = value.ReflectionValue
Expand All @@ -35,10 +35,10 @@ type FSharpKernel() =
context.OnNext(CodeSubmissionEvaluated(codeSubmission))
context.OnCompleted()
}
override __.Name = "fsharp"
override __.HandleAsync(command: IKernelCommand, _context: KernelInvocationContext): Task =
async {
match command with
| :? SubmitCode as submitCode -> submitCode.Handler <- fun invocationContext -> (handleSubmitCode submitCode invocationContext) |> Async.StartAsTask :> Task
| _ -> ()
} |> Async.StartAsTask :> Task

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task sends_ExecuteReply_message_when_submission_contains_only_a_dir
};

var handler = new ExecuteRequestHandler(kernel);
var request = Message.Create(new ExecuteRequest("#kernel csharp"), null);
var request = Message.Create(new ExecuteRequest("%%csharp"), null);
await handler.Handle(new JupyterRequestContext(_serverChannel, _ioPubChannel, request, _kernelStatus));

_serverRecordingSocket.DecodedMessages
Expand Down
98 changes: 98 additions & 0 deletions Microsoft.DotNet.Interactive.Jupyter.Tests/MagicCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CommandLine;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.Tests;
using Pocket;
using WorkspaceServer.Kernel;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.DotNet.Interactive.Jupyter.Tests
{
public class MagicCommandTests
{
private readonly CompositeDisposable _disposables = new CompositeDisposable();

public MagicCommandTests(ITestOutputHelper output)
{
_disposables.Add(output.SubscribeToPocketLogger());
}

[Fact]
public async Task lsmagic_lists_registered_magic_commands()
{
var kernel = new CompositeKernel()
.UseDefaultMagicCommands()
.LogEventsToPocketLogger();

kernel.AddDirective(new Command("%%one"));
kernel.AddDirective(new Command("%%two"));
kernel.AddDirective(new Command("%%three"));

using var events = kernel.KernelEvents.ToSubscribedList();

await kernel.SendAsync(new SubmitCode("%lsmagic"));

events.Should()
.ContainSingle(e => e is ValueProduced)
.Which
.As<ValueProduced>()
.Value
.As<string>()
.Should()
.ContainAll("%lsmagic", "%%one %%three %%two");
}

[Fact]
public async Task lsmagic_lists_registered_magic_commands_in_subkernels()
{
var subkernel1 = new CSharpKernel();
subkernel1.AddDirective(new Command("%%from-subkernel-1"));
var subkernel2 = new FSharpKernel();
subkernel2.AddDirective(new Command("%%from-subkernel-2"));

var compositeKernel = new CompositeKernel
{
subkernel1,
subkernel2
}
.UseDefaultMagicCommands()
.LogEventsToPocketLogger();

compositeKernel.AddDirective(new Command("%%from-compositekernel"));

using var events = compositeKernel.KernelEvents.ToSubscribedList();

await compositeKernel.SendAsync(new SubmitCode("%lsmagic"));

var valueProduceds = events.OfType<ValueProduced>().ToArray();

valueProduceds[0].Value
.As<string>()
.Should()
.ContainAll("%lsmagic",
"%%csharp",
"%%fsharp",
"%%from-compositekernel");

valueProduceds[1].Value
.As<string>()
.Should()
.ContainAll("%lsmagic",
"%%from-subkernel-1");
valueProduceds[2].Value
.As<string>()
.Should()
.ContainAll("%lsmagic",
"%%from-subkernel-2");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<EmbeddedResource Remove="TestResults\**" />
<None Remove="TestResults\**" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\WorkspaceServer.Tests\%28Recipes%29\LogEvents.cs" Link="%28Recipes%29\LogEvents.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Assent" Version="1.3.1" />
<PackageReference Include="FluentAssertions" Version="5.8.0" />
Expand All @@ -27,9 +31,6 @@
<PackageReference Include="PocketLogger" Version="0.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- <PackageReference Include="PocketLogger.For.MicrosoftExtensionsLogging" Version="0.1.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference> -->
<PackageReference Include="PocketLogger.For.Xunit" Version="0.1.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -41,6 +42,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Jupyter\Microsoft.DotNet.Interactive.Jupyter.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Tests\Microsoft.DotNet.Interactive.Tests.csproj" />
<ProjectReference Include="..\WorkspaceServer\WorkspaceServer.csproj" />
</ItemGroup>
</Project>
Loading