-
Notifications
You must be signed in to change notification settings - Fork 368
/
ScriptBasedExtensionLoader.cs
67 lines (57 loc) · 2.25 KB
/
ScriptBasedExtensionLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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.IO;
using System.Text;
using System.Threading.Tasks;
using Pocket;
using static Pocket.Logger<Microsoft.DotNet.Interactive.Extensions.ScriptBasedExtensionLoader>;
namespace Microsoft.DotNet.Interactive.Extensions
{
public class ScriptBasedExtensionLoader : IKernelExtensionLoader
{
private const string ExtensionScriptName = "extension.dib";
public async Task LoadFromDirectoryAsync(DirectoryInfo directory, Kernel kernel, KernelInvocationContext context)
{
if (directory is null)
{
throw new ArgumentNullException(nameof(directory));
}
if (kernel is null)
{
throw new ArgumentNullException(nameof(kernel));
}
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (!directory.Exists)
{
throw new ArgumentException($"Directory {directory.FullName} doesn't exist", nameof(directory));
}
await LoadScriptExtensionFromDirectory(
directory,
kernel,
context);
}
private async Task LoadScriptExtensionFromDirectory(
DirectoryInfo directory,
Kernel kernel,
KernelInvocationContext context)
{
var extensionFile = new FileInfo(Path.Combine(directory.FullName, ExtensionScriptName));
if (extensionFile.Exists)
{
var logMessage = $"Loading extension script from `{extensionFile.FullName}`";
using var op = new ConfirmationLogger(
Log.Category,
message: logMessage,
logOnStart: true,
args: new object[] { extensionFile });
context.DisplayAs(logMessage, "text/markdown");
var scriptContents = File.ReadAllText(extensionFile.FullName, Encoding.UTF8);
await kernel.SubmitCodeAsync(scriptContents);
}
}
}
}