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 async Task List(PluginManager manager)
{
Console.WriteLine($"Fetching plugin list from branch {manager.Branch} ...");
var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetPluginsList(), "Reading remote database");
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");
await manager.AppendPluginToDatabase(pluginInfo);
await RefreshPlugins(false);
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 RefreshPlugins(false);
}

View File

@@ -23,7 +23,8 @@ public class Plugin: ICommandAction
new InternalActionOption("load", "Loads all plugins"),
new InternalActionOption("install", "Installs a plugin"),
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;
@@ -44,6 +45,38 @@ public class Plugin: ICommandAction
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":
await PluginMethods.RefreshPlugins(true);
break;

View File

@@ -34,7 +34,10 @@ internal class Loader
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)
{
try

View File

@@ -17,8 +17,8 @@ public class PluginManager
private static readonly string _DefaultPluginsLink = "PluginsList.json";
public string Branch { get; init; }
public string BaseUrl { get; init; }
public string Branch { get; set; }
public string BaseUrl { get; set; }
private string PluginsLink => $"{BaseUrl}/{Branch}/{_DefaultPluginsLink}";
@@ -71,6 +71,13 @@ public class PluginManager
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)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
@@ -148,7 +155,8 @@ public class PluginManager
{
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;
@@ -160,26 +168,29 @@ public class PluginManager
await ServerCom.DownloadFileAsync(pluginData.DownLoadLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginData.Name}.dll", progress);
foreach (var dependency in pluginData.Dependencies)
{
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress);
currentProgress += stepProgress;
}
if (pluginData.HasFileDependencies)
foreach (var dependency in pluginData.Dependencies)
{
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress);
currentProgress += stepProgress;
}
foreach(var scriptDependency in pluginData.ScriptDependencies)
{
if (pluginData.HasScriptDependencies)
foreach (var scriptDependency in pluginData.ScriptDependencies)
{
string console = OperatingSystem.IsWindows() ? "cmd" : "bash";
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
string console = OperatingSystem.IsWindows() ? "start cmd.exe" : "bash";
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
await ServerCom.RunConsoleCommand(console, arguments);
currentProgress += stepProgress;
}
await ServerCom.RunConsoleCommand(console, arguments);
currentProgress += stepProgress;
}
PluginInfo pluginInfo = new PluginInfo(
pluginData.Name,
pluginData.Version,
pluginData.Dependencies.Select(dep => dep.DownloadLocation).ToList()
pluginData.Dependencies.Select(dep => dep.DownloadLocation)
.ToList()
);
await AppendPluginToDatabase(pluginInfo);

View File

@@ -76,14 +76,13 @@ public static class ServerCom
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.StartInfo.FileName = console;
process.StartInfo.Arguments = command;
process.Start();
await process.WaitForExitAsync();
return await process.StandardOutput.ReadToEndAsync();
}

View File

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

View File

@@ -15,7 +15,8 @@ public class PluginOnlineInfo
public List<OnlineDependencyInfo> Dependencies { get; private set; }
public List<OnlineScriptDependencyInfo> ScriptDependencies { 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]
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;
SupportedOS = supportedOS;
Dependencies = dependencies;
HasDependencies = dependencies.Count > 0;
ScriptDependencies = scriptDependencies;
}
@@ -39,7 +39,6 @@ public class PluginOnlineInfo
SupportedOS = supportedOS;
Dependencies = new List<OnlineDependencyInfo>();
ScriptDependencies = new List<OnlineScriptDependencyInfo>();
HasDependencies = false;
}
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);
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()
{
Value = isInstalled ? "Remove" : "Install",