Skip to content

Commit dda9000

Browse files
authored
Merge pull request #4299 from Flow-Launcher/IsMinimumAppVersionSatisfied
Add MinimumAppVersion support for plugin.json
2 parents 54dc790 + cce80ac commit dda9000

4 files changed

Lines changed: 131 additions & 95 deletions

File tree

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using Flow.Launcher.Plugin;
6-
using Flow.Launcher.Infrastructure;
6+
using Flow.Launcher.Core.Plugin;
77

88
namespace Flow.Launcher.Core.ExternalPlugins
99
{
@@ -41,11 +41,10 @@ public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = fals
4141
return false;
4242

4343
var updatedPluginResults = new List<UserPlugin>();
44-
var appVersion = SemanticVersioning.Version.Parse(Constant.Version);
4544

4645
for (int i = 0; i < results.Count; i++)
4746
{
48-
if (IsMinimumAppVersionSatisfied(results[i], appVersion))
47+
if (PluginManager.IsMinimumAppVersionSatisfied(results[i].Name, results[i].MinimumAppVersion))
4948
updatedPluginResults.Add(results[i]);
5049
}
5150

@@ -72,28 +71,5 @@ public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = fals
7271

7372
return false;
7473
}
75-
76-
private static bool IsMinimumAppVersionSatisfied(UserPlugin plugin, SemanticVersioning.Version appVersion)
77-
{
78-
if (string.IsNullOrEmpty(plugin.MinimumAppVersion))
79-
return true;
80-
81-
try
82-
{
83-
if (appVersion >= SemanticVersioning.Version.Parse(plugin.MinimumAppVersion))
84-
return true;
85-
}
86-
catch (Exception e)
87-
{
88-
PublicApi.Instance.LogException(ClassName, $"Failed to parse the minimum app version {plugin.MinimumAppVersion} for plugin {plugin.Name}. "
89-
+ "Plugin excluded from manifest", e);
90-
return false;
91-
}
92-
93-
PublicApi.Instance.LogInfo(ClassName, $"Plugin {plugin.Name} requires minimum Flow Launcher version {plugin.MinimumAppVersion}, "
94-
+ $"but current version is {Constant.Version}. Plugin excluded from manifest.");
95-
96-
return false;
97-
}
9874
}
9975
}

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 121 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Json;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using System.Windows;
910
using Flow.Launcher.Core.ExternalPlugins;
1011
using Flow.Launcher.Core.Resource;
1112
using Flow.Launcher.Infrastructure;
@@ -813,15 +814,13 @@ private static string GetContainingFolderPathAfterUnzip(string unzippedParentFol
813814
return string.Empty;
814815
}
815816

816-
private static bool SameOrLesserPluginVersionExists(string metadataPath)
817+
private static bool SameOrLesserPluginVersionExists(PluginMetadata metadata)
817818
{
818-
var newMetadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(metadataPath));
819-
820-
if (!Version.TryParse(newMetadata.Version, out var newVersion))
819+
if (!Version.TryParse(metadata.Version, out var newVersion))
821820
return true; // If version is not valid, we assume it is lesser than any existing version
822821

823822
// Get all plugins even if initialization failed so that we can check if the plugin with the same ID exists
824-
return GetAllInitializedPlugins(includeFailed: true).Any(x => x.Metadata.ID == newMetadata.ID
823+
return GetAllInitializedPlugins(includeFailed: true).Any(x => x.Metadata.ID == metadata.ID
825824
&& Version.TryParse(x.Metadata.Version, out var version)
826825
&& newVersion <= version);
827826
}
@@ -881,84 +880,116 @@ internal static bool InstallPlugin(UserPlugin plugin, string zipFilePath, bool c
881880
var tempFolderPluginPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
882881
System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, tempFolderPluginPath);
883882

