/// <summary>
        ///   Initializes the IPC server, scans for plugins and starts enabled plugins if
        ///   <paramref name="startEnabledPlugins" /> is true.
        /// </summary>
        /// <param name="startEnabledPlugins">Whether to start enabled plugins</param>
        /// <returns></returns>
        /// <exception cref="InvalidProgramException">When already initialized</exception>
        protected virtual async Task Initialize(bool startEnabledPlugins = true)
        {
            LogTo.Debug($"Initializing {GetType().Name}");

            if (PackageManager != null || IpcServer != null || AllPlugins.Any())
            {
                throw new InvalidProgramException("Initialize called while PluginManagerBase is already initialized");
            }

            PropertyChangedNotificationInterceptor.GlobalSynchronizationContext = UISynchronizationContext;

            PackageManager = await PluginPackageManager <TMeta> .Create(
                Locations.PluginDir,
                Locations.PluginHomeDir,
                Locations.PluginPackageDir,
                Locations.PluginConfigFile,
                CreateSourceRepositoryProvider).ConfigureAwait(false);

            StartIpcServer();
            //StartMonitoringPlugins();

            await RefreshPlugins().ConfigureAwait(false);

            if (startEnabledPlugins)
            {
                await StartPlugins().ConfigureAwait(false);
            }

            LogTo.Debug($"Initializing {GetType().Name}... Done");
        }
        /// <summary>
        ///   Search available NuGet repositories for all packages matching
        ///   <paramref name="searchTerm" /> and <paramref name="enablePrerelease" />. Only NuGet packages
        ///   that are also indexed by the API pointed to by <see cref="UpdateUrl" /> will be included.
        /// </summary>
        /// <param name="searchTerm">Part or totality of the package name to look for</param>
        /// <param name="enablePrerelease">Whether to include packages that are marked as pre-release</param>
        /// <param name="pm">The package manager</param>
        /// <param name="cancellationToken"></param>
        /// <returns>All available packages or <see langword="null" /></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual async Task <IEnumerable <PluginPackage <TMeta> > > SearchPlugins(
            string searchTerm,
            bool enablePrerelease,
            PluginPackageManager <TMeta> pm,
            CancellationToken cancellationToken = default)
        {
            if (pm == null)
            {
                LogTo.Error("pm is NULL");
                throw new ArgumentNullException(nameof(pm));
            }

            var allowedPluginMetadatas = await FetchPluginMetadataList(cancellationToken);

            if (allowedPluginMetadatas == null)
            {
                LogTo.Warning($"Fetching plugin metadatas from repository failed while searching plugins with term '{searchTerm}'. Aborting");
                return(null);
            }

            if (allowedPluginMetadatas.Any() == false)
            {
                return(new List <PluginPackage <TMeta> >());
            }

            var allowedPluginMetadataMap = allowedPluginMetadatas.ToDictionary(GetPackageIdFromMetadata);

            var plugins = (await pm.Search(
                               searchTerm,
                               enablePrerelease,
                               pn => GetMetaFromPackageName(pn, allowedPluginMetadataMap),
                               psm => FilterSearchResult(psm, allowedPluginMetadataMap),
                               cancellationToken))
                          ?.ToList();

            if (plugins == null)
            {
                LogTo.Warning($"Failed to fetch plugins from the NuGet repositories while searching for term '{searchTerm}'");
                return(null);
            }

            LogTo.Trace($"Found {plugins.Count} plugins:\n{plugins.Serialize(Formatting.Indented)}");

            return(plugins);
        }
Exemple #3
0
 public PluginManagerController(IPluginManagerService pluginManager, PluginPackageManager pluginPackageManager)
 {
     _pluginManager        = pluginManager;
     _pluginPackageManager = pluginPackageManager;
 }