From 126153bf20b258babca833fb35e0f22a7d10f15f Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Mon, 24 Feb 2025 13:35:17 +0800
Subject: [PATCH] Improve plugin settings directory clean & Support plugin
cache directory clean
---
Flow.Launcher.Core/Plugin/PluginManager.cs | 61 +++++++++-------------
Flow.Launcher/Languages/en.xaml | 2 +
Flow.Launcher/PublicAPIInstance.cs | 5 +-
3 files changed, 29 insertions(+), 39 deletions(-)
diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs
index 7f73bf1cf..9462df740 100644
--- a/Flow.Launcher.Core/Plugin/PluginManager.cs
+++ b/Flow.Launcher.Core/Plugin/PluginManager.cs
@@ -546,50 +546,41 @@ namespace Flow.Launcher.Core.Plugin
if (removePluginSettings)
{
- if (AllowedLanguage.IsDotNet(plugin.Language)) // for the plugin in .NET, we can use assembly loader
+ // For dotnet plugins, we need to remove their PluginJsonStorage instance
+ if (AllowedLanguage.IsDotNet(plugin.Language))
{
- // if user want to remove the plugin settings, we cannot call save method for the plugin json storage instance of this plugin
- // so we need to remove it from the api instance
var method = API.GetType().GetMethod("RemovePluginSettings");
- var pluginJsonStorage = method?.Invoke(API, new object[] { plugin.AssemblyName });
-
- // if there exists a json storage for current plugin, we need to delete the directory path
- if (pluginJsonStorage != null)
- {
- var deleteMethod = pluginJsonStorage.GetType().GetMethod("DeleteDirectory");
- try
- {
- deleteMethod?.Invoke(pluginJsonStorage, null);
- }
- catch (Exception e)
- {
- Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin json folder for {plugin.Name}", e);
- API.ShowMsg(API.GetTranslation("failedToRemovePluginSettingsTitle"),
- string.Format(API.GetTranslation("failedToRemovePluginSettingsMessage"), plugin.Name));
- }
- }
+ method?.Invoke(API, new object[] { plugin.AssemblyName });
}
- else // the plugin with json prc interface
+
+ try
{
- var pluginPair = AllPlugins.FirstOrDefault(p => p.Metadata.ID == plugin.ID);
- if (pluginPair != null && pluginPair.Plugin is JsonRPCPlugin jsonRpcPlugin)
- {
- try
- {
- jsonRpcPlugin.DeletePluginSettingsDirectory();
- }
- catch (Exception e)
- {
- Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin json folder for {plugin.Name}", e);
- API.ShowMsg(API.GetTranslation("failedToRemovePluginSettingsTitle"),
- string.Format(API.GetTranslation("failedToRemovePluginSettingsMessage"), plugin.Name));
- }
- }
+ var pluginSettingsDirectory = plugin.PluginSettingsDirectoryPath;
+ if (Directory.Exists(pluginSettingsDirectory))
+ Directory.Delete(pluginSettingsDirectory, true);
+ }
+ catch (Exception e)
+ {
+ Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin settings folder for {plugin.Name}", e);
+ API.ShowMsg(API.GetTranslation("failedToRemovePluginSettingsTitle"),
+ string.Format(API.GetTranslation("failedToRemovePluginSettingsMessage"), plugin.Name));
}
}
if (removePluginFromSettings)
{
+ try
+ {
+ var pluginCacheDirectory = plugin.PluginCacheDirectoryPath;
+ if (Directory.Exists(pluginCacheDirectory))
+ Directory.Delete(pluginCacheDirectory, true);
+ }
+ catch (Exception e)
+ {
+ Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin cache folder for {plugin.Name}", e);
+ API.ShowMsg(API.GetTranslation("failedToRemovePluginCacheTitle"),
+ string.Format(API.GetTranslation("failedToRemovePluginCacheMessage"), plugin.Name));
+ }
Settings.Plugins.Remove(plugin.ID);
AllPlugins.RemoveAll(p => p.Metadata.ID == plugin.ID);
}
diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml
index c66772c83..cbef73b84 100644
--- a/Flow.Launcher/Languages/en.xaml
+++ b/Flow.Launcher/Languages/en.xaml
@@ -131,6 +131,8 @@
Uninstall
Fail to remove plugin settings
Plugins: {0} - Fail to remove plugin settings files, please remove them manually
+ Fail to remove plugin cache
+ Plugins: {0} - Fail to remove plugin cache files, please remove them manually
Plugin Store
diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs
index e5bc74958..cfbe0e951 100644
--- a/Flow.Launcher/PublicAPIInstance.cs
+++ b/Flow.Launcher/PublicAPIInstance.cs
@@ -189,7 +189,7 @@ namespace Flow.Launcher
private readonly ConcurrentDictionary _pluginJsonStorages = new();
- public object RemovePluginSettings(string assemblyName)
+ public void RemovePluginSettings(string assemblyName)
{
foreach (var keyValuePair in _pluginJsonStorages)
{
@@ -199,11 +199,8 @@ namespace Flow.Launcher
if (name == assemblyName)
{
_pluginJsonStorages.Remove(key, out var pluginJsonStorage);
- return pluginJsonStorage;
}
}
-
- return null;
}
///