Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Logger;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -10,43 +11,45 @@ namespace Flow.Launcher.Core.ExternalPlugins
{
public static class PluginsManifest
{
static PluginsManifest()
{
UpdateTask = UpdateManifestAsync();
}

public static List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();
private const string manifestFileUrl = "https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json";

public static Task UpdateTask { get; private set; }
private static HttpClient httpClient = new HttpClient();

private static readonly SemaphoreSlim manifestUpdateLock = new(1);

public static Task UpdateManifestAsync()
{
if (manifestUpdateLock.CurrentCount == 0)
{
return UpdateTask;
}
private static string latestEtag = "";

return UpdateTask = DownloadManifestAsync();
}
public static List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();

private static async Task DownloadManifestAsync()
public static async Task UpdateManifestAsync(CancellationToken token = default)
{
try
{
await manifestUpdateLock.WaitAsync().ConfigureAwait(false);
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);

var request = new HttpRequestMessage(HttpMethod.Get, manifestFileUrl);
request.Headers.Add("If-None-Match", latestEtag);

var response = await httpClient.SendAsync(request, token).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);
if (response.StatusCode == HttpStatusCode.OK)
{
Log.Info($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Fetched plugins from manifest repo");

UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(jsonStream).ConfigureAwait(false);
var json = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);

UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(json, cancellationToken: token).ConfigureAwait(false);

latestEtag = response.Headers.ETag.Tag;
}
else if (response.StatusCode != HttpStatusCode.NotModified)
{
Log.Warn($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http response for manifest file was {response.StatusCode}");
}
}
catch (Exception e)
{
Log.Exception("|PluginManagement.GetManifest|Encountered error trying to download plugins manifest", e);

UserPlugins = new List<UserPlugin>();
Log.Exception($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http request failed", e);
}
finally
{
Expand Down
6 changes: 3 additions & 3 deletions Plugins/Flow.Launcher.Plugin.PluginsManager/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Flow.Launcher.Infrastructure.UserSettings;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Flow.Launcher.Plugin.PluginsManager
{
Expand Down Expand Up @@ -53,8 +53,8 @@ public List<Result> LoadContextMenus(Result selectedResult)
{
// standard UrlSourceCode format in PluginsManifest's plugins.json file: https://github.com/jjw24/Flow.Launcher.Plugin.Putty/tree/master
var link = pluginManifestInfo.UrlSourceCode.StartsWith("https://github.com")
? pluginManifestInfo.UrlSourceCode.Replace("/tree/master", "/issues/new/choose")
: pluginManifestInfo.UrlSourceCode;
? Regex.Replace(pluginManifestInfo.UrlSourceCode, @"\/tree\/\w+$", "") + "/issues/new/choose"
: pluginManifestInfo.UrlSourceCode;

Context.API.OpenUrl(link);
return true;
Expand Down
39 changes: 9 additions & 30 deletions Plugins/Flow.Launcher.Plugin.PluginsManager/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, I

internal PluginsManager pluginManager;

private DateTime lastUpdateTime = DateTime.MinValue;

public Control CreateSettingPanel()
{
return new PluginsManagerSettings(viewModel);
Expand All @@ -38,12 +36,6 @@ public Task InitAsync(PluginInitContext context)
viewModel = new SettingsViewModel(context, Settings);
contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings);
_manifestUpdateTask = pluginManager
.UpdateManifestAsync(true)
.ContinueWith(_ =>
{
lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion);

return Task.CompletedTask;
}
Expand All @@ -53,35 +45,23 @@ public List<Result> LoadContextMenus(Result selectedResult)
return contextMenu.LoadContextMenus(selectedResult);
}

private Task _manifestUpdateTask = Task.CompletedTask;

public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
var search = query.Search;

if (string.IsNullOrWhiteSpace(search))
if (string.IsNullOrWhiteSpace(query.Search))
return pluginManager.GetDefaultHotKeys();

if ((DateTime.Now - lastUpdateTime).TotalHours > 12 && _manifestUpdateTask.IsCompleted) // 12 hours
{
_manifestUpdateTask = pluginManager.UpdateManifestAsync().ContinueWith(t =>
{
lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}

return search switch
return query.FirstSearch switch
{
//search could be url, no need ToLower() when passed in
var s when s.StartsWith(Settings.HotKeyInstall, StringComparison.OrdinalIgnoreCase)
=> await pluginManager.RequestInstallOrUpdate(search, token),
var s when s.StartsWith(Settings.HotkeyUninstall, StringComparison.OrdinalIgnoreCase)
=> pluginManager.RequestUninstall(search),
var s when s.StartsWith(Settings.HotkeyUpdate, StringComparison.OrdinalIgnoreCase)
=> await pluginManager.RequestUpdate(search, token),
var s when s.Equals(Settings.HotKeyInstall, StringComparison.OrdinalIgnoreCase)
=> await pluginManager.RequestInstallOrUpdate(query.SecondToEndSearch, token),
var s when s.Equals(Settings.HotkeyUninstall, StringComparison.OrdinalIgnoreCase)
=> pluginManager.RequestUninstall(query.SecondToEndSearch),
var s when s.Equals(Settings.HotkeyUpdate, StringComparison.OrdinalIgnoreCase)
=> await pluginManager.RequestUpdate(query.SecondToEndSearch, token),
_ => pluginManager.GetDefaultHotKeys().Where(hotkey =>
{
hotkey.Score = StringMatcher.FuzzySearch(search, hotkey.Title).Score;
hotkey.Score = StringMatcher.FuzzySearch(query.Search, hotkey.Title).Score;
return hotkey.Score > 0;
}).ToList()
};
Expand All @@ -100,7 +80,6 @@ public string GetTranslatedPluginDescription()
public async Task ReloadDataAsync()
{
await pluginManager.UpdateManifestAsync();
lastUpdateTime = DateTime.Now;
}
}
}
93 changes: 17 additions & 76 deletions Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ internal PluginsManager(PluginInitContext context, Settings settings)

private Task _downloadManifestTask = Task.CompletedTask;

internal Task UpdateManifestAsync(bool silent = false)
internal Task UpdateManifestAsync(CancellationToken token = default, bool silent = false)
{
if (_downloadManifestTask.Status == TaskStatus.Running)
{
return _downloadManifestTask;
}
else
{
_downloadManifestTask = PluginsManifest.UpdateTask;
_downloadManifestTask = PluginsManifest.UpdateManifestAsync(token);
if (!silent)
_downloadManifestTask.ContinueWith(_ =>
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_update_failed_title"),
Expand All @@ -77,29 +77,32 @@ internal List<Result> GetDefaultHotKeys()
{
Title = Settings.HotKeyInstall,
IcoPath = icoPath,
AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Settings.HotKeyInstall} ",
Action = _ =>
{
Context.API.ChangeQuery("pm install ");
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Settings.HotKeyInstall} ");
return false;
}
},
new Result()
{
Title = Settings.HotkeyUninstall,
IcoPath = icoPath,
AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Settings.HotkeyUninstall} ",
Action = _ =>
{
Context.API.ChangeQuery("pm uninstall ");
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Settings.HotkeyUninstall} ");
return false;
}
},
new Result()
{
Title = Settings.HotkeyUpdate,
IcoPath = icoPath,
AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword} {Settings.HotkeyUpdate} ",
Action = _ =>
{
Context.API.ChangeQuery("pm update ");
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeyword} {Settings.HotkeyUpdate} ");
return false;
}
}
Expand Down Expand Up @@ -181,22 +184,7 @@ internal async Task InstallOrUpdate(UserPlugin plugin)

internal async ValueTask<List<Result>> RequestUpdate(string search, CancellationToken token)
{
if (!PluginsManifest.UserPlugins.Any())
{
await UpdateManifestAsync();
}

token.ThrowIfCancellationRequested();

var autocompletedResults = AutoCompleteReturnAllResults(search,
Settings.HotkeyUpdate,
"Update",
"Select a plugin to update");

if (autocompletedResults.Any())
return autocompletedResults;

var uninstallSearch = search.Replace(Settings.HotkeyUpdate, string.Empty, StringComparison.OrdinalIgnoreCase).TrimStart();
await UpdateManifestAsync(token);

var resultsForUpdate =
from existingPlugin in Context.API.GetAllPlugins()
Expand Down Expand Up @@ -285,7 +273,7 @@ await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath)
}
});

