Updated plugin command to enable branch switching. Updated Script runner

This commit is contained in:
2024-06-06 15:37:33 +03:00
parent de7c65c27b
commit 23961a48b0
8 changed files with 91 additions and 29 deletions

View File

@@ -17,8 +17,11 @@ namespace DiscordBot.Bot.Actions.Extra;
internal static class PluginMethods internal static class PluginMethods
{ {
internal static async Task List(PluginManager manager) internal static async Task List(PluginManager manager)
{ {
Console.WriteLine($"Fetching plugin list from branch {manager.Branch} ...");
var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetPluginsList(), "Reading remote database"); var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetPluginsList(), "Reading remote database");
TableData tableData = new(["Name", "Description", "Version", "Is Installed"]); TableData tableData = new(["Name", "Description", "Version", "Is Installed"]);
@@ -73,10 +76,17 @@ internal static class PluginMethods
} }
); );
if (!pluginData.HasDependencies) if (!pluginData.HasFileDependencies)
{ {
await manager.AppendPluginToDatabase(new PluginInfo(pluginName, pluginData.Version, [])); if (pluginData.HasScriptDependencies)
{
Console.WriteLine("Executing post install scripts ...");
await manager.ExecutePluginInstallScripts(pluginData.ScriptDependencies);
}
PluginInfo pluginInfo = new(pluginName, pluginData.Version, new List<string>());
Console.WriteLine("Finished installing " + pluginName + " successfully"); Console.WriteLine("Finished installing " + pluginName + " successfully");
await manager.AppendPluginToDatabase(pluginInfo);
await RefreshPlugins(false); await RefreshPlugins(false);
return; return;
} }
@@ -128,6 +138,13 @@ internal static class PluginMethods
} }
); );
if(pluginData.HasScriptDependencies)
{
Console.WriteLine("Executing post install scripts ...");
await manager.ExecutePluginInstallScripts(pluginData.ScriptDependencies);
}
await manager.AppendPluginToDatabase(new PluginInfo(pluginName, pluginData.Version, pluginData.Dependencies.Select(sep => sep.DownloadLocation).ToList())); await manager.AppendPluginToDatabase(new PluginInfo(pluginName, pluginData.Version, pluginData.Dependencies.Select(sep => sep.DownloadLocation).ToList()));
await RefreshPlugins(false); await RefreshPlugins(false);
} }

View File

