Skip to content

Commit 54a0f3d

Browse files
committed
Add support for delete plugin directory for non-dotnet plugins
1 parent 37837e7 commit 54a0f3d

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ internal abstract class JsonRPCPluginBase : IAsyncPlugin, IContextMenu, ISetting
4444
private string SettingConfigurationPath =>
4545
Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "SettingsTemplate.yaml");
4646

47-
private string SettingPath => Path.Combine(DataLocation.PluginSettingsDirectory,
48-
Context.CurrentPluginMetadata.Name, "Settings.json");
47+
private string SettingDirectory => Path.Combine(DataLocation.PluginSettingsDirectory,
48+
Context.CurrentPluginMetadata.Name);
49+
50+
private string SettingPath => Path.Combine(SettingDirectory, "Settings.json");
4951

5052
public abstract List<Result> LoadContextMenus(Result selectedResult);
5153

@@ -159,5 +161,13 @@ public Control CreateSettingPanel()
159161
{
160162
return Settings.CreateSettingPanel();
161163
}
164+
165+
public void DeletePluginSettingsDirectory()
166+
{
167+
if (Directory.Exists(SettingDirectory))
168+
{
169+
Directory.Delete(SettingDirectory, true);
170+
}
171+
}
162172
}
163173
}

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -544,38 +544,56 @@ internal static void UninstallPlugin(PluginMetadata plugin, bool removePluginFro
544544
throw new ArgumentException($"Plugin {plugin.Name} has been modified");
545545
}
546546

547-
if (removePluginFromSettings)
548-
{
549-
Settings.Plugins.Remove(plugin.ID);
550-
AllPlugins.RemoveAll(p => p.Metadata.ID == plugin.ID);
551-
}
552-
553547
if (removePluginSettings)
554548
{
555-
var assemblyLoader = new PluginAssemblyLoader(plugin.ExecuteFilePath);
556-
var assembly = assemblyLoader.LoadAssemblyAndDependencies();
557-
var assemblyName = assembly.GetName().Name;
549+
if (AllowedLanguage.IsDotNet(plugin.Language)) // for the plugin in .NET, we can use assembly loader
550+
{
551+
var assemblyLoader = new PluginAssemblyLoader(plugin.ExecuteFilePath);
552+
var assembly = assemblyLoader.LoadAssemblyAndDependencies();
553+
var assemblyName = assembly.GetName().Name;
558554

559-
// if user want to remove the plugin settings, we cannot call save method for the plugin json storage instance of this plugin
560-
// so we need to remove it from the api instance
561-
var method = API.GetType().GetMethod("RemovePluginSettings");
562-
var pluginJsonStorage = method?.Invoke(API, new object[] { assemblyName });
555+
// if user want to remove the plugin settings, we cannot call save method for the plugin json storage instance of this plugin
556+
// so we need to remove it from the api instance
557+
var method = API.GetType().GetMethod("RemovePluginSettings");
558+
var pluginJsonStorage = method?.Invoke(API, new object[] { assemblyName });
563559

564-
// if there exists a json storage for current plugin, we need to delete the directory path
565-
if (pluginJsonStorage != null)
566-
{
567-
var deleteMethod = pluginJsonStorage.GetType().GetMethod("DeleteDirectory");
568-
try
560+
// if there exists a json storage for current plugin, we need to delete the directory path
561+
if (pluginJsonStorage != null)
569562
{
570-
deleteMethod?.Invoke(pluginJsonStorage, null);
563+
var deleteMethod = pluginJsonStorage.GetType().GetMethod("DeleteDirectory");
564+
try
565+
{
566+
deleteMethod?.Invoke(pluginJsonStorage, null);
567+
}
568+
catch (Exception e)
569+
{
570+
Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin json folder for {plugin.Name}", e);
571+
}
571572
}
572-
catch (Exception e)
573+
}
574+
else // the plugin with json prc interface
575+
{
576+
var pluginPair = AllPlugins.FirstOrDefault(p => p.Metadata.ID == plugin.ID);
577+
if (pluginPair != null && pluginPair.Plugin is JsonRPCPlugin jsonRpcPlugin)
573578
{
574-
Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin json folder for {assemblyName}", e);
579+
try
580+
{
581+
jsonRpcPlugin.DeletePluginSettingsDirectory();
582+
}
583+
catch (Exception e)
584+
{
585+
Log.Exception($"|PluginManager.UninstallPlugin|Failed to delete plugin json folder for {plugin.Name}", e);
586+
}
575587
}
576588
}
577589
}
578590

591+
if (removePluginFromSettings)
592+
{
593+
Settings.Plugins.Remove(plugin.ID);
594+
AllPlugins.RemoveAll(p => p.Metadata.ID == plugin.ID);
595+
}
596+
579597
// Marked for deletion. Will be deleted on next start up
580598
using var _ = File.CreateText(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt"));
581599

0 commit comments

Comments
 (0)