884-
if(!plugin.IsFromLocalInstallPath)
885-
File.Delete(zipFilePath);
883+
try
884+
{
885+
if (!plugin.IsFromLocalInstallPath)
886+
File.Delete(zipFilePath);
886887

887-
var pluginFolderPath = GetContainingFolderPathAfterUnzip(tempFolderPluginPath);
888+
var pluginFolderPath = GetContainingFolderPathAfterUnzip(tempFolderPluginPath);
888889

889-
var metadataJsonFilePath = string.Empty;
890-
if (File.Exists(Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName)))
891-
metadataJsonFilePath = Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName);
890+
var metadataJsonFilePath = string.Empty;
891+
if (File.Exists(Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName)))
892+
metadataJsonFilePath = Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName);
892893

893-
if (string.IsNullOrEmpty(metadataJsonFilePath) || string.IsNullOrEmpty(pluginFolderPath))
894-
{
895-
PublicApi.Instance.ShowMsgError(Localize.failedToInstallPluginTitle(plugin.Name),
896-
Localize.fileNotFoundMessage(pluginFolderPath));
897-
return false;
898-
}
899-
900-
if (SameOrLesserPluginVersionExists(metadataJsonFilePath))
901-
{
902-
PublicApi.Instance.ShowMsgError(Localize.failedToInstallPluginTitle(plugin.Name),
903-
Localize.pluginExistAlreadyMessage());
904-
return false;
905-
}
894+
if (string.IsNullOrEmpty(metadataJsonFilePath) || string.IsNullOrEmpty(pluginFolderPath))
895+
{
896+
PublicApi.Instance.ShowMsgError(Localize.failedToInstallPluginTitle(plugin.Name),
897+
Localize.fileNotFoundMessage(pluginFolderPath));
898+
return false;
899+
}
906900

907-
var folderName = string.IsNullOrEmpty(plugin.Version) ? $"{plugin.Name}-{Guid.NewGuid()}" : $"{plugin.Name}-{plugin.Version}";
901+
PluginMetadata newMetadata;
902+
try
903+
{
904+
newMetadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(metadataJsonFilePath)) ??
905+
throw new JsonException("Deserialized metadata is null");
906+
}
907+
catch (Exception ex)
908+
{
909+
PublicApi.Instance.ShowMsgError(Localize.failedToInstallPluginTitle(plugin.Name),
910+
Localize.pluginJsonInvalidOrCorrupted());
911+
PublicApi.Instance.LogException(ClassName,
912+
$"Failed to deserialize plugin metadata for plugin {plugin.Name} from file {metadataJsonFilePath}", ex);
913+
return false;
914+
}
908915

909-
var defaultPluginIDs = new List<string>
916+
if (SameOrLesserPluginVersionExists(newMetadata))
910917
{
911-
"0ECADE17459B49F587BF81DC3A125110", // BrowserBookmark
912-
"CEA0FDFC6D3B4085823D60DC76F28855", // Calculator
913-
"572be03c74c642baae319fc283e561a8", // Explorer
914-
"6A122269676E40EB86EB543B945932B9", // PluginIndicator
915-
"9f8f9b14-2518-4907-b211-35ab6290dee7", // PluginsManager
916-
"b64d0a79-329a-48b0-b53f-d658318a1bf6", // ProcessKiller
917-
"791FC278BA414111B8D1886DFE447410", // Program
918-
"D409510CD0D2481F853690A07E6DC426", // Shell
919-
"CEA08895D2544B019B2E9C5009600DF4", // Sys
920-
"0308FD86DE0A4DEE8D62B9B535370992", // URL
921-
"565B73353DBF4806919830B9202EE3BF", // WebSearch
922-
"5043CETYU6A748679OPA02D27D99677A" // WindowsSettings
923-
};
918+
PublicApi.Instance.ShowMsgError(Localize.failedToInstallPluginTitle(plugin.Name),
919+
Localize.pluginExistAlreadyMessage());
920+
return false;
921+
}
924922

