Ejemplo n.º 1
0
        public static IMvcBuilder AddPlugins(this IMvcBuilder mvcBuilder, IServiceCollection serviceCollection,
                                             IConfiguration config, ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger(typeof(PluginManager));
            var pluginsFolder = config.GetPluginDir(DefaultConfiguration.GetNetworkType(config));
            var plugins       = new List <IBTCPayServerPlugin>();

            _logger.LogInformation($"Loading plugins from {pluginsFolder}");
            Directory.CreateDirectory(pluginsFolder);
            ExecuteCommands(pluginsFolder);
            List <(PluginLoader, Assembly, IFileProvider)> loadedPlugins = new List <(PluginLoader, Assembly, IFileProvider)>();
            var systemExtensions = GetDefaultLoadedPluginAssemblies();

            plugins.AddRange(systemExtensions.SelectMany(assembly =>
                                                         GetAllPluginTypesFromAssembly(assembly).Select(GetPluginInstanceFromType)));
            foreach (IBTCPayServerPlugin btcPayServerExtension in plugins)
            {
                btcPayServerExtension.SystemPlugin = true;
            }
            foreach (var dir in Directory.GetDirectories(pluginsFolder))
            {
                var pluginName = Path.GetFileName(dir);

                var plugin = PluginLoader.CreateFromAssemblyFile(
                    Path.Combine(dir, pluginName + ".dll"), // create a plugin from for the .dll file
                    config =>
                    // this ensures that the version of MVC is shared between this app and the plugin
                    config.PreferSharedTypes = true);

                mvcBuilder.AddPluginLoader(plugin);
                var pluginAssembly = plugin.LoadDefaultAssembly();
                _pluginAssemblies.Add(pluginAssembly);
                var fileProvider = CreateEmbeddedFileProviderForAssembly(pluginAssembly);
                loadedPlugins.Add((plugin, pluginAssembly, fileProvider));
                plugins.AddRange(GetAllPluginTypesFromAssembly(pluginAssembly)
                                 .Select(GetPluginInstanceFromType));
            }

            foreach (var plugin in plugins)
            {
                try
                {
                    _logger.LogInformation(
                        $"Adding and executing plugin {plugin.Identifier} - {plugin.Version}");
                    plugin.Execute(serviceCollection);
                    serviceCollection.AddSingleton(plugin);
                }
                catch (Exception e)
                {
                    _logger.LogError($"Error when loading plugin {plugin.Identifier} - {plugin.Version}{Environment.NewLine}{e.Message}");
                }
            }

            return(mvcBuilder);
        }
Ejemplo n.º 2
0
        public static IMvcBuilder AddExtensions(this IMvcBuilder mvcBuilder, IServiceCollection serviceCollection,
                                                IConfiguration config, ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger(typeof(ExtensionManager));
            var extensionsFolder = config.GetExtensionDir(DefaultConfiguration.GetNetworkType(config));
            var extensions       = new List <IBTCPayServerExtension>();

            _logger.LogInformation($"Loading extensions from {extensionsFolder}");
            Directory.CreateDirectory(extensionsFolder);
            ExecuteCommands(extensionsFolder);
            List <(PluginLoader, Assembly, IFileProvider)> plugins = new List <(PluginLoader, Assembly, IFileProvider)>();

            foreach (var dir in Directory.GetDirectories(extensionsFolder))
            {
                var pluginName = Path.GetFileName(dir);

                var plugin = PluginLoader.CreateFromAssemblyFile(
                    Path.Combine(dir, pluginName + ".dll"), // create a plugin from for the .dll file
                    config =>
                    // this ensures that the version of MVC is shared between this app and the plugin
                    config.PreferSharedTypes = true);

                mvcBuilder.AddPluginLoader(plugin);
                var pluginAssembly = plugin.LoadDefaultAssembly();
                _pluginAssemblies.Add(pluginAssembly);
                var fileProvider = CreateEmbeddedFileProviderForAssembly(pluginAssembly);
                plugins.Add((plugin, pluginAssembly, fileProvider));
                extensions.AddRange(GetAllExtensionTypesFromAssembly(pluginAssembly)
                                    .Select(GetExtensionInstanceFromType));
            }

            foreach (var extension in extensions)
            {
                _logger.LogInformation($"Adding and executing extension {extension.Identifier} - {extension.Version}");
                serviceCollection.AddSingleton(extension);
                extension.Execute(serviceCollection);
            }

            return(mvcBuilder);
        }
Ejemplo n.º 3
0
 private DirectoryInfo GetDataDir()
 {
     return(new DirectoryInfo(Configuration.GetDataDir(DefaultConfiguration.GetNetworkType(Configuration))));
 }