-
Notifications
You must be signed in to change notification settings - Fork 368
/
AssemblyBasedExtensionLoader.cs
147 lines (125 loc) · 4.91 KB
/
AssemblyBasedExtensionLoader.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Events;
using Pocket;
using static Pocket.Logger<Microsoft.DotNet.Interactive.Extensions.AssemblyBasedExtensionLoader>;
namespace Microsoft.DotNet.Interactive.Extensions
{
public class AssemblyBasedExtensionLoader : IKernelExtensionLoader
{
private readonly HashSet<AssemblyName> _loadedAssemblies = new HashSet<AssemblyName>();
private readonly object _lock = new object();
public async Task LoadFromDirectoryAsync(
DirectoryInfo directory,
Kernel kernel,
KernelInvocationContext context)
{
if (directory == null)
{
throw new ArgumentNullException(nameof(directory));
}
if (kernel == null)
{
throw new ArgumentNullException(nameof(kernel));
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (!directory.Exists)
{
throw new ArgumentException($"Directory {directory.FullName} doesn't exist", nameof(directory));
}
await LoadFromAssembliesInDirectory(
directory,
kernel,
context);
}
public async Task LoadFromAssembliesInDirectory(
DirectoryInfo directory,
Kernel kernel,
KernelInvocationContext context)
{
if (directory.Exists)
{
var extensionDlls = directory
.GetFiles("*.dll", SearchOption.TopDirectoryOnly)
.ToList();
if (extensionDlls.Count > 0)
{
using var op = new ConfirmationLogger(
Log.Category,
message: "Loading extensions in directory {directory}",
logOnStart: true,
args: new object[] { directory });
foreach (var extensionDll in extensionDlls)
{
await LoadFromAssembly(
extensionDll,
kernel,
context);
}
op.Succeed();
}
}
}
private async Task LoadFromAssembly(
FileInfo assemblyFile,
Kernel kernel,
KernelInvocationContext context)
{
if (assemblyFile == null)
{
throw new ArgumentNullException(nameof(assemblyFile));
}
if (kernel == null)
{
throw new ArgumentNullException(nameof(kernel));
}
if (!assemblyFile.Exists)
{
throw new ArgumentException($"File {assemblyFile.FullName} doesn't exist", nameof(assemblyFile));
}
bool loadExtensions;
lock (_lock)
{
loadExtensions = _loadedAssemblies.Add(AssemblyName.GetAssemblyName(assemblyFile.FullName));
}
if (loadExtensions)
{
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFile.FullName);
var extensionTypes = assembly
.ExportedTypes
.Where(t => t.CanBeInstantiated() && typeof(IKernelExtension).IsAssignableFrom(t))
.ToArray();
if (extensionTypes.Any())
{
context.Display($"Loading extensions from `{assemblyFile.Name}`", "text/markdown");
}
foreach (var extensionType in extensionTypes)
{
var extension = (IKernelExtension) Activator.CreateInstance(extensionType);
try
{
await extension.OnLoadAsync(kernel);
context.Publish(new KernelExtensionLoaded(extension, context.Command));
}
catch (Exception e)
{
context.Publish(new ErrorProduced(
$"Failed to load kernel extension \"{extensionType.Name}\" from assembly {assemblyFile.FullName}",
context.Command));
context.Fail(new KernelExtensionLoadException(e));
}
}
}
}
}
}