< Summary

Information
Class: SwitchBlade.Services.PluginService
Assembly: SwitchBlade
File(s): D:\a\switchblade\switchblade\Services\PluginService.cs
Tag: 203_23722840422
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%1010100%
get_Providers()100%11100%
LoadProviders()100%44100%
ReloadPlugins()100%11100%
GetPluginInfos()100%11100%

File(s)

D:\a\switchblade\switchblade\Services\PluginService.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using SwitchBlade.Contracts;
 5using SwitchBlade.Core;
 6
 7namespace SwitchBlade.Services
 8{
 9    /// <summary>
 10    /// Manages plugin discovery and lifecycle.
 11    /// Encapsulates PluginLoader and provides a clean API for the DI container.
 12    /// </summary>
 13    public class PluginService : IPluginService
 14    {
 15        private readonly IPluginContext _context;
 16        private readonly ISettingsService _settingsService;
 17        private readonly ILogger? _logger;
 18        private readonly IRegistryService _registryService;
 19        private readonly IPluginLoader _pluginLoader;
 20        private readonly WindowFinder _windowFinder;
 1421        private readonly List<IWindowProvider> _providers = new();
 22
 923        public IReadOnlyList<IWindowProvider> Providers => _providers;
 24
 1425        public PluginService(IPluginContext context, ISettingsService settingsService, IRegistryService registryService,
 1426        {
 1427            _context = context ?? throw new ArgumentNullException(nameof(context));
 1328            _settingsService = settingsService ?? throw new ArgumentNullException(nameof(settingsService));
 1229            _registryService = registryService ?? throw new ArgumentNullException(nameof(registryService));
 1130            _logger = logger;
 1131            _pluginLoader = pluginLoader ?? throw new ArgumentNullException(nameof(pluginLoader));
 1032            _windowFinder = windowFinder ?? throw new ArgumentNullException(nameof(windowFinder));
 33
 934            LoadProviders();
 935        }
 36
 37        private void LoadProviders()
 1038        {
 1039            _providers.Clear();
 40
 41            // 1. Internal provider: WindowFinder
 1042            _windowFinder.Initialize(_context);
 1043            _providers.Add(_windowFinder);
 44
 45            // 2. External plugins — discover and then initialize each exactly once
 46            try
 1047            {
 1048                var plugins = _pluginLoader.LoadPlugins();
 49
 2950                foreach (var plugin in plugins)
 351                {
 52                    // Create per-plugin context with settings and initialize once
 353                    var pluginSettings = new PluginSettingsService(plugin.PluginName, _registryService, _context.Logger)
 354                    var pluginContext = new PluginContext(_context.Logger, _context.Interop, _registryService, pluginSet
 355                    plugin.Initialize(pluginContext);
 356                    _providers.Add(plugin);
 357                }
 758            }
 359            catch (Exception ex)
 360            {
 361                if (_logger != null)
 262                {
 263                    _logger.LogError("Error loading plugins", ex);
 264                }
 365            }
 1066        }
 67
 68        public void ReloadPlugins()
 169        {
 170            LoadProviders();
 171        }
 72
 73        public IEnumerable<PluginInfo> GetPluginInfos()
 374        {
 375            return _providers.Select(PluginInfoMapper.MapToInfo);
 376        }
 77    }
 78}