Add support for deleting plugin settings when uninstalling plugins

This commit is contained in:
Jack251970 2025-02-01 13:10:32 +08:00
parent 6cf7674cac
commit e320ca1d49
5 changed files with 34 additions and 11 deletions

View file

@ -14,6 +14,7 @@ using ISavable = Flow.Launcher.Plugin.ISavable;
using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.Plugin.SharedCommands;
using System.Text.Json; using System.Text.Json;
using Flow.Launcher.Core.Resource; using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.Storage;
namespace Flow.Launcher.Core.Plugin namespace Flow.Launcher.Core.Plugin
{ {
@ -439,7 +440,7 @@ namespace Flow.Launcher.Core.Plugin
public static void UpdatePlugin(PluginMetadata existingVersion, UserPlugin newVersion, string zipFilePath) public static void UpdatePlugin(PluginMetadata existingVersion, UserPlugin newVersion, string zipFilePath)
{ {
InstallPlugin(newVersion, zipFilePath, checkModified:false); InstallPlugin(newVersion, zipFilePath, checkModified:false);
UninstallPlugin(existingVersion, removeSettings:false, checkModified:false); UninstallPlugin(existingVersion, removeSettings:false, removePluginSettings:false, checkModified: false);
_modifiedPlugins.Add(existingVersion.ID); _modifiedPlugins.Add(existingVersion.ID);
} }
@ -454,9 +455,9 @@ namespace Flow.Launcher.Core.Plugin
/// <summary> /// <summary>
/// Uninstall a plugin. /// Uninstall a plugin.
/// </summary> /// </summary>
public static void UninstallPlugin(PluginMetadata plugin, bool removeSettings = true) public static void UninstallPlugin(PluginMetadata plugin, bool removeSettings = true, bool removePluginSettings = false)
{ {
UninstallPlugin(plugin, removeSettings, true); UninstallPlugin(plugin, removeSettings, removePluginSettings, true);
} }
#endregion #endregion
@ -529,7 +530,7 @@ namespace Flow.Launcher.Core.Plugin
} }
} }
internal static void UninstallPlugin(PluginMetadata plugin, bool removeSettings, bool checkModified) internal static void UninstallPlugin(PluginMetadata plugin, bool removeSettings, bool removePluginSettings, bool checkModified)
{ {
if (checkModified && PluginModified(plugin.ID)) if (checkModified && PluginModified(plugin.ID))
{ {
@ -542,6 +543,18 @@ namespace Flow.Launcher.Core.Plugin
AllPlugins.RemoveAll(p => p.Metadata.ID == plugin.ID); AllPlugins.RemoveAll(p => p.Metadata.ID == plugin.ID);
} }
if (removePluginSettings)
{
var assemblyLoader = new PluginAssemblyLoader(plugin.ExecuteFilePath);
var assembly = assemblyLoader.LoadAssemblyAndDependencies();
var assemblyName = assembly.GetName().Name;
var directoryPath = Path.Combine(DataLocation.DataDirectory(), JsonStorage<object>.DirectoryName, Constant.Plugins, assemblyName);
if (Directory.Exists(directoryPath))
{
Directory.Delete(directoryPath, true);
}
}
// Marked for deletion. Will be deleted on next start up // Marked for deletion. Will be deleted on next start up
using var _ = File.CreateText(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")); using var _ = File.CreateText(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt"));

View file

@ -15,6 +15,8 @@
<system:String x:Key="plugin_pluginsmanager_installing_plugin">Installing Plugin</system:String> <system:String x:Key="plugin_pluginsmanager_installing_plugin">Installing Plugin</system:String>
<system:String x:Key="plugin_pluginsmanager_install_from_web">Download and install {0}</system:String> <system:String x:Key="plugin_pluginsmanager_install_from_web">Download and install {0}</system:String>
<system:String x:Key="plugin_pluginsmanager_uninstall_title">Plugin Uninstall</system:String> <system:String x:Key="plugin_pluginsmanager_uninstall_title">Plugin Uninstall</system:String>
<system:String x:Key="plugin_pluginsmanager_keep_plugin_settings_title">Keep plugin settings</system:String>
<system:String x:Key="plugin_pluginsmanager_keep_plugin_settings_subtitle">Do you want to keep the settings of the plugin for the next usage?</system:String>
<system:String x:Key="plugin_pluginsmanager_install_success_restart">Plugin {0} successfully installed. Restarting Flow, please wait...</system:String> <system:String x:Key="plugin_pluginsmanager_install_success_restart">Plugin {0} successfully installed. Restarting Flow, please wait...</system:String>
<system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">Unable to find the plugin.json metadata file from the extracted zip file.</system:String> <system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">Unable to find the plugin.json metadata file from the extracted zip file.</system:String>
<system:String x:Key="plugin_pluginsmanager_install_error_duplicate">Error: A plugin which has the same or greater version with {0} already exists.</system:String> <system:String x:Key="plugin_pluginsmanager_install_error_duplicate">Error: A plugin which has the same or greater version with {0} already exists.</system:String>

View file

@ -13,6 +13,8 @@
<system:String x:Key="plugin_pluginsmanager_installing_plugin">正在安装插件</system:String> <system:String x:Key="plugin_pluginsmanager_installing_plugin">正在安装插件</system:String>
<system:String x:Key="plugin_pluginsmanager_install_from_web">下载与安装 {0}</system:String> <system:String x:Key="plugin_pluginsmanager_install_from_web">下载与安装 {0}</system:String>
<system:String x:Key="plugin_pluginsmanager_uninstall_title">插件卸载</system:String> <system:String x:Key="plugin_pluginsmanager_uninstall_title">插件卸载</system:String>
<system:String x:Key="plugin_pluginsmanager_keep_plugin_settings_title">保留插件设置</system:String>
<system:String x:Key="plugin_pluginsmanager_keep_plugin_settings_subtitle">你希望保留插件设置以便于下次使用吗?</system:String>
<system:String x:Key="plugin_pluginsmanager_install_success_restart">插件安装成功。正在重新启动 Flow Launcher请稍候...</system:String> <system:String x:Key="plugin_pluginsmanager_install_success_restart">插件安装成功。正在重新启动 Flow Launcher请稍候...</system:String>
<system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">安装失败无法从新插件中找到plugin.json元数据文件</system:String> <system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">安装失败无法从新插件中找到plugin.json元数据文件</system:String>
<system:String x:Key="plugin_pluginsmanager_install_error_duplicate">错误:具有相同或更高版本的 {0} 的插件已经存在。</system:String> <system:String x:Key="plugin_pluginsmanager_install_error_duplicate">错误:具有相同或更高版本的 {0} 的插件已经存在。</system:String>

View file

@ -13,6 +13,8 @@
<system:String x:Key="plugin_pluginsmanager_installing_plugin">Installing Plugin</system:String> <system:String x:Key="plugin_pluginsmanager_installing_plugin">Installing Plugin</system:String>
<system:String x:Key="plugin_pluginsmanager_install_from_web">下載並安裝 {0}</system:String> <system:String x:Key="plugin_pluginsmanager_install_from_web">下載並安裝 {0}</system:String>
<system:String x:Key="plugin_pluginsmanager_uninstall_title">解除安裝擴充功能</system:String> <system:String x:Key="plugin_pluginsmanager_uninstall_title">解除安裝擴充功能</system:String>
<system:String x:Key="plugin_pluginsmanager_keep_plugin_settings_title">保留插件設置</system:String>
<system:String x:Key="plugin_pluginsmanager_keep_plugin_settings_subtitle">你希望保留插件設置以便於下次使用嗎?</system:String>
<system:String x:Key="plugin_pluginsmanager_install_success_restart">外掛安裝成功。正在重啟 Flow請稍後...</system:String> <system:String x:Key="plugin_pluginsmanager_install_success_restart">外掛安裝成功。正在重啟 Flow請稍後...</system:String>
<system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">Unable to find the plugin.json metadata file from the extracted zip file.</system:String> <system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">Unable to find the plugin.json metadata file from the extracted zip file.</system:String>
<system:String x:Key="plugin_pluginsmanager_install_error_duplicate">Error: A plugin which has the same or greater version with {0} already exists.</system:String> <system:String x:Key="plugin_pluginsmanager_install_error_duplicate">Error: A plugin which has the same or greater version with {0} already exists.</system:String>

View file

@ -682,7 +682,11 @@ namespace Flow.Launcher.Plugin.PluginsManager
{ {
try try
{ {
PluginManager.UninstallPlugin(plugin, removeSettings: true); var removePluginSettings = Context.API.ShowMsgBox(
Context.API.GetTranslation("plugin_pluginsmanager_keep_plugin_settings_subtitle"),
Context.API.GetTranslation("plugin_pluginsmanager_keep_plugin_settings_title"),
button: MessageBoxButton.YesNo) == MessageBoxResult.No;
PluginManager.UninstallPlugin(plugin, removeSettings: true, removePluginSettings: removePluginSettings);
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {