Skip to content

Commit

Permalink
move extension loading to tool project, improve extension.dib loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Jan 27, 2023
1 parent 9df30fa commit ae066a2
Show file tree
Hide file tree
Showing 20 changed files with 289 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Microsoft.DotNet.Interactive
public System.Collections.Generic.IEnumerator<Kernel> GetEnumerator()
Kernel GetHandlingKernel(Microsoft.DotNet.Interactive.Commands.KernelCommand command, KernelInvocationContext context)
System.Threading.Tasks.Task HandleRequestKernelInfoAsync(Microsoft.DotNet.Interactive.Commands.RequestKernelInfo command, KernelInvocationContext context)
public System.Threading.Tasks.Task LoadExtensionsFromDirectoryAsync(System.IO.DirectoryInfo directory, KernelInvocationContext context)
public System.Void SetDefaultTargetKernelNameForCommand(System.Type commandType, System.String kernelName)
protected System.Void SetHandlingKernel(Microsoft.DotNet.Interactive.Commands.KernelCommand command, KernelInvocationContext context)
public class DataDictionaryConverter : JsonConverter<System.Collections.Generic.IDictionary<System.String,System.Object>>
Expand Down Expand Up @@ -202,12 +201,10 @@ Microsoft.DotNet.Interactive
public class KernelExtensionLoadException : System.Exception, System.Runtime.Serialization.ISerializable
.ctor(System.Exception innerException)
public static class KernelExtensions
public static Kernel FindKernel(System.String name)
public static Kernel FindKernel(System.Func<Kernel,System.Boolean> predicate)
public static Kernel FindKernelByName(System.String name)
public static System.Collections.Generic.IEnumerable<Kernel> FindKernels(System.Func<Kernel,System.Boolean> predicate)
public static T LogCommandsToPocketLogger<T>()
public static T LogEventsToPocketLogger<T>()
public static System.Threading.Tasks.Task LoadAndRunInteractiveDocument(System.IO.FileInfo file)
public static System.Collections.Generic.IEnumerable<Kernel> Subkernels(System.Boolean recursive = False)
public static System.Collections.Generic.IEnumerable<Kernel> SubkernelsAndSelf(System.Boolean recursive = False)
public static System.Threading.Tasks.Task<KernelCommandResult> SubmitCodeAsync(System.String code)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive.CSharp/CSharpKernel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<IsPackable>true</IsPackable>
<PackageDescription Condition="'$(PackageDescription)'==''">Notebooks support for .NET Interactive</PackageDescription>
<PackageDescription Condition="'$(PackageDescription)'==''">Provides APIs for reading and writing .ipynb and other file formats for .NET Interactive</PackageDescription>
<PackageTags Condition="'$(PackageTags)'==''">polyglot notebook dotnet interactive</PackageTags>
<RootNamespace>Microsoft.DotNet.Interactive.Documents</RootNamespace>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive.Http/FileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class FileProvider : IFileProvider, IDisposable
{
private readonly EmbeddedFileProvider _root;
private readonly IDisposable _eventSubscription;
private readonly ConcurrentDictionary<string, EmbeddedFileProvider> _providers = new ConcurrentDictionary<string, EmbeddedFileProvider>();
private readonly ConcurrentDictionary<string, EmbeddedFileProvider> _providers = new();

public FileProvider(Kernel kernel, Assembly rootProviderAssembly)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.PowerShell;
using Microsoft.DotNet.Interactive.Tests;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Pocket;
using Xunit.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ public async Task When_restore_fails_then_an_error_is_displayed(Language languag
.Which
.Message
.Should()
.Contain($"error NU1101:", nonexistentPackageName);
.Contain("error NU1101:", nonexistentPackageName);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Dispose()
}
}

