Move PluginsManifest to Flow.Launcher.Core & Change PluginStore Binding to PluginsManifest

This commit is contained in:
Kevin Zhang 2021-10-10 16:46:54 -05:00
parent ccb565e70a
commit 3d06404614
8 changed files with 117 additions and 85 deletions

View file

@ -0,0 +1,57 @@
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Flow.Launcher.Core.ExternalPlugins
{
public static class PluginsManifest
{
static PluginsManifest()
{
UpdateTask = UpdateManifestAsync();
}
public static List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();
public static Task UpdateTask { get; private set; }
private static readonly SemaphoreSlim manifestUpdateLock = new(1);
public static Task UpdateManifestAsync()
{
if (manifestUpdateLock.CurrentCount == 0)
{
return UpdateTask;
}
return UpdateTask = DownloadManifestAsync();
}
private async static Task DownloadManifestAsync()
{
try
{
await manifestUpdateLock.WaitAsync().ConfigureAwait(false);
await using var jsonStream = await Http.GetStreamAsync("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json")
.ConfigureAwait(false);
UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(jsonStream).ConfigureAwait(false);
}
catch (Exception e)
{
Log.Exception("|PluginManagement.GetManifest|Encountered error trying to download plugins manifest", e);
UserPlugins = new List<UserPlugin>();
}
finally
{
manifestUpdateLock.Release();
}
}
}
}

View file

@ -1,7 +1,6 @@
 namespace Flow.Launcher.Core.ExternalPlugins
namespace Flow.Launcher.Plugin.PluginsManager.Models
{ {
public class UserPlugin public record UserPlugin
{ {
public string ID { get; set; } public string ID { get; set; }
public string Name { get; set; } public string Name { get; set; }
@ -12,5 +11,6 @@ namespace Flow.Launcher.Plugin.PluginsManager.Models
public string Website { get; set; } public string Website { get; set; }
public string UrlDownload { get; set; } public string UrlDownload { get; set; }
public string UrlSourceCode { get; set; } public string UrlSourceCode { get; set; }
public string IcoPath { get; set; }
} }
} }

View file

