Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move extension loading to tool project, improve extension.dib loading #2656

Merged
merged 3 commits into from
Jan 30, 2023
Merged
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
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,8 +8,8 @@

<PropertyGroup>
<IsPackable>true</IsPackable>
<PackageDescription Condition="'$(PackageDescription)'==''">Notebooks support for .NET Interactive</PackageDescription>
<PackageTags Condition="'$(PackageTags)'==''">polyglot notebook dotnet interactive</PackageTags>
<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 ipynb jupyter</PackageTags>
<RootNamespace>Microsoft.DotNet.Interactive.Documents</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
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 @@ -18,6 +18,7 @@

<ItemGroup>
<Compile Include="..\dotnet-interactive\(Recipes)\TaskExtensions.cs" />
<Compile Include="..\dotnet-interactive\Utility\KernelDiagnostics.cs" Link="KernelDiagnostics.cs" />
</ItemGroup>

<ItemGroup>
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 @@ -21,6 +21,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\dotnet-interactive\Utility\KernelDiagnostics.cs" Link="Utility\KernelDiagnostics.cs" />
<Compile Include="..\Microsoft.DotNet.Interactive.CSharpProject\%28Recipes%29\AsyncLazy{T}.cs" Link="Utility\AsyncLazy{T}.cs" />
<Compile Include="..\Microsoft.DotNet.Interactive.Jupyter\(Recipes)\BareObjectConverter.cs" />
<Compile Include="..\Microsoft.DotNet.Interactive.Jupyter\(Recipes)\JsonSerializationExtensions.cs" />
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
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