return Search(results, uninstallSearch);
return Search(results, search);
}

internal bool PluginExists(string id)
Expand Down Expand Up @@ -369,20 +357,13 @@ private bool InstallSourceKnown(string url)
return url.StartsWith(acceptedSource) && Context.API.GetAllPlugins().Any(x => x.Metadata.Website.StartsWith(contructedUrlPart));
}

internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token)
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string search, CancellationToken token)
{
if (!PluginsManifest.UserPlugins.Any())
{
await UpdateManifestAsync();
}
await UpdateManifestAsync(token);

token.ThrowIfCancellationRequested();

var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty, StringComparison.OrdinalIgnoreCase).Trim();

if (Uri.IsWellFormedUriString(searchNameWithoutKeyword, UriKind.Absolute)
&& searchNameWithoutKeyword.Split('.').Last() == zip)
return InstallFromWeb(searchNameWithoutKeyword);
if (Uri.IsWellFormedUriString(search, UriKind.Absolute)
&& search.Split('.').Last() == zip)
return InstallFromWeb(search);

var results =
PluginsManifest
Expand All @@ -408,7 +389,7 @@ internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName,
ContextData = x
});

return Search(results, searchNameWithoutKeyword);
return Search(results, search);
}

private void Install(UserPlugin plugin, string downloadedFilePath)
Expand Down Expand Up @@ -468,16 +449,6 @@ private void Install(UserPlugin plugin, string downloadedFilePath)

internal List<Result> RequestUninstall(string search)
{
var autocompletedResults = AutoCompleteReturnAllResults(search,
Settings.HotkeyUninstall,
"Uninstall",
"Select a plugin to uninstall");

if (autocompletedResults.Any())
return autocompletedResults;

var uninstallSearch = search.Replace(Settings.HotkeyUninstall, string.Empty, StringComparison.OrdinalIgnoreCase).TrimStart();

var results = Context.API
.GetAllPlugins()
.Select(x =>
Expand Down Expand Up @@ -508,7 +479,7 @@ internal List<Result> RequestUninstall(string search)
}
});

return Search(results, uninstallSearch);
return Search(results, search);
}

private void Uninstall(PluginMetadata plugin, bool removedSetting = true)
Expand All @@ -523,36 +494,6 @@ private void Uninstall(PluginMetadata plugin, bool removedSetting = true)
using var _ = File.CreateText(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt"));
}

private List<Result> AutoCompleteReturnAllResults(string search, string hotkey, string title, string subtitle)
{
if (!string.IsNullOrEmpty(search)
&& hotkey.StartsWith(search)
&& (hotkey != search || !search.StartsWith(hotkey)))
{
return
new List<Result>
{
new Result
{
Title = title,
IcoPath = icoPath,
SubTitle = subtitle,
Action = e =>
{
Context
.API
.ChangeQuery(
$"{Context.CurrentPluginMetadata.ActionKeywords.FirstOrDefault()} {hotkey} ");

return false;
}
}
};
}

return new List<Result>();
}

private bool SameOrLesserPluginVersionExists(string metadataPath)
{
var newMetadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(metadataPath));
Expand Down