@ -657,28 +657,28 @@
ToolTip="Change Plugin Results Priority" ToolTip="Change Plugin Results Priority"
Margin="5 0 0 0" Cursor="Hand" Margin="5 0 0 0" Cursor="Hand"
Click="OnPluginPriorityClick"> Click="OnPluginPriorityClick">
<!--#region Priority Button Style--> <!--#region Priority Button Style-->
<Button.Resources> <Button.Resources>
<Style TargetType="Border"> <Style TargetType="Border">
<Setter Property="CornerRadius" Value="2"/> <Setter Property="CornerRadius" Value="2"/>
</Style> </Style>
</Button.Resources> </Button.Resources>
<Button.Style> <Button.Style>
<Style TargetType="Button"> <Style TargetType="Button">
<Setter Property="Padding" Value="12 8 12 8" /> <Setter Property="Padding" Value="12 8 12 8" />
<Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderThickness" Value="0" />
<Setter Property="FontWeight" Value="DemiBold" /> <Setter Property="FontWeight" Value="DemiBold" />
<Setter Property="Foreground" Value="Black" /> <Setter Property="Foreground" Value="Black" />
<Style.Triggers> <Style.Triggers>
<DataTrigger Binding="{Binding ElementName=PriorityButton, UpdateSourceTrigger=PropertyChanged, Path=Content}" Value="0"> <DataTrigger Binding="{Binding ElementName=PriorityButton, UpdateSourceTrigger=PropertyChanged, Path=Content}" Value="0">
<Setter Property="Foreground" Value="#878787" /> <Setter Property="Foreground" Value="#878787" />
<Setter Property="FontWeight" Value="Normal" /> <Setter Property="FontWeight" Value="Normal" />
</DataTrigger> </DataTrigger>
</Style.Triggers> </Style.Triggers>
</Style> </Style>
</Button.Style> </Button.Style>
<!--#endregion--> <!--#endregion-->
</Button> </Button>
</Border> </Border>
</StackPanel> </StackPanel>
<ui:ToggleSwitch Grid.Column="4" OffContent="{DynamicResource disable}" <ui:ToggleSwitch Grid.Column="4" OffContent="{DynamicResource disable}"
@ -704,7 +704,7 @@
Margin="0 0 0 0" VerticalAlignment="Center" HorizontalAlignment="Left" DockPanel.Dock="Left"/> Margin="0 0 0 0" VerticalAlignment="Center" HorizontalAlignment="Left" DockPanel.Dock="Left"/>
<Button Grid.Column="2" Content="{Binding ActionKeywordsText}" <Button Grid.Column="2" Content="{Binding ActionKeywordsText}"
DockPanel.Dock="Right" DockPanel.Dock="Right"
Visibility="{Binding ActionKeywordsVisibility}" Visibility="{Binding ActionKeywordsVisibility}"
ToolTip="Change Action Keywords" ToolTip="Change Action Keywords"
@ -753,7 +753,7 @@
Content="{Binding SettingControl}" Content="{Binding SettingControl}"
Padding="1" Margin="0" VerticalAlignment="Stretch" Padding="1" Margin="0" VerticalAlignment="Stretch"
MaxWidth="750" MaxHeight="550"> MaxWidth="750" MaxHeight="550">
</ContentControl> </ContentControl>
</StackPanel> </StackPanel>
@ -840,7 +840,7 @@
</Border> </Border>
<Border Grid.Column="0" Grid.Row="1" Background="#f3f3f3" Padding="0 0 0 0"> <Border Grid.Column="0" Grid.Row="1" Background="#f3f3f3" Padding="0 0 0 0">
<ListBox x:Name="StoreListBox" <ListBox x:Name="StoreListBox"
ItemsSource="{Binding PluginViewModels}" ItemsSource="{Binding ExternalPlugins}"
Margin="6, 0, 0, 0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Margin="6, 0, 0, 0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemContainerStyle="{StaticResource StoreList}" ItemContainerStyle="{StaticResource StoreList}"
ScrollViewer.IsDeferredScrollingEnabled="True" ScrollViewer.CanContentScroll="False" ScrollViewer.IsDeferredScrollingEnabled="True" ScrollViewer.CanContentScroll="False"
@ -874,10 +874,10 @@
</Style.Triggers> </Style.Triggers>
</Style> </Style>
</StackPanel.Style> </StackPanel.Style>
<Image Source="{Binding Image, IsAsync=True}" <Image Source="{Binding IcoPath, IsAsync=True}"
Width="32" Height="32" Margin="8 0 6 0" Width="32" Height="32" Margin="8 0 6 0"
VerticalAlignment="Center"/> VerticalAlignment="Center"/>
</StackPanel> </StackPanel>
<StackPanel Grid.Column="1" Margin="0 0 8 0" VerticalAlignment="Center" Panel.ZIndex="0"> <StackPanel Grid.Column="1" Margin="0 0 8 0" VerticalAlignment="Center" Panel.ZIndex="0">
<StackPanel.Style> <StackPanel.Style>
<Style> <Style>
@ -889,11 +889,11 @@
</Style.Triggers> </Style.Triggers>
</Style> </Style>
</StackPanel.Style> </StackPanel.Style>
<TextBlock Text="{Binding PluginPair.Metadata.Name}" <TextBlock Text="{Binding Name}"
TextWrapping="WrapWithOverflow" Padding="0 0 20 0" TextWrapping="WrapWithOverflow" Padding="0 0 20 0"
ToolTip="{Binding PluginPair.Metadata.Version}" /> ToolTip="{Binding Version}" />
<TextBlock Opacity="0.5" TextWrapping="WrapWithOverflow" Margin="0 2 0 0" Padding="0 0 20 0"> <TextBlock Opacity="0.5" TextWrapping="WrapWithOverflow" Margin="0 2 0 0" Padding="0 0 20 0">
<Run Text="{Binding PluginPair.Metadata.Description}" FontSize="12" /> <Run Text="{Binding Description}" FontSize="12" />
</TextBlock> </TextBlock>
</StackPanel > </StackPanel >
@ -920,15 +920,15 @@
<StackPanel Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Stretch"> <StackPanel Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Stretch">
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding PluginPair.Metadata.Name}" FontWeight="Bold" <TextBlock Text="{Binding Name}" FontWeight="Bold"
TextWrapping="WrapWithOverflow" Padding="0 0 0 0" Margin="20 0 0 0" TextWrapping="WrapWithOverflow" Padding="0 0 0 0" Margin="20 0 0 0"
ToolTip="{Binding PluginPair.Metadata.Name}" /> ToolTip="{Binding Name}" />
<TextBlock Text="{Binding PluginPair.Metadata.Version}" <TextBlock Text="{Binding Version}"
TextWrapping="WrapWithOverflow" Padding="0 0 20 0" Margin="10 0 0 0" TextWrapping="WrapWithOverflow" Padding="0 0 20 0" Margin="10 0 0 0"
ToolTip="{Binding PluginPair.Metadata.Version}" /> ToolTip="{Binding Version}" />
</StackPanel> </StackPanel>
<TextBlock Opacity="0.5" TextWrapping="Wrap" Margin="20 2 0 0" Padding="0 0 20 0" > <TextBlock Opacity="0.5" TextWrapping="Wrap" Margin="20 2 0 0" Padding="0 0 20 0" >
<Run Text="{Binding PluginPair.Metadata.Author}" FontSize="12" /> <Run Text="{Binding Author}" FontSize="12" />
</TextBlock> </TextBlock>
</StackPanel> </StackPanel>