@@ -23,7 +23,8 @@ public class Plugin: ICommandAction
new InternalActionOption("load", "Loads all plugins"), new InternalActionOption("load", "Loads all plugins"),
new InternalActionOption("install", "Installs a plugin"), new InternalActionOption("install", "Installs a plugin"),
new InternalActionOption("refresh", "Refreshes the plugin list"), new InternalActionOption("refresh", "Refreshes the plugin list"),
new InternalActionOption("uninstall", "Uninstalls a plugin") new InternalActionOption("uninstall", "Uninstalls a plugin"),
new InternalActionOption("branch", "Sets a plugin option")
}; };
public InternalActionRunType RunType => InternalActionRunType.ON_CALL; public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
@@ -44,6 +45,38 @@ public class Plugin: ICommandAction
switch (args[0]) switch (args[0])
{ {
case "branch":
if (args.Length < 2)
{
Console.WriteLine("Usage : plugin branch <option> <value>");
return;
}
string option = args[1];
switch (option)
{
case "set":
{
if (args.Length < 3)
{
Console.WriteLine("Usage : plugin branch set <value>");
return;
}
string value = string.Join(' ', args, 2, args.Length - 2);
Application.CurrentApplication.PluginManager.Branch = value;
Console.WriteLine($"Branch set to {value}");
}
break;
case "get":
Console.WriteLine($"Branch is set to {Application.CurrentApplication.PluginManager.Branch}");
break;
default:
Console.WriteLine("Invalid option");
break;
}
break;
case "refresh": case "refresh":
await PluginMethods.RefreshPlugins(true); await PluginMethods.RefreshPlugins(true);
break; break;

View File

@@ -34,7 +34,10 @@ internal class Loader
return; return;
} }
var files = Directory.GetFiles(_SearchPath, $"*.{_FileExtension}", SearchOption.TopDirectoryOnly); var installedPlugins = await Application.CurrentApplication.PluginManager.GetInstalledPlugins();
var files = installedPlugins.Select(plugin => plugin.FilePath).ToArray();
//var files = Directory.GetFiles(_SearchPath, $"*.{_FileExtension}", SearchOption.TopDirectoryOnly);
foreach (var file in files) foreach (var file in files)
{ {
try try

View File

@@ -17,8 +17,8 @@ public class PluginManager
private static readonly string _DefaultPluginsLink = "PluginsList.json"; private static readonly string _DefaultPluginsLink = "PluginsList.json";
public string Branch { get; init; } public string Branch { get; set; }
public string BaseUrl { get; init; } public string BaseUrl { get; set; }
private string PluginsLink => $"{BaseUrl}/{Branch}/{_DefaultPluginsLink}"; private string PluginsLink => $"{BaseUrl}/{Branch}/{_DefaultPluginsLink}";
@@ -71,6 +71,13 @@ public class PluginManager
await JsonManager.SaveToJsonFile(Application.CurrentApplication.PluginDatabase,installedPlugins); await JsonManager.SaveToJsonFile(Application.CurrentApplication.PluginDatabase,installedPlugins);
} }
public async Task ExecutePluginInstallScripts(List<OnlineScriptDependencyInfo> listOfDependencies)
{
string consoleType = OperatingSystem.IsWindows() ? "cmd.exe" : "bash";
foreach(var script in listOfDependencies)
await ServerCom.RunConsoleCommand(consoleType, "/c " + script.ScriptContent);
}
public async Task AppendPluginToDatabase(PluginInfo pluginData) public async Task AppendPluginToDatabase(PluginInfo pluginData)
{ {
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase)); List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
@@ -148,7 +155,8 @@ public class PluginManager
{ {
installProgress?.Report(0f); installProgress?.Report(0f);
int totalSteps = pluginData.HasDependencies ? pluginData.Dependencies.Count + pluginData.ScriptDependencies.Count + 1: 1; int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1: 1;
totalSteps += pluginData.HasScriptDependencies ? pluginData.ScriptDependencies.Count : 0;
float stepProgress = 1f / totalSteps; float stepProgress = 1f / totalSteps;
@@ -160,16 +168,18 @@ public class PluginManager
await ServerCom.DownloadFileAsync(pluginData.DownLoadLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginData.Name}.dll", progress); await ServerCom.DownloadFileAsync(pluginData.DownLoadLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginData.Name}.dll", progress);
if (pluginData.HasFileDependencies)
foreach (var dependency in pluginData.Dependencies) foreach (var dependency in pluginData.Dependencies)
{ {
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress); await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress);
currentProgress += stepProgress; currentProgress += stepProgress;
} }
foreach(var scriptDependency in pluginData.ScriptDependencies) if (pluginData.HasScriptDependencies)
foreach (var scriptDependency in pluginData.ScriptDependencies)
{ {
string console = OperatingSystem.IsWindows() ? "cmd" : "bash"; string console = OperatingSystem.IsWindows() ? "start cmd.exe" : "bash";
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent; string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
await ServerCom.RunConsoleCommand(console, arguments); await ServerCom.RunConsoleCommand(console, arguments);
@@ -179,7 +189,8 @@ public class PluginManager
PluginInfo pluginInfo = new PluginInfo( PluginInfo pluginInfo = new PluginInfo(
pluginData.Name, pluginData.Name,
pluginData.Version, pluginData.Version,
pluginData.Dependencies.Select(dep => dep.DownloadLocation).ToList() pluginData.Dependencies.Select(dep => dep.DownloadLocation)
.ToList()
); );
await AppendPluginToDatabase(pluginInfo); await AppendPluginToDatabase(pluginInfo);

View File

@@ -76,14 +76,13 @@ public static class ServerCom
return DownloadFileAsync(URl, location, progress, null); return DownloadFileAsync(URl, location, progress, null);
} }
public static async Task<string> RunConsoleCommand(string console, string command) public static async Task RunConsoleCommand(string console, string command)
{ {
Process process = new(); Process process = new();
process.StartInfo.FileName = console; process.StartInfo.FileName = console;
process.StartInfo.Arguments = command; process.StartInfo.Arguments = command;
process.Start(); process.Start();
await process.WaitForExitAsync(); await process.WaitForExitAsync();
return await process.StandardOutput.ReadToEndAsync();
} }

