-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
129bd50
commit 7fbff8c
Showing
24 changed files
with
188 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,3 +287,4 @@ __pycache__/ | |
/docs/_build | ||
*.bin | ||
/src/WebStarter/data/conversations | ||
XMLs |
8 changes: 8 additions & 0 deletions
8
src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginDef.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BotSharp.Abstraction.Plugins.Models; | ||
|
||
public class PluginDef | ||
{ | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
public string Assembly { get; set; } | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace BotSharp.Abstraction.Plugins; | ||
|
||
public class PluginLoaderSettings | ||
public class PluginSettings | ||
{ | ||
public string[] Assemblies { get; set; } = new string[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
src/Infrastructure/BotSharp.Core/Infrastructures/PluginLoader.cs
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using BotSharp.Abstraction.Plugins.Models; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Xml; | ||
|
||
namespace BotSharp.Core.Plugins; | ||
|
||
public class PluginLoader | ||
{ | ||
private readonly IServiceCollection _services; | ||
private readonly IConfiguration _config; | ||
private readonly PluginSettings _settings; | ||
private static List<IBotSharpPlugin> _modules = new List<IBotSharpPlugin>(); | ||
private static List<PluginDef> _plugins = new List<PluginDef>(); | ||
private static string _executingDir; | ||
|
||
public PluginLoader(IServiceCollection services, | ||
IConfiguration config, | ||
PluginSettings settings) | ||
{ | ||
_services = services; | ||
_config = config; | ||
_settings = settings; | ||
} | ||
|
||
public void Load(Action<Assembly> loaded) | ||
{ | ||
_executingDir = Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName; | ||
|
||
_settings.Assemblies.ToList().ForEach(assemblyName => | ||
{ | ||
var assemblyPath = Path.Combine(_executingDir, assemblyName + ".dll"); | ||
if (File.Exists(assemblyPath)) | ||
{ | ||
var assembly = Assembly.Load(assemblyName); | ||
var modules = assembly.GetTypes() | ||
.Where(x => x.GetInterface(nameof(IBotSharpPlugin)) != null) | ||
.Select(x => Activator.CreateInstance(x) as IBotSharpPlugin) | ||
.ToList(); | ||
foreach (var module in modules) | ||
{ | ||
module.RegisterDI(_services, _config); | ||
string classSummary = GetSummaryComment(module.GetType()); | ||
_modules.Add(module); | ||
_plugins.Add(new PluginDef | ||
{ | ||
Name = module.GetType().Name, | ||
Description = classSummary, | ||
Assembly = assemblyName | ||
}); | ||
Console.Write($"Loaded plugin "); | ||
Console.Write(module.GetType().Name, Color.Green); | ||
Console.WriteLine($" from {assemblyName}."); | ||
if (!string.IsNullOrEmpty(classSummary)) | ||
{ | ||
Console.WriteLine(classSummary); | ||
} | ||
} | ||
loaded(assembly); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Can't find assemble {assemblyPath}."); | ||
} | ||
}); | ||
} | ||
|
||
public List<PluginDef> GetPlugins() | ||
{ | ||
return _plugins; | ||
} | ||
|
||
public string GetSummaryComment(Type member) | ||
{ | ||
string summary = string.Empty; | ||
XmlDocument xmlDoc = new XmlDocument(); | ||
|
||
// Load the XML documentation file | ||
var xmlFile = Path.Combine(_executingDir, $"{member.Module.Assembly.FullName.Split(',')[0]}.xml"); | ||
if (!File.Exists(xmlFile)) | ||
{ | ||
return ""; | ||
} | ||
xmlDoc.Load(xmlFile); // Replace with your actual XML documentation file name | ||
|
||
// Construct the XPath query to find the summary comment | ||
string memberName = $"T:{member.FullName}"; | ||
string xpath = $"/doc/members/member[@name='{memberName}']/summary"; | ||
|
||
// Find the summary comment using the XPath query | ||
XmlNode summaryNode = xmlDoc.SelectSingleNode(xpath); | ||
|
||
if (summaryNode != null) | ||
{ | ||
summary = summaryNode.InnerXml.Trim(); | ||
} | ||
|
||
return summary; | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
if (_modules.Count == 0) | ||
{ | ||
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow); | ||
} | ||
|
||
_modules.ForEach(module => | ||
{ | ||
if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null) | ||
{ | ||
(module as IBotSharpAppPlugin).Configure(app); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using BotSharp.Abstraction.Plugins.Models; | ||
using BotSharp.Core.Plugins; | ||
|
||
namespace BotSharp.OpenAPI.Controllers; | ||
|
||
[Authorize] | ||
[ApiController] | ||
public class PluginController : ControllerBase | ||
{ | ||
private readonly IServiceProvider _services; | ||
private readonly PluginSettings _settings; | ||
|
||
public PluginController(IServiceProvider services, PluginSettings settings) | ||
{ | ||
_services = services; | ||
_settings = settings; | ||
} | ||
|
||
[HttpGet("/plugins")] | ||
public List<PluginDef> GetPlugins() | ||
{ | ||
var loader = _services.GetRequiredService<PluginLoader>(); | ||
return loader.GetPlugins(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.