View file

@ -9,6 +9,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using Flow.Launcher.Core; using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration; using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.Plugin; using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Core.Resource; using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper; using Flow.Launcher.Helper;
@ -237,6 +238,14 @@ namespace Flow.Launcher.ViewModel
} }
} }
public IList<UserPlugin> ExternalPlugins
{
get
{
return PluginsManifest.UserPlugins;
}
}
public Control SettingProvider public Control SettingProvider
{ {
get get

View file

@ -1,5 +1,5 @@
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Plugin.PluginsManager.Models; using Flow.Launcher.Infrastructure.UserSettings;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;

View file

@ -38,7 +38,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
viewModel = new SettingsViewModel(context, Settings); viewModel = new SettingsViewModel(context, Settings);
contextMenu = new ContextMenu(Context); contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings); pluginManager = new PluginsManager(Context, Settings);
_manifestUpdateTask = pluginManager.UpdateManifest().ContinueWith(_ => _manifestUpdateTask = pluginManager.UpdateManifestAsync().ContinueWith(_ =>
{ {
lastUpdateTime = DateTime.Now; lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion); }, TaskContinuationOptions.OnlyOnRanToCompletion);
@ -62,7 +62,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
if ((DateTime.Now - lastUpdateTime).TotalHours > 12 && _manifestUpdateTask.IsCompleted) // 12 hours if ((DateTime.Now - lastUpdateTime).TotalHours > 12 && _manifestUpdateTask.IsCompleted) // 12 hours
{ {
_manifestUpdateTask = pluginManager.UpdateManifest().ContinueWith(t => _manifestUpdateTask = pluginManager.UpdateManifestAsync().ContinueWith(t =>
{ {
lastUpdateTime = DateTime.Now; lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion); }, TaskContinuationOptions.OnlyOnRanToCompletion);
@ -93,7 +93,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
public async Task ReloadDataAsync() public async Task ReloadDataAsync()
{ {
await pluginManager.UpdateManifest(); await pluginManager.UpdateManifestAsync();
lastUpdateTime = DateTime.Now; lastUpdateTime = DateTime.Now;
} }
} }

View file

@ -1,31 +0,0 @@
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
namespace Flow.Launcher.Plugin.PluginsManager.Models
{
internal class PluginsManifest
{
internal List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();
internal async Task DownloadManifest()
{
try
{
await using var jsonStream = await Http.GetStreamAsync("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json")
.ConfigureAwait(false);
UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(jsonStream).ConfigureAwait(false);
}
catch (Exception e)
{
Log.Exception("|PluginManagement.GetManifest|Encountered error trying to download plugins manifest", e);
UserPlugins = new List<UserPlugin>();
}
}
}
}

View file

@ -1,9 +1,9 @@
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.Plugin; using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Http; using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.PluginsManager.Models;
using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.Plugin.SharedCommands;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -17,8 +17,6 @@ namespace Flow.Launcher.Plugin.PluginsManager
{ {
internal class PluginsManager internal class PluginsManager
{ {
private PluginsManifest pluginsManifest;
private PluginInitContext Context { get; set; } private PluginInitContext Context { get; set; }
private Settings Settings { get; set; } private Settings Settings { get; set; }
@ -43,7 +41,6 @@ namespace Flow.Launcher.Plugin.PluginsManager
internal PluginsManager(PluginInitContext context, Settings settings) internal PluginsManager(PluginInitContext context, Settings settings)
{ {
pluginsManifest = new PluginsManifest();
Context = context; Context = context;
Settings = settings; Settings = settings;
} }
@ -51,7 +48,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
private Task _downloadManifestTask = Task.CompletedTask; private Task _downloadManifestTask = Task.CompletedTask;
internal Task UpdateManifest() internal Task UpdateManifestAsync()
{ {
if (_downloadManifestTask.Status == TaskStatus.Running) if (_downloadManifestTask.Status == TaskStatus.Running)
{ {
@ -59,7 +56,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
} }
else else
{ {
_downloadManifestTask = pluginsManifest.DownloadManifest(); _downloadManifestTask = PluginsManifest.UpdateTask;
_downloadManifestTask.ContinueWith(_ => _downloadManifestTask.ContinueWith(_ =>
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_update_failed_title"), Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_update_failed_title"),
Context.API.GetTranslation("plugin_pluginsmanager_update_failed_subtitle"), icoPath, false), Context.API.GetTranslation("plugin_pluginsmanager_update_failed_subtitle"), icoPath, false),
@ -171,9 +168,9 @@ namespace Flow.Launcher.Plugin.PluginsManager
internal async ValueTask<List<Result>> RequestUpdate(string search, CancellationToken token) internal async ValueTask<List<Result>> RequestUpdate(string search, CancellationToken token)
{ {
if (!pluginsManifest.UserPlugins.Any()) if (!PluginsManifest.UserPlugins.Any())
{ {
await UpdateManifest(); await UpdateManifestAsync();
} }
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
@ -190,7 +187,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
var resultsForUpdate = var resultsForUpdate =
from existingPlugin in Context.API.GetAllPlugins() from existingPlugin in Context.API.GetAllPlugins()
join pluginFromManifest in pluginsManifest.UserPlugins join pluginFromManifest in PluginsManifest.UserPlugins
on existingPlugin.Metadata.ID equals pluginFromManifest.ID on existingPlugin.Metadata.ID equals pluginFromManifest.ID
where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) < where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) <
0 // if current version precedes manifest version 0 // if current version precedes manifest version
@ -307,9 +304,9 @@ namespace Flow.Launcher.Plugin.PluginsManager
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token) internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token)
{ {
if (!pluginsManifest.UserPlugins.Any()) if (!PluginsManifest.UserPlugins.Any())
{ {
await UpdateManifest(); await UpdateManifestAsync();
} }
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
@ -317,7 +314,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim(); var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim();
var results = var results =
pluginsManifest PluginsManifest
.UserPlugins .UserPlugins
.Select(x => .Select(x =>
new Result new Result