From ce6c2cb1b2053071c240cd689c0d9d93a8dd8ee1 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 1 Jul 2025 09:05:21 +0800 Subject: [PATCH] Add return value for api functions --- Flow.Launcher.Core/Plugin/PluginInstaller.cs | 15 +++++++++++--- Flow.Launcher.Core/Plugin/PluginManager.cs | 16 ++++++++------- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 17 +++++++++++----- Flow.Launcher/PublicAPIInstance.cs | 6 +++--- .../PluginsManager.cs | 20 +++++++++++++------ 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/PluginInstaller.cs b/Flow.Launcher.Core/Plugin/PluginInstaller.cs index e39c24430..e69dd58ab 100644 --- a/Flow.Launcher.Core/Plugin/PluginInstaller.cs +++ b/Flow.Launcher.Core/Plugin/PluginInstaller.cs @@ -74,7 +74,10 @@ public static class PluginInstaller throw new FileNotFoundException($"Plugin {newPlugin.ID} zip file not found at {filePath}", filePath); } - API.InstallPlugin(newPlugin, filePath); + if (!API.InstallPlugin(newPlugin, filePath)) + { + return; + } if (!newPlugin.IsFromLocalInstallPath) { @@ -167,7 +170,10 @@ public static class PluginInstaller try { - await API.UninstallPluginAsync(oldPlugin, removePluginSettings); + if (!await API.UninstallPluginAsync(oldPlugin, removePluginSettings)) + { + return; + } } catch (Exception e) { @@ -223,7 +229,10 @@ public static class PluginInstaller return; } - await API.UpdatePluginAsync(oldPlugin, newPlugin, filePath); + if (!await API.UpdatePluginAsync(oldPlugin, newPlugin, filePath)) + { + return; + } } catch (Exception e) { diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index 97a1987a9..db02486f1 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -540,22 +540,24 @@ namespace Flow.Launcher.Core.Plugin return _modifiedPlugins.Contains(id); } - public static async Task UpdatePluginAsync(PluginMetadata existingVersion, UserPlugin newVersion, string zipFilePath) + public static async Task UpdatePluginAsync(PluginMetadata existingVersion, UserPlugin newVersion, string zipFilePath) { var installSuccess = InstallPlugin(newVersion, zipFilePath, checkModified:false); - if (!installSuccess) return; - await UninstallPluginAsync(existingVersion, removePluginFromSettings:false, removePluginSettings:false, checkModified: false); + if (!installSuccess) return false; + var uninstallSuccess = await UninstallPluginAsync(existingVersion, removePluginFromSettings:false, removePluginSettings:false, checkModified: false); + if (!uninstallSuccess) return false; _modifiedPlugins.Add(existingVersion.ID); + return true; } - public static void InstallPlugin(UserPlugin plugin, string zipFilePath) + public static bool InstallPlugin(UserPlugin plugin, string zipFilePath) { - InstallPlugin(plugin, zipFilePath, checkModified: true); + return InstallPlugin(plugin, zipFilePath, checkModified: true); } - public static async Task UninstallPluginAsync(PluginMetadata plugin, bool removePluginFromSettings = true, bool removePluginSettings = false) + public static async Task UninstallPluginAsync(PluginMetadata plugin, bool removePluginFromSettings = true, bool removePluginSettings = false) { - await UninstallPluginAsync(plugin, removePluginFromSettings, removePluginSettings, true); + return await UninstallPluginAsync(plugin, removePluginFromSettings, removePluginSettings, true); } #endregion diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index f47ee5e11..dfa7c9e97 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -547,8 +547,10 @@ namespace Flow.Launcher.Plugin /// /// Path to the zip file containing the plugin. It will be unzipped to the temporary directory, removed and installed. /// - /// - public Task UpdatePluginAsync(PluginMetadata pluginMetadata, UserPlugin plugin, string zipFilePath); + /// + /// True if the plugin is updated successfully, false otherwise. + /// + public Task UpdatePluginAsync(PluginMetadata pluginMetadata, UserPlugin plugin, string zipFilePath); /// /// Install a plugin. By default will remove the zip file if installation is from url, @@ -558,7 +560,10 @@ namespace Flow.Launcher.Plugin /// /// Path to the zip file containing the plugin. It will be unzipped to the temporary directory, removed and installed. /// - public void InstallPlugin(UserPlugin plugin, string zipFilePath); + /// + /// True if the plugin is installed successfully, false otherwise. + /// + public bool InstallPlugin(UserPlugin plugin, string zipFilePath); /// /// Uninstall a plugin @@ -567,8 +572,10 @@ namespace Flow.Launcher.Plugin /// /// Plugin has their own settings. If this is set to true, the plugin settings will be removed. /// - /// - public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false); + /// + /// True if the plugin is updated successfully, false otherwise. + /// + public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false); /// /// Log debug message of the time taken to execute a method diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 6e82032ff..43952ffba 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -566,13 +566,13 @@ namespace Flow.Launcher public bool PluginModified(string id) => PluginManager.PluginModified(id); - public Task UpdatePluginAsync(PluginMetadata pluginMetadata, UserPlugin plugin, string zipFilePath) => + public Task UpdatePluginAsync(PluginMetadata pluginMetadata, UserPlugin plugin, string zipFilePath) => PluginManager.UpdatePluginAsync(pluginMetadata, plugin, zipFilePath); - public void InstallPlugin(UserPlugin plugin, string zipFilePath) => + public bool InstallPlugin(UserPlugin plugin, string zipFilePath) => PluginManager.InstallPlugin(plugin, zipFilePath); - public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false) => + public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false) => PluginManager.UninstallPluginAsync(pluginMetadata, removePluginSettings); public long StopwatchLogDebug(string className, string message, Action action, [CallerMemberName] string methodName = "") => diff --git a/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs b/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs index c1d3a81a2..9eded239d 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs +++ b/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs @@ -352,8 +352,11 @@ namespace Flow.Launcher.Plugin.PluginsManager } else { - await Context.API.UpdatePluginAsync(x.PluginExistingMetadata, x.PluginNewUserPlugin, - downloadToFilePath); + if (!await Context.API.UpdatePluginAsync(x.PluginExistingMetadata, x.PluginNewUserPlugin, + downloadToFilePath)) + { + return; + } if (Settings.AutoRestartAfterChanging) { @@ -456,8 +459,9 @@ namespace Flow.Launcher.Plugin.PluginsManager if (cts.IsCancellationRequested) return; else - await Context.API.UpdatePluginAsync(plugin.PluginExistingMetadata, plugin.PluginNewUserPlugin, - downloadToFilePath); + if (!await Context.API.UpdatePluginAsync(plugin.PluginExistingMetadata, plugin.PluginNewUserPlugin, + downloadToFilePath)) + return; } catch (Exception ex) { @@ -686,7 +690,8 @@ namespace Flow.Launcher.Plugin.PluginsManager try { - Context.API.InstallPlugin(plugin, downloadedFilePath); + if (!Context.API.InstallPlugin(plugin, downloadedFilePath)) + return; if (!plugin.IsFromLocalInstallPath) File.Delete(downloadedFilePath); @@ -779,7 +784,10 @@ namespace Flow.Launcher.Plugin.PluginsManager Context.API.GetTranslation("plugin_pluginsmanager_keep_plugin_settings_subtitle"), Context.API.GetTranslation("plugin_pluginsmanager_keep_plugin_settings_title"), button: MessageBoxButton.YesNo) == MessageBoxResult.No; - await Context.API.UninstallPluginAsync(plugin, removePluginSettings); + if (!await Context.API.UninstallPluginAsync(plugin, removePluginSettings)) + { + return; + } } catch (ArgumentException e) {