2020-06-09 22:59:25 +00:00
|
|
|
|
using System;
|
2016-01-06 21:34:42 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-05-05 20:15:13 +00:00
|
|
|
|
using System.Linq;
|
2014-12-26 11:36:43 +00:00
|
|
|
|
using System.IO;
|
2020-06-09 22:59:25 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
|
|
|
|
using Flow.Launcher.Plugin;
|
2020-12-30 05:40:42 +00:00
|
|
|
|
using System.Text.Json;
|
2014-12-26 11:36:43 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Core.Plugin
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
internal abstract class PluginConfig
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2020-05-26 01:00:33 +00:00
|
|
|
|
/// Parse plugin metadata in the given directories
|
2014-12-26 11:36:43 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="pluginDirectories"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2016-04-27 01:15:53 +00:00
|
|
|
|
public static List<PluginMetadata> Parse(string[] pluginDirectories)
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2020-05-26 01:00:33 +00:00
|
|
|
|
var allPluginMetadata = new List<PluginMetadata>();
|
2021-03-18 03:43:41 +00:00
|
|
|
|
var directories = pluginDirectories.SelectMany(Directory.EnumerateDirectories);
|
2014-12-26 11:36:43 +00:00
|
|
|
|
|
2016-05-06 00:55:30 +00:00
|
|
|
|
// todo use linq when diable plugin is implmented since parallel.foreach + list is not thread saft
|
|
|
|
|
|
foreach (var directory in directories)
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2016-05-05 20:15:13 +00:00
|
|
|
|
if (File.Exists(Path.Combine(directory, "NeedDelete.txt")))
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2014-12-29 15:02:50 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(directory, true);
|
|
|
|
|
|
}
|
2016-01-06 21:34:42 +00:00
|
|
|
|
catch (Exception e)
|
2014-12-29 15:02:50 +00:00
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Exception($"|PluginConfig.ParsePLuginConfigs|Can't delete <{directory}>", e);
|
2014-12-29 15:02:50 +00:00
|
|
|
|
}
|
2014-12-26 11:36:43 +00:00
|
|
|
|
}
|
2016-05-05 20:15:13 +00:00
|
|
|
|
else
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2016-05-05 20:15:13 +00:00
|
|
|
|
PluginMetadata metadata = GetPluginMetadata(directory);
|
|
|
|
|
|
if (metadata != null)
|
|
|
|
|
|
{
|
2020-05-26 01:00:33 +00:00
|
|
|
|
allPluginMetadata.Add(metadata);
|
2016-05-05 20:15:13 +00:00
|
|
|
|
}
|
2014-12-26 11:36:43 +00:00
|
|
|
|
}
|
2016-05-06 00:55:30 +00:00
|
|
|
|
}
|
2020-05-26 01:00:33 +00:00
|
|
|
|
|
|
|
|
|
|
return allPluginMetadata;
|
2014-12-26 11:36:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
|
|
|
|
|
|
{
|
2020-06-09 22:59:25 +00:00
|
|
|
|
string configPath = Path.Combine(pluginDirectory, Constant.PluginMetadataFileName);
|
2014-12-26 11:36:43 +00:00
|
|
|
|
if (!File.Exists(configPath))
|
|
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Error($"|PluginConfig.GetPluginMetadata|Didn't find config file <{configPath}>");
|
2014-12-26 11:36:43 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PluginMetadata metadata;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-12-30 05:40:42 +00:00
|
|
|
|
metadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(configPath));
|
2014-12-26 11:36:43 +00:00
|
|
|
|
metadata.PluginDirectory = pluginDirectory;
|
2015-11-04 22:49:40 +00:00
|
|
|
|
// for plugins which doesn't has ActionKeywords key
|
2016-03-25 01:22:24 +00:00
|
|
|
|
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { metadata.ActionKeyword };
|
2015-11-04 22:49:40 +00:00
|
|
|
|
// for plugin still use old ActionKeyword
|
|
|
|
|
|
metadata.ActionKeyword = metadata.ActionKeywords?[0];
|
2014-12-26 11:36:43 +00:00
|
|
|
|
}
|
2016-01-06 21:34:42 +00:00
|
|
|
|
catch (Exception e)
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e);
|
2014-12-26 11:36:43 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
|
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>");
|
2014-12-26 11:36:43 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(metadata.ExecuteFilePath))
|
|
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
|
2014-12-26 11:36:43 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-12-29 14:09:54 +00:00
|
|
|
|
}
|