View File

@@ -20,7 +20,7 @@ public class JsonManager
var str = new MemoryStream(); var str = new MemoryStream();
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions
{ {
WriteIndented = true WriteIndented = true,
} }
); );
await File.WriteAllBytesAsync(file, str.ToArray()); await File.WriteAllBytesAsync(file, str.ToArray());

View File

@@ -15,7 +15,8 @@ public class PluginOnlineInfo
public List<OnlineDependencyInfo> Dependencies { get; private set; } public List<OnlineDependencyInfo> Dependencies { get; private set; }
public List<OnlineScriptDependencyInfo> ScriptDependencies { get; private set; } public List<OnlineScriptDependencyInfo> ScriptDependencies { get; private set; }
public OSType SupportedOS { get; private set; } public OSType SupportedOS { get; private set; }
public bool HasDependencies { get; init; } public bool HasFileDependencies => Dependencies is not null && Dependencies.Count > 0;
public bool HasScriptDependencies => ScriptDependencies is not null && ScriptDependencies.Count > 0;
[JsonConstructor] [JsonConstructor]
public PluginOnlineInfo(string name, PluginVersion version, string description, string downLoadLink, OSType supportedOS, List<OnlineDependencyInfo> dependencies, List<OnlineScriptDependencyInfo> scriptDependencies) public PluginOnlineInfo(string name, PluginVersion version, string description, string downLoadLink, OSType supportedOS, List<OnlineDependencyInfo> dependencies, List<OnlineScriptDependencyInfo> scriptDependencies)
@@ -26,7 +27,6 @@ public class PluginOnlineInfo
DownLoadLink = downLoadLink; DownLoadLink = downLoadLink;
SupportedOS = supportedOS; SupportedOS = supportedOS;
Dependencies = dependencies; Dependencies = dependencies;
HasDependencies = dependencies.Count > 0;
ScriptDependencies = scriptDependencies; ScriptDependencies = scriptDependencies;
} }
@@ -39,7 +39,6 @@ public class PluginOnlineInfo
SupportedOS = supportedOS; SupportedOS = supportedOS;
Dependencies = new List<OnlineDependencyInfo>(); Dependencies = new List<OnlineDependencyInfo>();
ScriptDependencies = new List<OnlineScriptDependencyInfo>(); ScriptDependencies = new List<OnlineScriptDependencyInfo>();
HasDependencies = false;
} }
public static async Task<PluginOnlineInfo> FromRawData(string jsonText) public static async Task<PluginOnlineInfo> FromRawData(string jsonText)

View File

@@ -32,7 +32,7 @@ namespace DiscordBotUI_Windows.WindowsForms
{ {
bool isInstalled = await DiscordBotCore.Application.CurrentApplication.PluginManager.IsPluginInstalled(plugin.Name); bool isInstalled = await DiscordBotCore.Application.CurrentApplication.PluginManager.IsPluginInstalled(plugin.Name);
string isInstalledMessage = isInstalled ? "Installed" : "Not Installed"; string isInstalledMessage = isInstalled ? "Installed" : "Not Installed";
int rowIndex = dataGridView1.Rows.Add(plugin.Name, plugin.Description, plugin.HasDependencies, plugin.Version, isInstalledMessage); int rowIndex = dataGridView1.Rows.Add(plugin.Name, plugin.Description, plugin.HasFileDependencies, plugin.Version, isInstalledMessage);
dataGridView1.Rows[rowIndex].Cells["Install"] = new DataGridViewButtonCell() dataGridView1.Rows[rowIndex].Cells["Install"] = new DataGridViewButtonCell()
{ {
Value = isInstalled ? "Remove" : "Install", Value = isInstalled ? "Remove" : "Install",