925-
// Treat default plugin differently, it needs to be removable along with each flow release
926-
var installDirectory = !defaultPluginIDs.Any(x => x == plugin.ID)
927-
? DataLocation.PluginsDirectory
928-
: Constant.PreinstalledDirectory;
923+
if (!IsMinimumAppVersionSatisfied(newMetadata.Name, newMetadata.MinimumAppVersion))
924+
{
925+
// Ask users if they want to install the plugin that doesn't satisfy the minimum app version requirement
926+
if (PublicApi.Instance.ShowMsgBox(
927+
Localize.pluginMinimumAppVersionUnsatisfiedMessage(newMetadata.Name, Environment.NewLine),
928+
Localize.pluginMinimumAppVersionUnsatisfiedTitle(newMetadata.Name, newMetadata.MinimumAppVersion),
929+
MessageBoxButton.YesNo) == MessageBoxResult.No)
930+
{
931+
return false;
932+
}
933+
}
929934

930-
var newPluginPath = Path.Combine(installDirectory, folderName);
935+
var folderName = string.IsNullOrEmpty(plugin.Version) ? $"{plugin.Name}-{Guid.NewGuid()}" : $"{plugin.Name}-{plugin.Version}";
931936

932-
FilesFolders.CopyAll(pluginFolderPath, newPluginPath, (s) => PublicApi.Instance.ShowMsgBox(s));
937+
var defaultPluginIDs = new List<string>
938+
{
939+
"0ECADE17459B49F587BF81DC3A125110", // BrowserBookmark
940+
"CEA0FDFC6D3B4085823D60DC76F28855", // Calculator
941+
"572be03c74c642baae319fc283e561a8", // Explorer
942+
"6A122269676E40EB86EB543B945932B9", // PluginIndicator
943+
"9f8f9b14-2518-4907-b211-35ab6290dee7", // PluginsManager
944+
"b64d0a79-329a-48b0-b53f-d658318a1bf6", // ProcessKiller
945+
"791FC278BA414111B8D1886DFE447410", // Program
946+
"D409510CD0D2481F853690A07E6DC426", // Shell
947+
"CEA08895D2544B019B2E9C5009600DF4", // Sys
948+
"0308FD86DE0A4DEE8D62B9B535370992", // URL
949+
"565B73353DBF4806919830B9202EE3BF", // WebSearch
950+
"5043CETYU6A748679OPA02D27D99677A" // WindowsSettings
951+
};
952+
953+
// Treat default plugin differently, it needs to be removable along with each flow release
954+
var installDirectory = !defaultPluginIDs.Any(x => x == plugin.ID)
955+
? DataLocation.PluginsDirectory
956+
: Constant.PreinstalledDirectory;
957+
958+
var newPluginPath = Path.Combine(installDirectory, folderName);
959+
960+
FilesFolders.CopyAll(pluginFolderPath, newPluginPath, (s) => PublicApi.Instance.ShowMsgBox(s));
961+
962+
// Check if marker file exists and delete it
963+
try
964+
{
965+
var markerFilePath = Path.Combine(newPluginPath, DataLocation.PluginDeleteFile);
966+
if (File.Exists(markerFilePath))
967+
File.Delete(markerFilePath);
968+
}
969+
catch (Exception e)
970+
{
971+
PublicApi.Instance.LogException(ClassName, $"Failed to delete plugin marker file in {newPluginPath}", e);
972+
}
933973

934-
// Check if marker file exists and delete it
935-
try
936-
{
937-
var markerFilePath = Path.Combine(newPluginPath, DataLocation.PluginDeleteFile);
938-
if (File.Exists(markerFilePath))
939-
File.Delete(markerFilePath);
940-
}
941-
catch (Exception e)
942-
{
943-
PublicApi.Instance.LogException(ClassName, $"Failed to delete plugin marker file in {newPluginPath}", e);
944-
}
974+
if (checkModified)
975+
{
976+
ModifiedPlugins.Add(plugin.ID);
977+
}
945978

