-
-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Plugin Metadata & Path Management #3272
Open
Jack251970
wants to merge
31
commits into
Flow-Launcher:dev
Choose a base branch
from
Jack251970:plugin_settings_cache_path
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
89bca3b
Add plugin cache path
Jack251970 5919418
Add assembly name & plugin settings path & plugin cache path in meta …
Jack251970 f1b5e68
Remove reflection codes for deleting csharp plugin settings
Jack251970 1aaba46
Use constants & data location for code quality
Jack251970 9cd3011
Add documents for plugin metadata
Jack251970 8f7ad27
Remove useless usings
Jack251970 d07b304
Improve code quality
Jack251970 b50db58
Add assembly name & plugin settings path & plugin cache path in meta …
Jack251970 6012111
Add log directory & version log directory & themes directory in data …
Jack251970 3efe550
Use context plugin settings path
Jack251970 126153b
Improve plugin settings directory clean & Support plugin cache direct…
Jack251970 3106b02
Support plugin directory update & validate
Jack251970 012ef49
Fix log directory fetch issue
Jack251970 58de625
Do not validate plugin settings & cache path
Jack251970 47adfd1
Improve code quality
Jack251970 a0c2a42
Let Program plugin use plugin cache path
Jack251970 a29ed64
Use metadata for plugin settings directory
Jack251970 65ae342
Add documents for Flow.Launcher.Plugin
Jack251970 6622815
Fix typos
Jack251970 fe86e23
Add exception handles
Jack251970 f8d0981
Update json rpc plugin directory before loading plugins
Jack251970 7975ab5
Merge branch 'dev' into plugin_settings_cache_path
Jack251970 ce3a3e9
Fix plugin settings delete issue
Jack251970 486cc6a
Fix async task issue
Jack251970 af3b391
Fix dispose
Jack251970 c690e59
Merge branch 'dev' into plugin_settings_cache_path
Jack251970 b0b1a26
Fix build issue & Cleanup codes
Jack251970 e3af882
Merge branch 'dev' into plugin_settings_cache_path
Jack251970 c87b731
Merge branch 'dev' into plugin_settings_cache_path
Jack251970 ee0b039
Merge update plugin directory functions
Jack251970 5f976b9
Remove useless assignment
Jack251970 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ public static class PluginManager | |
|
||
private static PluginsSettings Settings; | ||
private static List<PluginMetadata> _metadatas; | ||
private static List<string> _modifiedPlugins = new List<string>(); | ||
private static List<string> _modifiedPlugins = new(); | ||
|
||
/// <summary> | ||
/// Directories that will hold Flow Launcher plugin directory | ||
|
@@ -72,15 +72,20 @@ public static async ValueTask DisposePluginsAsync() | |
{ | ||
foreach (var pluginPair in AllPlugins) | ||
{ | ||
switch (pluginPair.Plugin) | ||
{ | ||
case IDisposable disposable: | ||
disposable.Dispose(); | ||
break; | ||
case IAsyncDisposable asyncDisposable: | ||
await asyncDisposable.DisposeAsync(); | ||
break; | ||
} | ||
await DisposePluginAsync(pluginPair); | ||
} | ||
} | ||
|
||
private static async Task DisposePluginAsync(PluginPair pluginPair) | ||
{ | ||
switch (pluginPair.Plugin) | ||
{ | ||
case IDisposable disposable: | ||
disposable.Dispose(); | ||
break; | ||
case IAsyncDisposable asyncDisposable: | ||
await asyncDisposable.DisposeAsync(); | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -154,7 +159,35 @@ public static void LoadPlugins(PluginsSettings settings) | |
_metadatas = PluginConfig.Parse(Directories); | ||
Settings = settings; | ||
Settings.UpdatePluginSettings(_metadatas); | ||
// Update Json RPC plugin directory before loading plugins so that we can pass the correct plugin directory | ||
UpdateJsonRPCPluginDirectory(_metadatas); | ||
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings); | ||
// Update dotnet plugin directory after loading plugins because we need to get assembly name first | ||
UpdateNotNetPluginDirectory(_metadatas); | ||
} | ||
|
||
private static void UpdateJsonRPCPluginDirectory(List<PluginMetadata> metadatas) | ||
{ | ||
foreach (var metadata in metadatas) | ||
{ | ||
if (!AllowedLanguage.IsDotNet(metadata.Language)) | ||
{ | ||
metadata.PluginSettingsDirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, metadata.Name); | ||
metadata.PluginCacheDirectoryPath = Path.Combine(DataLocation.PluginCacheDirectory, metadata.Name); | ||
} | ||
} | ||
} | ||
|
||
private static void UpdateNotNetPluginDirectory(List<PluginMetadata> metadatas) | ||
{ | ||
foreach (var metadata in metadatas) | ||
{ | ||
if (AllowedLanguage.IsDotNet(metadata.Language)) | ||
{ | ||
metadata.PluginSettingsDirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, metadata.AssemblyName); | ||
metadata.PluginCacheDirectoryPath = Path.Combine(DataLocation.PluginCacheDirectory, metadata.AssemblyName); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -228,11 +261,9 @@ public static ICollection<PluginPair> ValidPluginsForQuery(Query query) | |
if (query is null) | ||
return Array.Empty<PluginPair>(); | ||
|
||
if (!NonGlobalPlugins.ContainsKey(query.ActionKeyword)) | ||
if (!NonGlobalPlugins.TryGetValue(query.ActionKeyword, out var plugin)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code quality improvement |
||
return GlobalPlugins; | ||
|
||
|
||
var plugin = NonGlobalPlugins[query.ActionKeyword]; | ||
return new List<PluginPair> | ||
{ | ||
plugin | ||
|
@@ -539,63 +570,62 @@ internal static void InstallPlugin(UserPlugin plugin, string zipFilePath, bool c | |
} | ||
} | ||
|
||
internal static void UninstallPlugin(PluginMetadata plugin, bool removePluginFromSettings, bool removePluginSettings, bool checkModified) | ||
internal static async void UninstallPlugin(PluginMetadata plugin, bool removePluginFromSettings, bool removePluginSettings, bool checkModified) | ||
{ | ||
if (checkModified && PluginModified(plugin.ID)) | ||
{ | ||
throw new ArgumentException($"Plugin {plugin.Name} has been modified"); | ||
} | ||
|
||
if (removePluginSettings) | ||
if (removePluginFromSettings) | ||
{ | ||
if (AllowedLanguage.IsDotNet(plugin.Language)) // for the plugin in .NET, we can use assembly loader | ||
// If we want to remove plugin from AllPlugins, | ||
// we need to dispose them so that they can release file handles | ||
// which can help FL to delete the plugin settings & cache folders successfully | ||
var pluginPairs = AllPlugins.FindAll(p => p.Metadata.ID == plugin.ID); | ||
foreach (var pluginPair in pluginPairs) | ||
{ | ||
var assemblyLoader = new PluginAssemblyLoader(plugin.ExecuteFilePath); | ||
var assembly = assemblyLoader.LoadAssemblyAndDependencies(); | ||
jjw24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var assemblyName = assembly.GetName().Name; | ||
await DisposePluginAsync(pluginPair); | ||
} | ||
} | ||
|
||
// 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 | ||
if (removePluginSettings) | ||
{ | ||
// For dotnet plugins, we need to remove their PluginJsonStorage instance | ||
if (AllowedLanguage.IsDotNet(plugin.Language)) | ||
{ | ||
var method = API.GetType().GetMethod("RemovePluginSettings"); | ||
var pluginJsonStorage = method?.Invoke(API, new object[] { assemblyName }); | ||
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)); | ||
} | ||
} | ||
try | ||
{ | ||
var pluginSettingsDirectory = plugin.PluginSettingsDirectoryPath; | ||
if (Directory.Exists(pluginSettingsDirectory)) | ||
Directory.Delete(pluginSettingsDirectory, true); | ||
} | ||
else // the plugin with json prc interface | ||
catch (Exception e) | ||
{ | ||
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)); | ||
} | ||
} | ||
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); | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can merge these two function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Since only JsonRPC v2 & dotnet plugins both use InitAsync to get this path, we do not need to update directory before their constuctors