Skip to content

Commit a796ac4

Browse files
committed
add capability to only load unique plugins with the highest version
1 parent 4eeaaec commit a796ac4

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

Flow.Launcher.Core/Plugin/PluginConfig.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.IO;
@@ -45,8 +45,56 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
4545
}
4646
}
4747
}
48-
49-
return allPluginMetadata;
48+
49+
(List<PluginMetadata> uniqueList, List<PluginMetadata> duplicateList) = GetUniqueLatestPluginMetadata(allPluginMetadata);
50+
51+
duplicateList
52+
.ForEach(
53+
x => Log.Warn("PluginConfig",
54+
string.Format("Duplicate plugin name: {0}, id: {1}, version: {2} " +
55+
"not loaded due to version not the highest of the duplicates",
56+
x.Name, x.ID, x.Version),
57+
"GetUniqueLatestPluginMetadata"));
58+
59+
return uniqueList;
60+
}
61+
62+
private static (List<PluginMetadata>, List<PluginMetadata>) GetUniqueLatestPluginMetadata(List<PluginMetadata> allPluginMetadata)
63+
{
64+
var duplicate_list = new List<PluginMetadata>();
65+
var unique_list = new List<PluginMetadata>();
66+
67+
var duplicateGroups = allPluginMetadata.GroupBy(x => x.ID).Where(g => g.Count() > 1).Select(y => y).ToList();
68+
69+
foreach (var metadata in allPluginMetadata)
70+
{
71+
var duplicatesExist = false;
72+
foreach (var group in duplicateGroups)
73+
{
74+
if (metadata.ID == group.Key)
75+
{
76+
duplicatesExist = true;
77+
78+
// If metadata's version greater than each duplicate's version, CompareTo > 0
79+
var count = group.Where(x => metadata.Version.CompareTo(x.Version) > 0).Count();
80+
81+
// Only add if the meatadata's version is the highest of all duplicates in the group
82+
if (count == group.Count() - 1)
83+
{
84+
unique_list.Add(metadata);
85+
}
86+
else
87+
{
88+
duplicate_list.Add(metadata);
89+
}
90+
}
91+
}
92+
93+
if (!duplicatesExist)
94+
unique_list.Add(metadata);
95+
}
96+
97+
return (unique_list, duplicate_list);
5098
}
5199

52100
private static PluginMetadata GetPluginMetadata(string pluginDirectory)

0 commit comments

Comments
 (0)