protected CompositeKernel CreateCompositeKernel(Language defaultKernelLanguage = Language.CSharp,
protected virtual CompositeKernel CreateCompositeKernel(Language defaultKernelLanguage = Language.CSharp,
bool openTestingNamespaces = false)
{
return CreateCompositeKernel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class KernelExtensionTestHelper

return await CreateExtensionNupkg(
projectDir,
$"await kernel.SendAsync(new SubmitCode(\"\\\"SimpleExtension\\\"\"));",
"await kernel.SendAsync(new SubmitCode(\"\\\"SimpleExtension\\\"\"));",
packageName,
packageVersion,
timeout: TimeSpan.FromMinutes(5));
Expand All @@ -43,7 +43,7 @@ public static class KernelExtensionTestHelper

return await CreateExtensionNupkg(
projectDir,
$"await kernel.SendAsync(new SubmitCode(\"\\\"FileProviderExtension\\\"\"));",
"await kernel.SendAsync(new SubmitCode(\"\\\"FileProviderExtension\\\"\"));",
packageName,
packageVersion,
fileToEmbed: fileToEmbed,
Expand All @@ -58,6 +58,10 @@ public static class KernelExtensionTestHelper

var extensionScriptPath = new FileInfo(Path.Combine(projectDir.FullName, "extension.dib"));
var extensionScriptContent = @"
#!markdown
# This is an extension!
#!csharp
""ScriptExtension""
";
Expand Down
51 changes: 51 additions & 0 deletions src/Microsoft.DotNet.Interactive.Tests/Utility/KernelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// 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.Diagnostics;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Pocket;

namespace Microsoft.DotNet.Interactive.Tests.Utility;

Expand Down Expand Up @@ -40,4 +43,52 @@ public static class KernelExtensions

return (false, default);
}


[DebuggerStepThrough]
public static T LogCommandsToPocketLogger<T>(this T kernel)
where T : Kernel
{
kernel.AddMiddleware(async (command, context, next) =>
{
using var _ = Logger.Log.OnEnterAndExit($"Command: {command.ToString().Replace(Environment.NewLine, " ")}");

await next(command, context);
});
return kernel;
}

[DebuggerStepThrough]
public static T LogEventsToPocketLogger<T>(this T kernel)
where T : Kernel
{
var disposables = new CompositeDisposable();

disposables.Add(
kernel.KernelEvents
.Subscribe(
e =>
{
Logger.Log.Info("{kernel}: {event}",
kernel.Name,
e);
}));

kernel.VisitSubkernels(k =>
{
disposables.Add(
k.KernelEvents.Subscribe(
e =>
{
Logger.Log.Info("{kernel}: {event}",
k.Name,
e);
}));
});

kernel.RegisterForDisposal(disposables);

return kernel;
}

}
47 changes: 0 additions & 47 deletions src/Microsoft.DotNet.Interactive/CompositeKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public sealed class CompositeKernel :
{
private readonly ConcurrentQueue<PackageAdded> _packagesToCheckForExtensions = new();
private readonly KernelCollection _childKernels;
private readonly PackageDirectoryExtensionLoader _extensionLoader = new();
private string _defaultKernelName;
private Command _connectDirective;
private KernelHost _host;
Expand All @@ -36,16 +35,8 @@ public sealed class CompositeKernel :
public CompositeKernel(string name = null) : base(name ?? ".NET")
{
_childKernels = new(this);

ListenForPackagesToScanForExtensions();
}

private void ListenForPackagesToScanForExtensions() =>
RegisterForDisposal(KernelEvents
.OfType<PackageAdded>()
.Where(pa => pa?.PackageReference.PackageRoot is not null)
.Distinct(pa => pa.PackageReference.PackageRoot)
.Subscribe(added => _packagesToCheckForExtensions.Enqueue(added)));

public string DefaultKernelName
{
Expand Down Expand Up @@ -76,7 +67,6 @@ public void Add(Kernel kernel, IEnumerable<string> aliases = null)
kernel.ParentKernel = this;
kernel.RootKernel = RootKernel;

kernel.AddMiddleware(LoadExtensions);
kernel.SetScheduler(Scheduler);

if (aliases is not null)
Expand Down Expand Up @@ -122,33 +112,6 @@ private void AddChooseKernelDirective(Kernel kernel)
AddDirective(chooseKernelCommand);
}

private async Task LoadExtensions(
KernelCommand command,
KernelInvocationContext context,
KernelPipelineContinuation next)
{
await next(command, context);

while (_packagesToCheckForExtensions.TryDequeue(out var packageAdded))
{
var packageRootDir = packageAdded.PackageReference.PackageRoot;

var extensionDir =
new DirectoryInfo
(Path.Combine(
packageRootDir,
"interactive-extensions",
"dotnet"));

if (extensionDir.Exists)
{
await LoadExtensionsFromDirectoryAsync(
extensionDir,
context);
}
}
}

public KernelCollection ChildKernels => _childKernels;

protected override void SetHandlingKernel(KernelCommand command, KernelInvocationContext context)
Expand Down Expand Up @@ -302,16 +265,6 @@ bool IsDirectiveDefinedIn(Parser parser) =>

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public async Task LoadExtensionsFromDirectoryAsync(
DirectoryInfo directory,
KernelInvocationContext context)
{
await _extensionLoader.LoadFromDirectoryAsync(
directory,
this,
context);
}

public void AddKernelConnector(ConnectKernelCommand connectionCommand)
{
if (_connectDirective is null)
Expand Down
8 changes: 6 additions & 2 deletions src/Microsoft.DotNet.Interactive/DisplayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ public static class DisplayExtensions
/// <param name="value">The value to display.</param>
/// <param name="mimeTypes">The MIME types.</param>
/// <returns>An instance of <see cref="DisplayedValue"/> that can be used to later update the display.</returns>
public static DisplayedValue Display(this object value,
public static DisplayedValue Display(
this object value,
params string[] mimeTypes)
{
return KernelInvocationContext.Current.Display(value, mimeTypes);
}

public static DisplayedValue DisplayAs(this string value, string mimeType, params string[] additionalMimeTypes)
public static DisplayedValue DisplayAs(
this string value,
string mimeType,
params string[] additionalMimeTypes)
{
return KernelInvocationContext.Current.DisplayAs(value, mimeType, additionalMimeTypes);
}
Expand Down
15 changes: 0 additions & 15 deletions src/Microsoft.DotNet.Interactive/IKernelExtensionLoader.cs

This file was deleted.

Loading

0 comments on commit ae066a2

Please sign in to comment.