946-
try
947-
{
948-
if (Directory.Exists(tempFolderPluginPath))
949-
Directory.Delete(tempFolderPluginPath, true);
950-
}
951-
catch (Exception e)
952-
{
953-
PublicApi.Instance.LogException(ClassName, $"Failed to delete temp folder {tempFolderPluginPath}", e);
979+
return true;
954980
}
955-
956-
if (checkModified)
981+
finally
957982
{
958-
ModifiedPlugins.Add(plugin.ID);
983+
try
984+
{
985+
if (Directory.Exists(tempFolderPluginPath))
986+
Directory.Delete(tempFolderPluginPath, true);
987+
}
988+
catch (Exception e)
989+
{
990+
PublicApi.Instance.LogException(ClassName, $"Failed to delete temp folder {tempFolderPluginPath}", e);
991+
}
959992
}
960-
961-
return true;
962993
}
963994

964995
internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, bool removePluginFromSettings, bool removePluginSettings, bool checkModified)
@@ -1050,6 +1081,27 @@ internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, boo
10501081
return true;
10511082
}
10521083

1084+
internal static bool IsMinimumAppVersionSatisfied(string pluginName, string minimumAppVersion)
1085+
{
1086+
// If the minimum app version is not specified in plugin.json, this plugin is compatible with all app versions
1087+
if (string.IsNullOrEmpty(minimumAppVersion))
1088+
return true;
1089+
1090+
var appVersion = Version.Parse(Constant.Version);
1091+
1092+
if (!Version.TryParse(minimumAppVersion, out var minimumVersion))
1093+
{
1094+
PublicApi.Instance.LogError(ClassName,
1095+
$"Failed to parse the minimum app version {minimumAppVersion} for plugin {pluginName}.");
1096+
return false; // If the minimum app version specified in plugin.json is invalid, we assume it is not satisfied
1097+
}
1098+
1099+
if (appVersion >= minimumVersion)
1100+
return true;
1101+
1102+
return false;
1103+
}
1104+
10531105
#endregion
10541106

10551107
#endregion

Flow.Launcher.Plugin/PluginMetadata.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ internal set
137137
[JsonIgnore]
138138
public int QueryCount { get; set; }
139139

140+
/// <summary>
141+
/// The minimum Flow Launcher version required for this plugin. Default is "".
142+
/// </summary>
143+
public string MinimumAppVersion { get; set; } = string.Empty;
144+
140145
/// <summary>
141146
/// The path to the plugin settings directory which is not validated.
142147
/// It is used to store plugin settings files and data files.

Flow.Launcher/Languages/en.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@
232232
<system:String x:Key="fileNotFoundMessage">Unable to find plugin.json from the extracted zip file, or this path {0} does not exist</system:String>
233233
<system:String x:Key="pluginExistAlreadyMessage">A plugin with the same ID and version already exists, or the version is greater than this downloaded plugin</system:String>
234234
<system:String x:Key="errorCreatingSettingPanel">Error creating setting panel for plugin {0}:{1}{2}</system:String>
235+
<system:String x:Key="pluginMinimumAppVersionUnsatisfiedTitle">{0} requires Flow version {1} to run</system:String>
236+
<system:String x:Key="pluginMinimumAppVersionUnsatisfiedMessage">Flow does not meet the minimum version requirements for {0} to run. Do you want to continue installing it?{1}{1}We recommend updating Flow to the latest version to ensure that {0} works without issues.</system:String>
237+
<system:String x:Key="pluginJsonInvalidOrCorrupted">Failed to install plugin because plugin.json is invalid or corrupted</system:String>
235238

236239
<!-- Setting Plugin Store -->
237240
<system:String x:Key="pluginStore">Plugin Store</system:String>

0 commit comments

Comments
 (0)